Question: For this lab, you will create a generic version of the IntTrackMinMax class you wrote in lab 1.12, called TrackMinMax. The API is: Function Signature

For this lab, you will create a generic version of the IntTrackMinMax class you wrote in lab 1.12, called TrackMinMax. The API is:

Function Signature Description
constructor TrackMinMax() constructor
check void check(T i) compares i to the current minimum and maximum values and updates them accordingly
getMin T getMin() returns the minimum value provided to check() so far
getMax T getMax() returns the maximum value provided to check() so far
toString String toString() returns the string "[min,max]"

As before, your getMax() and getMin() functions may assume that check() has been called at least once. If getMin() or getMax() is called before the first call to check(), the results are undefined.

Your code will need to use the compareTo() function, so you need to be sure to limit your type parameter to only types that implement the Comparable interface.

Hints

You can look at my test code by clicking "Current File" above the editing window, and selecting "TestTrackMinMax". All the test program does is read integers from standard input, and calls check() with each one. It then prints out the minimum and maximum integer that was read, and then prints the object itself (which tests toString()). For example, enter this data as the input:

0 5 -5 3 -8 

When you run it with that data, it should print

Minimum: -8 Maximum: 5 [-8,5] 

For grading, tour code will be tested with both Integer and String as the type parameters, but due to a limitation of zyBooks, you can only test with integers in develop mode.

Note that because you don't know what the min/max values might be for the generic type parameter, you'll need to find some other way to initialize the min/max variables. Think about the fact that the type parameter must be a reference type, and that reference types are automatically initialized to a certain default value. You can check for that default value in your check() function to determine whether it is the first call to check(), and act accordingly.

--------------------------------------------------------------------------------------

What I wrote on the 1.12 assignment

public class IntTrackMinMax{ private int min; private int max; private int checkpoint; public IntTrackMinMax() { checkpoint =0; } public void check(int i){ if(checkpoint == 0) { min = max = i; checkpoint = 1; }else { if( i < min) { min = i; }else if(i>max) { max = i; } } } public int getMin() { return min; } public int getMax() { return max; } public String toString() { return String.format("[%d,%d]", min,max); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!