我的任务是完成下面显示的Date类。 Date类通过在私有实例变量中存储月,日和年来封装日期。
public class Date
{
// declare your instance variables here
// postcondition: instance variables are initialized with any valid values
// you choose
public Date ()
{
}
// postcondition: instance variables are initialized with the given values if they are valid
// the month instance variable should be between 1-12
// the day instance variable should be between 1-31
// if the parameters are not valid, the instance variables are given different values that are valid
public Date(int theMonth, int theDay, int theYear)
{
}
// postcondition: returns a String in the form month/day/year
public String toString()
{
}
}
以下代码是我到目前为止所做的。坦率地说,我对我应该做的事情感到很困惑,而且我没有教练要问。输出为“2/2/0”
编辑更新:如果我输入无效的年份,例如200,则不会打印错误消息...我对if语句的意图是捕获年份不是4位数的错误。它是否正确?感谢您的帮助!
public class Date
{
// declare your instance variables here
private int myMonth;
private int myDay;
private int myYear;
// postcondition: instance variables are initialized with any valid values
// you choose
public Date ()
{
myMonth = 11;
myDay = 11;
myYear = 2011;
}
// postcondition: instance variables are initialized with the given values if they are valid
// the month instance variable should be between 1-12
// the day instance variable should be between 1-31
// if the parameters are not valid, the instance variables are given different values that are valid
public Date(int theMonth, int theDay, int theYear)
{
if ( theMonth >= 1 && theMonth <= 12 ) {
myMonth = theMonth;
}
else {
System.out.print("Month Value invalid; default to 1");
myMonth = 1;
}
if( theDay >= 1 && theDay <= 31 ) {
myDay = theDay;
}
else {
System.out.print("Day Value invalid; default to 1");
myDay = 1;
}
if( theYear < 4 ) {
System.out.print("Year Value invalid; default to 2000");
myYear = 2000;
}
else {
myYear = theYear;
}
}
// postcondition: returns a String in the form month/day/year
public String toString()
{
return myMonth + "/" + myDay + "/" + myYear;
}
public static void main(String [] args)
{
int theMonth, theDay, theYear;
theMonth = 2;
theDay = 2;
theYear = 200;
Date date = new Date(theMonth, theDay, theYear);
date.toString();
System.out.println(date);
}
}
答案 0 :(得分:3)
您需要在多个地方修复您的实施:
private
final
if
条件;年度任务必须是无条件的这可以解决打印零而不是一年的问题。
注意:(如果您还没有 研究异常,请跳过这个)处理确保构造函数中的后置条件的常用方法是在参数无效时抛出异常,而不是尝试猜测这些参数应该是什么。当您检测到其中一个参数无效时,请考虑抛出IllegalArgumentException
:
public Date(int theMonth, int theDay, int theYear)
{
if ( theMonth < 1 || theMonth > 12 ) {
throw new IllegalArgumentException("month");
}
if( theDay < 1 || theDay > 31 ) {
throw new IllegalArgumentException("day");
}
myMonth = theMonth;
myDay = theDay;
myYear = theYear;
}
答案 1 :(得分:1)
请记住!当您致电date.toString();
时,您并非将整个date
变量转换为String
;因为这实际上并没有改变对象中的任何内容,所以该行什么都不做。
幸运的是,当您使用println(date)
对象PrintStream
致电System.out
时,它仍然有效,您实际上是在调用println()
的变体,需要Object
作为参数而不是采用String
的参数,所做的是获取参数的toString()
方法的结果并打印出来的。所以,最终结果完全相同。
真正的问题在于你的构造函数方法。仔细看看,并仔细阅读。假装你是参数theYear
,你的值是2222.你将在什么时候使用,以及如何使用?
下面的剧透,认真看看
除非参数myYear
无效,否则您实际上从不将字段theYear
设置为任何。这里最好的解决方案是在检查else
以匹配您对其余参数的处理后添加缺少theYear
子句。