Write a program that reads in an integer and breaks it into a sequence of indivi
ID: 3530299 • Letter: W
Question
Write a program that reads in an integer and breaks it into a sequence of individual digits in reverse order. For example, the input 16384 is displayed as 4 8 3 6 1 You may assume that the input has no more than five digits and is not negative. Declare a class DigitExtractor: public class DigitExtractor { /** Constructs a digit extractor that gets the digits of an integer in reverse order. @param anInteger the integer to break up into digits */ public DigitExtractor(int anInteger) { . . . } /** Returns the next digit to be extracted. @return the next digit */ public int nextDigit() { . . . } } In your main class DigitPrinter, call System.out.println(myExtractor.nextDigit()) five times.Explanation / Answer
- publicclassDigitExtractor{
- inttoBeBroken;
- /**
- Constructsadigitextractorthatgetsthedigitsinreverseorder.
- @paramanInteger-theintegertobreakupintodigits
- */
- publicDigitExtractor(intanInteger){
- toBeBroken=anInteger;
- }
- /**
- Returnsthenextdigittobeextracted.
- @returnthenextdigit
- */
- publicintnextDigit(){
- inttemp=-1;
- if(toBeBroken!=0)
- {
- temp=toBeBroken%10;
- toBeBroken=toBeBroken/10;
- }
- else
- {
- System.out.println("Nomoredigitstoextract");
- }
- returntemp;
- }
- }
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.