0

私の課題は、以下に示す 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);
}
}
4

2 に答える 2

3

いくつかの場所で実装を修正する必要があります。

  • カプセル化について学んでいるので、変数を宣言する必要がありますprivate
  • クラスに他のメソッドがないと仮定すると、変数も宣言する必要がありますfinal
  • 年を検証するように要求する規則はないため、if条件を変更する必要があります。年の割り当ては無条件である必要があります

これにより、年ではなくゼロを出力する問題が修正されます。

注: (まだ例外について学習していない場合はスキップしてください) コンストラクターの事後条件を確実にするための一般的な方法は、パラメーターが無効な場合に、これらのパラメーターがどうあるべきかを推測しようとするのではなく、例外をスローすることです。IllegalArgumentException引数の 1 つが無効であることを検出した場合は、スローすることを検討してください。

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;
}
于 2013-12-13T20:47:48.547 に答える