有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java数据封装、初始化和后置条件?

我的任务是完成下面所示的日期类。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);
}
}

共 (2) 个答案

  1. # 1 楼答案

    您需要在几个地方修复您的实现:

    • 因为您正在学习封装,所以应该声明变量private
    • 假设类中没有其他方法,那么还应该声明变量final
    • 没有要求验证年份的规则,因此if条件需要取消;年度分配必须是无条件的

    这将解决打印零而不是年份的问题

    注意:(如果您还没有研究异常,请跳过此步骤)处理确保构造函数中的post条件的常见方法是在参数无效时抛出异常,而不是试图猜测这些参数应该是什么。当检测到其中一个参数无效时,考虑抛出^{}

    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;
    }
    
  2. # 2 楼答案

    记住!当您调用date.toString();时,您并不是在将整个date变量转换为String;因为这实际上不会改变对象中的任何东西,所以这条线什么也不做

    幸运的是,它仍然可以工作,因为当您使用PrintStream对象System.out调用println(date)时,实际上是调用println()的变体,该变体将Object作为参数,而不是将String作为参数,所做的是获取参数的toString()方法的结果并打印出来。所以,最终的结果是完全相同的

    真正的问题在于构造函数方法仔细观察,并通读一遍。假设您是参数theYear,并且您的值是2222。在什么时候你会被利用,如何利用


    下面的剧透们,认真去看看


    事实上,除非参数theYear无效,否则您从未将字段myYear设置为任何!这里最好的解决方案是在检查theYear之后添加缺少的else子句,以匹配对其余参数的处理