CS 1063 Lab 6:
Designing and Implementing a Class using Assignment Statements
The Pair Class
Objectives
- Construct and use objects
- Design the public interface of a class
- Determine the instance variables necessary for a class
- Use assignment statements to design your class
Hand-in Requirements
All projects and laboratories will be submitted electronically through
webCT. Zip up your entire project folder to submit as the
source. (Right click on the project folder and follow
the SentTo link.) The project directory should include
the following:
- Public Interface as a word or plaintext document
- Pair.java
- PairTest.java
Details
Write a program that implements the class Pair.
A Pair class contains two double numbers and performs various
operations on the two numbers. In the constructor supply the
two double numbers necessary for the pair.
Your program should contain methods
to do the following
- toString that
returns the state of the Pair as a String
- Return the values stored in the two numbers. Methods getFirst and getSecond
- Calculate and return the sum of the two numbers
- Calculate and return the difference of the two numbers
(first - second)
- Calculate and return the product of the two numbers
- Calculate and return the quotient of the two numbers
(first/second)
- Calculate and return the distance of the two numbers (absolute
value of the difference)
- Calculate and return the average of the two numbers
- Calculate and return the maximum (the larger of the two)
To find the maximum of the two double numbers use the static
method Math.max. (That is, max is a static method
in the Math class).
double maxValue = Math.max(5.0, 6.0); // maxValue = 6.0
- Calculate and return the minimum (the smaller of the two)
To find the minimum of the two double numbers use the static
method Math.min. (That is, min is a static method
in the Math class).
double minValue = Math.min(5.0, 6.0); // minValue = 5.0
Remember to follow the steps we have used in class. Determine
the public interface, start typing in your class using stubs, and then
fill in your implementation.
Testing your Class
Test ALL of your methods with
three different Pair objects. In your
first pair make the first number greater than the second, in the
second pair make the second number greater, and in
the third pair make the two numbers equal.