08 Feb 2012 [ 201 hw ]

Email all files as attachments to me at jal2016@email.vccs.edu with subject CSC 201 HW5.

Alternately, put your homework in a hw5 folder in your 201-hw Mercurial repository on BitBucket.

Due Tuesday, Feb 21.

### 1: Rectangle

Implement a Rectangle class that represents an axis-aligned (sides are parallel to the x and y axes) rectangle at a given location in the x-y plane. Allow mutation of instances of your class (so you can change the location, width, or height after creating the object) by providing methods to set those values.

Your class must support a contains operation that tests whether a given point is contained in your rectangle. For example, if rectangle has corners at the coordinates shown below, the point (3,2) is contained in the rectangle, and the point (12,2) is not:

 (0,5) +---------+ (10,5)
|         |
|         |
|  x      | x
|         |
(0,0) +---------+ (10,0)


You'll have to decide how to represent your rectangle. One option is to base it on the location of a single corner, along with values for its width and height.

Include a main method that shows examples of how your class works.

### 2: Timer

Create a Timer class that you can use as a stopwatch. The System.currentTimeMillis method will help with measuring elapsed time. You class should support the following operations:

• start: start the timer
• stop: stop the timer
• reset: reset the timer
• elapsed: return the total number of seconds elapsed between start and stop calls.

Use your class to implement a program that tests your reaction time. Your program should wait a random amount of time, then tell the user to hit Enter, and measure the amount of time it takes before the key is pressed. The Thread.sleep method will be useful for waiting a random amount of time. Here's an outline that you can start from:

import java.util.Random;

/** Program to test reaction time.
*/
class Reaction {
// We have to declare that main can throw an exception, because
// the sleep call can throw an InterruptedException.
public static void main(String[] args) throws InterruptedException {
Random rand = new Random();
// This sleeps for 1000 ms (1 second), but you should instead
// use the rand object to pick a random number of ms to sleep.
System.out.println("Hit Enter!");
// Insert timing code, check for when Enter is hit
// (using System.in.read()), and print results.
}
}


Optional: in your reaction program, if you hit enter before the message appears, it will probably think that you were really quick, when you really just cheated. Fix the program so that it can tell when you cheat, and make you start over.

### 3: Interval

Create an Interval class that represents an interval of numeric values. For example, it could represent the interval [1,2].

This class will be used to perform computations on uncertain quantities. For example, if you have a number in the range [1, 2] and another number in the range [0.1, 0.3], then their sum must be in the range [1.1, 2.3]. This can be used to answer questions like "If you have between 1000 and 2000 people, and between 10% and 15% of them have a pet goat, what are the most and least people that could have a pet goat?"

Ensure that instances of your class are immutable (you can't change the interval that an object represents after you create it), and support the following operations:

• add: add two intervals, returning the interval in which the sum must lie.
• multiply: multiply two intervals, returning the interval in which the product must lie.
• equals: test if two Interval objects represent the same numeric interval.
• Implement a toString method that provides a nice representation of the interval.

Include a main method that tests your class's functionality. It should test the operations using a variety of intervals, including the following cases:

• 0-width intervals (like [1, 1])
• Intervals that lie in (0, +∞)
• Intervals that lie in (-∞, 0)
• Intervals containing 0.

Optional: support unbounded intervals, like (-∞, 3], [-3, ∞), or (-∞, ∞).