Jump to page content Jump to navigation

College Board

AP Central

AP Exam Reader
Siemens Awards for Advanced Placement

APAC 2010
Print Page
Home > AP Courses and Exams > Course Home Pages > Using a Date Class

Using a Date Class

by Laurie A. White
Mercer University
Macon, Georgia

More Nifty Assignments...

Programming Assignment: Using Classes
This program is designed to give you experience with classes in Java and get you started writing programs.

The Problem
Doing computations with dates is a frequently used operation. Businesses often give discounts when payment is received within 30 days, so there's a need to add 30 days to a given date. The 104th Congress promised many changes in the first hundred days, so 100 days had to be added to the opening date of Congress to find the end of the first period of reform. Subtracting days from a date of birth can be used in paternity suits.

Your program should prompt the user for an event, the date of the event, and the number of days to add, and then it should print the result of adding the days to the given start date.

Adding dates is not a trivial operation, but fortunately, there is a class that will print dates and do addition of dates for you. More details on this package are included below.

Solving the Problem
You will interactively enter the following quantities (be sure to provide clear user prompts) for a data arithmetic problem:
  • The name of the event (a string)
  • The date in question (a day, month, year, and era)
  • A number of days to add
Your program will use this input data to calculate the final date and print the information in a nice format.

A full run of the program appears below (user input appears in bold):
What is the event? Contract start
What is the starting day? 18
What is the starting month? 1
What is the starting year? 2004
What is the starting era (A or B)? A
How many days to add? 30

30 days after the Contract start on January 18, 2004 A.D. is February 17, 2004 A.D.
Use the prompts shown in the sample run in your program.

Using the Package
A class called My.Date is provided for you with some handy calendar routines. The methods you'll need for this program are described below.
  My_Date.java.zip

The Constructor
There is a default version of the constructor that creates the date January 1, 2003 A.D. If you'd prefer to create a different date, you can send four parameters representing the day, month, year, and era of the date. The day, month, and year should be int values (including int variables). The era should be a char (or char variable), either 'A' or 'B'. So, to create the date September 23, 2003 A.D., and store it in the variable day, you would use the instruction:
My_Date day = new My_Date (23, 9, 2003, 'A');
The Print Method
day.Print ();
This method will write a date in a "pretty format." For example, the call day.Print() (for the day defined above) will print:
September 23, 2003 A.D.
The Add Method
day.addDays (n);
This method will add n days to the date. For example, the call day.addDays (3) will change day to September 26, 2003 A.D.

Call your main program check_dates.

To Get Credit
Your program should run correctly, not only for the test data, but for any legal data. It should be well laid out and commented. Reread the programming standards in the syllabus. I'll be fairly lenient with the first program, but I will deduct points for flagrant violations.

Provide me with a printout of a script of the compilation and six runs of your program. Use the data. Don't enter the information in bold.
Contract start Data set 1
18 1 1995 A Jan. 18, 1995
30 Add 30 days
Start of the Congress Data set 2
3 1 1995 A  
100  
Signing of declaration Data set 3
4 7 1776 A  
1000 Add 30 days
1000  
Just an old old date Data set 4
10 10 10 B  
500  
Another random month Data set 5
20 3 1700 A  
1400  
Also add your own sixth data set.

A review of the commands to get the output:
[Note: This is system-specific. Please put your own instructions here.]

Hints on Starting
This may be the first program you have written on your own. (If it's not, feel free to skip this section.) How do you get started?

First, know how to do the problem by hand! Go through the example. Do you know how the answers came about? You may assume the methods in the class work correctly! If you can't do it by hand, you can't program it. (There are, of course, some problems that require so much number-crunching that they'd be impossible to do by hand, but we know how to do it, even if we can't practically do it.) Please come see me if the problem makes no sense.

Then, do the easy parts. The first lines of the program (the initial line, the comments, the declaration of main, and so on) should be easy.

Add the statements and declarations to read the input data. What type should each input value be? To be sure you have the right values, just write them out again.

Stop often! When you have the initial statements, stop, compile, and run it. Stop, compile, correct, and run after you add the data reads. This way you'll catch typos and syntax errors as they happen.

Once you have the input in, you can write the first line of the output statement -- it's just the input values rearranged (adding the call to Print). Is this formatted correctly? If not, fix the problems now. Then all you have to do is a good call to addDays and print the results.

You may have a different technique. Not every method works for everybody, so don't worry if you can do the program but don't do all of these steps. And please come by to see me (or drop me an email, or ask in class) if you want more help.

Contribute
If you would like to contribute your suggestions for nifty assignments, please submit your ideas.


  ABOUT MY AP CENTRAL
    Course and Email Newsletter Preferences
  AP COURSES AND EXAMS
    Course Home Pages
    Course Descriptions
    The Course Audit
    Sample Syllabi
    Teachers' Resources
    Exam Calendar and Fees
    Exam Questions
    FAQs
  PRE-AP
    Teachers' Corner
    Workshops
  AP COMMUNITY
    About Electronic Discussion Groups
    Become an AP Exam Reader

Back to top