Re: Unexpected Output Format
Quote:
Originally Posted by
playinmyblues
Hi. I have been using the book ...
I don't have that book, but here is what I found browsing through a description of the String class at the Java Docs Mother ship: Description of String.toString() method:
"toString---Returns a string representation of the object. In general, the toString method returns a string that 'textually represents' this object...It is recommended that all subclasses override this method..."
So, here's the thing:
System.out.print() and System.out.println() expect their arguments to be Strings. If the arguments are basic data types like ints or doubles (or whatever...) the values are "automatically" converted to Strings suitable for printing, and all is Good.
However...
If an argument is a class object, the object is converted to a string by the class toString() method. If you don't supply a toString() method for your class, you get a string that is something like what you saw: The class name and an @ sign and some kind of hash code that the compiler knows about for an object (but is totally irrelevant to "normal" programs and "normal" programmers).
So...
If you want to use System.out.print() or System.out.println() to print your rational number that looks like "3 / 7" or some such thing, then you can implement a toString() method in your RationalNumber class definition file.
For example for your class, you can define your toString() method to return a string whose value is numerator + "/" + denominator
The main() function, then, can be something like
Code java:
public static void main(String[] args)
{
RationalNumber a = new RationalNumber(3, 7);
System.out.println("a = " + a);
RationalNumber b = new RationalNumber(7, 3);
System.out.println("b = " + b);
RationalNumber c = b.add(a);
System.out.println("c = " + c);
}
And you should see something like
Code :
3
7
a = 3/7
7
3
b = 7/3
30
21
c = 30/21
Where the unlabeled output lines are from the debug print statements in your constructor. (As a matter of personal style, I always make output statements verbose enough to actually tell me something about what they mean. But maybe that's just me. I'm funny that way.)
Cheers!
Z
Re: Unexpected Output Format
You need to write a toString() method in your RationalNumber class
Re: Unexpected Output Format
Thank you for the answers. By the way, I think I have an error in my code that is not syntax but that would not effect the output that I described.
Re: Unexpected Output Format
Quote:
Originally Posted by
playinmyblues
...I think I have an error ...
Well, the result that came out of the main() that I showed was certainly incorrect, but I was trying to answer your question about how to display the rational number, not how to get the correct answer from the mathematical operation. (That's why I also printed out the original values from the constructor as well as the result of the addition. It verified that it can show the actual numbers.)
Anyhow...
After adding the toString() method to your class and running the test program, you have something to look at, and maybe you can figure out why it says 30/21 instead of the correct answer, 58/21. (Maybe you have figured it out already.)
Now, the next step will be to reduce the fraction to lowest terms, right? I mean, 58/21 is as good as it gets (I think), but what if you add 2/3 and 1/6. You don't really want to see 15/18 do you? Wouldn't you rather have 5/6? How will you approach that little problem? (Or is that not part of the assignment?)
Cheers!
Z