题目:编程用构造方法输入今天的日期,输出明天的日期.
我不知道怎么编写输出明天的日期,请高手帮我改造一下程序
class date
{
private int year,month,day;
date()
{
this.year=2000;
this.month=1;
this.day=1;
}
date(int year,int month,int day)//某年某月某日的构造方法
{
this.year=year;
this.month=month;
this.day=day;
}
public int dayInmonth() //判断月份的天数
{
switch(this.month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
case 31:
return 31;
case 4:
case 6:
case 9:
case 11:
return 30;
default:
if(year%4==0&&year%100!=0||year%400==0)
return 29;
else
return 28;
}
}
public date tomrrow() //明天的日期
{
date d=new date();
d.day++;
if(d.day>d.Inmonth())
{d.month++;
d.day=1;
}
else if(d.month>12)
{d.year++;
d.month=1;
}
return d;
}
public void outDate() //打印今天的日期
{
System.out.println(this.year+"/"+this.month+"/"+this.day);
}}
public class exam3_5
{
public static void main(String [] args)
{
date d1=new date(2006,11,17);
d1.outDate();
}
}
我不知道怎么编写输出明天的日期,请高手帮我改造一下程序
class date
{
private int year,month,day;
date()
{
this.year=2000;
this.month=1;
this.day=1;
}
date(int year,int month,int day)//某年某月某日的构造方法
{
this.year=year;
this.month=month;
this.day=day;
}
public int dayInmonth() //判断月份的天数
{
switch(this.month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
case 31:
return 31;
case 4:
case 6:
case 9:
case 11:
return 30;
default:
if(year%4==0&&year%100!=0||year%400==0)
return 29;
else
return 28;
}
}
public date tomrrow() //明天的日期
{
date d=new date();
d.day++;
if(d.day>d.Inmonth())
{d.month++;
d.day=1;
}
else if(d.month>12)
{d.year++;
d.month=1;
}
return d;
}
public void outDate() //打印今天的日期
{
System.out.println(this.year+"/"+this.month+"/"+this.day);
}}
public class exam3_5
{
public static void main(String [] args)
{
date d1=new date(2006,11,17);
d1.outDate();
}
}
public date tomrrow() //明天的日期
{
date d=new date();
d.day++;
if(d.day>d.Inmonth())
{d.month++;
d.day=1;
}
else if(d.month>12)
{d.year++;
d.month=1;
}
return d;
}
这里date重新开辟了内存空间了.你的year,month,day几个变量又初始化了.
有两个建议,1、不要new它。
2、可以把变量设置成static
private int year, month, day; Date(int year, int month, int day)// 某年某月某日的构造方法
{
this.year = year;
this.month = month;
this.day = day;
} public int dayInmonth()// 判断月份的天数
{
switch (this.month) { case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
case 31:
return 31;
case 4:
case 6:
case 9:
case 11:
return 30;
default:
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
return 29;
else
return 28;
}
} public Date tomrrow()// 明天的日期
{
this.day++;
if (this.day > this.dayInmonth()) {
this.month++;
this.day = 1;
} else if (this.month > 12) {
this.year++;
this.month = 1;
}
Date d = new Date(this.year, this.month, this.day);
return d;
} public void outDate()// 打印今天的日期
{
System.out.println(this.year + "/" + this.month + "/" + this.day);
}}
public class Exam { /**
* @param args
*/
public static void main(String[] args) {
Date d2;
Date d1=new Date(2006,2,28);
d2 = d1.tomrrow();
d2.outDate();
}}
去掉那个new就好了。
private int year,month,day;
exam3_5 ()
{
year=2000;
month=1;
day=1;
}
exam3_5 (int a,int b,int c)//某年某月某日的构造方法
{
year=a;
month=b;
day=c;
}
exam3_5 (exam3_5 d)
{
year=d.year;
month=d.month;
day=d.day;
}public void outDate()
{System.out.print(year+"/"+month+"/"+day);
}
public Exam3_5 tomrrow()// 明天的日期
{ Exam3_5 d=new Exam3_5(this);
d.day++;
if (d.day > d.dayInmonth()) {
d.day = 1;
d.month++;
if (d.month > 12) {
d.month = 1;
d.year++;
}
}
return d;
}
public int daysInmonth()//判断月份的天数
{
switch(month)
{case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
case 31:
return 31;
case 4:
case 6:
case 9:
case 11:
return 30;
default:
if(year%4==0&&year%100!=0||year%400==0)
return 29;
else
return 28;
}
}
public static void main(String args[])
{
Exam3_5 d1=new Exam3_5();
System.out.print("The current date is(year/month/day):");
d1.outDate();
System.out.println();
System.out.print("The tomorrow is(year/month/day):");
d1.tomorrow().outDate();
Exam3_5 dd=new Exam3_5(2006,11,18);
System.out.print("The current date is(year/month/day):");
dd.outDate();
System.out.println();
System.out.print("The tomorrow is(year/month/day):");
dd.tomorrow().outDate();}
}