Saturday, November 03, 2007

Wracking my Brains 2

I have moved on to Chapter 5, "Methods", and I was about to give up on this problem:
5.4** (Returning an integer reversed) Write the following method to return an integer reversed:
public static int reverse(int number)

For example, reverse(3456) returns 6543.
I was running TestReturnReverse.java which referred to a method reverse in ReturnReverseClass.java, and I was not getting the results I wanted. For instance, inputting 12345 would yield "The reverse of 12345 is 5." I was getting frustrated, so I made a separate Test.java which had the reverse method integrated into the main class. Look at the three different code snippets below:

==
TestReturnReverse.java
==
import javax.swing.JOptionPane;

public class TestReturnReverse {
public static void main(String[] args) {
String numberInputString = JOptionPane.showInputDialog("Enter a number more " +
"than one to be reversed in value:");
int numberInput = Integer.parseInt(numberInputString);
int reverseNumber = ReturnReverseClass.reverse(numberInput);
System.out.print("The reverse of " + numberInput + " is " + reverseNumber + ".");
}
}

==
ReturnReverseClass.java
==
public class ReturnReverseClass {
public static int reverse(int number) {
int numOfDigits = 1;
int sumDigits = 0;
int singleDigit = 0;
int placeholderVal = number;
while (placeholderVal > 10) {
int placeholder = placeholderVal / 10;
placeholderVal = placeholder;
numOfDigits++;
}
for (int count = 1; count <= numOfDigits; count++) { int a = (int)(number % (Math.pow(10, count))); int b = a / (int)(Math.pow(10, (count - 1))); singleDigit = b * (int)(Math.pow(10, (numOfDigits - count))); sumDigits += singleDigit; } return sumDigits; } }
==
Test.java
==
public class Test { public static void main(String[] args) { int number = 12345; int numOfDigits = 1; int sumDigits = 0; int singleDigit = 0; int placeholderVal = number; while (placeholderVal > 10) {
int placeholder = placeholderVal / 10;
placeholderVal = placeholder;
numOfDigits++;
System.out.println(placeholder + " , " + placeholderVal + " , "
+ numOfDigits);
}
for (int count = 1; count <= numOfDigits; count++) { int a = (int)(number % (Math.pow(10, count))); int b = a / (int)(Math.pow(10, (count - 1))); singleDigit = b * (int)(Math.pow(10, (numOfDigits - count))); sumDigits += singleDigit; System.out.println(a + " , " + b + " , " + singleDigit + " , " + sumDigits); } System.out.println("The reverse of " + number + " is " + sumDigits + "."); } }
==

See any difference? There is no difference. Imagine my frustration when, after running Test.java, I see my compiler output:
1234 , 1234 , 2
123 , 123 , 3
12 , 12 , 4
1 , 1 , 5
5 , 5 , 50000 , 50000
45 , 4 , 4000 , 54000
345 , 3 , 300 , 54300
2345 , 2 , 20 , 54320
12345 , 1 , 1 , 54321
The reverse of 12345 is 54321.
What??? I was ready to give up when I saw an icon on the tab referring to ReturnReverseClass.java - it was a badge indicating that the file needed to be compiled.

Sure enough, when I compiled it and ran TestReturnReverse.java, I got the results I wanted. All that trouble for nothing.

Careless, careless, careless.

No comments: