This program creates two classes that implement the Comparable interface. They w
ID: 668142 • Letter: T
Question
This program creates two classes that implement the Comparable interface. They will be tested in a third class that creates a group of objects from data in a file. These objects will sorted and the sorted objects are displayed. There is example code in the slides for virtually all of this program.
Create a project using Eclipse called Lab03. Create a tester class called PersonSort and two classes that implement the Comparable interface –one called the Person class and the other called the MyDate class.
The MyDate class has three private instance variables: int month (1-12), int day (1-31) and int year. It implements the Comparable interface (see slides for the method to provide for that interface) and 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. DO NOT create any other instance variables or methods.
The Person class has two private instance variables: String name and MyDate birthday. It implements the Comparable interface (see slides for the method to provide for that interface) and 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). DO NOT create any other instance variables or methods.
The PersonSort class has three static methods: main, populate and sort. The main method creates an array list (ArrayList<Person>) of Person objects and populates them from a file that is supplied to you, sorts them using one of three algorithms and displays the sorted list. 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. The sort method sorts the array list using either selection sort, insertion sort or bubble sort. You must implement the sort algorithm – you cannot use the sort method of the Collections framework. The algorithm for these sorts can be found either in the slides, your book or on the Internet.
The Person class 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 . 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
Explanation / Answer
// personsort
package personsort;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
public class PersonSort {
static final String PERSON_FILE = "Persons.txt"; // give inputs
//The format of the file is: name month day year
public static void main(String[] args) {
// TODO code application logic here
try{
BufferedReader reader = new BufferedReader(new FileReader(PERSON_FILE));
ArrayList<String> results = new ArrayList<>();
String line = reader.readLine(); // reading lines from file
while (line != null) {
results.add(line);
line = reader.readLine();
}
Person p[]=new Person[results.size()];
p=populate(results,p);
sort(p);
// results displaying in sorted order
for(int i=0;i<p.length;i++)
p[i].toString();
}
catch(IOException | NumberFormatException e)
{
}
}
public static Person[] populate(ArrayList <String> results, Person[]p) {
// TODO code application logic here
p=new Person[results.size()];
int k=0;
for (String result : results) { // spliting data and assigning to person object
String[] tokens = result.split(" ");
p[k].name=tokens[0];
p[k].birthday=new MyDate(Integer.parseInt(tokens[1]),
Integer.parseInt(tokens[2]),Integer.parseInt(tokens[3]));
k++;
}
return p;
}
public static void sort(Person[] p) {
for (int c = 0; c < ( p.length - 1 ); c++) {
for (int d = 0; d < p.length - c - 1; d++) {
if (strcmp(p[c].name,p[c+1].name)>0)
{
String t = p[c].name;
p[c].name = p[c+1].name;
p[c+1].name = t;
}
}
}
}
private static int strcmp(String name, String name0) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
//MyDate
package personsort;
public class MyDate implements Comparable{
int month;
int day;
int year;
MyDate(int m, int d, int y)
{
this.month=m;
this.day=d;
this.year=y;
}
@Override
public String toString() {
return String.format(month+"/"+day+"/"+year);
}
}
//Person.java
package personsort;
public class Person implements Comparable{
String name;
MyDate birthday;
Person(String nam,int m, int d, int y)
{
this.name=nam;
birthday=new MyDate(m,d,y);
}
@Override
public String toString() {
return String.format(name+" "+birthday.toString());
}
}
// Comparable.java
package personsort;
public abstract interface Comparable {
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.