Question
LinkedList Create a java program that allows the user to enter as many names and birthdays as they like. Each of these should be stored
LinkedList
Create a java program that allows the user to enter as many names and birthdays as they like. Each of these should be stored in an object of the supplied Birthday class. You must create an InvalidBirthdayException that will be used by Birthday. Your drivers should handle the exceptions thrown. Once the names are entered, you should print the list using the Birthday toString() method. Finally, create an iterator to traverse the list and return the oldest person in the list.
Additional instructions/hints.
The birthday class contains two instance variables, a String name and an int bday. Defaults will be Fred with a birthday of 20160714.
Birthdays should be entered in the format yyyymmdd.
You should structure your program with methods, recommend one to enter the values for the list, one to print the list, and one to find the oldest.
findOldest must use an iterator to traverse the list, you are not allowed to sort on the birthday to find the oldest. printList can use an enhanced for to print the contents of the list.
You need to modify Birthday to implement Comparable. This should be used to find the oldest person. Think about who would be the oldest
Example output
Enter the person's name. Enter -1 to exit
Name cannot be empty.
Enter the person's name. Enter -1 to exit
Sally
Enter the birthday (yyyymmdd)
0
Enter the person's name. Enter -1 to exit
Fred
Enter the birthday (yyyymmdd)
-19961212
Birthday of -19961212 is not valid. Must be between 0 and 20160714
Enter the birthday (yyyymmdd)
19961212
Enter the person's name. Enter -1 to exit
Sue
Enter the birthday (yyyymmdd)
20160715
Birthday of 20160715 is not valid. Must be between 0 and 20160714
Enter the birthday (yyyymmdd)
2016071$
Invalid characters found in the entered birthday
Enter the birthday (yyyymmdd)
20160714
Enter the person's name. Enter -1 to exit
-1
Sally 0
Fred 19961212
Sue 20160714
The oldest person in the list is Sally 0
this is what i got till now
public class Birthday
{
//name of person
private String name = "";
//Birth date of person in yyyymmdd format
private int bday = 0;
public final static int TODAY = 20160714;
public Birthday()
{
setName("Fred");
setBday(TODAY);
}
public Birthday(String name, int birthday) throws InvalidBirthdayException
{
setName(name);
setBday(birthday);
}
public String getName()
{
return name;
}
public void setName(String name) throws InvalidBirthdayException
{
if (name.length() < 1)
{
throw new InvalidBirthdayException("Name cannot be empty.");
}
else
{
}
this.name = name;
}
public int getBday()
{
return bday;
}
public void setBday(int bday) throws InvalidBirthdayException
{
if (bday < 0 || bday > TODAY)
{
throw new InvalidBirthdayException("Birthday of " + bday + " is not valid. Must be between 0 and " + TODAY);
}
if (bday != 0 && bday < 10000)
{
throw new InvalidBirthdayException("Date of " + bday + " is invalid.");
}
this.bday = bday;
}
public String toString()
{
String data = "";
data += name + " " + bday;
return data;
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started