Problem 5: Miscellaneous

1. Pipes

Consider a class Pipe with two private fields, diameter (of type double) and number (of type integer), and a public constant field named TOLERANCE, defined as 10E-2. Provide an implementation of class Pipe, with two different ways to compare pipes: one compares pipes by their diameter, up to the TOLERANCE value, and the other compares pipes by the number of pipes.
Note: The only types and methods you can use from java package are:

  • java.lang.Comparable with method public int compareTo(T obj);
  • java.util.Comparator with method public int compare(T obj1, T obj2); and 
  • java.util.Arrays with methods:
    • public static void sort(Object[] a); and
    • static <T> void sort(T[] a, Comparator<? super T> c).
  1. Provide an implementation of the Pipe class. 
  2. Provide the pipe comparison by their diameter. 
  3. Provide the pipe comparison by number of pipes. 
  4. Provide a main method in a Main class where five pipes are built and stored in an array of pipes. 
  5. Within the main method sort the array of pipes according to the two criteria previously implemented and print the result to the terminal.

2. Relatable shapes

Consider an interface Shape with a no-arg method area that returns a double.

Consider the interface Relatable with a predicate isBiggerThan receiving another Relatable and returning a boolean. The method of Relatable should compare any two Relatable objects (strings vs strings, numbers vs numbers, shapes vs shapes, etc); whenever objects are not relatable it should throw an ObjectsNotRelatable exception 

Provide the implementation of classes Rectangle and Triangle that implement both interfaces. When comparing shapes, isBiggerThan should compare them by their area. For instance, a rectangle is bigger than a rectangle/triangle if its area is greater that the rectangle/triangle’s area.
Note: you might use more classes than those directly asked. 

  1. Provide the Shape interface. 
  2. Provide the Relatable interface. 
  3. Provide the Rectangle class, with all needed fields and methods, and three constructors: a no-arg constructor, a copy constructor and a constructor that receives all fields needed to be initialized. 
  4. Provide the Triangle class, with all needed fields and methods, and three constructors as before. 
  5. Provide a Main class with a main method where a Rectangle and a Triangle are built and compared by Relatable. Print the result to the terminal.

Attachments