Question
Create 3 different classes that implement Comparable . That is, each class must contain a compareTo method. The classes are: a) public class StudentRecord implements
Create 3 different classes that implement Comparable . That is, each class must contain a compareTo method. The classes are: a) public class StudentRecord implements Comparable { private String ID; private String firstName; private String lastName; public StudentRecord(String I, String F, String L) { /* code!! */
ID=I; firstName=F; lastName=L;
} public String to String() { /* Code */
return "ID: "+ID + ", First name: " +firstName+ ", Last name: " +lastName; } public int compareTo(Object other) { /* this student is less than other student if this.ID is less than other.ID /*Code*/ } } b) The following class is the same class you used in your word count program (with additions) public class WordCounter implements { private String word; private int count; /* you need only implement a constructor, a toString method and a compareTo method compareTo should compare this.word to other.word */ } c) public class WordLength { private String word; private int word_length; // this variable stores the length of the word. It can be set in the constructor /* implement a constructor, a toString method and a compareTo method compareTo should compare the lengths of the word, that is this is less than other if this.word_length
public class GeneralizedSelectionSort {
public static void sort(Comparable[] a, int numberUsed) {
int index, indexOfNextSmallest;
for (index = 0; index < numberUsed - 1; index++) { // Place the correct value in a[index]:
indexOfNextSmallest = indexOfSmallest(index, a, numberUsed);
interchange(index, indexOfNextSmallest, a);
// a[0], a[1],..., a[index] are correctly ordered and
// these are
// the smallest of the original array elements. The remaining
// positions contain the rest of the original array elements.
}
}
/**
* Returns the index of the smallest value among a[startIndex], a[startIndex+1],
* ... a[numberUsed - 1]
*/
private static int indexOfSmallest(int startIndex, Comparable[] a, int numberUsed) {
Comparable min = a[startIndex];
int indexOfMin = startIndex;
int index;
for (index = startIndex + 1; index < numberUsed; index++)
if (a[index].compareTo(min) < 0) // if a[index] is less than min
{
min = a[index];
indexOfMin = index;
// min is smallest of a[startIndex] through a[index]
}
return indexOfMin;
}
private static void interchange(int i, int j, Comparable[] a) {
Comparable temp;
temp = a[i];
a[i] = a[j];
a[j] = temp; // original value of a[i]
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started