Question: Need help with this Java program please. StackX code: // stack.java // demonstrates stacks // to run this program: C>java StackApp //////////////////////////////////////////////////////////////// class StackX {
Need help with this Java program please.

StackX code:
// stack.java // demonstrates stacks // to run this program: C>java StackApp //////////////////////////////////////////////////////////////// class StackX { private int maxSize; // size of stack array private long[] stackArray; private int top; // top of stack //-------------------------------------------------------------- public StackX(int s) // constructor { maxSize = s; // set array size stackArray = new long[maxSize]; // create array top = -1; // no items yet } //-------------------------------------------------------------- public void push(long j) // put item on top of stack { stackArray[++top] = j; // increment top, insert item } //-------------------------------------------------------------- public long pop() // take item from top of stack { return stackArray[top--]; // access item, decrement top } //-------------------------------------------------------------- public long peek() // peek at top of stack { return stackArray[top]; } //-------------------------------------------------------------- public boolean isEmpty() // true if stack is empty { return (top == -1); } //-------------------------------------------------------------- public boolean isFull() // true if stack is full { return (top == maxSize-1); } //-------------------------------------------------------------- } // end class StackX //////////////////////////////////////////////////////////////// class StackApp { public static void main(String[] args) { StackX theStack = new StackX(10); // make new stack theStack.push(20); // push items onto stack theStack.push(40); theStack.push(60); theStack.push(80);
while( !theStack.isEmpty() ) // until it's empty, { // delete item from stack long value = theStack.pop(); System.out.print(value); // display it System.out.print(" "); } // end while System.out.println(""); } // end main() } // end class StackApp ////////////////////////////////////////////////////////////////
StackTest.java is:
class StackTest { public static void main(String[] args) { StackX theStack = new StackX(10); // make new stack theStack.push(20); // push items onto stack theStack.push(40); theStack.push(60); theStack.push(80);
theStack.print(); theStack.removeSecond(); System.out.println("After removeSecond()"); theStack.print(); } // end main() }
QueueTest.java is:
class QueueTest { public static void main(String[] args) { Queue theQueue = new Queue(10); // queue holds 15 items theQueue.insert(10); // insert 4 items theQueue.insert(20); theQueue.insert(30); theQueue.insert(40);
theQueue.print(); theQueue.removeSecond(); System.out.println("After removeSecond()"); theQueue.print(); } // end class QueueApp }
1- Write a program that converts decimal to binary using a stack. Ask the user to enter a decimal number, then print the equivalent binary number. 2a- Add a method to the Stack class print() to print the contents starting with the earliest to the latest. Eg: stk.push(3); stk.push(4); stk.push(5): stk.print(); // would print 3 4 5 2b-Write a method public long removeSecond (): It removes and returns the element just below the top element if the stack has at least two elements, otherwise it simply returns null without modifying the stack Eg: stk.push(3): stk.push(4); stk.push(5): stk.push(6); stk.removeSecond(); stk.print(): //would print 346 StackTest.java B 3a-Add a method to the Queue class print() to print the contents starting with the head. Eg: que.insert(3); que.insert(4): que.insert(5); que.print(); //would print 3 4 5 3b- Write a method public static Object removeSecond (): It removes and returns the element just behind the front element. Precondition: The queue has at least two elements. Eg: que.insert(3); que.insert(4); que.insert(5); que.insert(6); que.removeSecond(): que.print() //would print 356 QueueTest.java
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
