Question

In: Computer Science

Make the Measurable interface from Chapter 10 into a generic class. Provide a static method that...

Make the Measurable interface from Chapter 10 into a generic class. Provide a static method that returns the largest element of an ArrayList, provided that the elements are instances of Measurable. Be sure to return a value of type T.

Measurable interface:

public interface Measurable {

double getMeasure();

}

Using JAVA please

Solutions

Expert Solution

import java.util.ArrayList;
import java.util.Arrays;

public class LargestMeasurable {

    public static <T extends Measurable> T max(ArrayList<T> list) {
        if (list == null || list.isEmpty())
            return null;
        T largest = list.get(0);
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getMeasure() > largest.getMeasure()) {
                largest = list.get(i);
            }
        }
        return largest;
    }

    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;
                    }
                },
        };
        System.out.println(max(new ArrayList<>(Arrays.asList(arr))).getMeasure());
    }
}


Related Solutions

Using the Comparable Interface Write a class Compare3 that provides a static method largest. Method largest...
Using the Comparable Interface Write a class Compare3 that provides a static method largest. Method largest should take three Comparable parameters and return the largest of the three (so its return type will also be Comparable). Recall that method compareTo is part of the Comparable interface, so largest can use the compareTo method of its parameters to compare them. Write a class Comparisons whose main method tests your largest method above. First prompt the user for and read in three...
JAVA Add static methods largest and smallest to the Measurable interface. The methods should return the...
JAVA Add static methods largest and smallest to the Measurable interface. The methods should return the object with the largest or smallest measure from an array of Measurable objects.
Generic types A class or interface that declares one or more generic variables is called a...
Generic types A class or interface that declares one or more generic variables is called a generic type. In this portion of the activity, you will make a class generic. Open GenericsC.java in jGRASP then compile it. At this point you should be familiar with the two type-safety warnings given by the compiler. You should be able to understand the source of the error: the use of the raw types List and Collection. Since the List being declared (al) is...
Make a public class Creater that provides a single class (static) method named subtractor. subtractor takes...
Make a public class Creater that provides a single class (static) method named subtractor. subtractor takes a single int argument and returns a method that implements the following Create interface: public interface Create { int change(int something); --------------------------------------------------------------------- The returned “function” should implement Create so that it subtracts the value passed to subtractor. For example Create one = Creater.substractor(5); Create two = Creater.subtractor(-3); System.out.println(one.change(5)); // should print 0 System.out.println(second.change(3); // should print -6 The answer should be is a single...
Make a class called CashRegister that has the method public static double getTotalCostOfOfferings(ArrayList<Offering> o) Make this...
Make a class called CashRegister that has the method public static double getTotalCostOfOfferings(ArrayList<Offering> o) Make this method calculate the total cost of all Offering objects in the ArrayList. Submit Product, Service, and CashRegister. Given Files: import java.util.ArrayList; public class Demo3 { public static void crunch(ArrayList<Offering> o) { System.out.println("Adding up the following offerings:"); for (Offering current : o) { System.out.println(current); } System.out.printf("Total for all: $%,.2f\n", CashRegister.getTotalCostOfOfferings(o)); System.out.println("---------------------------------\n"); } public static void main(String[] args) { ArrayList<Offering> offeringList = new ArrayList<>(); offeringList.add(new Product("iPhone",...
for java!! Make a public class Value that provides a static method named test. test takes...
for java!! Make a public class Value that provides a static method named test. test takes a single int argument and returns an anonymous object that implements the following Absolute interface: public interface Absolute { int same(); int opposite(); } -------------------------------------------------------------------------- The returned object should implement same so that it returns the passed int as it is, and opposite so that it returns the passed int as the int * -1. For example Absolute first = Value.test(-90) Absolute second =...
Static methods can be called directly from the name of the class that contains the method....
Static methods can be called directly from the name of the class that contains the method. Static methods are usually created to do utility operations. For example: public class Test{ public static int timesTwo(int value){ return value * value; } } public class TestDriver{ public static void main(String[] args){ int var = Test.timesTwo(5); System.out.println(var); } } Create a class called ManyLinkedLists. It will contain a static method called createLinkedList(). That method takes an argument that is a constant defined in...
C++ Static Stack Template In this chapter you studied IntStack, a class that implements a static...
C++ Static Stack Template In this chapter you studied IntStack, a class that implements a static stack of integers. Write a template that will create a static stack of any data type. Demonstrate the class with a driver program. Dynamic Stack Template In this chapter you studied DynIntStack, a class that implements a dynamic stack of integers. Write a template that will create a dynamic stack of any data type. Demonstrate the class with a driver program. Static Queue Template...
(The Colorable interface) Design an interface named Colorable with a void method named howToColor(). Every class...
(The Colorable interface) Design an interface named Colorable with a void method named howToColor(). Every class of a colorable object must implement the Colorable interface. Design a class named Square that extends GeometricObject and implements Colorable. Design another class named Triangle that extends GeometricObject and implements Colorable. Implement howToColor in Square to display the message Color all four sides. Implement howToColor in Triangle to display the message Color all three sides. Draw a UML diagram that involves Colorable, Square, Triangle,...
(The Colorable interface) Design an interface named Colorable with a void method named howToColor(). Every class...
(The Colorable interface) Design an interface named Colorable with a void method named howToColor(). Every class of a colorable object must implement the Colorable interface. Design a class named Square that extends GeometricObjectand implements Colorable. Design another class named Trianglethat extends GeometricObjectand implements Colorable. Implement howToColor inSquareto display the message Color all four sides. ImplementhowToColor inTriangleto display the message Color all three sides. Draw a UML diagram that involves Colorable, Square, Triangle, and GeometricObject. Write a test program that creates...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT