Question

In: Computer Science

TrackMinMax i want the generic version based on java For this lab, you will create a...

TrackMinMax
i want the generic version based on java

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.

Solutions

Expert Solution

TrackMinMax.java (code to copy)

//declare generic classs whose elements are of comparable type
class TrackMinMax<T extends Comparable<T>>{
    //declare member variables to store current min and max
    private T cur_min;
    private T cur_max;
    //this variable tells if the check function has been called once or not
    private static boolean is_checked = false;
    //constructor
    public TrackMinMax(){}
    //this method updates cur_min and cur_max
    public void check(T i){
        //if the method has never been called, directly update min and max values
        if(!is_checked){
            is_checked=true;
            cur_min=i;
            cur_max=i;
        }else{
            //update cur_min
            if(cur_min.compareTo(i)>0){
                cur_min=i;
            }
            //update cur_max
            if(cur_max.compareTo(i)<0){
                cur_max=i;
            }
        }
    }
    //member variable to get cur_min
    public T getMin(){
        return cur_min;
    }
    //member variable to get cur_max
    public T getMax(){
        return cur_max;
    }
    //member variable to print the object
    public String toString(){
        return "["+cur_min+","+cur_max+"]";
    }
}

TrackMinMax.java (code screenshot)

Test.java (code to copy)

import java.util.*;
import java.text.*; 
class Test{
    public static void main(String[] args) throws Exception{
        //create Scanner object to read from the user
        Scanner sc = new Scanner(System.in);
        //read a line
        String input = sc.nextLine();
        //store this line in the scanner so that we can read numbers/string separated by spaces
        sc = new Scanner(input);
        //check if command line argument was provided and the value was String
        if(args.length==1 && args[0].equals("String")){
            //we have to implement string version of the program
            TrackMinMax<String> tmm = new TrackMinMax<String>();
            //variable to store each word
            String val;
            //read until line ends
            while(sc.hasNext()){
                //read next words
                val=sc.next();
                //call check method
                tmm.check(val);
            }
            //print the result in the format given in the problem statement
            System.out.println("Minimum: "+tmm.getMin());
            System.out.println("Maximum: "+tmm.getMax());
            System.out.println(tmm);
        }else{
            //we have to implement integer version of the program
            TrackMinMax<Integer> tmm = new TrackMinMax<Integer>();
            //variable to store each integer
            Integer val;
            //read until line ends
            while(sc.hasNextInt()){
                //read next integer
                val=sc.nextInt();
                //call check method
                tmm.check(val);
            }
            //print the result in the format given in the problem statement
            System.out.println("Minimum: "+tmm.getMin());
            System.out.println("Maximum: "+tmm.getMax());
            System.out.println(tmm);
        }
    }   
}

Test.java (code screenshot)

Sample Input/Output Screenshot 1 when called like - java Test

Sample Input/Output Screenshot 2 when called like - java Test String

Let me know in the comments if you have any doubts.
Do leave a thumbs up if this was helpful.


Related Solutions

(JAVA) For your homework, I want you to create the order of your mini-programs based on...
(JAVA) For your homework, I want you to create the order of your mini-programs based on how it is listed in this assignment description (i.e., Program 1 should be the first program implemented, Program 2 should be the second program, etc.). All your mini-programs are housed inside one main method. The name of your class for this homework should be called Homework3. You will be submitting that single Java file to this submission box. There are a total of two...
JAVA Generic versions of allOf() and anyOf() In this lab, you will write generic versions of...
JAVA Generic versions of allOf() and anyOf() In this lab, you will write generic versions of the allOf() and anyOf() methods you wrote in an earlier lab. Then you will take those generic versions and create versions that work with a predicate rather than direct comparison of values. Part 1 - Create generic versions of allOf() and anyOf() Recall that the integer-only versions of allOf() and anyOf() looked like this: // Returns true if any element of 'a' is equal...
I WANT TO IMPLEMENT THIS IN JAVA PLEASE I want to create a small user input...
I WANT TO IMPLEMENT THIS IN JAVA PLEASE I want to create a small user input system for a university student, where they put the season and year of when they started their uni course. For example the system will ask "What year did you start your degree?", the user will input "Autumn/2022" as a string. Now from a string format as shown, it should take that user input and calculate for example +2 or +3 years to the date....
I WANT TO IMPLEMENT THIS IN JAVA PLEASE I want to create a small user input...
I WANT TO IMPLEMENT THIS IN JAVA PLEASE I want to create a small user input system for a university student, where they put the season and year of when they started their uni course. For example the system will ask "What year did you start your degree?", the user will input "Autumn/2022" as a string. Now from a string format as shown, it should take that user input and calculate for example +2 or +3 years to the date....
you will create a program with Java to implement a simplified version of RSA cryptosystems. To...
you will create a program with Java to implement a simplified version of RSA cryptosystems. To complete this project, you may follow the steps listed below (demonstrated in Java code) to guide yourself through the difficulties. Step I Key-gen: distinguish a prime number (20 pts) The generation of RSA's public/private keys depends on finding two large prime numbers, thus our program should be able to tell if a given number is a prime number or not. For simplicity, we define...
Introduction: In this project you will create a generic linked list using Java Generics. Description: Create...
Introduction: In this project you will create a generic linked list using Java Generics. Description: Create a generic class called GenLinkedList. GenLinkedList will use nodes that store a value of the generic type to store its contents. It should have the following methods. The methods should all operate on the object making the call (none are static). Perform checking of the parameters and throw exceptions where appropriate. The linked list should be singly-linked. It should not use sentinel nodes (empty...
IN JAVA, For this exercise, you will create your own example of using a generic method....
IN JAVA, For this exercise, you will create your own example of using a generic method. First write a program that calls a method to multiply two integers and return the integer result. Then modify that program to make the method generic, calling it with two generic data types (T1 and T2) and then returning the result in the same type (also needs to be generic). Demonstrate your program by calling the method two times, once with an integer and...
Lab Assignment Write a Java program that implements a queue in a hospital. I want your...
Lab Assignment Write a Java program that implements a queue in a hospital. I want your program to ask the user to enter the number of patients then enter the patient number starting from 110 till the end of the queue then print number of patients waiting in the queue. Suppose you have a queue D containing the numbers (1,2,3,4,5,6,7,8), in this order. Suppose further that you have an initially empty Stack S. Give a code fragment that uses S,...
I want you guys to rewrite this lab procedure, and it is fine if it was...
I want you guys to rewrite this lab procedure, and it is fine if it was shorter than this. An air track was fitted with two photocell bridges, one on each side of the collision region. Each bridge was operating in a gate mode timer that allows the collection of time intervals before and after collision. To ensure that friction and gravity have minimal effects, the track was leveled. Before starting the experiment, we ensured that loss in velocity was...
In this lab, I want you to take in a numeric grade as an integer and...
In this lab, I want you to take in a numeric grade as an integer and then print a letter grade per the table below: 90 or above is an A 80 or above is a B 70 or above is a C 60 or above is a D Below a 60 is an F You must use a branching statement that prints the appropriate letter grade based on the user input2. Please comment your code
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT