java - how to print object value with String method -
i have abstract class
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(); public abstract temperature tokelvin(); }
then have classes extend temperature class, example:
public class celsius extends temperature { public celsius(float t) { super(t); } public string tostring() { return ""; } @override public temperature tocelsius() { // todo auto-generated method stub return this; } public temperature tokelvin(){ return new kelvin(this.getvalue() + 273); } @override public temperature tofahrenheit() { // todo auto-generated method stub return new fahrenheit(this.getvalue() * 9 / 5 +32); }
}
main method creates objects of of celcius
temperature inputtemp = null, outputtemp = null; inputtemp = new celsius(temp_val); outputtemp = inputtemp.tocelsius();
then prints object calling method
system.out.println("\n converted temperature " + outputtemp.tostring() +"\n\n"); }
what have put in tostring method in order print desired value? this.super.getvalue() didnt work , im kinda clueless. since not going returning same object everytime, dont have use superclass?
it enough if use:
public string tostring() { return float.tostring(this.getvalue()); }
Comments
Post a Comment