Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Here is my code. The correct output is part of the 2nd file. (in comments ) My question how do I simplfy f1 and f2.

Here is my code. The correct output is part of the 2nd file. (in comments ) My question how do I simplfy f1 and f2. If you can show me a fix version of the code that would be great. Thanks in advance.

My current out put is :

f1=44/14 f2=21/14 65/14 23/14 33/7 44/21 7/22

/* Fraction.java A class (data type) definition file This file just defines what a Fraction is This file is NOT a program ** data members are PRIVATE ** method members are PUBLIC */ public class Fraction { private int numer; private int denom;

// ACCESSORS public int getNumer() { return numer; }

public int getDenom() { return denom; }

public String toString() { return numer + "/" + denom; }

// MUTATORS public void setNumer(int n) { numer = n; }

public void setDenom(int d) { if (d != 0) denom = d; else { // error msg OR exception OR exit etc. } }

// DEFAULT CONSTRUCTOR - no args passed in public Fraction() { this(0, 1); // "this" means call a fellow constructor }

// 1 arg CONSTRUCTOR - 1 arg passed in // assume user wants whole number public Fraction(int n) { this(n, 1); // "this" means call a fellow constructor }

// FULL CONSTRUCTOR - an arg for each class data member public Fraction(int n, int d) { setNumer(n); setDenom(d); // call your reduce here }

// COPY CONSTRUCTOR - takes ref to some already initialized Fraction object public Fraction(Fraction other) { this(other.numer, other.denom); // call my full C'Tor with other // Fraction's data }

private void reduce() { // reduces this fraction to lowest form

int GCD = gcd(getNumer(), getDenom()); setNumer(getNumer() / GCD); setDenom(getDenom() / GCD);

}

// gcd : greatest common denom, used to reduce fractions public int gcd(int n1, int n2) { int M, N, R; if (n1 < n2) { N = n1; M = n2; } else { N = n2; M = n1; } R = M % N; while (R != 0) { M = N; N = R; R = M % N; } return N; }

/** * operation(+): addition between fractions * * @param b * @return result of addition */ public Fraction add(Fraction b) { int numer = (this.numer * b.denom) + (b.numer * this.denom); int denom = this.denom * b.denom; Fraction fraction = new Fraction(numer, denom); fraction.reduce(); return fraction; }

/** * @param b * @return */ public Fraction subtract(Fraction b) { return add(new Fraction(-b.numer, b.denom)); }

/** * method to multiply * * @param b * @return */ public Fraction multiply(Fraction b) { // check preconditions if ((denom == 0) || (b.denom == 0)) throw new IllegalArgumentException("invalid denom"); // create new fraction to return as product Fraction product = new Fraction(); // calculate product product.numer = numer * b.numer; product.denom = denom * b.denom; // reduce the resulting fraction product.reduce(); return product; }

/** * method to devide * * @param b * @return */ public Fraction divide(Fraction b) { // check preconditions if ((denom == 0) || (b.numer == 0)) throw new IllegalArgumentException("invalid denom"); // create new fraction to return as result Fraction result = new Fraction(); // calculate result result.numer = numer * b.denom; result.denom = denom * b.numer; // reduce the resulting fraction result.reduce(); return result; }

public Fraction reciprocal() { Fraction fraction = new Fraction(this.denom, this.numer); fraction.reduce(); return fraction; }

}// EOF

/*

FractionTester.java A program that declares Fraction variables

We will test your Fraction class on THIS tester.

*/

public class FractionTester

{

public static void main( String args[] )

{

// use the word Fraction as if were a Java data type

Fraction f1 = new Fraction( 44,14 ); // reduces to 22/7

System.out.println( "f1=" + f1 ); // should output 22/7

Fraction f2 = new Fraction( 21,14 ); // reduces to 3/2

System.out.println( "f2=" + f2 ); // should output 3/2

System.out.println( f1.add( f2 ) ); // should output 65/14

System.out.println( f1.subtract( f2 ) ); // should output 23/14

System.out.println( f1.multiply( f2 ) ); // should output 33/7

System.out.println( f1.divide( f2 ) ); // should output 44/21

System.out.println( f1.reciprocal() ); // should output 7/22

} // END main

} // EOF

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions