Anda di halaman 1dari 11

PRGM10: Age Utilities

due: Thursday 04/06/17, 12:00 noon (42 pts)


Overview:
For this assignment, were going to start working with selection (decision-making, or logical forks in the
road) in Java programs. Youll be doing several useful things in the course of this assignment:

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.

Get something simple working FIRST, and then keep adding to it


Java Objectives:
1) Selection logic (if-else, nested and/or compound) [CSLO-1]
2) Static methods [CSLO-1]
3) Method creation [CSLO-3]
4) Code modification and reuse [CSLO-3]
5) Use of existing classes Scanner, JOptionPane, and CS12Date [CSLO-2]
6) Translation of a problem specification in Java code [CSLO-4]
7) Application of Java to solve simple calculations [CSLO-4]
8) Ongoing use of good programming conventions [CSLO-3]

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.

Then look over the Requirements, Sample Outputs,


Structure, Design, and Age Algorithm sections, to get a
big-picture sense of whats being asked for in this
assignment!
Requirements:
1) Structure: Your application must consist of the following two Java classes:
a. An AgeClientFL client class (created from your program template).
b. A UtilsFL utilities class (created by modifying the provided starter code).
c. You will also need to use the CS12Date class.
d. You will also need to use the provided UtilsTest1RL class test code to check your
program.
e. See the Structure section which follows for the expected code structure.

2) Utility class: Your utilities class needs the following modifications:


a. Modify only the indicated TODO code. All other methods must be left unchanged.
b. All methods in this class will be called statically from the client, using the class name:
UtilsFL.method()
c. See the Design section which follows for the expected method interfaces.

d. In the readInt() method:


i. Add a second boolean input mode flag., allowing switching between input
data sources.
ii. Add appropriate selection logic.
iii. If the mode flag is false, use the provided Scanner input code.
iv. If the mode flag is true, add the equivalent JOptionPane input code.
v. Use the common input prompt text for both input modes.
vi. Return one common int value to the calling client. Do NOT print anything from
this method!

e. In the 2-input getAge() method:


i. Create an algorithm which correctly calculates the age for a given input
birthdate, as of any given reference date. Both inputs are CS12Date objects.
ii. Assume that in general, the reference date is NOT the current date!
iii. If the input birthdate is AFTER the reference date in time, set a -1 value for the
age, and print an error message to the command line from this method.
iv. Return the calculated age value to the client, without printing it.
v. No error checking on the dates is needed. You can safely assume that any
CS12Date inputs have been correctly created upstream by the client.
vi. See the Age Algorithm notes which follow, for some ideas how to implement
the needed age logic.
3) Client program: Your client class AgeClientFL tests the UtilsFL class by doing these 3 things:
a. Test 1-input (as of today) age calculation using Scanner (false) mode of readInt()
i. Prompt 3x for month, day, year ,and assemble these ints into a CS12Date.
ii. Display the input date AND the resultant age to the command line.
b. Test 1-input (as of today) age calculation using GUI (true) mode of readInt()
i. Prompt 3x for month, day, year, and assemble these 3 ints into a CS12Date.
ii. Display the input date AND the resulting age to the command line.
c. Test 2-input, age as of a specific date calculation using 2 hardwired dates (no inputs)
i. Create a new CS12Date with your birthdate, and another one with some
milestone date for you (your 21st or 50th BD, your wedding date, etc.).
ii. YOU do NOT have to prompt for these, simply create both dates directly.
iii. Determine your age as of that milestone date, and display BOTH dates and the
resultant age on that date, to the command line.
d. There must be NO direct use of Scanner or JOptionPane in your client main() method.
Use your created Utils readInt() method for all needed input values (6 total).
e. There must be NO age logic or error code in the client code.
f. Your expected client program outputs should resemble those shown in the Sample
Outputs.

4) Testing: Test your code in the following two ways:


a. By running the client program, as described above.
i. You should be able to replicate the results shown in the Sample Outputs
section.
ii. Use YOUR birthdate and some milestone date for you (future OR past).
b. By running the provided test program.
i. ALL results should indicate pass.
ii. If any age results do not pass, go back and debug your age logic as needed!

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:

All variables and objects declared at the head of any method


General commenting of program logic, explain what is happening
Nesting of all code inside any braces, now including if-else logic
Whitespace in code for readability
Whitespace and labeling of outputs for readability
Dont leave any debug prints or unused variables in the code: comment it out, or get rid of it
Sample Output:
Your resulting client program outputs should look something like this (you can label it however you
wish). Here, we are exercising both the input modes of our UtilsFL.readInt() method:

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??

Some other things to take into consideration:

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:

Turn in the 2 Java files called AgeClientFL.java and UtilsFL.java

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.

PRGM10: TOTAL POINTS POSSIBLE: 42


1.00 Program Elements/Structure 20
1.01 2-file solution: Utils and AgeClient classes 1
1.02 Use of provided Utils template, other methods left unaltered 2
1.03 Utils readInt() with proper input flag, 2 input modes, proper mode convention 3
1.04 Utils getAge() 2-input version: age algorithm, return int value WITHOUT printing 4
1.05 Utils getAge() 2-input version: error checking, age= -1 returned WITH message printed 2
1.06 AgeClient, prompt for 3 non-GUI inputs, 1-input call to calculate age 2
1.07 AgeClient, prompt for 3 GUI inputs, 1-input call to calculate age 2
1.08 AgeClient, milestone BD and 2-input call to calculate age (can hardwire dates) 2
1.09 NO Scanner, GUI, age logic, or error message code directly inside client (static util calls only) 2
2.00 Program Execution 15
2.01 Both files compile and run as-submitted 2
2.02 Adherence to specified interface, test program compiles/runs against Utils 2
2.03 Correct age results for all test program cases 5
2.04 All outputs clearly displayed and labeled, date(s) AND age for each output case (x3) 3
2.05 Correct age results for all 3 client cases 3
3.00 Program Style 7
3.01 General adherence to coding standard 5
3.02 Adequate commenting of all program logic, methods 2
4.00 Late Deduction 0
4.01 3 pts/day 0

Anda mungkin juga menyukai