Question
In this lab, you are asked to create a generic interface (with a static method) and modify the generic class Pair from chapter 9 to
In this lab, you are asked to create a generic interface (with a static method) and modify the generic class Pair from chapter 9 to implement the comparable method. You'll also implement a class that implements your generic interface. The generic interface you are to create is called Measurable and is also based on an example from chapter 9.15. The interface provides an abstract method definition and a static method implementation: double measure() this method returns a double value that represents the object's "measure," which is basically a that can used as a statistic public static > T largest(ArrayList a) this static method is implemented by Measurable and takes an ArrayList object's whose class implement Measurable. The method returns the object with the maximum measure. The Coordinate class implements Measurable and Comparable. It has two private variables that represent the X and Y coordinates for a graph. The class must implement the following methods: public Coordinate(double x, double y) a constructor that sets the X and Y coordinates public double getX() a getter method that returns the X coordinate public double getY() a getter method that returns the Y coordinate public void setX(double x) a setter method to change the X coordinate public void setY(double y) a setter method to change the Y coordinate public String toString() an override to toString() that returns a string for a Coordinate object. If the X coordinate is 5 and the Y coordinate is 10, the return string is "<5,10>" public double measure() an implementation of the Measurable method measure() that returns the distance of the Coordinate object from the origin "<0,0>". This defined as Math.sqrt(Math.pow(xcoor, 2) + Math.pow(ycoor, 2)). public int compareTo(Coordinate) an implementation of the Comparable method compareTo() that orders Coordinate objects by x then by y. Lastly, modify the Pair from chapter 18.2 so that it restricts its T and S parameter types to types that implement Comparable and use the extends keyword as used in the definition of the largest() method. implement the Comparable> interface. Since T and S are Comparable, this will allow the compareTo method that you define to use the compareTo method of Pair's first and second variables. The Pair compareTo method used order by first, then by second. import java.util.*; public class Main { public static void main(String [] args) { Scanner in = new Scanner(System.in); ArrayList clist = new ArrayList<>(); int x, y; x = in.nextInt(); y = in.nextInt(); while( x != 0 && y != 0) { clist.add(new Coordinate(x, y)); x = in.nextInt(); y = in.nextInt(); } Coordinate max = Measurable.largest(clist); if(max != null) System.out.printf("The largest coordinate is %s, with measure %.1f%n", max, max.measure()); else System.out.println("No coordinates to check."); String pname = in.next(); x = in.nextInt(); y = in.nextInt(); Pair psc1 = new Pair<>(pname, new Coordinate(x, y)); pname = in.next(); x = in.nextInt(); y = in.nextInt(); Pair psc2 = new Pair<>(pname, new Coordinate(x, y)); int test = psc1.compareTo(psc2); if(test < 0) System.out.printf("%s is greater than %s by natural order%n", psc2, psc1); else if(test > 0) System.out.printf("%s is greater than %s by natural order%n", psc1, psc2); else System.out.printf("%s is equal to %s by natural order%n", psc1, psc2); //System.out.printf("%s has measure %f and %s has measure %f%n", psc1, psc1.getSecond().measure(), psc2, psc2.getSecond().measure()); Comparator> byMeasure = (o1, o2) -> { double m1 = o1.getSecond().measure(); double m2 = o2.getSecond().measure(); if(m1 < m2) return -1; else if (m1 > m2) return 1; else return 0; }; test = byMeasure.compare(psc1, psc2); if(test < 0) System.out.printf("%s is greater than %s by measure%n", psc2, psc1); else if(test > 0) System.out.printf("%s is greater than %s by measure%n", psc1, psc2); else System.out.printf("%s is equal to %s by measure%n", psc1, psc2); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started