CSE142—Computer Programming I

Programming Assignment #4

Due: Tuesday, 4/28/09, 9 pm

This assignment will give you practice with interactive programs, if/else statements and methods that return values.  You are to write a program that prompts the user for (1) a minimum allowed age, (2) the current date, and (3) the birth dates of some number of people.  The program determines if everyone is at least the minimum age.  It also gives a birthday message when encountering a person on his/her birthday.

See the sample logs of execution at the end of this handout to see how the program should behave.  Your program should have the exact same output.  The output-comparison tool on the course website has these logs.

When the program runs, it prints an introductory message and then prompts the user for the following:

·        The minimum age (in years)

·        The current year

·        The current month

·        The current day

·        The number of people whose birthdates should be entered

It then prompts for the birth year, birth month, and birth day-of-month for each person.  Note that all the answers should be whole numbers.

For each person it calculates the person’s age in years and uses this information to compute the minimum age of any person that has been entered.  If it is a person’s birthday, it prints a message like, “Happy 34th birthday!” or, “Happy 22nd birthday!” where the number is the person’s (new) age.  After all the information has been entered, the program outputs two more lines.  The first looks like, “The youngest person is age 12” if 12 is the age in years of the youngest person.  The second line depends on whether the youngest person is at least the minimum age.  If so, the output is, “Everyone is old enough. Welcome!  Otherwise, the output is something like, “Sorry, you have not all reached your 21st birthday.” Or, “Sorry, you have not all reached your 34th birthday.” The number in these messages is the minimum age.

Notice there are a couple places where you need to print an ordinal number (e.g., 5th) instead of just a number.  Here is how to compute the proper suffix to make a number an ordinal in English:

                           

If the last two digits of a number are 11, 12, or 13, then the suffix is th,                Last digit          Suffix

otherwise the last one digit of a number determines the suffix like this:                   1                      st

                                                     2         nd

                                                     3         rd

                                                                                                                                    anything else  th

Hint: The mod operator with 100 gets the last two digits and the mod operator with 10 gets the last digit.

You do not have to perform any error checking.  That is, you can assume all information entered is a number in the appropriate range.  You may assume the number of people is at least 1.  You may assume everyone has an age of at least 0 (i.e., everyone is already born).  You may assume everyone was born in a positive year (1 or later).  These assumptions can make it easier to compute the youngest age since nobody can be older than the current year, but it is also possible to compute the youngest age without making these assumptions.

There are several ways to compute an age, but keep in mind that the current month and day could be earlier in the year or later in the year than a person’s birth month and day.  Nothing in this assignment requires knowing the number of months, the days in a month, the presence of leap years, etc. -- such information is not helpful.

In terms of program style, follow these instructions:

·        Use static methods to break the problem into logical parts.  Have at least 5 methods in addition to main. 

o       While main should be short and use other methods for most of the work, you are allowed to have main prompt for the minimum age, current data, and number of people.

o       Include a method that returns the correct suffix for an ordinal number.  This method must return a useful result and must not do any printing itself.

o       At least one (and two is probably a good idea) of your methods in addition to your suffix method should also return results that the caller then uses or passes to another method. 

o       Keep separate the code to compute the youngest age and the code to check if everyone is old enough.  Do not have the youngest-age code call the age-checking code.  Instead, the youngest-age computation should return a result and other code, perhaps in main, should then pass this result to the age-checking code.

o       None of your methods should be more than 25 lines long, preferably shorter.  Lines that contain only comments do not count.

·        Several parts of your solution need if/else statements.  Use appropriate forms, such as nested if/else statements, to make your code clear and concise.

·        Construct only one Scanner object.  Pass it to other methods as necessary.  Note a method may need it just to pass it to other methods, even if it does not prompt for input itself.

·        Include a short comment at the beginning of your program and a short comment for each method describing what it does.

·        Use only constructs covered in Chapters 1, 2, 3, and 4 of the textbook.

Your program should be stored in a file called AgeCheck.java.  Because your program will be using a Scanner object, you need to include the following declaration at the beginning of your program:

import java.util.*;

Here are three sample executions.  The user input is bold and underlined. These examples are available from the output-comparison tool on the class web page.  Reproduce this output exactly.  Notice the blank lines.

 

 

 

This program prompts you for the current date

and several birthdates.

It computes the age in years of the youngest

person and wishes Happy Birthday as appropriate.

Minimum allowed age? 21

Current year? 2009

Current month? 4

Current day? 28

Number of people? 3

 

Birth year? 1988

Birth month? 4

Birth day of month? 29

 

Birth year? 1986

Birth month? 4

Birth day of month? 28

Happy 23rd birthday!

 

Birth year? 1985

Birth month? 1

Birth day of month? 1

 

The youngest person is age 20

Sorry, you have not all reached your 21st birthday.

 

 

 

This program prompts you for the current date

and several birthdates.

It computes the age in years of the youngest

person and wishes Happy Birthday as appropriate.

Minimum allowed age? 10

Current year? 2010

Current month? 2

Current day? 28

Number of people? 2

 

Birth year? 1970

Birth month? 2

Birth day of month? 28

Happy 40th birthday!

 

Birth year? 1999

Birth month? 12

Birth day of month? 31

 

The youngest person is age 10

Everyone is old enough.  Welcome!

 

 

 

This program prompts you for the current date

and several birthdates.

It computes the age in years of the youngest

person and wishes Happy Birthday as appropriate.

Minimum allowed age? 5

Current year? 2009

Current month? 4

Current day? 28

Number of people? 3

 

Birth year? 2009

Birth month? 4

Birth day of month? 28

Happy 0th birthday!

 

Birth year? 2007

Birth month? 4

Birth day of month? 28

Happy 2nd birthday!

 

Birth year? 2005

Birth month? 4

Birth day of month? 28

Happy 4th birthday!

 

The youngest person is age 0

Sorry, you have not all reached your 5th birthday.