Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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(I included at the bottom) StuRecArray[ ] = { new StudentRecord("33221","Bill","Jones"), new StudentRecord("12121","Sue","Harris", new StudentRecord("22222","Mary","Edwards"); WordCounterArray[ ] = { new WordCounter("far"), new WordCounter("apple"), new WordCounter("home"), new WordCounter("barricuda") }; WordLenghtArray[ ]= { new WordLength("far"), new WordLength("apple"), new WordLength("home"), new WordLength("barricuda") }; Sort all three arrays using the same code!! Make sure to output each array.

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

JDBC Database Programming With J2ee

Authors: Art Taylor

1st Edition

0130453234, 978-0130453235

More Books

Students also viewed these Databases questions