Class member in java and assignment -
below program have doubts about:
import java.util.scanner; class degrees { float faren; //float cel = (faren-32)*0.55f; doesn work void conversion() { system.out.println("fahrenheit "+(faren-32)*0.55f+"celcius"); //system.out.println("fahrenheit "+cel+"celcius"); doesnt work } } class problem4 { public static void main(string args[]) { degrees deg = new degrees(); system.out.println("enter temperature in fahrenheit"); try(scanner n1 = new scanner(system.in)) { deg.faren = n1.nextfloat(); } deg.conversion(); } } so 2 statements have made comments
float cel = (faren-32)*0.55f;// 1 , system.out.println("fahrenheit "+cel+"celcius");//this 1 both above statement not work. m assigning value of faren calling object still. can explain me why?
its simple program please don't correct mistakes if any. need figure out myself. me above 2 statement guys.
use instead:
class degrees { float faren = 0; float cel = 0; void degrees(float faren) { this.faren = faren; this.cel = (this.faren-32)*5.0f/9.0f; } void conversion() { //system.out.println("fahrenheit "+(this.faren-32)*0.55f+"celcius"); system.out.println("fahrenheit "+this.cel+"celcius"); } } use this:
var deg = new degrees(farheneitvaluehere); deg.conversion(); this float cel = (faren-32)*0.55f; not work because cannot initialise property when defined expression (especially expression includes property). system.out.println("fahrenheit "+cel+"celcius"); not work because cel has not been defined/converted (by 1st reason). initialise values in constructor instead (as in example above)
Comments
Post a Comment