Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Calling a method in a class  RSS feed

 
Pete Low
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am checking how I can call a method getFarenH(double degreesC )
from another method.

Below is the relevant portion of the class temperature.
Thnks


class Temperature {
private double value;
private char scale;

// some constructors

// Mutator Methods



public double getFarenH(double degreesC )
{
double degreesF = 0;
degreesF = ( (9/5 * degreesC) + 32 ) ;
value = degreesF;
return value;
}



public boolean isLessThan(Temperature other)
{
double F = 0;
Temperature FarenH = new Temperature();
FarenH.getFarenH(F);
F = value;
return true;
}
//
//

}

 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like you're calling the method just fine with FarenH.getFarenH(F); The problem is that the returned value isn't being assigned to anything, like it would be if you used...

F = FarenH.getFarenH(F);
[ June 15, 2005: Message edited by: marc weber ]
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
boy, i'd really reccomend against using variables with names like "F". it really doesn't tell me much about what the variable is.

Even if i know that F is supposed to be farenheight, i'd be confused because you're passing it into a method that is expecting a Celsius temp...

and then what marc said - you're getting the value back, but your not saving it.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might also consider that division of type int always truncates. And in the context of (9/5 * 2.0), it appears that 9/5 is evaluated as an int before being widened to type double. See what you get out of compiling and running this...

[ June 15, 2005: Message edited by: marc weber ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!