aspose file tools*
The moose likes Beginning Java and the fly likes Help on understanding this code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help on understanding this code" Watch "Help on understanding this code" New topic
Author

Help on understanding this code

Stacey Johnson
Ranch Hand

Joined: Jan 11, 2004
Posts: 55
Hello everyone,
Here I am agian struggling to figure things out. Right now I am trying to understand this class and test program to go with. I just don't understand how it works. Can anyone take a look at it and give me some insight. In my assignment I'm suppose to use the following class (Rational Class) and write a program thta will compute a summation series as follows:
1/1 + 1/2 + 1/3 +... +1/n
1/1 + 1/2 + 1/2exponent2 +...+ 1/2exponentn
So I figure I need a better understanding of this class before I attempt a program for the above.
One other thing is can someone explain the "tostring()" method to me?
The Rational Class is as follows:

And the testprogram:

Thanks
Stacey
Carlos Failde
Ranch Hand

Joined: Oct 20, 2000
Posts: 84
The Java Tutorial

Object contains the toString method, which returns a String object containing the name of the object's class and its hash code. Most, if not all, classes will want to override this method and print out something meaningful for that class.



toString()

Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method.


The above class overrides toString to return a representation of a rational nubmer.
Incidentally the println's in TestRationalClass could have been simplified:

as the toString() method is automatically invoked when you concatenate an object and a string
 
Consider Paul's rocket mass heater.
 
subject: Help on understanding this code