Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please follow the specification exactly and PLEASE INCLUDE DETAILED COMMENTS so

ID: 3878763 • Letter: P

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 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 Object’s 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 Object’s 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<Person>) of Person objects

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

Explanation / Answer

MyDate.java

public class MyDate implements Comparable<MyDate> {

private int month;

private int day;

private int year;

// Parameterized Constructor

public MyDate(int month, int day, int year) {

this.month = month;

this.day = day;

this.year = year;

}

// Overriden compareTo method

// 0 will be returned if both Dates are equal

// -1 will be returned if Current Date is less than Obj

// 1 will be returned if Current Date 1 is greater than Obj

@Override

public int compareTo(MyDate obj) {

// Comparing Years

if (this.year < obj.year) {

// Returning -1 because Current Year is less than Obj's Year

return -1;

} else if (this.year == obj.year) {

// Both Years are equal here

// Comparing Months

if (this.month < obj.month) {

// Returning -1 because Current Month is less than Obj's Month

return -1;

} else if (this.month == obj.month) {

// Both Months are equal here

// Comparing Days

if (this.day < obj.day) {

// Returning -1 because Current Day is less than Obj's Day

return -1;

} else if (this.day == obj.day) {

// Both Days are same here

// Returning 0 because Current Date and Obj are same

return 0;

} else {

// Returning 1 because Current Day is greater than Obj's Day

return 1;

}

} else {

// Returning 1 because Current Month is greater than Obj's Day

return 1;

}

} else {

// Returning 1 because Current Year is greater than Obj's Day

return 1;

}

}

// Overriden toString method

@Override

public String toString() {

// Using StringBuilder as it provides better performance than String

// while concatenating

StringBuilder sb = new StringBuilder();

sb.append(this.month);

sb.append("-");

sb.append(this.day);

sb.append("-");

sb.append(this.year);

return sb.toString();

}

}

Person.java

public class Person implements Comparable<Person> {

private String name;

private MyDate birthDay;

// Parameterized Constructor

public Person(String name, int month, int day, int year) {

this.name = name;

this.birthDay = new MyDate(month, day, year);

}

// Overriden compareTo method

// 0 will be returned if both Persons are equal

// -1 will be returned if Person 1 is less than Person 2

// 1 will be returned if Person 1 is greater than Person 2

@Override

public int compareTo(Person obj) {

// Comparing birthdays using compareTo method of MyDate class

int birthDayCompare = this.birthDay.compareTo(obj.birthDay);

// If birthdays are same then comparing names

if (birthDayCompare == 0) {

// Comparing names

// compareTo method String class compares two Strings

// lexicographically

int nameCompare = this.name.compareTo(obj.name);

// If both names are same then returning 0.

if (nameCompare == 0) {

return 0;

} else if (nameCompare < 0) {

// Returning -11 because current object's name is less than

// Obj's name

return -1;

} else {

// Returning 1 because current object's name is less than Obj's

// name

return 1; //

}

} else if (birthDayCompare == -1) {

// Returning -1 because current object's birthDay is less than Obj's

// birthDay

return -1;

} else {

// Returning 1 because current object's birthDay is greater than

// Obj's birthDay

return 1;

}

}

// Overriden toString method

@Override

public String toString() {

// Using StringBuilder as it provides better performance than String

// while concatenating

StringBuilder sb = new StringBuilder();

sb.append(this.name);

sb.append(": ");

sb.append(birthDay.toString());

return sb.toString();

}

}

PersonSort.java

import java.io.BufferedReader;

import java.io.File;

import java.io.FileReader;

import java.io.IOException;

import java.util.ArrayList;

import java.util.List;

public class PersonSort {

private static List<Person> insertionList = new ArrayList<Person>();

private static List<Person> selectionList = new ArrayList<Person>();

static final String PERSON_FILE = "./src/Person.txt";

public static void main(String[] args) throws IOException {

populate();

selectionSort();

insertionSort();

}

static void populate() throws IOException {

// Reading Person.txt using BufferedReader

try (BufferedReader br = new BufferedReader(new FileReader(new File(PERSON_FILE)))) {

String currentLine = null;

// Checking if the current line in the file is null or not

while ((currentLine = br.readLine()) != null) {

// Splitting the input using space as delimiter

// currentDetails[0] = Name

// currentDetails[1] = Month

// currentDetails[2] = Day

// currentDetails[3] = Year

String[] currentDetails = currentLine.split("\s", -1);

// Creating a new anonymous object of Person class

// Adding the anonymous object into insertionList

insertionList.add(new Person(currentDetails[0], Integer.parseInt(currentDetails[1]),

Integer.parseInt(currentDetails[2]), Integer.parseInt(currentDetails[3])));

// Creating a new anonymous object of Person class

// Adding the anonymous object into selectionList

selectionList.add(new Person(currentDetails[0], Integer.parseInt(currentDetails[1]),

Integer.parseInt(currentDetails[2]), Integer.parseInt(currentDetails[3])));

}

}

}

static void selectionSort() {

// Size of array list

int listSize = selectionList.size();

// Moving the unsorted array list one by one

for (int i = 0; i < listSize - 1; i += 1) {

// Minimum person in the unsorted array list

int minimum = i;

// Finding the minimum person in the unsorted array list

for (int j = i + 1; j < listSize; j += 1) {

if (selectionList.get(j).compareTo(selectionList.get(minimum)) < 0) {

minimum = j;

}

}

// Swapping the minimum element with the first element of unsorted array list

// Storing the references of two Objects

Person swapOne = selectionList.get(minimum);

Person swapTwo = selectionList.get(i);

// Swapping the elements

selectionList.set(minimum, swapTwo);

selectionList.set(i, swapOne);

}

System.out.println("List sorted by selection sort ");

// Printing the elements of selectionList

for (Person person : selectionList) {

System.out.println(person);

}

System.out.println();

}

static void insertionSort() {

// Size of array list

int listSize = insertionList.size();

for (int i = 0; i < listSize; i += 1) {

Person key = insertionList.get(i);

int j = i - 1;

// Finding all the elements which are greater than key

while (j >= 0 && insertionList.get(j).compareTo(key) > 0) {

insertionList.set(j + 1, insertionList.get(j));

j -= 1;

}

// Swapping the key

insertionList.set(j + 1, key);

}

System.out.println("List sorted by insertion sort ");

// Printing the elements of insertionList

for (Person person : insertionList) {

System.out.println(person);

}

}

}