0
whats the problem?
the purpose is to convert seconds to minutes / hours. code: class Time{ double sec; double min = sec / 60; double hour = min / 60; Time (int sec){ this.sec = sec; } } public class Main { public static void main(String[] args) { Time t = new Time(12); System.out.println(t.min); } }
2 Answers
+ 1
What you have done here is declared a constructor that only assigns value to instance variable 'sec'. But that 'sec' is separate for each instance of class Time.
Since you have not declared any value to 'sec' it takes the default value of 0.0 so your 'min' will be 0.0/60 which is 0.0
So your class must look like this for your code to work.
class Time{
double sec;
double min;
double hour;
Time (double sec){
this.sec = sec;
this.min = sec/60;
this.hour = min/60;
}
}
0
Avinash , thank you!