the out put in console is differint

0 Amro Sadik · January 23, 2015
00:00:00
00:00:00
no change occur after write the code
please show me what my mistake
public class tuna {

private int hour;
private int minute;
private int seconde;

public void setTime(int h,int m,int s){

h = ((h<=0 && h>24) ? h : 0);
m = ((m<=0 && m>24) ? m : 0);
s = ((s<=0 && s>24) ? s : 0);


}

public String toMilitary(){

return String.format("%02d:%02d:%02d",hour,minute,seconde);
}


}




 

public class apples {

public static void main(String args[]){

tuna tunaObject = new tuna();
System.out.println(tunaObject.toMilitary());

tunaObject.setTime(10, 14, 20);
System.out.println(tunaObject.toMilitary());
}
}


Post a Reply

Replies

Oldest  Newest  Rating
+1 Scott M · January 23, 2015
^ That, and your conditional statements will always be false, so it will always assign the variables to 0.

Look at this and compare it to yours:
public void setTime(int h, int m, int s) {
hour = ((h>0 && h<=24) ? h : 0);
minute = ((m>0 && m<=24) ? m : 0);
seconde = ((s>0 && s<=24) ? s : 0);
}
0 Mike Conroy · January 23, 2015
This is because you are not setting the Objects variables to the values when you call the setTime method.
Try this:

	public void setTime(int h,int m,int s){

hours = ((h<=0 && h>24) ? h : 0);
minute = ((m<=0 && m>24) ? m : 0);
seconde = ((s<=0 && s>24) ? s : 0);
}
0 Mike Conroy · January 24, 2015
I didn't even spot that Scott!
I noticed the incorrect variables being used and just assumed that would be the only problem but you are right.
  • 1

Java / Android Development

107,083 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator