Reputation: 7
When I run this program, it returns:
00:00:00
00:00:00
00:00:00
00:00:00
What am I doing wrong and how can I get it to return the numbers I intended to return?
This is the code in my main class.
public static void main(String[] args) {
tuna tunaObject1 = new tuna();
tuna tunaObject2 = new tuna(5);
tuna tunaObject3 = new tuna(5, 13);
tuna tunaObject4 = new tuna(5, 13, 43);
System.out.println(tunaObject1.toMilitary());
System.out.println(tunaObject2.toMilitary());
System.out.println(tunaObject3.toMilitary());
System.out.println(tunaObject4.toMilitary());
}
}
This is my code in the class outside the main one.
public class tuna {
private int hour;
private int minute;
private int second;
public tuna() {
this(0, 0, 0);
}
public tuna(int h) {
this(h, 0, 0);
}
public tuna(int h, int m) {
this(h, m, 0);
}
public tuna(int h, int m, int s) {
setTime(h, m, s);
}
public void setTime(int h, int m, int s) {
setHour(h);
setMinute(m);
setSecond(s);
}
public void setHour(int h) {
hour = ((hour >= 0 && hour < 24) ? hour:0);
}
public void setMinute(int m) {
minute = ((minute >= 0 && minute < 60) ? minute:0);
}
public void setSecond(int s) {
second = ((second >= 0 && second < 60) ? second:0);
}
public int getHour() {
return hour;
}
public int getMinute() {
return minute;
}
public int getSecond() {
return second;
}
public String toMilitary() {
return String.format("%02d:%02d:%02d", getHour(), getMinute(), getSecond());
}
}
Upvotes: 0
Views: 260
Reputation: 21
Assuming intended output is 00:00:00 05:00:00 05:13:00 05:13:43
you have to make changes in the tuna class
public void setHour(int h) {
hour = ((hour >= 0 && hour < 24) ? h:0);
}
public void setMinute(int m) {
minute = ((minute >= 0 && minute < 60) ? m:0);
}
public void setSecond(int s) {
second = ((second >= 0 && second < 60) ? s:0);
}
Reason: In setter methods , value passed should be assigned to class property/attributes and here value passed is stored in variable "s" not in "second".
Upvotes: 0
Reputation: 51
please try these methods: The passing values have no effect on the actual fields due to which they are displaying default values.
public void setHour(int h) {
hour = ((h >= 0 && h < 24) ? h : 0);
}
public void setMinute(int m) {
minute = ((m >= 0 && m < 60) ? m : 0);
}
public void setSecond(int s) {
second = ((s >= 0 && s < 60) ? s : 0);
}
Upvotes: 0
Reputation: 556
public void setHour(int h) {
hour = ((hour >= 0 && hour < 24) ? hour:0);
}
should be
public void setHour(int h) {
hour = ((h >= 0 && h < 24) ? h:0);
}
Likewise for other setters
Upvotes: 3
Reputation: 12880
Change this
public void setHour(int h) {
hour = ((hour >= 0 && hour < 24) ? hour:0);
}
should be
public void setHour(int h) {
hour = ((h >= 0 && h < 24) ? h:0);
}
Likewise, for others you would have to change. The problem is you have wrongly assigned the variables. Please look at the answer from @Maroun Maroun. He has clearly told you why it didn't work. Please follow Java conventions while writing code.
Upvotes: 2
Reputation: 95948
Look for example at your setHour
method:
hour = ((hour >= 0 && hour < 24) ? hour:0);
Note that class members has default values, since hour
is an int
, it gets 0 as default value (JLS 4.12.5. Initial Values of Variables). So your method is equivalent to:
hour = ((0 >= 0 && 0 < 24) ? 0 : 0);
The same holds for other methods.
Additional notes:
Tuna
After I described the problem, it's easy to fix it. Use h
instead of hour
, I'm sure you can handle this by yourself.
Upvotes: 3