15-100 Sections U-V-AA / Spring 2009
Homework 8 Mastery
2 questions - 30 Minutes

1.      cubeRootWithBinarySearch:  Write a method, cubeRootWithBinarySearch, that takes a non-negative double and returns its cube root without using Math.pow but instead using binary search as you did on the homework.  You may assume the argument is non-negative.

2.      The Date Class:  Start with the appended file.  Do not modify the main class. Make this code work by adding the appropriate classes with the appropriate methods as described by the test methods called by this main method. Note that you do not have to add any code to the test cases, though you do have to solve them with general-purpose solutions (and not just hard-code the example test cases!).  Also:  you may not use Java’s built-in Date class.  You must write your own Date class!


// Appendix: Hw8Quiz.java

public class Hw8Quiz {
  public static void main(String[] args) throws Exception {
    testDateClass();
  }

  //////////////////////////////////
  // testDateClass
  //////////////////////////////////

  private static void testDateClass() {
    System.out.print("Running Date class tests...  ");
    // default constructor and some accessors
    Date date = new Date();
    assert(date.getMonth() == 1);    // 1 == January
    assert(date.getDay() == 1);      // 1 == 1st
    assert(date.getYear() == 2000); // the year 2000
    assert(date.toString().equals("1/1/2000")); // month/day/year
    
    // a mutator
    date.setMonth(2);
    assert(date.getMonth() == 2);    // 2 == February
    assert(date.toString().equals("2/1/2000"));
    
    date.setMonth(13);  // illegal, so it is ignored!
    assert(date.getMonth() == 2);    // still February!
    assert(date.toString().equals("2/1/2000"));
    
    // another accessor
    assert(date.isFebruary() == true);
    date.setMonth(3);                // 3 = March
    assert(date.isFebruary() == false);
    
    // second instance, using a different constructor
    Date date2 = new Date(5,3,2005); // month, day, year
    assert(date2.toString().equals("5/3/2005"));
    
    // method that returns a new Date (rather than modifying the existing date)
    Date date3 = date2.startOfYear();  // returns Jan 1st of same year
    assert(date3.toString().equals("1/1/2005")); // new date
    assert(date2.toString().equals("5/3/2005")); // but this is unchanged!
    
    System.out.println("Passed all tests!!!");
  }
}

//////////////////////////////////
// Date class
//////////////////////////////////

class Date {
  // ADD CODE HERE!!!
}