|
CSCI A201/A597 and I210
|
Your fourth assignment consists of only one problem, due on
Saturday, March 3, 2001 at the end of the day
Strict due date is
Please note the test program below.Sat Mar 3 at 11:59pm
It provides tests for each of the ten features of your class definition.
Implement a class Fraction that works as follows:
Assume the following test program:
public static void main(String[] args) {
Fraction f = new Fraction(6, 9);
Fraction g = new Fraction(-4, 6);
System.out.println("Test of operations: ");
System.out.println(" Add: " + f + " + " + g + " = " + f.plus(g));
System.out.println(" Sub: " + f + " - " + g + " = " + f.minus(g));
System.out.println(" Mul: " + f + " * " + g + " = " + f.times(g));
System.out.println(" Div: " + f + " / " + g + " = " + f.divideBy(g));
System.out.println("Test of predicates: ");
System.out.print(" 1. Does " + f + " equal " + g + "? ");
System.out.println(" The answer is: " + f.equals(g));
Fraction h = new Fraction(8, -2);
System.out.print(" 2. Is " + h + " an integer? ");
System.out.println("The answer is: " + h.isInt());
Fraction i, j;
i = (f.minus(g)).times(f.plus(g));
j = f.times(f).minus(g.times(g));
System.out.print(" 3. Does " + i + " equal " + j + "? ");
System.out.println("The answer is: " + i.equals(j));
System.out.print(" 4. Is 5/8 greater than 2/3? The answer is: ");
System.out.println((new Fraction(5, 8)).greaterThan(new Fraction (2, 3)));
}
For this example the output should be:
You should feel free (and eager) to useTest of operations: Add: 2/3 + (-2/3) = 0 Sub: 2/3 - (-2/3) = 4/3 Mul: 2/3 * (-2/3) = (-4/9) Div: 2/3 / (-2/3) = (-1) Test of predicates: 1. Does 2/3 equal (-2/3)? The answer is: false 2. Is (-4) an integer? The answer is: true 3. Does 0 equal 0? The answer is: true 4. Is 5/8 greater than 2/3? The answer is: false
gcd defined by Euclid:
class Euclid {
static int gcd(int a, int b) {
a = Math.abs(a);
b = Math.abs(b);
if (a == 0) return b; // 0 is error value
if (b == 0) return a;
int temp;
while (b > 0) {
temp = a % b;
a = b;
b = temp;
}
return a;
}
}
Additional information will be
presented
in class.