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

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

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 * 10 + digit * 10+ 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. 
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 3 images

Blurred answer
Knowledge Booster
Adjacency Matrix
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education