Question: Please follow the specification exactly and PLEASE INCLUDE DETAILED COMMENTS so that I may understand the code. Thank you. There will be three classes- the
Please follow the specification exactly and PLEASE INCLUDE DETAILED COMMENTS so that I may understand the code. Thank you.
There will be three classes- the Person class has 2 private instance variables and 3 public methods ONLY, the MyDate class has 3 private instance variables and 3 public methods ONLY- they both must implement the Comparable interface. You will then create a test class called PersonSort that has four static methods ONLY. Detailed specifications for all three classes are below.
MyDate Class
The MyDate class has three private instance variables:
int month (1-12)
int day (1-31)
int year.
The MyDate class has three public methods:
MyDate (int month, int day, int year)
int compareTo (MyDate obj)
String toString()
DO NOT create any other instance variables or methods. This is an immutable class.
It implements the Comparable interface. In the compareTo() method, make sure you check for year, month and day in that order. This class has two other methods: a three-parameter constructor that initializes the three instance variables (month, day and year) and an override of Objects toString() method that displays the date as month, day, and year.
................................................................................
Person Class
The Person class has two private instance variables:
String name
MyDate birthday
The Person class has three public methods:
Person (String name, int month, int day, int year)
int compareTo (Person obj)
String toString()
DO NOT create any other instance variables or methods. This is an immutable class.
It implements the Comparable interface. In the compareTo() method, compare the birthdays first, and then the names, using the compareTo() methods of the MyDate and String classes. It has two other methods: a four- parameter constructor that initializes the instance variables (name and birthday) passing three of the parameters to the MyDate birthday object in order to initialize it and an override of Objects toString method that displays the name and the birthday (using the toString method of the birthday object).
...............................................................................................
PersonSort Class
The PersonSort class is the tester class for this program. It has four static methods: main, populate, selectionSort and insertionSort.
The main method
creates an ArrayList (ArrayList
populates it from a file that is supplied to you
copies the ArrayList to another one
uses the first ArrayList and sorts it using an insertion sort algorithm
displays the sorted ArrayList
uses the second ArrayList and sorts it using a selection sort algorithm
displays the sorted ArrayList
The populate method reads in the Person data from a file (see below for the format). DO NOT assume that you know how many records are in the file. It creates a Person object from each line in the file and adds the object to the first ArrayList.
The selectionSort method sorts the ArrayList using the selection sort algorithm.
The insertionSort method sorts the ArrayList using the insertion sort algorithm.
You are not permitted to use the sort method of the Collections framework, or its swap method. The Person objects must be sorted, first by birthday and then by name.
.................................................................................................
The filename of the supplied file is Persons.txt. Place it in the src folder of either Eclipse or IntelliJ. Create a global static constant to hold the file name that will be passed to the File object for reading the file. DO NOT assume that you know how many records are in the file.
static final String PERSON_FILE = ./src/Persons.txt;
The format of the file is: name month day year
You may use a try/catch block or let main throw the FileIO exception.
Contents of Person.txt:
Merli 9 10 1998 Elijah 3 19 2013 Christine 7 29 1981 Annie 8 28 2002 Bella 8 28 2010 Matt 9 8 1952 Cassidy 8 5 1997 Crystal 12 12 1978 Glenda 12 27 1971 Hen 4 1 1985 Jacob 4 17 1978 Matt 9 8 1950 Jenell 12 27 1971 Lilly 12 7 2008 April 7 2 1983 Yung 4 2 1986 Madon 9 2 1976 Chris 12 27 1970 Mary 10 2 1954 Jane 3 1 1985 Mark 9 12 1952 Chaz 9 10 1998 Phoenix 10 25 2004 Suri 8 14 2014 Terra 1 16 1999 Tom 8 21 1952 Ashton 8 10 1995 Thiec 8 30 1984 Jan 4 2 1986 Glenn 11 27 1970
.....................................................................................
Sample Output:
List sorted by selection sort
Matt: 9-8-1950 Tom: 8-21-1952 Matt: 9-8-1952 Mark: 9-12-1952 Mary: 10-2-1954 Glenn: 11-27-1970 Chris: 12-27-1970 Glenda: 12-27-1971 Jenell: 12-27-1971 Madon: 9-2-1976 Jacob: 4-17-1978 Crystal: 12-12-1978 Christine: 7-29-1981 April: 7-2-1983 Thiec: 8-30-1984 Jane: 3-1-1985 Hen: 4-1-1985 Jan: 4-2-1986 Yung: 4-2-1986 Ashton: 8-10-1995 Cassidy: 8-5-1997 Chaz: 9-10-1998 Merli: 9-10-1998 Terra: 1-16-1999 Annie: 8-28-2002 Phoenix: 10-25-2004 Lilly: 12-7-2008 Bella: 8-28-2010 Elijah: 3-19-2013 Suri: 8-14-2014
List sorted by insertion sort
Matt: 9-8-1950
Tom: 8-21-1952
Matt: 9-8-1952
Mark: 9-12-1952
Mary: 10-2-1954
Glenn: 11-27-1970
Chris: 12-27-1970
Glenda: 12-27-1971
Jenell: 12-27-1971
Madon: 9-2-1976
Jacob: 4-17-1978
Crystal: 12-12-1978
Christine: 7-29-1981
April: 7-2-1983
Thiec: 8-30-1984
Jane: 3-1-1985
Hen: 4-1-1985
Jan: 4-2-1986
Yung: 4-2-1986
Ashton: 8-10-1995
Cassidy: 8-5-1997
Chaz: 9-10-1998
Merli: 9-10-1998
Terra: 1-16-1999
Annie: 8-28-2002
Phoenix: 10-25-2004
Lilly: 12-7-2008
Bella: 8-28-2010
Elijah: 3-19-2013
Suri: 8-14-2014
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
