4. Interval Class

The goal of this practice is to develop the code for the class Interval from the object modeling in UML to the final code in Java.

The class Interval represents numbers that have imprecision. An interval is a real number that has a precision range associated to it. For instance, the value of resistance of commercial resistors is given by an interval number. A 20K 10% resistor has its value in between 18K and 22K.

This new class has to be able to perform the four basic arithmetic operations: addition, subtraction, multiplication and division. It should have, at least, the following:

    class Interval extends Number implements Ops {

        Interval(Interval i);
        Interval(double number, float precision);
        float getPrecision();

           (The same for add, sub, mult, div)
           (Implement all Number methods)
           (Implement the toString, equals and hash from Object)

    }

It should implement the Ops interface:

    public interface Ops {

        // Add a number to this number
        public Interval add(Interval a);

        // Subtract a number from this number
        public Interval sub(Interval b);

        // Multiply a number to this number
        public Interval mult(Interval a);
        
        // Divide this number by
        public Interval div(Interval b);

    }

For 20K 10%: Interval(<number>, <precision%>) e.g.: Interval(20000, 10).

For instance, if one wants to calculate the resistance of two parallel resistors:

Input:
    R1 = 20K 10%   R2 = 10K 5%

Code:
    Interval r1 = new Interval(20000, 10);
    Interval r2 = new Interval(10000, 5);
    Interval rp = new Interval(r1).mult(r2).div(new Interval(r1).add(r2));
    System.out.println("Parallel: " + r1 + " " + r2 + " = " + rp);

Output:
    Parallel: 20000 10% 10000 5% = 6662.9 6.7%

1) First draw the UML diagrams.

2) Generate the code from UML.

3) Complete the code and run it using the Eclipse editor.

4) The code should be able to run the example above.

5) Which of the three interfaces do you think is best? BasicOps, FourOps or Ops? Why?