 # Consider a class Fraction of fractions. Each fraction is signed and...

## Question

Consider a class Fraction of fractions. Each fraction is signed and has a numerator and a denominator that are integers. Your class should be able to add, subtract, multiply, and divide two fractions. These methods should have a fraction as a parameter and should return the result of the operation as a fraction. The class should also be able to find the reciprocal of a fraction, compare two fractions, decide whether two fractions are equal, and convert a fraction to a string.

Your class should handle denominators that are zero. Fractions should always occur in lowest terms, and the class should be responsible for this requirement. For example, if the user tries to create a fraction such as 4/8, the class should set the fraction to 1/2. Likewise, the results of all arithmetic operations should be in lowest terms. Note that a fraction can be improper—that is, have a numerator that is larger than its denominator. Such a fraction, however, should be in lowest terms.

Design the class Fraction. Begin by writing a CRC card for this class. Then write a Java interface that declares each public method.
To reduce a fraction such as 4/8 to lowest terms, you need to divide both the numerator and the denominator by their greatest common denominator. The greatest common denominator of 4 and 8 is 4, so when you divide the numerator and denominator of 4/8 by 4, you get the fraction 1/2. The following recursive algorithm finds the greatest common denominator of two positive integers:

Algorithm gcd(integerOne, integerTwo)
if (integerOne % integerTwo == 0)
result = integerTwo
else
result = gcd(integerTwo, integerOne % integerTwo)
return result
It will be easier to determine the correct sign of a fraction if you force the fraction’s denominator to be positive. However, your implementation must handle negative denominators that the client might provide. Write a program that adequately demonstrates your class.

## Solution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

/*
* Fraction class
*/
public class Fraction implements FractionInterface {

private int numerator;
private int denominator;

/*
* Constructor
*/
public Fraction(int numerator, int denominator) {
this.numerator = numerator;
this.denominator = denominator;
if (this.denominator < 0) {
this.numerator *= -1;
this.denominator *= -1;
}
int gcd = gcd(this.numerator, this.denominator);
if (gcd > 0) {
this.numerator /= gcd;
this.denominator /= gcd;
}
}

private int gcd(int a, int b) {
if (a % b == 0)
return b;
else
return gcd(b, a % b);
}

/*
* Method to get numerator
*/
@Override
public int getNumerator() {
return this.numerator;
}

/*
* Method to get denominator
*/
@Override
public int getDenominator() {
return this.denominator;
}

/*
* Method to add fractions
*/
@Override
public FractionInterface add(FractionInterface other) {
return new Fraction(this.numerator * other.getDenominator() + this.denominator * other.getNumerator(),
this.denominator * other.getDenominator());
}

/*
* Method to subtract fractions
*/
@Override
public FractionInterface subtract(FractionInterface other) {
return new Fraction(this.numerator * other.getDenominator() - this.denominator * other.getNumerator(),
this.denominator * other...

By purchasing this solution you'll be able to access the following files:
Solution.zip.

\$60.00
for this solution

or FREE if you
register a new account!

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

### Find A Tutor

View available Java Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.