In: Computer Science
import java.util.ArrayList;
import java.util.Collections;
public class BirthdayList
{
/**
* This is the main process for class BirthdayList
*/
public static void main()
{
System.out.println("\n\tBegin Birthday List Program\n");
System.out.println("Declare an ArrayList for Birthday
objects");
// 1. TO DO: Declare an ArrayList to store Birthday objects (3
Pts)
// 2. TO DO: Replace the xxx.xxxx() with the appropriate method (2
Pts)
System.out.printf("\nBirthday List is empty: %s\n",
xxx.xxxx() ? "True" : "False");
System.out.println("Add at least five Birthdays to the
list");
// 3. TO DO: Add five or more Birthday objects to the Birthday list
(5 Pts)
// Format is: xxxxx.add(new Birthday("mm/dd","name")):
System.out.printf("Birthday List is empty: %s\n",
bdayList.isEmpty() ? "True" : "False");
// 4. TO DO: Replace the xxx.xxxx() with the appropriate method (2
Pts)
System.out.printf("Birthday list has %d entries\n",
xxx.xxxx());
System.out.println("\nDisplay unsorted Birthday list");
listBirthdays(bdayList);
System.out.println("\nDisplay 3rd Birthday in the listlist");
// 5. TO DO: Replace the xxx.xxxx() with the appropriate method (2
Pts)
System.out.printf("%s\n", xxx.xxxx());
// 6. TO DO: Remove a Birthday object from the list by index;
// Replace the xxx.xxxx() with the appropriate method (2 Pts)
System.out.printf("%s was removed from the list\n",
xxx.xxxx());
System.out.printf("Birthday list has %d entries\n",
bdayList.size());
// 7. TO DO: declare a new Birthday object to replace one in the
list (1 Pts)
// 8. TO DO: Replace (set) an element in the ArrayList with the new
Birthday
// object and complete the System.out.printf statement to display
the original
// Birthday object and the new (replacement) Birthday object (5
Pts)
System.out.printf("\nChange %s to %s\n", xxx.xxxx(),
xxx.xxxx());
System.out.println("\nCopy original Birthday list to new Birthday
list");
// 9. TO DO: Copy the original ArrayList to a new one called
newList (3 Pts)
System.out.println("\nSort new Birthday list");
// 10. TO DO: sort the new ArrayList (2 Pts)
System.out.println("\nDisplay new sorted Birthday list");
listBirthdays(newList);
System.out.println("\n\tEnd Birthday List Program\n");
}
/**
* This function lists all the Birthdays in the list
* @param list - reference to the Birthday ArrayList
*/
public static void listBirthdays(ArrayList<Birthday>
list)
{
int number = 1;
for(Birthday bDay : list)
System.out.printf("%3d - %s\n", number++, bDay.toString());
}
}
import java.lang.String;
public class Birthday implements
Comparable<Birthday>
{
private String date; // Format mm/dd
private String name;
/**
* Constructor for objects of class Birthday
* @param date - in the format mm/dd
* @param name - name associated with date
*/
public Birthday(String date, String name)
{
this.date = date;
this.name = name;
}
/**
* This method provides the comparison function for sorting
* It uses the compareTo method supplied with the String class
* @param other - reference to the Birthday to be compared to
* @return positive value if host date > other date
* negative value if host date < other date
* zero (0) if dates are equal
*/
public int compareTo(Birthday other)
{
return date.compareTo(other.date);
}
/**
* This method overrides the toString() method of the Object
class
* It returns a String with the Birthday informtion
* @return Birthday information as a String
*/
@Override
public String toString()
{
return date + " - " + name;
}
}
As only BirthdayList class was changed so posting that class only
import java.util.ArrayList;
import java.util.Collections;
public class BirthdayList {
/**
* This is the main process for class BirthdayList
*/
public static void main(String args[]) {
System.out.println("\n\tBegin Birthday List Program\n");
System.out.println("Declare an ArrayList for Birthday objects");
// 1. TO DO: Declare an ArrayList to store Birthday objects (3 Pts)
ArrayList<Birthday> bdayList = new ArrayList<>();
// 2. TO DO: Replace the xxx.xxxx() with the appropriate method (2 Pts)
System.out.printf("\nBirthday List is empty: %s\n", bdayList.isEmpty() ? "True"
: "False");
System.out.println("Add at least five Birthdays to the list");
// 3. TO DO: Add five or more Birthday objects to the Birthday list (5
// Pts)
// Format is: xxxxx.add(new Birthday("mm/dd","name")):
bdayList.add(new Birthday("03/26", "Sam"));
bdayList.add(new Birthday("07/01", "Nitya"));
bdayList.add(new Birthday("06/15", "Meena"));
bdayList.add(new Birthday("12/25", "Neeraj"));
bdayList.add(new Birthday("07/13", "Lipsy"));
bdayList.add(new Birthday("11/17", "Manisha"));
System.out.printf("Birthday List is empty: %s\n",
bdayList.isEmpty() ? "True" : "False");
// 4. TO DO: Replace the xxx.xxxx() with the appropriate method (2 Pts)
System.out.printf("Birthday list has %d entries\n", bdayList.size());
System.out.println("\nDisplay unsorted Birthday list");
listBirthdays(bdayList);
System.out.println("\nDisplay 3rd Birthday in the listlist");
// 5. TO DO: Replace the xxx.xxxx() with the appropriate method (2 Pts)
System.out.printf("%s\n", bdayList.get(2));
// 6. TO DO: Remove a Birthday object from the list by index;
// Replace the xxx.xxxx() with the appropriate method (2 Pts)
System.out.printf("%s was removed from the list\n", bdayList.remove(1));
System.out.printf("Birthday list has %d entries\n", bdayList.size());
// 7. TO DO: declare a new Birthday object to replace one in the list (1
// Pts)
Birthday newObj = new Birthday("09/04", "Sushant");
// 8. TO DO: Replace (set) an element in the ArrayList with the new
// Birthday
// object and complete the System.out.printf statement to display the
// original
// Birthday object and the new (replacement) Birthday object (5 Pts)
Birthday oldObj = bdayList.set(4, newObj);
System.out.printf("\nChange %s to %s\n", oldObj, bdayList.get(4));
System.out
.println("\nCopy original Birthday list to new Birthday list");
// 9. TO DO: Copy the original ArrayList to a new one called newList (3
// Pts)
ArrayList<Birthday> newList = new ArrayList<>(bdayList);
System.out.println("\nSort new Birthday list");
// 10. TO DO: sort the new ArrayList (2 Pts)
Collections.sort(newList);
System.out.println("\nDisplay new sorted Birthday list");
listBirthdays(newList);
System.out.println("\n\tEnd Birthday List Program\n");
}
/**
* This function lists all the Birthdays in the list
*
* @param list
* - reference to the Birthday ArrayList
*/
public static void listBirthdays(ArrayList<Birthday> list) {
int number = 1;
for (Birthday bDay : list)
System.out.printf("%3d - %s\n", number++, bDay.toString());
}
}
Output
Begin Birthday List Program
Declare an ArrayList for Birthday objects
Birthday List is empty: True
Add at least five Birthdays to the list
Birthday List is empty: False
Birthday list has 6 entries
Display unsorted Birthday list
1 - 03/26 - Sam
2 - 07/01 - Nitya
3 - 06/15 - Meena
4 - 12/25 - Neeraj
5 - 07/13 - Lipsy
6 - 11/17 - Manisha
Display 3rd Birthday in the listlist
06/15 - Meena
07/01 - Nitya was removed from the list
Birthday list has 5 entries
Change 11/17 - Manisha to 09/04 - Sushant
Copy original Birthday list to new Birthday list
Sort new Birthday list
Display new sorted Birthday list
1 - 03/26 - Sam
2 - 06/15 - Meena
3 - 07/13 - Lipsy
4 - 09/04 - Sushant
5 - 12/25 - Neeraj
End Birthday List Program