Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

2. Write a program that reads in an integer and breaks it into a sequence of ind

ID: 3638274 • Letter: 2

Question

2. 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

  1. publicclassDigitExtractor{
  2. inttoBeBroken;
  3. /**
  4. Constructsadigitextractorthatgetsthedigitsinreverseorder.
  5. @paramanInteger-theintegertobreakupintodigits
  6. */
  7. publicDigitExtractor(intanInteger){
  8. toBeBroken=anInteger;
  9. }
  10. /**
  11. Returnsthenextdigittobeextracted.
  12. @returnthenextdigit
  13. */
  14. publicintnextDigit(){
  15. inttemp=-1;
  16. if(toBeBroken!=0)
  17. {
  18. temp=toBeBroken%10;
  19. toBeBroken=toBeBroken/10;
  20. }
  21. else
  22. {
  23. System.out.println("Nomoredigitstoextract");
  24. }
  25. returntemp;
  26. }
  27. }
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote