Exercise 1: Write a program using the java.util.Stack data structure that prompts for an integer and reverses it: 7359 becomes 9537. Hints: Think of how base-10 works: A base-10 number is a sum of...




Exercise 1:


Write a program using the java.util.Stack data structure that prompts for an integer and reverses it:  7359 becomes 9537.



Hints:



  1.   Think of how base-10 works:  A base-10 number is a sum of digit * 100 + digit * 101+ digit * 102, etc.,  where each digit in your number is that digit times a power of 10.

  2. Using that understanding, you can use modulo and integer arithemetic to get each digit and add it to a stack.   Algorithm:   Your number % 10 will give you the rightmost number.  Example:  7359 % 10 = 9.   Put the 9 on the stack.  Now substract the 9 from original number:  7359 - 9 = 7350.  Now take the  7350/10 to remove the zero.  That is 7350 /10 = 735.  Repeat the process, that is 735 % 10 = 5,  put the 5 on the stack,  substract the 5 and divide by 10.  730/10 = 73, use 73 % 10 to get the 3.    Keep going,  adding digits to the stack until you run out of numbers.

  3.  When you pop the numbers off your stack, they will come off teh stack reversed.

  4. Note yourStack.isEmpty() tells you whether there are more elements left in the stack so that you will know when to stop.




Jun 03, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here