CIS-3030 Homework #7: Intervals

Due: Wednesday, April 17, 2019

Read Chapter 4 and Chapter 6 in PiS. Chapter 21 contains useful information on implicits (used by type classes). Chapter 30 contains information about defining equality in Scala. Review the Scala core features list.

An interval is a floating point value represented by a (normally small) range. It is useful when modeling data read from the real world with imprecise instruments. For example instead of saying that the speed was 32.4 km/hr you might say that it is somewhere in the range [29.2, 35.6] km/hr. This would be an appropriate range for a gauge that had 10% accuracy.

  1. Create a Scala class called Interval that represents such intervals. Interval objects should be immutable (once constructed they can never change). Your class should support the following operations.

    1. It should be possible to add, subtract, multiply, and divide intervals. Note that interval arithmetic is tricky. See the Wikipedia page on interval arithemtic for the specifics. Note the formulas at the top of the page. If division by zero is attempted throw a java.lang.ArithmeticException object with the message "interval division by zero."

    2. You should provide a toString method that converts an Interval into an appropriate representation such as "[1.0, 2.0]" (the precise format is not too important as long as it looks readible to humans).

    3. You will need a constructor that takes two Double parameters representing the lower and upper bounds of the interval.

    4. You will also want to be able to decode a string representation of an Interval into the specified bounds (basically the reverse of toString). HINT: Scala strings are Java strings and so have all the methods of Java's String class. You should provide this service as a method in the Interval companion object.

    The archive contains a skeletal Interval.scala that you can use. It also contains a main method in Hw07.scala that implements a simple, stack-based interval calculator. The additional file IntervalStack.scala supports the calculator. You shouldn't have to modify any file but Interval.scala.

    The demonstration interval calculator treates each line of input as either an Interval or one of "+", "-", "*", or "/". If the input is an Interval it is pushed onto a stack. If it is an operator, the top two stack elements are popped and operated upon as indicated. The result is returned to the stack and displayed.

    Here is a sample run

            > [1.2, 4.5]  # Input an interval.
            > [0.5, 3.3]  # Input another interval.
            > +           # Pop last two intervals and add them.
            [1.7, 7.8]    # Output printed by program.
            >             # End program by entering a blank line.
  2. Modify your Interval class to contain a properly defined equals method (don't forget hashCode as well).

  3. Intervals can be ordered. Modify your class to extend the Ordered trait and implement compare in an appropriate manner. Use the center point of each interval to decide the ordering.

  4. In the companion object for Interval introduce a suitable implicit object that allows Interval instances to be treated as numeric. To illustrate this working, write a short program that creates a list of Interval objects and then adds them together with the generic sum method in the List class. See Rational2 as a template for what needs to be done.

Submit your file Interval.scala only (put your name in comments). Don't submit the entire project!

Last Revised: 2019-03-29
© Copyright 2019 by Peter C. Chapin <>