//Single parameter
public Time(double input){
number= 20.75;
double temp ;
temp= (number%1*60*100)/100; // Prints out 0.00 why?
minutes= (int)temp;
hours= (int)number-(int)number%1;
}
My code is working perfectly fine, but I'm a bit confused as to why it printed out 0.00
instead of 45.0000
for the temp
variable.
How I think it works: number
= 20.75, therefore 20.750000 % 1 = 0.750000 * 60 * 100 / 100 = 45.0000, therefore temp
= 45.00000.
Here is the full code:
public class TimetestProgram {
public static void main(String[]args){
Time object = new Time(20,329);
Time gamma= new Time(20.75);
System.out.println(object);
System.out.println(gamma);
System.out.println("temp:"+gamma.temp);
System.out.println("minutes:"+gamma.minutes);
System.out.println("number:"+gamma.number%1);
}
}
// Double parameter
class Time {
int hours,minutes;
double number,temp;
public Time(int x,int y){
hours= x;
minutes=y;
hours+=minutes/60;
minutes%= 60;
}
//Single parameter
public Time(double input){
number= input;
double temp ;
temp= (number%1*60*100)/100;
minutes= (int)temp;
hours= (int)number-(int)number%1;
}
public String toString(){
return String.format(hours+":"+minutes);
}
}