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

In java, a data record is usually represented by a class object. Create an appli

ID: 3554075 • Letter: I

Question

In java, a data record is usually represented by a class object. Create an application by sorting the objects on the key value, id, last name, or item number, etc.

Include these classes:

- An user defined data type (you can use the classes you created from previous homework)

-Use either an Array or a Linked List as an underlying data structure. Of course you need to create class Array or class LinedList in order to implement the methods like mergeSort(), merge(), etc

-Driver program. In your driver program, it needs to include the following

-Create an Array object or a linked list object

-Insert objects by calling insert() method

-Display unsorted objects

-Call mergeSort() method. NOTE: mergeSort() method DOES NOT have parameters

-Display sorted objects


can be linked list or array or both

Explanation / Answer


import java.util.Arrays;

import java.util.Comparator;


public class Record implements Comparable<Record>{

int id;

String firstName;

String lastName;

int age;

public Record(int id, String firstName, String lastName, int age) {

super();

this.id = id;

this.firstName = firstName;

this.lastName = lastName;

this.age = age;

}

public Record() {

// TODO Auto-generated constructor stub

}

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

public String getFirstName() {

return firstName;

}

public void setFirstName(String firstName) {

this.firstName = firstName;

}

public String getLastName() {

return lastName;

}

public void setLastName(String lastName) {

this.lastName = lastName;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

@Override

public int compareTo(Record arg0) {

int tempId = ((Record)arg0).getId();

//for ascending order

return (this.id - tempId);

}

public static Comparator<Record> RecordLastNameComparator

= new Comparator<Record>() {


public int compare(Record record1, Record record2) {


String lastName1 = record1.getLastName().toUpperCase();

String lastName2 = record2.getLastName().toUpperCase();


//ascending order

return lastName1.compareTo(lastName2);


//descending order

//return fruitName2.compareTo(fruitName1);

}


};


public String toString(){

return "Id : "+this.id +", first name :"+this.firstName+", last name :"+this.lastName+", age :"+this.age;

}



public static void main(String[] args){

Record r1 = new Record(10, "steves", "spellburg",25);

Record r2 = new Record(4, "steve", "jobs",75);

Record r3 = new Record(6, "babage", "charles",35);

Record r4 = new Record(2, "iron", "man",55);

Record r5 = new Record(9, "super", "man",105);

Record arr[] = new Record[5];

arr[0]=r1;

arr[1]=r2;

arr[2]=r3;

arr[3]=r4;

arr[4]=r5;

Record arr1[] = arr;

System.out.println("Before sorting...");

for(int i=0;i<arr.length;i++){

System.out.println(arr[i].toString());

}

// To sort based on id

Arrays.sort(arr1);

System.out.println(" after sorting with ID...");

for(int i=0;i<arr1.length;i++){

System.out.println(arr1[i].toString());

}

//To sort based on lastName

Arrays.sort(arr,RecordLastNameComparator);

System.out.println(" after sorting with LastName...");

for(int i=0;i<arr.length;i++){

System.out.println(arr[i].toString());

}

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote