Question: Please write the class for the interface below. This is generics. public class Bowler implements Comparable { // instance variables - replace the example below
Please write the class for the interface below. This is generics.
public class Bowler
{
// instance variables - replace the example below with your own
private String name;
private int score;
public Bowler( )
{
name = "unknown";
score = 0;
}
/**
* @param String - to hold the Bowlers name
* @param int- to hold the Bowler's score
* @return
*/
public Bowler(String inName, int inScore)
{
name = inName;
score = inScore;
}
public String toString()
{
return "Name: " + name + "\tScore: " + score + " ";
}
//notice the method heading must me this way to
//override the method in the interface, Comparable
public int compareTo(T inB)
{
Bowler cBowler = (Bowler)(inB);
if(score > cBowler.score)
return 1;
else
if(score == cBowler.score)
return 0;
else
return -1;
}
//notice the method heading must take Object as a a paramenter
//to override the equals( ) mthod from the Object class
public boolean equals(Object ob)
{
Bowler pBowler = (Bowler)ob;
if(name.equals(pBowler.name) && score == pBowler.score)
return true;
return false;
}
}
And here is the driver to test:
/**
* This is a test driver for your ArrayFunctions class
*
* @author
* @version 4/15/2014
*/
public class DriverGeneric
{
public static void main(String [ ] args)
{
Bowler [ ] testArray = new Bowler[5];
testArray[0] = new Bowler("Robert", 234);
testArray[1] = new Bowler("Wade",185);
testArray[2] = new Bowler("Anne", 225);
testArray[3] = new Bowler("Ellen", 158);
testArray[4] = new Bowler("Joy", 125);
Bowler [ ] results;
ArrayFunctionsGeneric
ArrayFunctionsGeneric
System.out.println("This is the original array: ");
af.printMe(testArray);
System.out.println(" This is a test of the sortMe( ) method and
the printMe( ) methods. ");
results = (Bowler[ ])af.sortMe(testArray);
af.printMe(results);
System.out.println(" This is a test of getMax( )");
System.out.println("The maximum value in the array is: "
+af.getMax(testArray));
System.out.println(" This is a test of getMin( )");
System.out.println("The minimum value in the array is: " +
af.getMin(testArray));
System.out.println(" This is a test of reverseMe( )");
af.printMe(af.reverseMe(testArray));
System.out.println(" This is a test of whereAmI( )");
System.out.println("The value Wade is at subscript " +
af.whereAmI(testArray, new Bowler("Wade",185)));
System.out.println(" This is a test of doubleMyCapactiy( )");
af.printMe(af.doubleMyCapacity(testArray));
System.out.println(" End of tests");
} //end of main( )
}//end of Driver class
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
