Question

JAVA PLEASE

0 0
Add a comment Improve this question Transcribed image text
Answer #1

/*Source code of the above program is given below*/

/*NOTE: If you want to change something or want in a different way, please let me know through comments; I will surely revert back to you.*/

/***************OwnStack.java*******************/

import java.util.EmptyStackException;

public class OwnStack {

public static final int MAXSIZE=500;

  

public OwnStack()

{

topLoc = -1;

item = new char[MAXSIZE];

}

public boolean isEmpty()

{ return (topLoc==-1); }

  

public boolean isFull()

{ return (topLoc==MAXSIZE-1); }

  

public void push(char newVal)

{

if(isFull())

{

System.out.println("Stack is Full!!");

return;

}

topLoc++;

item[topLoc] = newVal;

size++;

}

public char peek()

{

if (isEmpty()) {

throw new EmptyStackException();

}

return item[topLoc];

}

public char pop()

{

if (isEmpty()) {

throw new EmptyStackException();

}

size--;

return item[topLoc--];

}

public int size() {

return size;

}

public boolean search(char c)

{

for(int i=0;i<size;i++)

{

if(item[i]==c)

return true;

}

return false;

}

private int topLoc;

private char[] item;

int size;

}

/***************TestStack.java********************/

public class TestStack {

public static void main(String[] args) {

OwnStack os = new OwnStack();

os.push('a');

os.push('d');

os.push('B');

os.push('N');

System.out.println("The size of the current stack: "+os.size());

System.out.println("The top element in the current stack: "+os.peek());

os.pop();

os.pop();

System.out.println("After 2 pop() the size of the current stack: "+os.size());

System.out.println("The top element in the current stack: "+os.peek());

System.out.println("The element 'a' is present in the current stack: "+os.search('a'));

}

}

/**************OUTPUT************************/

2 public class TestStack { 4 public static void main(String[] args) 6 OwnStack os = new OwnStack(); os.push(a) os.push (d);

/*If this helps you, please let me know by giving a positive thumbs up. In case you have any queries, do let me know. I will revert back to you. Thank you!!*/

Add a comment
Know the answer?
Add Answer to:
JAVA PLEASE Exercise 1: Build our own stack Do our own stack data structure for char's...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java - data structures Suppose that in the array-based stack, the array doubles in size after...

    Java - data structures Suppose that in the array-based stack, the array doubles in size after multiple push operations. But later on, fewer than half of the array’s locations might actually be used by the stack due to pop operations. Revise the implementation so that its array also can shrink in size as objects are removed from the stack. Accomplishing this task will require two new private methods, as follows: The first new method checks whether we should reduce the...

  • I need to implement a stack array but the top of the stack has to be...

    I need to implement a stack array but the top of the stack has to be Initialize as the index of the last location in the array.    //Array implementation of stacks.    import java.util.Arrays;       public class ArrayStack implements Stack {        //Declare a class constant called DEFAULT_STACK_SIZE with the value 10.           private static final int DEFAULT_STACK_SIZE = 10;           /* Declare two instance variables:            1. An integer called...

  • c program Here we see a Stack ADT implemented using array. We would like the stack...

    c program Here we see a Stack ADT implemented using array. We would like the stack to be usable for different max sizes though, so we need to use dynamic memory allocation for our array as well. #include <stdio.h> #include <stdlib.h> typedef struct { int *data;   // stack data, we assume integer for simplicity int top;     // top of the stack int maxSize; // max size of the stack } Stack; void StackInit(Stack* stack, int size) {     // this...

  • Build the following data structures in c++: STACK (Do not use the STL library for your...

    Build the following data structures in c++: STACK (Do not use the STL library for your containers) C++ Stack and Queue should contain Insertion (Push/Enqueue) Deletion (Pop/Dequeue) Print/Display Sort (Stacks: Value or Color, Queue: Alphabetical) Search (Contains, position, and how many instances as three separate functions) Clear/empty Size (if empty, print that it’s empty) Each data structure should contain a set of overloaded operators (Respect innate object behavior): ‘+’ (addition) that allows you to add two objects of the same...

  • Use Java to implement a basic stack using an array of integers. For the stack, you...

    Use Java to implement a basic stack using an array of integers. For the stack, you will create an array of integers that holds 5 numbers. To make it easier, you can declare the array at the class level. That way you will be able to use the array in any method in your class without using parameters. Your input/output interface should look something like the following: What operation do you want to do? push What number do you want...

  • how would I complete this code without calling any built-in java collection framework classes like ArrayList,...

    how would I complete this code without calling any built-in java collection framework classes like ArrayList, LinkedList, etc? import java.util.Iterator; class CallStack<T> implements Iterable<T> { // You'll want some instance variables here public CallStack() { //setup what you need } public void push(T item) { //push an item onto the stack //you may assume the item is not null //O(1) } public T pop() { //pop an item off the stack //if there are no items on the stack, return...

  • Write a Python function that uses the stack data structure to check if a string is...

    Write a Python function that uses the stack data structure to check if a string is a palindrome or not. You can use any of the dollowing stack methods: push(), pop(), peek, empty(). Note that a palindrome is a word, phrase, or sequence that reads the same backwards as forward

  • Build the following data structures in c++: STACK (Do not use the STL library for your...

    Build the following data structures in c++: STACK (Do not use the STL library for your containers, PREFERRED IF YOU USED LINKED LIST OR ARRAYS ) C++ Stack should contain Insertion (Push) Deletion (Pop) Print/Display Clear/empty Size (if empty, print that it’s empty) Fill the stack with 52 values representing cards in a deck, then print a 7-card hand.

  • JAVA QUESTION 2.One use of a Stack is to reverse the order of input. Write a...

    JAVA QUESTION 2.One use of a Stack is to reverse the order of input. Write a complete method that reads a series of Strings from the user. The user enters "end" to stop inputting words. Then, output the Strings in reverse order of how they were entered. Do not output the String “end”. Use a stack to accomplish this task. Invoke only the methods push, pop, peek, and isEmpty on the stack object. Here is an example of how the...

  • Data Structures Java Language include also (Test class ) to test the methods Q2) Using a...

    Data Structures Java Language include also (Test class ) to test the methods Q2) Using a single queue (linkedQueue), re-implement the concept of Stack ADT, what is the complexity of the method push, pop, top, isEmpty, and size. You should not use any extra data structure.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT