Question
Question: Write a program that converts decimal to binary using a stack. Ask the user to enter a decimal number, then print the equivalent binary
Question: 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.
1) 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
2) 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 3 4 6
StackTest.java
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() }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started