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

<p>Write a method that takes an integer value and returns the number with its di

ID: 3637342 • Letter: #

Question

<p>Write a method that takes an integer value and returns the number with its digits reversed.For example, given the number 7631, the method should return 1367.&#160;</p>

Explanation / Answer

// Reverse.java // Program takes a four digit number // and prints out its digits reversed import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Reverse extends JApplet implements ActionListener { JTextField input; JLabel prompt; int number; public void init() { input = new JTextField( 6 ); input.addActionListener( this ); prompt = new JLabel( "Enter a four digit number: " ); Container c = getContentPane(); c.setLayout( new FlowLayout() ); c.add( prompt ); c.add( input ); } public void actionPerformed( ActionEvent e ) { number = Integer.parseInt( input.getText() ); reverseDigits(); } public void reverseDigits() { int digit1 = 0, digit2 = 0, digit3 = 0, digit4 = 0, factor = 1000, value = 0; while ( factor >= 1 ) { int temp = number / factor; switch ( factor ) { case 1000: digit4 = temp; break; case 100: digit3 = temp * 10; break; case 10: digit2 = temp * 100; break; case 1: digit1 = temp * 1000; break; } number %= factor; factor /= 10; } if ( digit1 == 0 ) // special case when last digit initially is 0 showStatus( String.valueOf( 0 ) + String.valueOf( digit2 / 100 ) + String.valueOf( digit3 / 10 ) + String.valueOf( digit4 ) ); else showStatus( String.valueOf(digit1 + digit2 + digit3 + digit4) ); } }

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