Question

In: Computer Science

USE JAVA Write a static generic method PairUtil.minmax that computes the minimum and maximum elements of...

USE JAVA

Write a static generic method PairUtil.minmax that computes the minimum and maximum elements of an array of type T and returns a pair containing the minimum and maximum value. Require that the array elements implement the Measurable interface of Chapter 10.

Solutions

Expert Solution

import javafx.util.Pair;

public class PairUtil {

    public static <T extends Measurable> Pair<T, T> minmax(T[] arr) {
        T min = arr[0], max = arr[0];
        for (int i = 0; i < arr.length; i++) {
            if (arr[i].getMeasure() > max.getMeasure()) {
                max = arr[i];
            }
            if (arr[i].getMeasure() < min.getMeasure()) {
                min = arr[i];
            }
        }
        return new Pair<>(min, max);
    }

    public static void main(String[] args) {
        Measurable[] arr = {
                new Measurable() {
                    @Override
                    public double getMeasure() {
                        return 10;
                    }
                },
                new Measurable() {
                    @Override
                    public double getMeasure() {
                        return 2;
                    }
                },
                new Measurable() {
                    @Override
                    public double getMeasure() {
                        return 7;
                    }
                },
                new Measurable() {
                    @Override
                    public double getMeasure() {
                        return 9;
                    }
                },
        };
        Pair<Measurable, Measurable> pair = minmax(arr);
        System.out.println("Min: " + pair.getKey().getMeasure());
        System.out.println("Max: " + pair.getValue().getMeasure());
    }
}


Related Solutions

Write a generic method in java code public static double jaccard(HashSet A, HashSet B) that on...
Write a generic method in java code public static double jaccard(HashSet A, HashSet B) that on input two sets represented as hash sets, returns their Jaccard similarity. The following are a few sample runs: Input :    A=1, 2, 3, 4,   B=2, 4, 6, 8 Return:   0.3333333333333333 Input :    A=Larry, Michael, Shaq, Kobe, LeBron                    B=Steve, Kobe, Shaq, LeBron, Steph, Jeremy, Michael Return:   0.5 Your method must have time complexity On and space complexity O1, where n is the size of...
1) Consider the following Java method. Which term best describes what this method computes? static int...
1) Consider the following Java method. Which term best describes what this method computes? static int doSomething(int[] a) {     int b = a[0];     for (int c : a) if (b > c) b = c;     return b; } a. average b. maximum c. minimum d. sum e. transpose 2) Consider the following Java program, what starts on line 2? 1 public class HelloWorld { 2     // My first program! 3     public static void main(String[] args) { 4        ...
In java: 4. Using non-generic techniques, implement a static method getMax() that takes an array of...
In java: 4. Using non-generic techniques, implement a static method getMax() that takes an array of type Comparable and returns the maximum element in the array. (i.e. "public static Comparable getMax(Comparable [] anArray)"). 5. Using the generic techniques to specify super-class relationships, implement a type safe version of the method in 4 named getMaxGen().
Please use Java language in an easy way with comments! Thanks! Write a static method called...
Please use Java language in an easy way with comments! Thanks! Write a static method called "evaluate" that takes a string as a parameter. The string will contain a postfix expression, consisting only of integer operands and the arithmetic operators +, -, *, and / (representing addition, subtraction, multiplication, and division respectively). All operations should be performed as integer operations. You may assume that the input string contains a properly-formed postfix expression. The method should return the integer that the...
Please use Java language in an easy way with comments! Thanks! Write a static method called...
Please use Java language in an easy way with comments! Thanks! Write a static method called "evaluate" that takes a string as a parameter. The string will contain a postfix expression, consisting only of integer operands and the arithmetic operators +, -, *, and / (representing addition, subtraction, multiplication, and division respectively). All operations should be performed as integer operations. You may assume that the input string contains a properly-formed postfix expression. The method should return the integer that the...
Write a Java method that takes an input string and computes the income minus the expenses....
Write a Java method that takes an input string and computes the income minus the expenses. The income components are indicated by numbers; while the expenses from your spending are numbers starting with a minus sign '-'. The input string may contain lowercase and uppercase letters, as well as other characters. Note that Character.isDigit(char) tests if a char is one of the chars '0', '1', ..., '9'. Also recall that Integer.parseInt(string) converts a string to an int. Test cases :...
Write a Java method that computes the future investment value at a given interest rate for...
Write a Java method that computes the future investment value at a given interest rate for a specified number of years. Your method should return the future investment value after calculation. The future investment is determined using the formula below:     futureInvestmentValue = investmentAmount * (1+monthlyInterestRate)numberOfYears*12 You can use the following method header: public static double futureInvestmentValue (double investmentAmount, double monthlyInterestRate, int years);
FOR JAVA: Need to write a code for the implementation of this public static method: findLongestPalindrome...
FOR JAVA: Need to write a code for the implementation of this public static method: findLongestPalindrome takes a Scanner scn as its parameter and returns a String. It returns the longest token from scn that is a palindrome (if one exists) or the empty string (otherwise). (Implementation note: You'll find your isPalindrome method helpful here. This method calls for an optimization loop.)
IN JAVA write a program that asks a user for a maximum of 20 user-inputted elements...
IN JAVA write a program that asks a user for a maximum of 20 user-inputted elements and create an array. Then, write a Merge Sort function with recursion (in the main) that takes the user inputted elements in the array, sorts them, and prints them back.
Use the method of Lagrange multipliers to find the absolute maximum and minimum values of the...
Use the method of Lagrange multipliers to find the absolute maximum and minimum values of the function f(x, y, z) = x^2yz^2 subject to the constraint 2x ^2 + 3y^ 2 + 6z^ 2 = 33
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT