CPSC 155 – COMPUTER PROGRAMMING
Due: December 4
There is an input file called Presidents.txt on Canvas. You have to write a Java program to help a historian work with the data in the file. For each president, the file has the following information in it:
o Status – an A for presidents who are still alive, a D for presidents who are dead.
o Last Name
o First name
o Month Born
o Day Born
o Year Born
o For dead presidents only:
o Month died
o Day died
o Year died
Sample from the input file:
D Nixon Richard 01 09 1913 04 22 1994
D Ford Gerald 07 14 1913 12 26 2006
A Carter Jimmy 10 01 1924
D Reagan Ronald 02 06 1911 06 05 2004
A Bush George 06 12 1924
You will use four classes in this program.
1. The first class is the Date class. The class has three fields month, day and year. All three are integers.
a. You need to add a constructor and methods to getDay, getMonth , getYear and getMonthName (the String corresponding to the month, like “January”).
b. You should also have a method formattedDate that will return the date in the format Month Name Day, Year (for example, “January 9, 1913”).
2. The second class is the Person class. The class should have the following fields: lastName (String), firstName (String), alive (Boolean), birthDate (Date), deathDate (Date). The class should have the following:
a. A constructor for presidents who have died. It would get the following parameters: last name; first name; month, day and year of birth; month, day and year of death. Give appropriate values to all the fields.
b. A constructor for living presidents. It would get the following parameters: last name; first name; month, day and year of birth. Give appropriate values to all the fields. deathDate should be set to null since there’s not an appropriate value for it.
c. It should have the following methods: getLastName(), getFirstName, isAlive(), getBirthDate(), and getDeathDate().
3. The third class is the Presidents class. The class has an array of Persons as a field. You can add any other fields that you need/want. You may assume that there are 50 or fewer presidents. Your program should, of course, work even if there is different data in the file (though you may assume it is in a valid format and there are no more than 50 presidents). The class should have the following:
i) An appropriate constructor that initializes the fields and calls the getInfo method. You should use a named constant for the maximum size of the array (50) rather than having a “magic number” directly in your code
i) getInfo: Read in all the information from the input file and store it in the array. Remember to create the objects for the people.
ii) printIt: Print out the information to a file in a neat well formatted table (see sample). The table should have 3 columns – the full name, the birth day and the date they died. Both dates should be in the format MonthName DayNumber, Year (for example, January 9, 1913). For people who are still alive, print “Still Alive” instead of the date they died. Print your name at the end.
iii) averageAge: Calculate and print out the average age of the presidents when they died (to the screen). You only need to use the year while calculating ages. The average should be printed to one decimal place. You may assume some presidents have died. Do not include presidents who are still living in this calculation.
iv) youngestToDie: Print out the name of the president who lived the shortest (and how old they were when they died) to the screen. Do not forget to exclude presidents who are still alive.
v) sortIt: Sort all the data according to the president’s last names. If two presidents have the same last name, sort the data according to the first names.
4. Your fourth class should include a main method. Use it to define an object of the Presidents class and in order call the averageAge, youngestToDie, sortIt and printIt methods.
Submit the following:
1) A listing of your classes. (printout)
2) A printout of the output on the screen
3) A printout of the output file your program created
Sample output created by the program on the screen:
The average age of presidents when they died was 71.6 years.
The president who died the youngest is John Kennedy at 46 years.
File printed successfully.
Sample from the Output File:
Full Name Birth Date Death Date
——— ———- ———-
George Washington February 22, 1732 December 14, 1799
John Adams October 30, 1735 July 4, 1826
Thomas Jefferson April 13, 1743 July 4, 1826
Ronald Reagan February 6, 1911 June 5, 2004
George Bush June 12, 1924 – Still Alive –
Bill Clinton August 19, 1946 – Still Alive –
GeorgeW Bush July 6, 1946 – Still Alive –
Barack Obama August 4, 1961 – Still Alive –
Created by Biff