0

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()); } } 
7
  • what is the expected result? Commented May 26, 2015 at 7:03
  • 4
    Your assign the instance fields back to them selves, hour = ((hour >= 0 && hour < 24) ? hour:0);, when h is the parameter Commented May 26, 2015 at 7:03
  • 1
    You actually never really set anything; all parameters of your setters are just plainly ignored. Look carefully at your code... Commented May 26, 2015 at 7:04
  • 2
    You should be using h and not hour. Same issue occurs for second and minute Commented May 26, 2015 at 7:05
  • 2
    I would suggest following the java style of starting class names with a capital letter Commented May 26, 2015 at 7:09

5 Answers 5

3

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:

  • Debug your code - the debugger will help you to discover the reasons of bugs very quickly
  • Follow the Java Naming Conventions and rename your class to 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.

Sign up to request clarification or add additional context in comments.

Comments

3
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

Comments

2

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.

Comments

0

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

Comments

0

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".

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.