java - returning a value within a abstract method -
i have abstract class called temperature 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 have celcius , fahrenheit class extend temperature, space sake ill show celcius
public class celsius extends temperature { public celsius(float t) { super(t); } public string tostring() { // todo: complete 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 main program creates new celcius object follows
temperature inputtemp = null , outputtemp = null; inputtemp = new celsius(temp_val); outputtemp assigned inputemp calling tofahrenheit method
outputtemp = inputtemp.tofahrenheit(); the resulting answer placed in tostring method
outputtemp.tostring as far know, celcius constructor uses temperature constructor store passed in parameter of temp_val. however, gets me confused how return converted value of celcius fahrenheit inputtemp.tofahrenheit method?? tried returning this.getvalue() * 9 / 5 + 32, eclipse complains since getvalue() float method, either have change overriding method float, or change getvalue() temperature, both dont work....
in celsius, tocelsuis should return this because temperature in celsius. tofahrenheit method should return new fahrenheit instance :
@override public temperature tocelsius() { return this; } @override public temperature tofahrenheit() { return new fahrenheit(this.getvalue() * 9d / 5d + 32d); } and other way around fahrenheit class.
Comments
Post a Comment