英文:
I am losing data when going from constructor to main method in a different class. What is happening?
问题
public class tuna
{
private int hour;
private int minute;
private int second;
// 0 参数的构造函数
public tuna()
{
this(0,0,0);
}
// 1 参数的构造函数
public tuna(int h)
{
this(h,0,0);
}
// 2 参数的构造函数
public tuna(int h, int m)
{
this(h,m,0);
}
// 3 参数的构造函数
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 = ((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);}
/*----------获取方法----------*/
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());
}
}
class apples
{
public static void main(String[] args)
{
tuna obj1 = new tuna(); // 0 参数构造函数
tuna obj2 = new tuna(5); // 1 参数构造函数
tuna obj3 = new tuna(5,13); // 2 参数构造函数
tuna obj4 = new tuna(5,13,43); // 3 参数构造函数
System.out.printf("%s\n", obj1.toMilitary());
System.out.printf("%s\n", obj2.toMilitary());
System.out.printf("%s\n", obj3.toMilitary());
System.out.printf("%s\n", obj4.toMilitary());
}
}
英文:
```
public class tuna
{
private int hour;
private int minute;
private int second;
// Constructor with 0 passed arguments
public tuna()
{
this(0,0,0);
}
// Constructor with 1 passed arguments
public tuna(int h)
{
this(h,0,0);
}
// Constructor with 2 passed arguments
public tuna(int h, int m)
{
this(h,m,0);
}
// Constructor with 3 passed arguments
public tuna(int h, int m, int s)
{
setTime(h,m,s);
}
// setTime method
public void setTime(int h, int m, int s)
{
setHour(h);
setMinute(m);
setSecond(s);
}
/*----------Set methods----------*/
public void setHour(int h) {hour = ( (h>=0 && h<24)?h : 0);}
public void setMinute(int m) {hour = ( (m>=0 && m<60)?m : 0);}
public void setSecond(int s) {hour = ( (s>=0 && s<60)?s : 0);}
/*----------Get methods----------*/
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());
}
}
```
Above is the class containing the constructors and methods and below is the class with the main function that actually calls on the constructors and methods.
I was making this simple program that displays the time and the exercise was showing that you can have multiple constructors with different amounts of parameters. The only problem is when I am at the step of the constructor all the information is correct but when the debugger jumps from the constructor to the class it is being used in all the information is gone except this one line. I think it has something to do with the way I set the constructors up but I can not figure out what I did wrong. Any help would be great. Thank you!
Here is the output that I get:
The desired output:
And in case any of you recognize this exercise it is one of Bucky's Tutorials.
P.S. I normally would not post all the code but I literally have no idea what may or may not be pertinent to my problem.
class apples
{
public static void main(String[] args)
{
tuna obj1 = new tuna(); // No parameter constructor
tuna obj2 = new tuna(5); // 1 parameter constructor
tuna obj3 = new tuna(5,13); // 2 parameter constructor
tuna obj4 = new tuna(5,13,43); // 3 parameter constructor
System.out.printf("%s\n", obj1.toMilitary());
System.out.printf("%s\n", obj2.toMilitary());
System.out.printf("%s\n", obj3.toMilitary());
System.out.printf("%s\n", obj4.toMilitary());
}
}
答案1
得分: 2
你在所有的设置器中只分配了 hour
,但你应该设置 hour
、minute
和 second
:
/*----------设置方法----------*/
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);}
英文:
You are assigning hour
in all setters, while you should be setting hour
, minute
and second
:
/*----------Set methods----------*/
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);}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论