Begin creating a dedicated Utils class. This class will contain useful, reusable static utilities. We
will add to it over the coming weeks, and use it for all coming assignments for all user input
needs no more direct use of Scanner (code reuse!). The first two methods will be:
o A method to read one int value, using either Scanner (command line) or JOptionPane
(GUI popup) modes.
o A method to accurately calculate an age as of any reference date.
Develop a first short algorithm. Using two input CS12Date input objects, determine an age,
taking into account future birthdates, and whether or not a yearly birthday has happened yet.
Test the above Utils class and its new methods from a short client class. Prompt the user the
month, day, and year of two birthdates (once using each input mode), and use these to check
the age algorithm. Then, see if your algorithm accurately predicts YOUR age as of some future
milestone date, such as your 50th or 100th birthday.
Test your age algorithm thoroughly using an automated test program. Given a test program,
run it against your Utils class and check your age calculations for a wide range of birthdates:
past, present AND future. If any results do not pass, you will need to go back and debug your
age logic algorithm! The debugger can be a powerful tool for this, so spend a little time
learning to use it on this assignment. I will demo such use at the (recorded) office hours.
Learn to create some code from a program specification. Here, your client (uh, thats ME) has
provided you with a detailed program spec. Iteratively synthesize the list of requirements,
method interfaces, desired program structure, and some pseudocode into the working
prototype envisioned by the customer. (BTW, a detailed program spec like this is usually not
the norm usually there is far vaguer guidance than this!)
Were going to be enhancing and extending this Utils class weekly for the rest of the semester, and
using it for ALL our user input needs from here on out. Therefore, its critical that you get it completed
on time for THIS assignment. Here, you will be given a starting shell for the Utils file, so just adapt it
according to the program spec.
The good news is, once we get our first small capability working, it will be easy to extend and clone
it for other uses. For example, this week well concentrate on reading int-typed user inputs. Next week,
well add certain error checking. After that, it will be trivial to clone it for reading doubles, chars, Strings,
etc.
Problem Summary:
Create a Utils utility class, using a provided code template, and modify TWO methods:
o Adapt a readInt() method to prompt for an int input, using either of two input modes:
Scanner command line input or JOptionPane popup GUI input
o Create a 2-input getAge() method which calculates an age as of any reference date
Create a client test program, to test the Utils class:
o Use readInt() 2x (both modes) to prompt for month, day, and year of two birthdates
o Calculate the ages of these two birthdates as of the default date of today
o Calculate a third age for your birthdate as of some milestone BD such as 100 or 50
Test your Utils class by running against a provided test program. Thoroughly test your age
algorithm over a wide range of dates. Debug your age algorithm as needed.
Preliminaries:
1) As always, begin by FIRST reviewing all the weekly lecture materials, videos, and source code
examples. They should answer most, if not all, questions. If anything is still cloudy in your mind,
please ask!
2) IMPORTANT: pay close attention to the last example in the selection hybrid PDF and lecture
video (about T=6:12 onward). This example is VERY similar to what youre being expected to
do in your Utils class. Study this code and make sure you understand how it works.
3) Start out by collecting into one place all the program raw materials youll need:
This program specification
The starting Utils code template: UtilsFL.java
The provided test program: UtilsTest1RL.java
The CS12Date class: CS12Date.java
The CS12Date API: CS12Date API and UML.pdf
4) Rename the file UtilsFL.java to your own initials. Update the header block. Then, compile the
file to make sure it works. Dont try and run it, because it wont run standalone.
5) Inspect the layout of the UtilsFL.java class. Locate the 2 methods containing the text TODO.
These are the only 2 methods in this file which require updates. See the Requirements and
Design sections for the modifications needed.
6) Next, open the test file UtilsTest1RL.java. Do a global replace of the pattern UtilsRL to the
name of YOUR utilities file. Within jGRASP, use the Edit: Find/Replace menu item, or the Ctrl-F
shortcut.
7) Then, compile and execute the file UtilsTest1RL.java and make sure it runs to completion. Most
of the test cases will fail with an age of -1, but now you have some working test code against
which to iteratively test your age algorithm, as you develop it. Retest against this file
frequently!
8) Create the starter client file AgeClientFL.java. Use your template as a starting point. Dont
forget to update the comment block accordingly. See the Requirements and Design sections for
the needed code descriptions.
9) Begin making the needed changes to your UtilsFL.java file. See the Requirements and Design
sections for the needed code descriptions. Test your code iteratively both by using the
AgeClientFL.java client file, and by running the UtilsTest1RL.java test code frequently as you
develop your ae algorithm.
5) Coding style: By this point in the course, good general adherence to the course Coding Standard
is expected on ALL programs. Pay attention to things such as:
The last example is the day BEFORE my 100th BD, when I will still be 99 years old.
Structure:
Your application will be structured as shown below. It will consist of the primary two Java classes
AgeClientFL and UtilsFL, plus 2 more in a supporting capacity.
Design:
These are the method interfaces needed for the UtilsFL class. The only changes you need to make are
indicated in RED.
Age Algorithm:
There are multiple possible ways of solving this problem, but it boils down to two main things:
1) Is the birthdate later in absolute time that the reference date? If so, age=-1 by our definition, so
return that value and print an error message from getAge().
2) Otherwise, determine if the birthdate has or has not taken place in the current calendar year.
What is ones age if their BD has happened already, versus ones age if their BD has NOT yet
taken place within one calendar year??
Start by taking apart the two input CS12Dates into 6 individual ints: 2 sets of month, day,
year. What CS12Date API methods will let you do this??
Devise some good, consistent naming scheme for these 6 ints, to keep good track of them.
Think in terms of a reference date, and NOT just todays date. We want to find ages as of
any reference date in time, today is just one specific case of a reference date.
Anything in the CS12Date API weve used that lets you compare two dates in absolute time??
Use those 6 ints to make the needed relative comparisons between past and future months in
the same year, past and future days within the current month, etc. See the second figure above.
How would you express such logic programmatically??
A newborn can have an age of 0, and anyone whos been born has an age as of any date.
Suggested Implementation:
I suggest the following sequence of implementation. Take things one small step at a time!
First, make sure you understand how the provided starter Utils class works. Use the existing
readInt() to prompt for 3 int values in your client program. Use these to construct 2 CS12Date
objects, and use these objects to call both the getAge() methods. Print the resulting ages, which
should be -1 for now, but thats OK for now as a starting point.
Next, update the readInt() in your Utils class. Add the second input flag, implement GUI popup
input, and test both input modes from your client program, to set up your CS12Date objects.
Next, implement the age algorithm. Develop the algorithm in stages. For example, first figure
out how to reject future dates (later than the reference date) and return a -1 age. Then,
consider how to express birthdates within any one calendar year that are BEFORE and AFTER the
reference date. See the Age Algorithm notes.
Structure. You can use sequential or nested selection, whatever logical structure you wish.
Which one seems more reasonable to you??
Test your program. Check your results against the provided sample outputs. Also test your
algorithm by running the test program against it. See the comments below.
Testing:
Test your client program before submitting it. Each execution of the client should prompt for two sets
of inputs, a month/day/year each time, using both command line AND GUI popups. Each iteration
should print the birthdate AND the corresponding age. For the milestone case, it should also display the
as of reference date
Run your program with multiple inputs. Test with a variety of birthdates, and make sure all the results
are correct. If not, go back and tighten up your age algorithm!
Test versus the provided test program. Carefully inspect the output to make sure all results are pass
(true). If any do not pass, go back into your age logic and figure out why!
Diagnostic outputs. If your age logic isnt giving correct results for all test cases, you will need to do
some debugging. Add some debug print statements in your logic, to show which branches are reached
(or NOT reached). This will give you clues as to what needs tightening up
However, using the debugger itself is ideal for this kind of debug, and much simpler and faster in the long
haul. I highly suggest learning to use it on this assignment!
Clean up your final code. Remove any debugging print statements, unused variables, etc. I do NOT want
to see any kind of debug or diagnostic outputs in your final code outputs.
Check your code and output. Check your code against the rubric, and check your outputs against the
provided sample outputs, before submitting.
Sample test program output:
Running the provided test code should yield results something like this. Look for all pass results. If
not, go back and debug your age logic!
Submitting the assignment:
Submit the both Java source files in Canvas, under this assignment. You dont need to also include
CS12Date or the test driver file.
Make sure your code adheres to all the usual coding conventions described in the course Coding
Standard. Check your program against the rubric below AND the sample outputs, before submitting
it!
Grading:
See the rubric associated with this assignment in Canvas, or below.