|
CSCI A201/A597 and I210
Homework Four Solutions
Second semester 2000-2001
|
Date: Tue, 20 Feb 2001 02:50:57 -0500 (EST)
From: Adrian German <dgerman@cs.indiana.edu>
To: Andrey Salaev <asalaev@cs.indiana.edu>,
Adrian German <dgerman@cs.indiana.edu>,
Francisco Lara-Dammer <flaradam@cs.indiana.edu>, malani@indiana.edu,
myadroff@indiana.edu, Alex Leykin <oleykin@cs.indiana.edu>,
Robert Najlis <rnajlis@cs.indiana.edu>, sapark@indiana.edu,
sgrogg@indiana.edu, vpavlor@indiana.edu, Ying Jin <yinjin@cs.indiana.edu>,
Zhen Pan <zhpan@cs.indiana.edu>
Subject: Assignment Four posted
/* Solution for assignment 4, posted just now, due March 3rd 11:59pm.
Notice grading is particularly objective this time: they need to
define 5 instance methods, 4 predicates, one constructor. Give up
to 9.5 for each, for a total of up to 95, which is an A. Allow the
extra 5 points for surprisingly exceptional work that deserves an
A+ without hesitation.
Let me know if I can be of any help.
... Adrian */
class Fraction {
private int numerator;
private int denominator;
public Fraction(int num, int den) {
int divisor;
if (den == 0) {
System.out.println(" Fraction with denominator zero!");
System.exit(1);
}
if (num == 0) { numerator = 0; denominator = 1; }
else {
if (den < 0) {
num *= -1;
den *= -1;
}
if ((divisor = Euclid.gcd(num, den)) != 1) {
num /= divisor;
den /= divisor;
}
numerator = num;
denominator = den;
}
}
public String toString() {
String fraction;
if (denominator == 1) { fraction = numerator + ""; }
else { fraction = numerator + "/" + denominator; }
if (denominator * numerator < 0) {
return "(" + fraction + ")";
} else {
return fraction;
}
}
public boolean isZero() {
return (denominator == 1 && numerator == 0);
}
public boolean isInt() {
return (denominator == 1);
}
public boolean equals(Fraction other) {
return (numerator == other.numerator && denominator == other.denominator);
}
public boolean greaterThan(Fraction other) {
return (numerator * other.denominator >
denominator * other.numerator);
}
public Fraction minus(Fraction other) {
return new Fraction(
numerator * other.denominator - other.numerator * denominator,
denominator * other.denominator
);
}
public Fraction plus(Fraction other) {
return new Fraction(
numerator * other.denominator + other.numerator * denominator,
denominator * other.denominator
);
}
public Fraction times(Fraction other) {
return new Fraction(numerator * other.numerator, denominator * other.denominator);
}
public Fraction divideBy(Fraction other) {
return new Fraction(numerator * other.denominator, denominator * other.numerator);
}
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)));
}
}
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;
}
}
Remember that there's always more than one way to achieve
a result, and if you have a different solution that's
actually better! With this in mind here's a different
implementation of equals:
boolean equals(Fraction other) {
return (this.minus(other)).isZero();
}
and (assuming a predicate isPositive)
boolean isPositive() {
return numerator * denominator > 0;
}
here's a definition for greaterThan:
boolean greaterThan(Fraction other) {
return (this.minus(other)).isPositive();
}
Hope you enjoyed this assignment. I truly hope so.
Last updated: Feb 4, 2001 by Adrian German for A201