Lab #3: Simple Date [M]

Preparation

Do the following before arriving at lab:

  • Read our textbook through Chapter 3
  • Bring your textbook to lab

Objectives

After completing this lab, you should be able to:

  • write a class definition
  • use conditional statements to solve problems

Exercises

  1. Create a new class called SimpleDate (in any project).  In the comment near the beginning of the code, specify your name as the author.
  2. Define three integer instance variables: day, month, and year.
  3. public SimpleDate(int m, int d, int y) : write a constructor with this signature to initialize the three instance variables. 
  4. public SimpleDate() : this default constructor should initialize the three instance variables to your birth date.  Use four digits for the year, e.g. 1964 rather than 64. (;-)
  5. public double approxPercentThroughYear() : write a method with this signature to calculate approximately how far through the year the current date is, using the following formula:
        (month-1)*365.25/12 + day
        ------------------------- * 100
                  365.25
    
    For example, September 24 is approximately 73% through the year.  (Do not print anything.)
  6. public boolean isHalloween() : return true if the represented date is October 31.  (Do not print anything.)
  7. public boolean isNewYearsDay() : return true if the represented date is January 1.  (Do not print anything.)
  8. public boolean isMyBirthday() : return true if the represented date is your birthday (in any year).  (Do not print anything.)
  9. public boolean isInALeapYear() : return true if the represented year is a leap year.  A leap year is divisible by 4, except not divisible by 100 unless it is divisible by 400.  For examples, 2009 is not a leap year, 2008 was a leap year, we say that 1900 was not a leap year, and we say that 1600 was a leap year.  (Do not print anything.)
  10. public String toString() : use the three instance variables to build and return a String representation of the date.  For example, 10/20/2006 yields "October 20, 2006".  (Do not print anything.)
  11. Add code back in the first constructor (the one that takes arguments) to output an error message if any one of the given values is really obviously bad: the month needs to be between 1 and 12, the day needs to be between 1 and 31, and the year needs to be at least 1.  (For this assignment, you don't need to worry about the precise length of each month here in this constructor: some code indicated below should do so, and that's enough for this assignment.)
  12. public void increment() : increase the current date to the next day.  Pay attention to ends of months, the end of the year, and leap years for special cases.
  13. public static void test(int m, int d, int y) : this temporary method can be used to quickly determine if all of the methods work.  A SimpleDate object is declared and created and then a series of method calls and condition statements print appropriate results (match the sample results shown below).  For example, if the SimpleDate object that you declare and create is date, then in an if statement you should check date.isHalloween(), and if it's true then output "is Halloween" or otherwise output "is not Halloween.".
  14. Ensure the source code meets our Java Style Guide requirements
  15. Print the final source code and sample outputs from the Terminal Window: one exactly as shown, and another using the date of your birthday in the current year.
Sample Results

After writing and testing all methods, the following output should display to the terminal window by executing SimpleDate.test(2,25,2008):

February 25, 2008
is approximately 15.177960301163587% through the year.
is in a leap year.
is not Halloween.
is not New Year's Day.
is not my Birthday :-(

And the next 10 days are:
February 26, 2008
February 27, 2008
February 28, 2008
February 29, 2008
March 1, 2008
March 2, 2008
March 3, 2008
March 4, 2008
March 5, 2008
March 6, 2008

Grading Criteria

This lab is worth a possible 30 points.  Style counts as 10% of the credit.