Reputation: 1515
I have a abstract class called temperature with the following
public abstract class Temperature
{
private float value;
public Temperature(float v)
{
value = v;
}
public final float getValue()
{
return value;
}
public abstract Temperature toCelsius();
public abstract Temperature toFahrenheit();
}
then i have a Celcius and Fahrenheit class that extend from temperature, but for space sake ill show Celcius
public class Celsius extends Temperature
{
public Celsius(float t)
{
super(t);
}
public String toString()
{
// TODO: Complete this method
return "";
}
@Override
public Temperature toCelsius() {
// TODO Auto-generated method stub
return null;
}
@Override
public Temperature toFahrenheit() {
// TODO Auto-generated method stub
return null;
}
}
so my main program creates a new celcius object as follows
Temperature inputTemp = null , outputTemp = null;
inputTemp = new Celsius(temp_val);
outputTemp is then assigned to inputemp calling the toFahrenheit method
outputTemp = inputTemp.toFahrenheit();
the resulting answer is then placed in the toString method
outputTemp.toString
As far as i know, the Celcius
constructor uses the Temperature
constructor to store the passed in parameter of temp_val
. However, what gets me confused is that how do i return the converted value of Celcius
to Fahrenheit
with inputTemp.toFahrenheit
method??
I tried returning this.getValue() * 9 / 5 + 32
, but eclipse complains that since getValue()
is a float
method, i either have to change the Overriding method to float
, or change getValue()
a Temperature
, but both dont really work....
Upvotes: 1
Views: 5666
Reputation: 1472
Why don't you change the toCelsius and toFahrenheit methods to return a float rather than a temperature?
@Override
public Float toFahrenheit() {
// TODO Auto-generated method stub
return (getValue() * 9.0 /5.0 +32.0 );
}
Also, does it really make sense to declare the methods toFahrenheit and toCelcius in the base class? Would you not rather declare toFahrenheit only in the Celcius class and toCelcius only in the Fahrenheit class?
Upvotes: 0
Reputation: 1406
In the toFahrenheit()
you need to return the current object of type Temperature like this,
@Override
public Temperature toFahrenheit() {
// TODO Auto-generated method stub
return this;
}
you can get the converted resultant value like this,
Temperature inputTemp = null , outputTemp = null;
inputTemp = new Celsius(temp_val);
outputTemp = inputTemp.toFahrenheit();
float result=outputTemp.getValue();
Upvotes: 0
Reputation: 53829
In Celsius
, toCelsuis
should return this
because the Temperature
is already in celsius. The toFahrenheit
method should return a new Fahrenheit
instance :
@Override
public Temperature toCelsius() {
return this;
}
@Override
public Temperature toFahrenheit() {
return new Fahrenheit(this.getValue() * 9D / 5D + 32D);
}
And the other way around for the Fahrenheit
class.
Upvotes: 3
Reputation: 27702
toFahrenheit
returns a reference to Temperature
.
You have only two concrete classes matching that return value: Celsius
and Fahrenheit
so you should return a new object of one of these classes. Presumably Fahrenheit
is it seems to be the aim of your method so, for Celsius
class, you should have something like:
@Override
public Temperature toFahrenheit() {
return new Fahrenheit( this.getValue() * 9.0 /5.0 +32.0 );
}
And, as your class is already representing temperature in celsius:
@Override
public Temperature toCelsius() {
return this;
}
Upvotes: 1