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

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 =...
(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,...
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...
If a class A implements interface I1 and class C and D are derived from class...
If a class A implements interface I1 and class C and D are derived from class A, a variable of type I1 can be used to hold references of what type of objects? which one is correct 1. A, C, and D 2. A and D
The abstract class SayHello and the static method process are defined as followed: class SayHello {...
The abstract class SayHello and the static method process are defined as followed: class SayHello {             private String greeting; SayHello(   )             {                         greeting = “Hello guys”;             } SayHello( String wts )             {                         greeting = wts;             }             void printGreeting( ); } static void process( SayHello obj ) {             obj.printGreeting(   ); } Write the statements to instantiate an object (with its instance variable initialized with the string “Bonjour mes amis” ) of the anonymous class that extends this abstract class by using the...
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().
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT