1. 程式人生 > >CS61b homework2 打卡

CS61b homework2 打卡

正則 days for h+ == mage 運行 lean cs6

主要考點: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 打卡