Question

Please use Java only. Write the class TopResult, which keeps track of the best (highest numbered ...

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:

  • TopResult, a default constructor. Initially there should be no top result since no results have been seen yet.
  • newResult, which takes an object of type T, representing a new result, as a parameter, but does not return anything. This method will check whether the new result is better than the current top result, and if so, replace it, otherwise it will ignore it. If there is currently no top result (because no results had been seen yet), then the current result automatically becomes the top result.
  • getTopResult, which does not take any parameters but returns the current top result. If there is currently no top result (because no results have been seen yet), then this method should return null.
  • toString, which will return the toString value of the current top result, or a null if there have been no results yet.

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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// 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:

n777922

Add a comment
Know the answer?
Add Answer to:
Please use Java only. Write the class TopResult, which keeps track of the best (highest numbered ...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please use Java only. Write the class TopResult, which keeps track of the best (highest numbered ...

    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...

  • Need Help! Please Solve using JAVA! TopResult Task: There are a number of situations in which we ...

    Need Help! Please Solve using 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...

  • Please Do It With Java. Make it copy paste friendly so i can run and test...

    Please Do It With Java. Make it copy paste friendly so i can run and test it. Thnak You. Write programs for challenges 1 and 2 and 6 at the end of the chapter on Generics. 1) Write a generic class named MyList, with a type parameter T. The type parameter T should be constrained to an upper bound: the Number class. The class should have as a field an ArrayList of T. Write a public method named add, which...

  • Java programming: please help with the following assignment: Thank you. Create a class called GiftExchange that...

    Java programming: please help with the following assignment: Thank you. Create a class called GiftExchange that simulates drawing a gift at random out of a box. The class is a generic class with a parameter of type T that represents a gift and where T can be a type of any class. The class must include the following An ArrayList instance variable that holds all the gifts,The ArrayList is referred to as theBox. A default constructors that creates the box....

  • Please USE JAVA only Write a class named InputSplitter. This class will take input from the...

    Please USE JAVA only Write a class named InputSplitter. This class will take input from the keyboard (using a Scanner) and split the incoming tokens into integers, floating point numbers, and strings. The incoming tokens will be added to one of three accumulators which start out at zero, and which keep a running total. Thus, the class will have an accumulator for integers. It starts out with the value zero. Every time another integer is read in, it is added...

  • Solve it by JAVA please. Now, write a class named InputSplitter. This class will take input...

    Solve it by JAVA please. Now, write a class named InputSplitter. This class will take input from the keyboard (using a Scanner) and split the incoming tokens into integers, floating point numbers, and strings. The incoming tokens will be added to one of three accumulators which start out at zero, and which keep a running total. Thus, the class will have an accumulator for integers. It starts out with the value zero. Every time another integer is read in, it...

  • Generics Objectives: OOWorking with a Generic Class 1. Create a class called Node that is Generic...

    java Generics Objectives: OOWorking with a Generic Class 1. Create a class called Node that is Generic. The class has one class attribute that is an element. It will need 2 Constructors, a setter and getter for the class attribute, and a toString method. 2. Write a Lab10Driver that will: Instantiate a Node Integer object with no value. I. Il. Ill. IV. a. Then call the set method to set the value to 5. Instantiate a Node String object with...

  • Please use Java only. Write a class, ZeroException, which is an Exception, and is used to signal...

    Please use Java only. Write a class, ZeroException, which is an Exception, and is used to signal that something is zero when it shouldn't be. -- Not needed Write the class ArrayManipulator which creates an array and provides several methods to manipulate values taken from an array. --needed ZeroException Task: -- Not needed Exceptions are used to signal many types of problems in a program. We can write our own as well to describe specific exceptional conditions which may arise....

  • This question is about java program. Please show the output and the detail code and comment.And...

    This question is about java program. Please show the output and the detail code and comment.And the output (the test mthod )must be all the true! Thank you! And the question is contain 7 parts: Question 1 Create a Shape class with the following UML specification: (All the UML "-" means private and "+" means public) +------------------------------------+ | Shape | +------------------------------------+ | - x: double | | - y: double | +------------------------------------+ | + Shape(double x, double y) | |...

  • Implement a new Generic class called a ShapeBox. This class will have two instance data, a...

    Implement a new Generic class called a ShapeBox. This class will have two instance data, a Shape object (as in the graphics Shape like Circle, Rectangle, Ellipse), and a double to store the Shape's area. The specific type of Shape will be specified via Generics so that your class will need a placeholder for the type of Shape. This placeholder must extend Shape. Additionally, ShapeBox will implement the Comparable interface requiring that you implement a compareTo method to compare this...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT