In this project you will practice creating and manipulating objects. Specifically, you will learn more about
the GregorianCalendar class we saw in our first lab. It captures a single point in time down to millisecond
precision. Rather than working
with time as we did in lab, you will instead manipulate the date features of
the calendar. As I mentioned in
lecture and lab, objects of type GregorianCalendar are quite complex and can be
a little intimidating at first, so I will give you several pointers to help you
get started.
Your program should perform the following tasks:
1.
Ask the person running it to enter their name.
2.
Ask them for their date of birth (of course they need not
enter their real birthday!).
3.
Display a personalized greeting that includes the current date
with day of week, month, day of month, and year, e.g. Sunday, September 9,
2007. The format of the greeting
and date is up to you.
4.
Display the date of birth and tell the user on which day of
the week they were born.
5.
Display a message stating on which day of the week their
birthday falls this year.
6.
Display a message stating on which day of the week their 100th
birthday will fall.
7.
Display a message stating what the date and day of week will
be in 100 days.
8.
Display a message stating what the date and day of week will
be in 3 months.
9.
[Optional] Generate a random number of days, months, and years
and display a message stating the date and day of the week that many days,
months, and years from today. We
will talk about random number generation in lab this week.
To simplify grading create a project called username-proj1, where username is your login/email address, e.g.
srussell-proj1. Create a class
file in it called BirthdayGreeter that performs the above action. Please do not call your project or
class something different! It
makes things harder for me and the grader when there are 60 different naming
conventions. Please remove any
additional classes you create for testing before you submit your final
solution.
Solicit inputs, i.e. ask questions and get answers, from the user via
graphical dialog boxes as we did in lab.
Recall that we used JOptionPane to display a dialog box with a question
and a field into which the user typed their answer. See NameGetter and PersonalGUIGreeter for help. Since the JOptionPane method we use to
solicit input returns a string, we need to convert this value to a number for
the month, day, and year of birth.
This is done using the Integer object type (which is an object version
of int).
For example, new Integer("5") creates a new object of type Integer
with the value 5. Java implicitly
converts Integers to type int as need.
So:
int dayOfMonth= new Integer("5");
creates a new variable dayOfMonth and initializes its vale to 5.
Display output messages to the user both in the terminal display window and in graphical dialog boxes.
The description of Project 2.1 in the textbook on page 71 describes the
basics of creating and manipulating GregorianCalendar objects. You can also look at the Greeter
classes we used in the first lab.
Additional information about this class (and any standard Java class)
can be found at: http://java.sun.com/j2se/1.5/docs/api/index.html. The Java Class Libraries option in the
Help menu of BlueJ will take also take you there. Search for GregorianCalendar in the list of classes in the
left frame.
To make your dates aesthetically pleasing I will provide you with a class
called CalendarLabeler
which you can use to get the month and day of week text. Place the CalendarLabeler.class file in
the same directory as your class file(s).
That way the compiler will be able to find it without the help of an
import statement like we use for GregorianCalendar and JOptionPane in lab. The HTML documentation for
CalendarLabeler is here. The class has two methods:
getDayOfWeekLabel() and getMonthOfYearLabel(). Both of them take an integer argument and return a
string. Like the main method,
these are so-called static methods
(we'll learn about them a little later).
The syntax for invoking a static method is ClassName.methodname(), e.g.
the command System.out.println(CalendarLabeler.getDayOfWeekLabel(1)); displays
Sunday in the terminal window.
You will submit your projects electronically. To do so
1.
First, make a copy of your project folder/directory, e.g.,
srussell-proj1.
2.
Remove from the copy all source (.java) files except for
BirthdayGreeter.java. Also remove
all .class files.
3.
Connect to the Academics volume of the server called
fileserver1. You do this by
choosing Connect to Server ... from the Go menu in the Finder, typing afp://Fileserver1
in the dialog box, and selecting Academics from the volume list.
4.
In Comp there is a folder for our course (CS151) and inside
that there is a folder called DropBox.
Copy your clean project folder into the DropBox.