In Java:
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>>).
TOP RESULT TASK INFO:
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:
Test cases:
import org.junit.*;
import static org.junit.Assert.*;
import java.util.*;
import java.io.*;
public class E10tester {
public static void main(String args[]){
org.junit.runner.JUnitCore.main("E10tester");
}
@Test public void topresult_exists() {
TopResult<Integer> tr_i = new TopResult<Integer>();
TopResult<String> tr_s = new TopResult<String>();
TopResult<Character> tr_c = new TopResult<Character>();
TopResult<Boolean> tr_b = new TopResult<Boolean>();
TopResult<Double> tr_d = new TopResult<Double>();
}
@Test public void topresult_iscomparable() {
TopResult<Integer> tr = new TopResult<Integer>();
Comparable<Integer> c = tr.getTopResult();
}
private Integer[] int_input = { 5, 3, 6, 2, 1, 6, 8, 9, 6, 7, 10 };
private Integer[] int_top = { 5, 5, 6, 6, 6, 6, 8, 9, 9, 9, 10 };
@Test public void topresult_test_int() {
TopResult<Integer> tr = new TopResult<Integer>();
assertTrue("top result initially should be null", tr.getTopResult() == null);
assertTrue("top result toString should initially be null", tr.toString() == null);
for (int i = 0; i < int_input.length; i++) {
tr.newResult(int_input[i]);
Integer r_act = tr.getTopResult();
Integer r_exp = int_top[i];
String errMsg = String.format("wrong top result after entry %d of %s", i, Arrays.toString(int_input));
assertEquals(errMsg, r_exp, r_act);
String s_act = tr.toString();
String s_exp = int_top[i].toString();
errMsg = String.format("wrong toString after entry %d of %s", i, Arrays.toString(int_input));
assertEquals(errMsg, s_exp, s_act);
}
}
private String[] str_input = { "a", "c", "b", "d", "c", "e", "m", "h", "p" };
private String[] str_top = { "a", "c", "c", "d", "d", "e", "m", "m", "p" };
@Test public void topresult_test_str() {
TopResult<String> tr = new TopResult<String>();
assertTrue("top result initially should be null", tr.getTopResult() == null);
assertTrue("top result toString should initially be null", tr.toString() == null);
for (int i = 0; i < str_input.length; i++) {
tr.newResult(str_input[i]);
String r_act = tr.getTopResult();
String r_exp = str_top[i];
String errMsg = String.format("wrong top result after entry %d of %s", i, Arrays.toString(str_input));
assertEquals(errMsg, r_exp, r_act);
String s_act = tr.toString();
String s_exp = str_top[i].toString();
errMsg = String.format("wrong toString after entry %d of %s", i, Arrays.toString(str_input));
assertEquals(errMsg, s_exp, s_act);
}
}
}Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
// TopResult.java
public class TopResult<T extends Comparable<T>> {
// instance variable holding current top result
private T top;
// default constructor initializing top to null
public TopResult() {
top = null;
}
// getter for top result
public T getTopResult() {
return top;
}
// adds a new result to see if it is better than top, if yes, top will be
// updated
public void newResult(T result) {
if (top == null) {
// first result.
top = result;
} else if (top.compareTo(result) < 0) {
// result > top, so updating top with the value of result
top = result;
}
}
@Override
public String toString() {
// returns null if top is null, or else returns the toString value of
// top
return top == null ? null : top.toString();
}
}
//OUTPUT of JUnit test

In Java: Write the class TopResult, which keeps track of the best (highest numbered or highest...