user4179796
user4179796

Reputation:

Why did my code print out 0.00 instead of 45.000

//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);
  }

}

Upvotes: 1

Views: 88

Answers (2)

Yassin Hajaj
Yassin Hajaj

Reputation: 21965

You have an instance variable :

double temp;

aswell as a local variable within the constructor.


You should only have one and it must be the instance variable since you're calling gamma.temp in your main, delete the below line in the constructor

Also, I'd recommend you to use encapsulation (see below) to access your variables outside of your class.


Solution

public Time(double input){
    number=  input;
    //double temp ; This should be deleted


    this.temp =  (number%1*60*100)/100; 
    minutes= (int)temp;
    hours= (int)number-(int)number%1;

}

Useful Links

Upvotes: 2

Shiladittya Chakraborty
Shiladittya Chakraborty

Reputation: 4408

This will work for you :

Remove temp variable declaration from Time constructor, thats why you are getting 0.00 every time.

public class Test {
  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;
    temp =  (number%1*60*100)/100; 
    minutes= (int)temp;
    hours= (int)number-(int)number%1;

  }


  public String toString(){
    return String.format(hours+":"+minutes);
  }

}

Upvotes: 0

Related Questions