首页 > 代码库 > CS61b homework2 打卡

CS61b homework2 打卡

主要考点:String类matches和split方法的运用,正则表达式的运用。

判断闰年,计算天数等算法。代码如下:

import java.io.*;

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

  public Date(int month, int day, int year) {
      if(!isValidDate(month,day,year))
          System.exit(0);
      this.month=month;
      this.day=day;
      this.year=year;
  }

  public Date(String s) {
      if(s.matches("\\d{1,2}\\/\\d{1,2}\\/\\d{1,4}")){
          String[]o=s.split("/");
          if(isValidDate(Integer.parseInt(o[0]),Integer.parseInt(o[1]),Integer.parseInt(o[2])))
          {
              month=Integer.parseInt(o[0]);
              day=Integer.parseInt(o[1]);
              year=Integer.parseInt(o[2]);
          }else
              System.exit(0);
          
      }else
          System.exit(0);

  }

  public static boolean isLeapYear(int year) {
    if(year%4==0&&year%100!=0||year%400==0)
        return true;
    else return false;
  }

  public static int daysInMonth(int month, int year) {
      if(month<1||month>12)
          System.exit(0);
    switch(month){
    case 1:return 31;
    case 2:if(isLeapYear(year))return 29;
    else return 28;
    case 4:
    case 6:
    case 9:
    case 11:
        return 30;
    default:return 31;
         }
  }

  public static boolean isValidDate(int month, int day, int year) {
    if(day>daysInMonth(month,year))return false;
    else return true;
  }

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

  public boolean isBefore(Date d) {
    if(this.year<d.year)return true;
    else if(this.year==d.year&&this.month<d.month)return true;
    else if(this.year==d.year&&this.month==d.month&&this.day<d.day)return true;
    else return false;
  }

  public boolean isAfter(Date d) {
    if(isBefore(d))return false;
    else if(this.year==d.year&&this.month==d.month&&this.day==d.day)
        return false;
    else return true;
  }

  public int dayInYear() {
      int total=0;
    for(int i=1;i<month;i++)
        total+=daysInMonth(i,this.year);
    total+=this.day;
    return total;
  }

  public int difference(Date d) {
    if(isAfter(d)){
        int total = 0;
        for(int i=d.year;i<this.year;i++){
            if(isLeapYear(i))total+=366;
            else total+=365;
        }
        int dayDifference=this.dayInYear()-d.dayInYear();
        total+=dayDifference;
        return total;
    }else if(isBefore(d)){
        return -d.difference(this);
    }
    else return 0;
  }

  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");

    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));
  }
}

 

运行结果:技术分享技术分享

CS61b homework2 打卡