Chapter 13 - Using Interfaces
Programming Assignment
1) 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!! */
}
public String to String()
{ /* Code */
}
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<other.word_length
}
Next, use the code contained in the Powerpoint for chapter 13 for GeneralizedSelectionSort and include the following code:
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.