Homework 2

/* Date.java */

import java.io.*;

public class Date {
      /* Put your private data fields here. */
      private int month;
      private int day;
      private int year;

      /** Constructs a date with the given month, day and year.   If the date is
       *  not valid, the entire program will halt with an error message.
       *  @param month is a month, numbered in the range 1...12.
       *  @param day is between 1 and the number of days in the given month.
       *  @param year is the year in question, with no digits omitted.
       */
      public Date(int month, int day, int year) {
          if(isValidDate(month, day, year) == true){
              this.month = month;
              this.day = day;
              this.year = year;
          }else{
              System.out.println("Invalid date. Program halts.");
              System.exit(0);
          }

      }

      /** Constructs a Date object corresponding to the given string.
       *  @param s should be a string of the form "month/day/year" where month must
       *  be one or two digits, day must be one or two digits, and year must be
       *  between 1 and 4 digits.  If s does not match these requirements or is not
       *  a valid date, the program halts with an error message.
       */
      public Date(String s) {
          String[] splitString = s.split("/");
          int month = Integer.parseInt(splitString[0]);
          int day = Integer.parseInt(splitString[1]);
          int year = Integer.parseInt(splitString[2]);

          if(isValidDate(month, day, year) == true){
              this.month = month;
              this.day = day;
              this.year = year;
          }else{
              System.out.println("Invalid date. Program halts.");
              System.exit(0);
          }
      }

      /** Checks whether the given year is a leap year.
       *  @return true if and only if the input year is a leap year.
       */
      public static boolean isLeapYear(int year) {
          if(year % 4 == 0){
              if((year % 100 == 0) && (year % 400 != 0)){
                  return false;
              }else{
                  return true; 
              }  
          }else{
              return false;                        // replace this line with your solution
          }
      }

      /** Returns the number of days in a given month.
       *  @param month is a month, numbered in the range 1...12.
       *  @param year is the year in question, with no digits omitted.
       *  @return the number of days in the given month.
       */
      public static int daysInMonth(int month, int year) {
          switch(month){
          case 2:
              if(isLeapYear(year) == true){
                  return 29;
              }else {
                  return 28;
              }
          case 4:
          case 6:
          case 9:
          case 11:
              return 30;
          default:
              return 31;
          }

      }

      /** Checks whether the given date is valid.
       *  @return true if and only if month/day/year constitute a valid date.
       *
       *  Years prior to A.D. 1 are NOT valid.
       */
      public static boolean isValidDate(int month, int day, int year) {
          if(month >= 1 && month <= 12 && day >= 1 && day <= daysInMonth(month, year) && year >= 1){
              return true;
          }else{
              return false;
          }
      }

      /** Returns a string representation of this date in the form month/day/year.
       *  The month, day, and year are expressed in full as integers; for example,
       *  12/7/2006 or 3/21/407.
       *  @return a String representation of this date.
       */
      public String toString() {
          String s = this.month + "/" + this.day + "/" + this.year;
          return s;                     
      }

      /** Determines whether this Date is before the Date d.
       *  @return true if and only if this Date is before d. 
       */
      public boolean isBefore(Date d) {
          if((this.year < d.year) || 
             (this.year == d.year && this.month < d.month) || 
             (this.year == d.year && this.month == d.month && this.day < d.day)){
              return true;
          }else{
              return false;
          }
      }

      /** Determines whether this Date is after the Date d.
       *  @return true if and only if this Date is after d. 
       */
      public boolean isAfter(Date d) {
          if(isBefore(d) ||
             (this.year == d.year && this.month == d.month && this.day == d.day)){
              return false;
          }else{
              return true;
          }
      }

      /** Returns the number of this Date in the year.
       *  @return a number n in the range 1...366, inclusive, such that this Date
       *  is the nth day of its year.  (366 is used only for December 31 in a leap
       *  year.)
       */
      public int dayInYear() {
          int n = 0;
          for(int i = 1; i < this.month; i++){
              n = n + this.daysInMonth(i, this.year);
          }
          n = n + this.day;
          return n;
      }

      /** Determines the difference in days between d and this Date.  For example,
       *  if this Date is 12/15/2012 and d is 12/14/2012, the difference is 1.
       *  If this Date occurs before d, the result is negative.
       *  @return the difference in days between d and this date.
       */
      public int difference(Date d) {
          int diff = 0;
          if(this.isAfter(d)){
              for(int i = d.year; i < this.year; i++){
                  if(isLeapYear(i)){
                      diff = diff + 366;
                  }else{
                      diff = diff + 365;
                  }  
              }
              diff = diff + this.dayInYear() - d.dayInYear();
          }else if(this.isBefore(d)){
              for(int i = this.year; i < d.year; i++){
                  if(isLeapYear(i)){
                      diff = diff + 366;
                  }else{
                      diff = diff + 365;
                  }  
              }
              diff = diff + d.dayInYear() - this.dayInYear();
              diff = (-1) * diff;
          }
          return diff;                           
      }

      public static void main(String[] argv) {
        System.out.println("\nTesting constructors.");
        Date d1 = new Date(1, 1, 1);
        System.out.println("Date should be 1/1/1: " + d1);
        d1 = new Date("2/4/2");
        System.out.println("Date should be 2/4/2: " + d1);
        d1 = new Date("2/29/2000");
        System.out.println("Date should be 2/29/2000: " + d1);
        d1 = new Date("2/29/1904");
        System.out.println("Date should be 2/29/1904: " + d1);
        d1 = new Date(12, 31, 1975);
        System.out.println("Date should be 12/31/1975: " + d1);
        Date d2 = new Date("1/1/1976");
        System.out.println("Date should be 1/1/1976: " + d2);
        Date d3 = new Date("1/2/1976");
        System.out.println("Date should be 1/2/1976: " + d3);

        Date d4 = new Date("2/27/1977");
        Date d5 = new Date("8/31/2110");

        /* I recommend you write code to test the isLeapYear function! */

        System.out.println("\nTesting before and after.");
        System.out.println(d2 + " after " + d1 + " should be true: " + 
                           d2.isAfter(d1));
        System.out.println(d3 + " after " + d2 + " should be true: " + 
                           d3.isAfter(d2));
        System.out.println(d1 + " after " + d1 + " should be false: " + 
                           d1.isAfter(d1));
        System.out.println(d1 + " after " + d2 + " should be false: " + 
                           d1.isAfter(d2));
        System.out.println(d2 + " after " + d3 + " should be false: " + 
                           d2.isAfter(d3));

        System.out.println(d1 + " before " + d2 + " should be true: " + 
                           d1.isBefore(d2));
        System.out.println(d2 + " before " + d3 + " should be true: " + 
                           d2.isBefore(d3));
        System.out.println(d1 + " before " + d1 + " should be false: " + 
                           d1.isBefore(d1));
        System.out.println(d2 + " before " + d1 + " should be false: " + 
                           d2.isBefore(d1));
        System.out.println(d3 + " before " + d2 + " should be false: " + 
                           d3.isBefore(d2));

        System.out.println("\nTesting difference.");
        System.out.println(d1 + " - " + d1  + " should be 0: " + 
                           d1.difference(d1));
        System.out.println(d2 + " - " + d1  + " should be 1: " + 
                           d2.difference(d1));
        System.out.println(d3 + " - " + d1  + " should be 2: " + 
                           d3.difference(d1));
        System.out.println(d3 + " - " + d4  + " should be -422: " + 
                           d3.difference(d4));
        System.out.println(d5 + " - " + d4  + " should be 48762: " + 
                           d5.difference(d4));
      }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值