2016-10-16 7 views
1

私は、Javaプログラム(クラスDate)と、「次の日の日、月、および年を入力して計算して出力するクラスNextDay」を記述する必要があります。 public Date getNextDay()方法で私の場合はnullを返さないようにするにはどうすればいいですか?

私はそれ以外の場合はエラーになります、リターンヌルを使用する必要があります。 nullを返さないようにするには

これは私のコードです。

public class Date { 
    private int day; 
    private int month; 
    private int year; 


    public Date(int day, int month, int year){ 
     this.day = day; 
     this.month = month; 
     this.year = year; 
    } 


    public int getMaxDaysInMonth() 
    { 
     int daysInMonth = 0; 
     switch(month) 
     { 
      case 1: 
      case 3: 
      case 5: 
      case 7: 
      case 8: 
      case 10: 
      case 12: 
       daysInMonth = 31; 
       break; 
      case 2: 
       if(isLeapYear()) 
       { 
        daysInMonth = 29; 
       } 
       else 
       { 
        daysInMonth = 28; 
       } 
       break; 
      case 4: 
      case 6: 
      case 9: 
      case 11: 
       daysInMonth = 30; 
     } 

     return daysInMonth; 
    } 



    public Date getNextDay(){ 
     try { 
      if(day < getMaxDaysInMonth()){ 
       return new Date(day + 1, month, year); 
      } 
      else if(day == getMaxDaysInMonth() & month < 12){ 
       return new Date(1, month+1, year); 
      } 
      else if(day == getMaxDaysInMonth() & month == 12){ 
       return new Date(1, 1, year+1); 
      } 
     } catch (Exception e) { 
      System.out.println("You have entered an invalid Date."); 
      e.printStackTrace(); 

     } 
     return null; 
    } 




    public int getDay(){ 
     return day; 
    } 

    public int getMonth(){ 
     return month; 
    } 

    public int getYear(){ 
     return year; 
    } 

    public boolean isLeapYear(){ 
     return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); 
    } 

    public String toString(){ 
     return this.day + "." + this.month + "." + this.year; 
    } 
} 




public class NextDay { 

      public static void main(String args[]) throws Exception{ 
       Date dateObj = new Date(28, 2, 2015); 
       System.out.println("Old Date: " + dateObj.getDay() + "." + dateObj.getMonth() + "." + dateObj.getYear() + "."); 
       System.out.println("The next day is " + dateObj.getNextDay().toString() + "."); 
      } 
} 
+0

あなたは1970年1月1日 – Cristian

+0

はい、私がすることができますが、それは良い解決策であるた新しいDateオブジェクトを返すことができますか? – meert

+0

クラスを使用する開発者は、提供するドキュメントに従わなければならないことに注意してください。無効な日付が指定された場合や1970年の日付が返された場合、それはほんの練習です – Cristian

答えて

2

は、ローカル変数を使用し、それを割り当て、メソッドの最後に戻り、それがパフォーマンスを向上させることができれば、あなたのコード内でジャンプステートメントを使用することができます

public Date getNextDay(){ 
      Date date = new Date(); 
      try { 
       if(day < getMaxDaysInMonth()){ 
        date= new Date(day + 1, month, year); 
       } 
       else if(day == getMaxDaysInMonth() & month < 12){ 
        date = new Date(1, month+1, year); 
       } 
       else if(day == getMaxDaysInMonth() & month == 12){ 
        date = new Date(1, 1, year+1); 
       } 
      } catch (Exception e) { 
       System.out.println("You have entered an invalid Date."); 
       e.printStackTrace(); 

      } 
      return date; 
     } 
1

過去に日付を返し、nullを返すことをお勧めします。

2

私は例外をスローすることをお勧めします。例外の一環として、was/was wrongについての説明を与えることもできます。この日はまた、有効な方法で作成された日付に一致する可能性があるため、特定の日付を返す

は、良いアイデアではありません。私。 01-01-1970の日付は問題なく作成できます。したがって、問題の種類やマーカーとして返されるべきではありません。あなたの日付の表現に関しては

あなたは、月、日、年のためInteger.MAX_VALUEのと日付を初期化することができます覚えておいてください。この場合の予想される出力は?

関連する問題