## CS 1401 Assignment #8

Date Assigned: Monday, October 22, or Tuesday, October 23, 2007.

Due Date: Monday, October 29, or Tuesday, October 30, 2007, before the beginning of your lab section.

Objective: The main objective of this assignment is to learn to design user-defined classes and ADTs.

Programming assignment: an interval [a, b] is characterized by its lower endpoint a and its upper endpoint b. A midpoint of the interval is defined as (a + b)/2, and its width is defined as b - a.

Arithmetic operations on the intervals are defined based on the following logic. For example, for addition, if I have between 5 and 8 dollars with me, and my friend has between 10 and 30, then the smallest amount we have together is 5 + 10 = 15, and the largest is 8 + 30 = 38.

For subtraction, if I have between 10 and 30 dollars, and I spend between 5 and 8 for lunch, then the smallest amount that will remain is when I had the smallest amount and spend the largest amount, i.e., 10 - 8 = 2. The largest remaining amount is when I had most money and spent the least amount of money, i.e., 30 - 5 = 25.

As a result, the arithmetic operations with intervals are defined as follows:

```[a, b] + [c, d] = [a + c, b + d]
[a, b] - [c, d] = [a - d, b - c]
[a, b] * [c, d] = [min(a*c, a*d, b*c, b*d), max(a*c, a*d, b*c, b*d)]
1/[a, b] = [1/b, 1/a] if 0 is not in the interval [a, b]
(and undefined otherwise)
[a, b]/[c, d] = [a, b] * (1/[c, d])
```

Describe a class whose elements are intervals, and methods include:

• a constructor method,
• methods to return (accessor) and to change (mutator) the values of the parameters,
• methods for computing the midpoint and the width of an interval, and
• method for the arithmetic operations.
Use your class in the main program to create the intervals [5, 8] and [10, 30] and to check that [5, 8] + [10, 30] = [15, 38] and [10, 30] - [5, 8] = [2, 25].

```
Solution:

public class Interval {
//endpoints
private double low;
private double up;

//constructor
public (double lower, double upper)
{low = lower; up = upper;}

//assessor methods
public double getLower()
{return low;}

public double getUpper()
{return up;}

//mutator methods
public void setLower(double newLower)
{low = newLower;}

public void setUpper(double newUpper)
{up = newUpper;}

//midpoint and width
public double midpoint()
{return (low + up)/2.0;}

public double width()
{return up - low;}

//arithmetic operations
{
double newLower = low + secondInterval.getLower();
double newUpper = up + secondInterval.getUpper();
return new Interval(newLower, newUpper);
}

public Interval subtract(Interval secondInterval)
{
double newLower = low - secondInterval.getUpper();
double newUpper = up - secondInterval.getLower();
return new Interval(newLower, newUpper);
}

public Interval multiply(Interval secondInterval)
{
double product1 = low * secondInterval.getLower();
double product2 = low * secondInterval.getUpper();
double product3 = up * secondInterval.getLower();
double product4 = up * secondInterval.getUpper();

//computing the lower endpoint
double newLower = product1;
newLower = Math.min(newLower, product2);
newLower = Math.min(newLower, product3);
newLower = Math.min(newLeft, product4);

//computing the upper endpoint
double newUpper = product1;
newUpper = Math.max(newUpper, product2);
newUpper = Math.max(newUpper, product3);
newUpper = Math.max(newUpper, product4);

return new Interval(newLower, newUpper);
}

public Interval inverse()
{
if (low <= 0 && 0 <= up)
{System.out.println("Inverse not defined");
else
{
double newLower = 1/up;
double newUpper = 1/low;
return new Interval(newLower, newUpper);
}
}

public Interval divide(Interval secondInterval)
{
return self.multiply(secondInterval.inverse());
}
}

public class Assignment8
{
public static void main(String[] args)
{
Interval int1 = new Interval(5.0, 8.0);
Interval int2 = new Interval(10.0, 30.0);