• Post Reply Bookmark Topic Watch Topic
  • New Topic

Method that changes and Int to a Double  RSS feed

 
Chetan Graham
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello to All,

I am working the programming book "Think Like a Computer Scientist in Java" and am on page 138 with this problem.
I like the Author's manner of teaching as well as his idea that the title is a joke of sorts. (Agreed this latter could spark blog wars about the current state of "Computer Scientists coming out of schools.")

The lesson:
"Write a method called toDouble that converts the rational number to a double
(floating-point number) and returns the result. This method is a pure function; it does not modify the object. As always, test the new method."

There are other minor errors in the other methods that are getting slowly 'debugged.'
This is my work on this set of examples with a roadblock on the method toDouble. By the way I use Eclipse Europa on Linux.
Blessings,
Chetanji


 
Chetan Graham
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To All,
I just wanted to add that I have tried adding a fourth 'double constructor' that uses numerator and denominator instead of the int objects 'num' and 'denom'.
I found that it just made things more complicated.
I know there is a simple way to do this. I am just bouncing off the wall that is barring my way forward.
Any help is much appreciated. I am excited about moving on to the more serious OOP programming. I know it's one step at a time though.
Chetanji
 
Chetan Graham
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please change the code to this. This does nothing but retain the 'int'.
I am searching for the way to make this method turn the rational to a Double.

 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
return y.num / (double) y.denom;
}
[/code]

By the way, why have you made all the methods static? A much more Java-like way to write this class would have many the methods be instance methods and operate directly on the object itself:

[code]
public double toDouble (){
return num / (double) denom;
}
[/code]
 
Chetan Graham
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, why have you made all the methods static? A much more Java-like way to write this class would have many the methods be instance methods and operate directly on the object itself:

code:



[Chetan writes:]
I am a beginner with OOP and Java and am just following the examples in the above mentioned document written by a University Professor. He said in the beginning he will bring things along slowly and by the end of the document you will have a decent awareness of quality Java coding.
I am only just beginning the Object making part of it now. I am not even half way through the lessons.
I much appreciate your help and will adopt your lesson.
Thank you
Chetanji
 
Campbell Ritchie
Marshal
Posts: 55761
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know I ought not to butt into other people's discussions, but . . .

There are lots of books which put lots of code into the main method. The Java Tutorial does it too. This is nice for printing because it takes less paper, and allows one to see a particular syntactic point in less space.
BUT: It does not demonstrate creating objects at all well. I always worry about books, whether free or paid-for, which put so much code into main methods. Downey says that he delays object-orientation so as to introduce other concepts first. I prefer to see object-orientation introduced very early (as, for example, Deitel and Deitel do). Otherwise you get the sort of confusion see in this thread.

The main method is static, so it cannot access instance members, only other static members. Mr Friedmann-Hill's approach is correct. Call the toDouble() method from inside the object.

And did you notice the (double) cast? If you pass two ints and try to divide them, you will get an int as the result, not a fraction.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm! Something weird happened to my reply above -- you see how the code tag is cut off? The first half of my post, where I actually explained why that cast is there, is missing for some reason. Normally I wouldn't just show something like that and not explain it.

In Java, the result of dividing two ints is an int. Since an int has no fractional part, that means that if you divide, say, 2/4, the answer is 0 -- which is 0.5 with the fractional part cut off. If you want the "right" answer, you have to do the calculation with doubles or floats. You can do this just by casting one number to a double, and Java will automatically promote the other one, too. That's why I've just done the one cast.
 
Campbell Ritchie
Marshal
Posts: 55761
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and I thought you were testing his powers of observation about the (double) cast.

But more about the "Think Like A Computer Scientist" book . . . Apart from the fact that it leaves proper object orientation to chapter 9, which I don't like, I have found a few minor mistakes in it, not many, probably only three so far:
  • It says you can't do comparison between ints and doubles.
  • It says you can't apply the ++ and -- operators to doubles, only to ints.
  • It says that int fields default to 0 if they are not initialised, and booleans to true.
  • Shame, since the book is so easy to read and understand.
     
    Chetan Graham
    Greenhorn
    Posts: 21
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you
    Ernest Friedman-Hill
    and Campbell Ritchie
    for your help and replys.

    I very much appreciate both of your open willingness to help someone obviously new to a language so rich and diverse as Java; a true 'programmers' language.

    Probobly the main reason I am using Downey's book over so many others is how much I relate to his style of writing and simple manner of dispatching the information. I rarely feel overwhelmed. And he seems to build one lesson on another so all you have to do if stumped is look harder at what was written in the last lesson as an example.

    However, I am moving into a SOFIA, Hibernate, Beans, MySQL, JSP framework where the company is developing large user base programs that can affect thousands of users simultaneously, spread over different locations and this indeed can be a bit overwhelming when I look at my simple lessons compared to the code these amazing Java Coders have beautifully crafted in my office.

    I guess it all takes time. Thanks again.
    Chetanji
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!