Question

TrackMinMax For this lab, you will create a generic version of the IntTrackMinMax class you wrote...

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();
   }
}

Homework Answers

Answer #1

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

Know the answer?
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
Similar Questions
Java Program: You will be inserting values into a generic tree, then printing the values inorder,...
Java Program: You will be inserting values into a generic tree, then printing the values inorder, as well as printing the minimum and maximum values in the tree. Given main(), write the methods in the 'BSTree' class specified by the // TODO: sections. There are 5 TODOs in all to complete. Ex: If the input is like ferment bought tasty can making apples super improving juice wine -1 the output should be: Enter the words on separate lines to insert...
Fix the program: what if the input is three? import java.util.Scanner public class Test { public...
Fix the program: what if the input is three? import java.util.Scanner public class Test { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter an integer: "); int m = in.nextInt(); System.out.print("Enter another integer: "); int n = in.nextInt(); System.out.println(m + " " + n); } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new...
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); // Read the input string String input = sc.nextLine(); BalancedParentheses bp = new BalancedParentheses(); // Print whether the string has balanced parentheses System.out.println(bp.hasBalancedParentheses(input)); } } class BalancedParentheses { public boolean hasBalancedParentheses(String input) { // Remove this and implement code throw new UnsupportedOperationException(); } }
import java.util.Scanner; public class InRange { private int min; private int max; public InRange(int initialMin, int...
import java.util.Scanner; public class InRange { private int min; private int max; public InRange(int initialMin, int initialMax) { min = initialMin; max = initialMax; } // You need to write two instance methods: // 1.) A method named inRange, which takes an int. // This returns true if the int is within the given range // (inclusive), else false. // // 2.) A method named outOfRange which takes an int. // This returns false if the int is within the...
IN JAVA Methods*: Calorie estimator Write a method ActivityCalories that takes a string indicating an activity...
IN JAVA Methods*: Calorie estimator Write a method ActivityCalories that takes a string indicating an activity (sit, walk, jog, bike, swim) and duration in minutes (integer), and returns the estimated calories expended (double). Calories per minute for a 150 lb person (source): sit: 1.4 walk: 5.4 run: 13.0 bike: 6.8 swim: 8.7 If the input is sit 2, the output is 2.8 Hints: Use an if-else statement to determine the calories per minute for the given activity. Return the calories...
7.6 LAB: Exception handling to detect input String vs. Integer The given program reads a list...
7.6 LAB: Exception handling to detect input String vs. Integer The given program reads a list of single-word first names and ages (ending with -1), and outputs that list with the age incremented. The program fails and throws an exception if the second input on a line is a String rather than an Integer. At FIXME in the code, add a try/catch statement to catch java.util.InputMismatchException, and output 0 for the age. Ex: If the input is: Lee 18 Lua...
I need to come up with the pseudocode for a min and max value finder for...
I need to come up with the pseudocode for a min and max value finder for an array, uses recursion. first I started writing a java program to get a better understanding but it seams not to work properly. also Im having trouble calculating the time efficiency for the code. public class Max_min {     /**      * @param args the command line arguments      */     static int Max;     static int Min;     static int index;     static...
Question: Squares. Write a program class named SquareDisplay that asks the user for a positive integer...
Question: Squares. Write a program class named SquareDisplay that asks the user for a positive integer no greater than 15. The program should then display a square on the screen using the character ‘X’. The number entered by the user will be the length of each side of the square. For example, if the user enters 5, the program should display the following:       XXXXX       XXXXX       XXXXX       XXXXX       XXXXX INPUT and PROMPTS. The program prompts for an integer as follows: "Enter...
Provide A UML for the Following CODE public class Employee{ public String strName, strSalary; public Employee(){...
Provide A UML for the Following CODE public class Employee{ public String strName, strSalary; public Employee(){    strName = " ";    strSalary = "$0";    } public Employee(String Name, String Salary){    strName = Name;    strSalary = Salary;    } public void setName(String Name){    strName = Name;    } public void setSalary(String Salary){    strSalary = Salary;    } public String getName(){    return strName;    } public String getSalary(){    return strSalary;    } public String...
I need to get the Min and Max value of an array when a user inputs...
I need to get the Min and Max value of an array when a user inputs values into the array. problem is, my Max value is right but my min value is ALWAYS 0. how do i fix this? any help please!!! _________________________________________________________________________________________________ import java.util.Scanner; public class ArrayMenu{ static int count; static Scanner kb = new Scanner(System.in);             public static void main(){ int item=0; int[] numArray=new int[100]; count=0;       while (item !=8){ menu(); item = kb.nextInt();...