• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Help on understanding this code

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
See where your hand is? Not there. It's next to this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic