Homeworks

HW2

Halt an entire program.

System.out.println("ERROR message.");
System.exit(0);

Break complicated problems into smaller pieces (methods).

Call to this() or super() must be first statement in constructor.

Code comparison.

// snippet 1
public Date(int month, int day, int year) {    +  public Date(int month, int day, int year) {
  if(!isValidDate(month, day, year)) {         |    if (isValidDate(month, day, year)) {
    System.out.println("ERROR...");            |      this.month = month;
    System.exit(0);                            |      this.day = day;
  }                                            |      this.year = year;
  this.month = month;                          |    } else {
  this.day = day;                              |      System.out.println("ERROR...");
  this.year = year;                            |      System.exit(0);
}                                              |    }
                                               +  }

// snippet 2
// what to put after if / else ?

public Date(String s) {                                           +   public Date(String s) {
    String pattern = "([0-9]{1,2})/([0-9]{1,2})/([0-9]{1,4})";    |     if (s.matches("(\\d{1,2})\\/(\\d{1,2})\\/(\\d{1,4})")) {
    Pattern r = Pattern.compile(pattern);                         |       String[] date = s.split("/");
                                                                  |       int m = Integer.parseInt(date[0]);
    Matcher m = r.matcher(s);                                     |       int d = Integer.parseInt(date[1]);
    if (m.find()) {                                               |       int y = Integer.parseInt(date[2]);
        this.month = Integer.parseInt(m.group(1));                |       if (isValidDate(m, d, y)){
        this.day = Integer.parseInt(m.group(2));                  |         this.month = m;
        this.year = Integer.parseInt(m.group(3));                 |         this.day = d;
                                                                  |         this.year = y;
        if (!isValidDate(this.month, this.day, this.year)) {      |       } else {
            System.out.println("ERROR...");                       |         System.out.println("Error! Not a Valid Date: " + s);
            System.exit(0);                                       |         System.exit(0);
        }                                                         |       }
    }                                                             |     } else {
    else {                                                        |       System.out.println("Error! Not a Valid Date: " + s);
        System.out.println("ERROR: given string not match!");     |       System.exit(0);
        System.exit(0);                                           |     }
    }                                                             |   }
}                                                                 +

// snippet 3
// leap year is any year divisible by 4, except that a year divisible by 100 is not a leap 
// year, except that a year divisible by 400 is a leap year after all.

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

// snippet 4

public boolean isBefore(Date d) {         +  public boolean isBefore(Date d) {
  if (this.year < d.year) {               |    if (year < d.year) {
    return true;                          |      return true;
  } else if (this.year > d.year) {        |    } else if (this.year > d.year) {
      return false;                       |      return false;
  } else {                                |    }
      if (this.month < d.month) {         |    //same year
          return true;                    |    if (month < d.month) {
      } else if (this.month > d.month) {  |      return true;
          return false;                   |    } else if (month > d.month) {
      } else {                            |      return false;
          if (this.day < d.day) {         |    }
              return true;                |    //same month
          } else {                        |    if (this.day < d.day) {
              return false;               |      return true;
          }                               |    } else {
      }                                   |      return false;
  }                                       |    }
}                                         +  }

// snippet 5

public boolean isAfter(Date d) {   +   public boolean isAfter(Date d) {
    if (isSameDay(d)) {            |     return d.isBefore(this);
        return false;              |   }
    }                              |
    else {                         |
        return !isBefore(d);       |
    }                              |
}                                  +


// snippet 6

public int difference(Date d) {                                                        +   public int difference(Date d) {
    if (isAfter(d)) {                                                                  |     int diff = this.dayInYear() - d.dayInYear();
        // this > d                                                                    |     if (year == d.year) {
        if (this.year == d.year)  {                                                    |       return diff;
            return this.dayInYear() - d.dayInYear();                                   |     } else if (this.isBefore(d)) {
        }                                                                              |       return -differenceCalculator(this, d);
        else {                                                                         |     } else {
            int diff = 0;                                                              |       return differenceCalculator(d, this);
            diff += isLeapYear(d.year) ? 366 - d.dayInYear() : 365 - d.dayInYear();    |     }
            for (int i = d.year+1; i < this.year; i++){                                |   }
                diff += isLeapYear(i) ? 366 : 365;                                     |
            }                                                                          |   /** Determines the difference in days between a same day in different years.
            diff += this.dayInYear();                                                  |    *  d1 is before d2.
            return diff;                                                               |    *  The return value will not be negative.
        }                                                                              |    */
    }                                                                                  |   public int differenceCalculator(Date d1, Date d2) {
    else if (isSameDay(d)) {                                                           |     int days = 0;
        return 0;                                                                      |     for (int i=0; i < d2.year - d1.year; i++) {
    }                                                                                  |       days += new Date(12, 31, d1.year+i).dayInYear();
    else {                                                                             |     }
        return -d.difference(this);                                                    |     return days - d1.dayInYear() + d2.dayInYear();
    }                                                                                  |   }
}                                                                                      +

results matching ""

    No results matching ""