In: Computer Science
Please use Java only.
Write the class TopResult, which keeps track of the best (highest numbered or highest ordered) result it has seen so far. The class will be a generic type, so the type of results it sees depends on how it is declared.
TopResult Task:
There are a number of situations in which we want to keep track of the highest value we've seen so far - a highest score, a most recent entry, a name which is closest to the end (or the front) of a list, etc. We can imagine writing a simple class which will do this: a class which will let us enter new results one by one, and at any point will let us stop and ask for the highest value.
So far, so good, right? But what if we have a number of different applications for this sort of class - for example, we want to find the top (integer) score on a test, or the highest (decimal) temperature reading, or the GUI window which is closest to the top, etc. Subclassing isn't quite the way to go here, because a TopResult which holds Integers isn't an instance of a TopResult which holds Doubles, and vice versa. But simply writing a class for every possible type we may need may seem like overkill, since the structure of the code is essentially the same from class to class.
In situations such as these, generics come to our rescue. When we define a generic class, the data type we use is essentially represented as a wildcard - a generic type parameter, which we can call T for instance. We write the class assuming that we have a type T in mind, with the idea that we will fill in the type once we know what it is. This is how an ArrayList is implemented, for example. It is a dynamic array of some generic type T, and the exact type is decided (specialized) when we declare the ArrayList variable, for example ArrayList<String> a. If we write our entire class in terms of this unkown type parameter, we will be able to simply name the type later when we want to use it.
In this exercise, we will write the class TopResult. It will have a generic type parameter (you can call it T or something else). Type T must be a Comparable type (thus, the full generic type parameter would be <T extends Comparable<T>>).
A Comparable is an interface which is implemented by classes such as Integer, Double, Character, and String, which allow two members of the same type to be compared to one another to see which is larger. The interface has one method, compareTo, which returns a negative, zero, or positive result indicating whether the object is less than, equal to, or greater than the object it is being compared against:
> Integer three = 3, four = 4;
> three.compareTo(four)
-1
> four.compareTo(three)
1
> four.compareTo(four)
0
The TopResult task should implement the following public methods:
The following shows an example use of this class:
> TopResult<Integer> tr = new
TopResult<Integer>();
> tr.getTopResult() // no results seen yet, should be null
null
> tr.newResult(7);
> tr.getTopResult()
7
> tr.newResult(3);
> tr.getTopResult()
7
> tr.newResult(4);
> tr.getTopResult()
7
> tr.newResult(9);
> tr.getTopResult()
9
> tr.newResult(20);
> tr.getTopResult()
20
> tr.toString() // this will print the toString() of the current
top result
"20"
Please make sure that it passes the following: https://repl.it/@micky123/SweetEmotionalExtraction
Please be sure that the work is your own, not a duplicate of somebody else's. Thank you.
// TopResult.java
public class TopResult<T extends Comparable<T>> {
private T result;
public TopResult()
{
result = null;
}
public void newResult(T new_result)
{
if(result == null || new_result.compareTo(result) > 0)
result = new_result;
}
public T getTopResult()
{
return result;
}
public String toString()
{
return result.toString();
}
public static void main(String[] args) {
TopResult<Integer> tr = new TopResult<Integer>();
System.out.println(tr.getTopResult()); // null
tr.newResult(7);
System.out.println(tr.getTopResult()); // 7
tr.newResult(3);
System.out.println(tr.getTopResult()); // 7
tr.newResult(4);
System.out.println(tr.getTopResult()); // 7
tr.newResult(9);
System.out.println(tr.getTopResult()); // 9
tr.newResult(20);
System.out.println(tr.getTopResult()); //20
System.out.println(tr.toString()); // 20
}
}
//end of TopResult.java
Output:
