2011-09-01 31 views
0

我是Java新手,在學習C之後嘗試學習它,在語法上遇到一些麻煩。這是我的教授給我看的代碼,基本上我可以理解它在做什麼,接受最後一節[public boolean isAfter(Date b)]。 Date b來自哪裏,什麼是compareTo?基本回報和類問題

import java.util.Calendar; 
import java.util.GregorianCalendar; 

public class Date implements Proj1Constants { 

    private static final int[] DAYS = { 0, 31, 29, 31, 30, 31, 30, 31, 31, 
      30, 31, 30, 31 }; 
    private static final int LEAP_YEAR = 366; 
    private static final int NON_LEAP_YEAR = 365; 

    private final int month; // month (between 1 and 12) 
    private final int day; // day (between 1 and DAYS[month]) 
    private final int year; // year 



    /** 
    * Constructor: default; returns today's date 
    */ 
    // creates today's date as the date; obtaines it from Java library 

    public Date() 

    { 
     GregorianCalendar c = new GregorianCalendar(); 
     month = c.get(Calendar.MONTH)+1; //our month starts from 1 
     day = c.get(Calendar.DAY_OF_MONTH); 
     year = c.get(Calendar.YEAR); 
    } 



    /** 
    * Constructor: Does bounds-checking to ensure object represents a valid 
    * date 
    * 
    * @param m represents the month between 1 and 12 
    * @param d represents the date between 1 and the corresponding number 
    * from array DAYS 
    * @param y represents the year 
    * @exception RuntimeException 
    * if the date is invalid 
    */ 
    public Date(int m, int d, int y) 

     { 
     if (!isValid(m, d, y)) 
      throw new RuntimeException("Invalid date"); 
     month = m; 
     day = d; 
     year = y; 
    } 


    /** 
    * Constructor: Does bounds-checking to ensure string represents a valid 
    * date 
    * 
    * @param sDate represents a date given in format mm-dd-yyyy 
    * @exception RuntimeException if the date is invalid 
    */ 

    public Date(String sDate) 

     { 
     int m, d, y; 
     String[] chopDate = sDate.split("-"); 
     m = Integer.parseInt(chopDate[ZEROI]); 
     d = Integer.parseInt(chopDate[ONEI]); 
     y = Integer.parseInt(chopDate[TWOI]); 
     if (!isValid(m, d, y)) 
      throw new RuntimeException("Invalid date"); 
     month = m; 
     day = d; 
     year = y; 
    } 


    /** 
    * constructor: creates a date with a given year; fills in valid month and day; 
    * as december 31st. 
    * 
    * @param y represents a date given in year as integer 
    */ 


    public Date(int y) 

     { 

     month = 12; 
     day = DAYS[12]; 
     year = y; 
    } 


    /** 
    * create a date with a given month and year; fills in valid day; 
    * 
    * @param m represents the month between 1 and 12 
    * @param y represents the year 
    * @exception RuntimeException if the date is invalid 
    */ 

    public Date(int m, int y) 

    { 

     if (!isValid(m, DAYS[m], y)) 
      throw new RuntimeException("Invalid date; correct input"); 
     month = m; 
     day = DAYS[m]; 
     year = y; 
    } 


    /** 
    * Is the given date valid? 
    * 
    * @param month, day, and year 
    * @return false if month exceeds 12 or is less than 1 
    * @return false if day exceeds the corresponding days for a month from 
    * array DAYS 
    * @return false if the year is not a leap year and has 29 days 
    */ 

    private static boolean isValid(int m, int d, int y) 

    { 
     if (m < 1 || m > 12) 
      return false; 
     if (d < 1 || d > DAYS[m]) 
      return false; 
     if (m == 2 && d == 29 && !isLeapYear(y)) 
      return false; 
     return true; 
    } 


    /** 
    * is y a leap year? 
    * 
    * @param y 
    * represents the year specified 
    * @return true if year divisible by 400 
    * @return false if year divisible by 100 and not by 400 
    */ 

    private static boolean isLeapYear(int y) 

    { 
     if (y % 400 == 0) 
      return true; 
     if (y % 100 == 0) 
      return false; 
     return (y % 4 == 0); 
    } 


    /** 
    * is (m, y) a leap month? 
    * 
    * @param m represents the month specified 
    * @param y represents the year specified 
    * @return true if it is a leap month 
    * @return false otherwise 
    */ 

    private static boolean isLeapMonth(int m, int y) 
    { 
     if (isLeapYear(y)) return ((m == 2) ? true : false); 
     return false; 
    } 


    // return the next Date 
    /** 
    * adds a day and returns a new Date object 
    * 
    * @return returns a new Date object adding a day 
    */ 

    public Date next() 

    { 
     if (isValid(month, day + 1, year)) 
      return new Date(month, day + 1, year); 
     else if (isValid(month + 1, 1, year)) 
      return new Date(month + 1, 1, year); 
     else 
      return new Date(1, 1, year + 1); 
    } 


    // is this Date after b? 
    /** 
    * compares two Date objects 
    * 
    * @param b Date object 
    * @return true if this Date is after Date b 
    */ 

    public boolean isAfter(Date b) 

    { 
     return compareTo(b) > 0; 
    } 
+0

代碼清單在課程結束前結束。 compareTo方法缺失。 –

+0

對於'isLeapMonth(int,int)'實現,您可以簡單地使用'return m == 2 && isLeapYear(y)'更具可讀性。 –

回答

2

isAfter是一個實例方法,其參數爲Date,該函數的引用範圍爲b

如果這是完整的代碼,則它無效,因爲方法compareTo未在任何地方定義。

+0

是的代碼不完整,他寫了它,並給我們修復。 –

+0

那麼你需要實現compareTo方法。正如@sanjay所說,compareTo是在Comparable界面上定義的。你的類實現了一個接口,並且該接口本身可能會實現可比較的。 – hvgotcodes

+0

我看到了猜我會在書中挖掘一些更多的感謝幫助 –

2

compareTo是其實現Comparable所有類應實現的方法。該陳述可簡單地解釋爲this.compareTo(b),這意味着如果compareTo在此實例上調用的結果大於1,則此日期更長或位於傳入日期之後。請參閱Comparable接口的compareTo方法的合同以瞭解更多詳細信息。

順便說一句,我認爲你在你的課中缺少compareTo的定義,你似乎必須自己實現它?