A9: Date With Exceptions

On This Page


Overview

During this assignment, you will:

  • Create a class representing dates
  • Learn the rules for leap years
  • Write exception handling classes and methods for invalid dates
  • Write a graphical user interface (GUI) to use your Date class

Background Information

Most calendars today are based upon how many days it takes the Earth to circle around the Sun. The typical year is about 365.2422 days long. Because of the fractional part, calendars need to be adjusted occasionally. The calendar used by most of the world today is called the Gregorian Calendar.

Leap Years

To keep the calendar synchronized with the Earth's rotation around the Sun, the Gregorian Calendar uses a leap year. A leap year is a year with 366 days. Leap years are divisible by 4 (for example, 1992). However, a year is not a leap year if it is divisible by 100, unless it is divisible by 400. Thus, 1900 is not a leap year, but 2000 is a leap year. (more...)

Specifications

Create a Date class with three instance fields, year, month, and day. Within this class, implement all the following methods:

  1. public Date(): Default constructor that sets this Date to a valid date.
  2. public Date(int year, int month, int day): Overloaded constructor to set this Date to the specified values.
  3. public void setDate(int newYear, int newMonth, int newDay): Sets this Date to the specified values.
  4. public int getYear(): Returns the year.
  5. public int getMonth(): Returns the month.
  6. public int getDay(): Returns the day.
  7. public Date nextDay(): Increments the day, adjusting the month and year if necessary. Returns the current Date object adjusted to the new settings.
  8. public String toString(): Returns a String representation for this Date. For example, if the year were 2002, the month 11, and the day 12, you should return the string 11/12/2002.

Make certain to include at least these methods, though you may add more methods if desired.

Define two exception classes, one called MonthException and another called DayException. Do not use inner classes for this assignment. If an invalid Date is attempted, throw the appropriate exception to the caller of the method. Be sure to check for leap years and make certain that a Date object always remains in a valid state. Do not catch any of these exceptions in the Date class. Instead, handle them in the driver application.

Write a GUI program in another class that makes use of your Date class. Extend JFrame and add components to the JFrame container. The program must ask the user to enter a month, day and year. If the user enters an invalid month, day or year, the program must present an error message in a JOptionPane error dialog. After a valid month, day and year are entered, the program must display the date for the next day.

Sample Output

  • Example test output:
  • C:\>java DateTest
    Testing Date() and get methods
    Testing Date(int, int, int)
    Testing setDate(int, int, int)
    Testing nextDay()
    Testing toString()
    *** All automatic tests passed ***
    
  • Example GUI for the Date class:
  • Example error message for an incorrect entry:

Test Cases

Testing is an important part of software development. To assist you in developing your code, I provide some tests for this assignment. These tests, known collectively as "test cases", help to make sure that your assignment meets its requirements. I will use these test cases (and perhaps others) to test your assignment, and you should too. If the test cases do not pass, then you will lose points for the assignment.

The tests operate using a testing class. Install the tests by copying the following file into the same directory as your .java source code files:

Run the test code just like any other Java program.

Extra Credit

The following are worth extra credit points:

  1. Add a static method named isValid that tests whether a specific month, day and year are valid. (1 point)
  2. Add a method to Date that accepts a String and constructs a Date. (2 points)
  3. Use your imagination and be creative. (0 to 2 points)

Make certain that your README.txt file lists any extra credit attempted.

Grading Criteria

The instructor will evaluate your assignment using the following criteria. Each criteria represents a specific achievement of your assignment and has a scoring guide. The scoring guide explains the possible scores you can receive.

Some scoring guides have a list of indicators. These indicators are a sign of meeting, or a symptom of not meeting, the specific criterion. Note that a single indicator may not always be reliable or appropriate in a given context. However, as a group, they show the condition of meeting the criterion.

For information on grading policies, including interpretation of scores, see the course Syllabus.

Program Compilation

  • 4: Source code compiles with no errors or warnings
  • 0: Does not compile

Functionality

  • 10: Demonstrates mastery of the assignment
    • Has extra features or demonstrates techniques beyond the assignment
    • Applies concepts from the lessons appropriately
    • Meets all specifications (see above) with particularly elegant solutions
    • No errors encountered during operation
  • 8: Has all the functionality expected of the assignment
    • Demonstrates many techniques from the lesson
    • Meets all specifications (see above)
    • Implementation seems more complicated than necessary.
    • May have one minor error
  • 6: Has most of the functionality expected of the assignment
    • Demonstrates some techniques from the lesson
    • Meets all but one of the specifications (see above)
    • Implementation seems excessively complicated.
    • May have 2-3 minor errors
  • 4: Has some of the functionality expected of the assignment
    • Demonstrates some techniques from the lesson
    • Meets at least 1/2 of the specifications (see above)
    • Implementation seems excessively complicated.
    • May have more than 3 minor errors
  • 2: Serious functional problems but shows some effort and understanding
    • Meets less than 1/2 of the of the specifications (see above)
    • Has a major error or many minor errors
    • Implementation seems very convoluted
    • Demonstrates few techniques from the lesson
  • 0: Does not execute

Program Style

  • 4: Code is well-documented
  • 3: Code has minor documentation errors
    • Has 1 documentation error
  • 2: Code has some documentation errors
    • Has 2-3 documentation errors
  • 1: Code has many documentation errors
    • Has more than 3 documentation errors
  • 0: No apparent attempt to document code

README.txt File

  • 2: README.txt file submitted with specified information included
  • 1: README.txt submitted but some information was not included
  • 0: No README.txt submitted

Maximum Score: 20, plus extra credit

What to Turn In

Submit your assignment following the instructions for homework. Include the following items for grading:

  1. README.txt file
  2. All of your source code (i.e. .java files)
  3. Any other source code needed to make your program function

You must submit all the files needed to make your assignment function properly. Do not assume that the instructors has any files unless explicitly stated by the instructor. Your assignment must work as submitted.

Home | WebCT | Announcements | Schedule | Expectations | Syllabus
Help | FAQ's | HowTo's | Links

Last Updated: November 08 2003 @16:38:15