TrackMinMax
For this lab, you will create a generic version of the IntTrackMinMax class you wrote in a previous lab, 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<T> 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. If you want to test with strings in develop mode, you will need to provide the word "String" (without the quotes) as a command line parameter.
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.
TestTrackMinMax.java
import java.util.Scanner;
public class TestTrackMinMax {
private static void testString() {
Scanner scnr = new
Scanner(System.in);
TrackMinMax<String> tmms = new
TrackMinMax<String>();
while (scnr.hasNext()) {
tmms.check(scnr.next());
}
System.out.println("Minimum: " +
tmms.getMin());
System.out.println("Maximum: " +
tmms.getMax());
System.out.println(tmms);
}
private static void testInteger() {
Scanner scnr = new Scanner(System.in);
TrackMinMax<Integer> tmmi = new
TrackMinMax<Integer>();
while (scnr.hasNextInt()) {
tmmi.check(scnr.nextInt());
}
System.out.println("Minimum: " +
tmmi.getMin());
System.out.println("Maximum: " +
tmmi.getMax());
System.out.println(tmmi);
}
public static void main(String[] args) {
if ((args.length == 0)
|| (args[0].equals("Integer")))
testInteger();
else if
(args[0].equals("String"))
testString();
}
}
public class maxmin{
int min,max;
int count;
maxmin()
{
count=0;
}
void check(int i)
{
if(count==0)
{
min = max = i;
count=1;
}
else
{
if(i<min)min=i;
if(max<i)max=i;
}
}
int getMin(){
return min;
}
int getMax()
{
return max;
}
public String toString()
{
String s = "["+min+","+max+"]" ;
return s;
}
public static void main(String []argv)
{
maxmin t = new maxmin();
t.check(0);
t.check(5);
t.check(-5);
t.check(3);
t.check(-8);
System.out.println("min :"+t.getMin());
System.out.println("max :"+t.getMax());
System.out.println(t.toString());
}
}
output:
run:
min :-8
max :5
Get Answers For Free
Most questions answered within 1 hours.