A ss i gnment #5 Implement the Fraction class as described below. Filenames shou
ID: 644776 • Letter: A
Question
Assignment #5
Implement the Fraction class as described below.
Filenames should be
Fraction.java
Note that this filename starts with a capital letter. Please make sure your filenames match mine exactly.
Fraction exercise
Start with this copy of the Fraction class, and add the following features:
Write a method called Simplify, which returns a simplified version of the calling object (no parameters are needed). The method should return a new Fraction (simplified), but not change the original one. The fraction in the form
0/N should have simplified form 0/1. Any other fraction h as the usual mathematical definition of "simplified form". Keep within the rules already
established in this Fraction class (e.g. denominator always positive, any negative values go in the numerator, etc).
o
f1.add(f2)
// means f1 + f2
o
f1.subtract(f2)
// means f1 - f2
Write methods add, subtract, multiply, and divide. Each one should take in a Fraction as a parameter and perform the given computation between the calling object and the parameter object. (The calling object is always the first operand). The result of each operation should always be a fraction returned in simplified form. Example calls:
In divide, if an attempt is made to divide by a fraction with the value 0, default the result to 0/1. (Such division is actually undefined. 0/1 is not the "true" result, but we need to return something from this method).
Here are your method signatures:
o public Fraction simplify()
o public Fraction add(Fraction f)
o public Fraction subtract(Fraction f)
o public Fraction multiply(Fraction f)
o public Fraction divide(Fraction f)
Make sure that your new methods enforce the same rules on the data that my
original ones do -- the denominator must be non-negative (any negative sign goes in the numerator) and the denominator must never be zero (this would be undefined).
Testing
I've provided a file to help you get started with testing. This is not a comprehensive
set of tests (so you'll want to try some of your own). However, this will get you started.
And you do need to include the FractionTester class when you submit. See instructions at the bottom.
The FractionTester.java file. This contains some tests for the Fraction classes.
Here's the sample run
Fraction tests:
4/6 simplified = 2/3
75/175 simplified = 3/7
-6/17 simplified = -6/17
f1 = 4/6
f2 = 75/175
f3 = -6/17
4/6
+
75/175
=
23/21
4/6
-
75/175
=
5/21
4/6
*
75/175
=
2/7
4/6
/
75/175
=
14/9
75/175
+
-6/17
=
9/119
75/175
-
-6/17
=
93/119
75/175
*
-6/17
=
-18/119
75/175
/
-6/17
=
-17/14
75/175 / 0/1 = 0/1
Submitting:
Use the Assignment 5 submission link on Blackboard. Submit only your source code:
public static void main(String[] args) {
System.out.println(" Fraction tests: ");
Fraction f1 = new Fraction(4, 6);
Fraction f2 = new Fraction(75, 175);
Fraction f3 = new Fraction(-6, 17);
System.out.println(f1 + " simplified = " + f1.simplify());
System.out.println(f2 + " simplified = " + f2.simplify());
System.out.println(f3 + " simplified = " + f3.simplify());
// show that f1, f2, f3 haven't changed
System.out.println("f1 = " + f1);
System.out.println("f2 = " + f2);
System.out.println("f3 = " + f3);
// arithmetic
System.out.println(f1 + " + " + f2 + " = " + f1.add(f2));
System.out.println(f1 + " - " + f2 + " = " + f1.subtract(f2));
System.out.println(f1 + " * " + f2 + " = " + f1.multiply(f2));
System.out.println(f1 + " / " + f2 + " = " + f1.divide(f2));
System.out.println();
System.out.println(f2 + " + " + f3 + " = " + f2.add(f3));
System.out.println(f2 + " - " + f3 + " = " + f2.subtract(f3));
System.out.println(f2 + " * " + f3 + " = " + f2.multiply(f3));
System.out.println(f2 + " / " + f3 + " = " + f2.divide(f3));
System.out.println();
// test 'division by zero' handling
Fraction zero = new Fraction();
System.out.println(f2 + " / " + zero + " = " + f2.divide(zero));
}
}
o
f1.add(f2)
// means f1 + f2
o
f1.subtract(f2)
// means f1 - f2
Explanation / Answer
public class Fraction {
private int numerator = 0; // numerator (and keeps sign)
private int denominator = 1; // always stores positive value
public Fraction() {
}
public Fraction(int n, int d) {
if (set(n, d) == false)
set(0, 1);
}
public boolean set(int n, int d) {
if (d > 0) {
numerator = n;
denominator = d;
return true;
} else
return false;
}
public String toString() {
return (numerator + "/" + denominator);
}
public int getNumerator() {
return numerator;
}
public int getDenominator() {
return denominator;
}
public double decimal() {
return (double) numerator / denominator;
}
//using Euclids iterative gcd
public Fraction simplify(){
int gcd = Math.abs(this.numerator),den = Math.abs(this.denominator);
while(den > 0){
int mod = gcd % den;
gcd = den;
den = mod;
}
if(this.numerator < 0 && this.denominator < 0){
this.numerator = Math.abs(this.numerator);
this.denominator = Math.abs(this.denominator);
}
if(this.denominator < 0){
this.denominator = Math.abs(this.denominator);
this.numerator = -this.numerator;
}
this.numerator /= gcd;
this.denominator /= gcd;
return this;
}
/*
5/8 + 3/10 = (5*10 + 3*8) / (8*10) = 74/80
*/
public Fraction add(Fraction f){
int d = this.denominator , n = this.numerator;
if(d == f.denominator){
n += f.numerator;
}else{
n = (n * f.denominator) + (f.numerator * d);
d = d * f.denominator;
}
return new Fraction(n, d).simplify();
}
public Fraction subtract(Fraction f){
int d = this.denominator , n = this.numerator;
if(d == f.denominator){
n -= f.numerator;
}else{
n = (n * f.denominator) - (f.numerator * d);
d = d * f.denominator;
}
return new Fraction(n,d).simplify();
}
public Fraction multiply(Fraction f){
return new Fraction(this.numerator*f.numerator,this.denominator*f.denominator).simplify();
}
public Fraction divide(Fraction f){
int d = this.denominator , n = this.numerator;
if(d == f.denominator){
d = f.numerator;
}else{
n = (n * f.denominator);
d = f.numerator * d;
}
if(n < 0 && d < 0){
n = Math.abs(n);
d = Math.abs(d);
}
if(d < 0){
d = Math.abs(d);
n = -n;
}
return new Fraction(n,d).simplify();
}
}
Fraction tests:
4/6 simplified = 2/3
75/175 simplified = 3/7
-6/17 simplified = -6/17
f1 = 2/3
f2 = 3/7
f3 = -6/17
2/3 + 3/7 = 23/21
2/3 - 3/7 = 5/21
2/3 * 3/7 = 2/7
2/3 / 3/7 = 14/9
3/7 + -6/17 = 9/119
3/7 - -6/17 = 93/119
3/7 * -6/17 = -18/119
3/7 / -6/17 = -17/14
3/7 / 0/1 = 0/1
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.