首页 > 代码库 > hw2

hw2

代码如下:

1)Date(String s)那块我注释出来的那一块,把for语句里改成j<s.length()也可以运行出来。

2)想问一下大家是怎么想到用match和split的方法的?

3)difference那块可以用下面两句改写,很巧妙。

else if (isBefore(d)) 
133      return -d.difference(this); 

4)作业做了很久,但真的有对之前lecture没懂的地方有了更多的认识



package homework2;

/* Date.java */

import java.io.*;

class Date {
	private int month;
	private int day;
	private int year;
  /* 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)!=true) {  
		  System.out.println("The date is not valid");
	  }else {
		  this.day=day;
		  this.month=month;
		  this.year=year;
	  }
  }


  /** 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) {
	 if(s.matches("\\d{1,2}\\/\\d{1,2}\\/\\d{1,4}")){
          String[] d=s.split("/");
          month=Integer.parseInt(d[0]);
          day=Integer.parseInt(d[1]);
          year=Integer.parseInt(d[2]);
      }else System.out.println("error Date(String s)");

  }
	 
	  
	/* int[] line=new int[2];
	  int i=0;
	  for(int j=0;j<=s.length();j++) {
		  char result=s.charAt(j);
		  if(result==‘/‘) {
			  line[i]=j;
			  i++; 
		  }
	  }
	  int stringmonth= Integer.parseInt(s.substring(0,line[0]));
	  int stringday= Integer.parseInt(s.substring(line[0]+1,line[1]));
	  int stringyear= Integer.parseInt(s.substring(line[1]+1,s.length()));
	  if(stringmonth>=1&&stringmonth<=12) {
		  if(stringday>=1&&stringday<=31) {
			  if(stringyear>=1&&stringyear<=9999) {
				  month=stringmonth;
				  day=stringday;
				  year=stringyear;
			  }else {
				  System.out.println("error");
			  }
		  }
	  }
  }*/

  /** 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%400==0)||((year%400!=0)&&(year%100!=0)&&(year%4==0))) {
		  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) {
	  /*System.out.print("days in month "+month+" is:");*/
	  if(month==2) {
		  if(isLeapYear(year)) {
			  return 29;
		  }else return 28;
	  }else
		  switch(month) {
			  case 4:
			  case 6:
			  case 9:
			  case 11:
				  return 30;	
			  default:
				  return 31;
			}
		  }
  	// replace this line with your solution

  /** 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) {
	  /*System.out.print("The given date "+month+"/"+day+"/"+year+" is:");*/
	  if(month>=1&&month<=12&&day>=1&&day<=daysInMonth(month,year)&&year>=1) {
		return true;
	  }else {
		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 month+"/"+day+"/"+year;                // 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 (this.year<d.year) {
		  return true;
	  }else if(this.year==d.year) {
		  if(this.month<d.month) {
			  return true;
		  }else if(this.month==d.month) {
			  if(this.day<d.day) {
				  return true;
			  }else return false;
		  }else return false;
	  } else 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(!isBefore(d)&&d!=this) {
		  return true;
	  }else return false;
                          // 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() {
	  int count=0;
	  for(int j=this.month-1;j>0;j--) {
		  count+=daysInMonth(j,this.year);		  
	  }
	  count+=this.day;
	  return count;                           // 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 difference(Date d) {
	 int total=0;
	 if(isAfter(d)) {
		 for(int j=d.year;j<this.year;j++) {
			 if(isLeapYear(j)) {
				  total+=366;
			  }else {
				  total+=365;
			  }
			}
		return (total+this.dayInYear()-d.dayInYear());
	 }else 
		 if(isBefore(d)) {
		 for(int j=this.year;j<d.year;j++) {
			 if(isLeapYear(j)) {
				  total+=366;
			  }else {
				  total+=365;
			  }
			}
		 	return -(total+d.dayInYear()-this.dayInYear());
		 }
			else return 0;
  }
//replace this line with your solution
  
  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 the isLeapYear function!");
    int y1=2014;
    System.out.println("That "+y1+" is leap year should be:"+isLeapYear(y1));
    int y2=1900;
    System.out.println("That "+y2+" is leap year should be:"+isLeapYear(y2));
    int y3=1600;
    System.out.println("That "+y3+" is leap year should be:"+isLeapYear(y3));
    int y4=2012;
    System.out.println("That "+y4+" is leap year should be:"+isLeapYear(y4));
    /* 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));

  }
}

运行结果:

技术分享

 

hw2