创建一个 初始化的参数构造函数 年 变量 同时设置 天 和 月 变量 至 1。 这个构造函数必须调用三个 - 参数 构造函数
//constructor one
public MyDate(int year){
// this is what I have so far for the first constructor but it doesnt seem to be correct.
this.day = 1;
this.month = 1;
// call for the three parameter constructor .
MyDate myDate = new MyDate ( day=1 , month=1, year);
}
// constructor two
public MyDate(int day, int month, int year){
........
}
public static void main(String[] args){
//Check whether a user input advances correctly
java.util.Scanner scan = new java.util.Scanner(System.in);
System.out.println("Enter a date to advance, in format day month year: ");
int year,month,day;
day = scan.nextInt();
month = scan.nextInt();
year = scan.nextInt();
MyDate date = new MyDate(day, month, year);
System.out.println("Entered date is "+ date);
MyDate correctDate;
//TC 1 : one parameter constructor.
// not passing this test
date = new MyDate(2013);
correctDate = new MyDate(1,1,2013);
if (date.equals(correctDate))
System.out.println("TC 1 passed");
else
System.out.println("TC 1 failed");
}
答案 0 :(得分:1)
您的日期等级比现有的更糟糕。您应该使用java.time
包。
但如果你必须:
public class MyDate {
private final int year;
private final int month;
private final int day;
public MyDate(int y) {
this(y, 1, 1);
}
public MyDate(int y, int m, ind d) {
this.year = y;
this.month = m;
this.day = d;
}
}