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

}
Biffen
  • 6,249
  • 6
  • 28
  • 36

2 Answers2

2

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

Community
  • 1
  • 1
Yassin Hajaj
  • 21,337
  • 9
  • 51
  • 89
  • Is it bad coding practice to have instance variables such as temp within the constructor? –  Nov 21 '15 at 09:18
  • @KapookyHandy If it is declared within the constructor, it is not an instance variable anymore but a local variable, usable only in the scope of your constructor. If you have the project to use it out of the constructor's scope, you should think of creating an instance one. – Yassin Hajaj Nov 21 '15 at 09:20
0

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

}
Shiladittya Chakraborty
  • 4,270
  • 8
  • 45
  • 94