hw2

终于完成了hw2,打卡打卡,想说的都在备注里了,今天太晚了,哪天再过来更,需要总结一下编程思路什么的。这次作业学到了很多啊,强装围笑鼓个掌吧。

/* Date.java */

import java.io.*;

class Date {
    private String Year;
    private String Month;
    private String Day;
    private int Y;
    private int M;
    private int D;
    private int n;
      private int firstSlash;
      private int secondSlash;

    
  /* Put your private data fields here. */

  
      /** 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)){
      M=month;
      D=day;//只有在date有效前提下赋值,赋值语句要在if语句里
      Y=year;        
      }
      else{System.out.println("inValidDate");
      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) {
      StringBuilder z=new StringBuilder(s);//通过把类型转换为StringBuilder
      firstSlash=z.indexOf("/");
      secondSlash=z.indexOf("/",firstSlash+1);//识别两个“/”的index
      Year=z.substring(secondSlash+1, s.length());//将String转化为年月日三部分
      Month=z.substring(0, firstSlash);
      Day=z.substring(firstSlash+1, secondSlash);
      int m=Integer.parseInt(Month);
      int d=Integer.parseInt(Day);
      int y=Integer.parseInt(Year);
     //尝试用this.(m.d.y) 失败了,要求constructor必须在第一行,so有没有更好的方法??
     if(Year.length()>4||Year.length()==0||Month.length()>2||Month.length()==0
              ||Day.length()>2||Day.length()==0||!isValidDate(m, d, y)){ 
   /*判断年月日字符数是否符合要求,引用isValidDate(),但不能只用这个盘踞,
    * 保证在002这种月份出现的情况下仍能正确判断
    */
       System.out.println("Error-invalidInputString");
       System.exit(0);
      } else {
         M=m;
         D=d;
         Y=y;
         //同第一个constructor,在符合要求之后才进行赋值
      }
      }
  

  /** 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||(year%4==0&&year%100==0&&year%400!=0)){
            return false;
        }
        return true;
  }
    // 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) {
     if(month==1||month==3||month==5||month==7||month==8||month==10||month==12){
        return 31; 
     } else if(month==4||month==6||month==9||month==11){
     return 30;}else if(month==2){
         if(Date.isLeapYear(year)){
             return 29;
         }
     }             
     return 28;//返回值语句至少有一句必须在if braces外面
     }
  

  /** 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(year>=0&&year<=9999&&month>0&&month<13&&day<=daysInMonth(month, year)&&day>0){
        return true;}
    return false;// replace this line with your solution
    }
  

  /** 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() {
    return (M+"/"+D+"/"+Y);              
             // replace this line with your solution
  }

  /** 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(Y<d.Y||(Y==d.Y&&M<d.M)||(Y==d.Y&&M==d.M&&D<d.D)){
        return true; 
    }
        return false;
                               // replace this line with your solution
  }

  /** 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(Y==d.Y&&D==d.D&&M==d.M){
           return false; // 不能直接用isBefore()的非,需要增加日期相同的情况
       }
        return !isBefore(d);    
    }     // replace this line with your solution
   
  

  /** 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() {
      n=0;
      for(int i=1;i<M;i++){
          n=daysInMonth(i, Y)+n;// 求本月之前月份天数总和
          }
      n=n+D; //加上本月的日期
    return n;                           // replace this line with your solution
  }

  /** 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 numberOfDaysInYear(){
        int k1=0;
          for(int i=0;i<Y;i++){
              if(isLeapYear(i)){
              k1=366+k1;
          }else{
              k1=365+k1;
          }
          }
        k1=k1+dayInYear();
        return k1;
  }
   //以上为新建method 为了方便下面的difference()做减法,避免重复代码
  public int difference(Date d) {
     
         return numberOfDaysInYear()-d.numberOfDaysInYear();
    
     
                        // replace this line with your solution
  }

  public static void main(String[] argv) {
    System.out.println("
Testing 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("
Testing 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("
Testing 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));
  }
}
View Code

运行结果:

原文地址:https://www.cnblogs.com/Jingjunw/p/7192511.html