Java Problem Implement selection sort to be used to sort customer records using
ID: 3759781 • Letter: J
Question
Java Problem
Implement selection sort to be used to sort customer records using selected customer fields as the sort key.
1. Notice that key is static and of type KeyField. This type should be an enum with three members: ID, FIRST and LAST. These represent the three Customer fields that can be used as a key when sorting the customers.
Customer
-id: int
-first:String
-last:String
-key: KeyField
2.This class holds an array of customers. Make the array of size 100. The sorted() method returns an array that is sorted by the current key, and leaves the original array unchanged. The sort() method updates the customers arrays. Use selection sort.
Customers
-customers: Customer[ ]
-length: int
+add(o: Customer): void +sorted(): Customer[ ] +sort(): void
3.The driver class will create the object clients, to represent an instance of the Customers class. Use this object to sort and list the contents of the clients object as shown in the screen capture. Notice that the key is changed to reflect the desired sorting order.
Original client list:
333 : Adam : Cabbage : key = ID
111 : Carie : Banana : key = ID
222 : Berry : Apple : key = ID
A sorted copy of the original:
111 : Carie : Banana : key = ID
222 : Berry : Apple : key = ID
333 : Adam : Cabbage : key = ID
Original client list:
333 : Adam : Cabbage : key = ID
111 : Carie : Banana : key = ID
222 : Berry : Apple : key = ID
Sorted by ID:
111 : Carie : Banana : key = ID
222 : Berry : Apple : key = ID
333 : Adam : Cabbage : key = ID
Sorted by First:
333 : Adam : Cabbage : key = FIRST
222 : Berry : Apple : key = FIRST
111 : Carie : Banana : key = FIRST
Sorted by Last:
222 : Berry : Apple : key = LAST
111 : Carie : Banana : key = LAST
333 : Adam : Cabbage : key =
LAST BUILD SUCCESSFUL (total time: 0 seconds
Customer
-id: int
-first:String
-last:String
-key: KeyField
Explanation / Answer
package current;
public class CustomerSelectionSort {
public static void main(String args[]) {
Customer c1 = new Customer();
c1.setId(333);
c1.setFirst("Adam");
c1.setLast("Cabbage");
c1.setKey(KeyField.ID);
Customer c2 = new Customer();
c2.setId(111);
c2.setFirst("Carie");
c2.setLast("Banana");
c2.setKey(KeyField.ID);
Customer c3 = new Customer();
c3.setId(222 );
c3.setFirst("Berry");
c3.setLast("Apple");
c3.setKey(KeyField.ID);
Customers custs = new Customers();
custs.add(c1);
custs.add(c2);
custs.add(c3);
System.out.println("Original client list:");
for(int i=0; i < custs.getLength(); i++)
System.out.println((custs.getCustomers()[i]).toString());
Customer[] sorted = custs.sorted();
System.out.println("A sorted copy of the original:");
for(int i=0; i < custs.getLength(); i++)
System.out.println((sorted[i]).toString());
}
}
class Customers {
private Customer customers[];
private int length;
public Customers() {
customers = new Customer[100];
length = 0;
}
public void add(Customer c) {
if(length < 100)
customers[length++] = c;
else
System.out.println("Array is full");
}
public Customer[] sorted() {
Customer[] sorted = new Customer[customers.length];
for(int i = 0; i < customers.length; i++) {
sorted[i] = customers[i];
}
sorted = selectionSort(customers, sorted[0].getKey());
return sorted;
}
public void sort() {
customers = this.sorted();
}
private void swap(Customer[] a, int i, int j) {
Customer temp = a[i];
a[i] = a[j];
a[j] = temp;
}
public Customer[] selectionSort(Customer[] list, KeyField key) {
for (int i = 0; i < length - 1; i++) {
// Find the index of the minimum value
int minPos = i;
for (int j = i + 1; j < length; j++) {
if(key == KeyField.ID) {
if (list[j].getId() < list[minPos].getId()) {
minPos = j;
}
} else if(key == KeyField.FIRST) {
if(list[j].getFirst().compareTo(list[minPos].getFirst()) < 0) {
minPos = j;
}
} else if(key == KeyField.LAST) {
if(list[j].getLast().compareTo(list[minPos].getLast()) < 0) {
minPos = j;
}
}
}
swap(list, minPos, i);
}
return list;
}
public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
public Customer[] getCustomers() {
return customers;
}
}
enum KeyField{
ID, FIRST, LAST;
};
class Customer {
private int id;
private String first;
private String last;
private KeyField key;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirst() {
return first;
}
public void setFirst(String first) {
this.first = first;
}
public String getLast() {
return last;
}
public void setLast(String last) {
this.last = last;
}
public KeyField getKey() {
return key;
}
public void setKey(KeyField key) {
this.key = key;
}
public String toString() {
return id+" : "+first+" : "+last+" : key = "+key;
}
}
----output------------
Original client list:
333 : Adam : Cabbage : key = ID
111 : Carie : Banana : key = ID
222 : Berry : Apple : key = ID
A sorted copy of the original:
111 : Carie : Banana : key = ID
222 : Berry : Apple : key = ID
333 : Adam : Cabbage : key = ID
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.