Help with part of a JAVA program and Thanks in advance. Essentially I just need
ID: 3741219 • Letter: H
Question
Help with part of a JAVA program and Thanks in advance. Essentially I just need to sort items in the file using insertion sort and selection sort. here is the part of lab requirement I need help with:
The PersonSort class is the tester class for this program. It has four static methods: main, populate, selectionSort and insertionSort. The main method 1. creates an ArrayList (ArrayList) of Person objects 2. populates it from a file that is supplied to you 3. copies the ArrayList to another one 4. uses the first ArrayList and sorts it using an insertion sort algorithm 5. displays the sorted ArrayList 6. uses the second ArrayList and sorts it using a selection sort algorithm 7. displays the sorted ArrayList The populate method reads in the Person data from a file (see below for the format). See slide 99 for a good example of how to do this. 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 must implement these sorting algorithms. A version of this code is containined in the Lesson 3 Source Code. 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.
Explanation / Answer
package TypesOfLinkedList;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
class Person{
String name;
int month,day,year;
public Person(String name,int month,int day,int year){
this.month=month;
this.day=day;
this.name=name;
this.year=year;
}
public String getName(){
return name;
}
public int getMonth(){
return month;
}
public int getDay(){
return day;
}
public int getYear(){
return year;
}
}
public class PersonSortTest {
static final String PERSON_FILE = "D:\Use\JavaEE\NewLang\src\person.txt";
public static List<Person> Populate() throws IOException {
List<Person> lst= new ArrayList<Person>();
List<String> allLines = Files.readAllLines(Paths.get(PERSON_FILE));
for (String line : allLines) {
String[] str=line.split(" ");
Person person=new Person(str[0],Integer.valueOf(str[1]),Integer.valueOf(str[2]),Integer.valueOf(str[3]));
lst.add(person);
}
return lst;
}
private static boolean compare(Person d1, Person d2)
{
// All cases when true should be returned
if (d1.getYear() > d2.getYear())
return true;
if (d1.getYear() == d2.getYear() && d1.getMonth() > d2.getMonth())
return true;
if (d1.getYear() == d2.getYear() && d1.getMonth() == d2.getMonth() &&
d1.getDay() > d2.getDay())
return true;
// If none of the above cases satisfy, return false
return false;
}
static void insertionSort(Person arr[])
{
int n = arr.length;
for (int i=1; i<n; ++i)
{
Person key = arr[i];
int j = i-1;
/* Move elements of arr[0..i-1], that are
greater than key, to one position ahead
of their current position */
while (j>=0 && compare(arr[j],key))
{
arr[j+1] = arr[j];
j = j-1;
}
arr[j+1] = key;
}
}
static void selectionSort(Person arr[])
{
int n = arr.length;
// One by one move boundary of unsorted subarray
for (int i = 0; i < n-1; i++)
{
// Find the minimum element in unsorted array
int min_idx = i;
for (int j = i+1; j < n; j++)
if (arr[j].getName().compareTo(arr[min_idx].getName())<0)
min_idx = j;
// Swap the found minimum element with the first
// element
Person temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
}
}
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
List<Person> fstLst =Populate();
System.out.println("**********Polpulating from files person.txt*************");
for (Person person : fstLst) {
System.out.println("Name-"+person.getName()+" B-Day:"+person.getMonth()
+"/"+person.getDay()+"/"+person.getYear());
}
List<Person> secLst=new ArrayList<>();
for (Person person : fstLst) {
secLst.add(person);
}
int size=fstLst.size();
Person[] persons= new Person[size];
int i=0;
for (Person person : fstLst) {
persons[i]=person;
i++;
}
insertionSort(persons);
System.out.println("**********After insertion sort by B-DAY*************");
for (int j = 0; j < persons.length; j++) {
System.out.println("Name-"+persons[j].getName()+" B-Day:"+persons[j].getMonth()
+"/"+persons[j].getDay()+"/"+persons[j].getYear());
}
System.out.println("**********start Selection sort by Name for second list*************");
int size2=secLst.size();
Person[] persons2= new Person[size2];
int i2=0;
for (Person person : secLst) {
persons2[i2]=person;
i2++;
}
for (int j = 0; j < persons2.length; j++) {
System.out.println("Name-"+persons2[j].getName()+" B-Day:"+persons2[j].getMonth()
+"/"+persons2[j].getDay()+"/"+persons2[j].getYear());
}
System.out.println("**********After selection sort by Name*************");
selectionSort(persons2);
for (int j = 0; j < persons2.length; j++) {
System.out.println("Name-"+persons2[j].getName()+" B-Day:"+persons2[j].getMonth()
+"/"+persons2[j].getDay()+"/"+persons2[j].getYear());
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.