Question
Create a fraction class. This will have two attributes, a numerator and a denominator, both int. This class will have constructors accessors and mutators (for
Create a fraction class. This will have two attributes, a numerator and a denominator, both int. This class will have
- constructors
- accessors and mutators (for the attributes)
- a toString method which will allow us to print the fraction in the form 3/4
- an Add method so we can add two fractions
- a subtract method (subtracts one fraction from the other)
- a multiply method (multiply two fractions)
- a divide method (divides one fraction by the other)
DO NOT (for now) worry about reducing the fractions.
//Code to copy into program
public class H4{
{
private int num; // Numerator
private int den; // Denominator
public Fraction()
{
// set fraction to default = 0/1
setFraction(0, 1);
} // end default constructor
public Fraction(int numerator, int denominator)
{
setFraction(numerator, denominator);
this.reduceToLowestTerms(); //called to reduce to lowest terms
} // end constructor
public int getNumerator()
{
return num;
} // end getNumerator
public int getDenominator()
{
return den;
} // end getDenominator
public Fraction add(Fraction secondFraction)
{
// a/b + c/d is (ad + cb)/(bd)
int resultDen;
int resultNum;
resultDen = this.getDenominator()*secondFraction.getDenominator();
resultNum = (this.getNumerator()*secondFraction.getDenominator())+(secondFraction.getNumerator()*this.getDenominator());
Fraction sum = new Fraction(resultNum, resultDen);
sum.reduceToLowestTerms();
return sum;
} // end add
public Fraction subtract(Fraction secondFraction)
{
// a/b - c/d is (ad - cb)/(bd)
int resultDen;
int resultNum;
resultDen = this.getDenominator()*secondFraction.getDenominator();
resultNum = (this.getNumerator()*secondFraction.getDenominator())-(secondFraction.getNumerator()*this.getDenominator());
Fraction difference = new Fraction(resultNum, resultDen);
difference.reduceToLowestTerms();
return difference;
} // end subtract
public Fraction multiply(Fraction secondFraction)
{
// a/b * c/d is (ac)/(bd)
int resultDen;
int resultNum;
resultNum = this.getNumerator()*secondFraction.getNumerator();
resultDen = this.getDenominator()*secondFraction.getDenominator();
Fraction product = new Fraction(resultNum, resultDen);
product.reduceToLowestTerms();
return product;
} // end multiply
public Fraction divide(Fraction secondFraction)
{
// return ArithmeticException if secondFraction is 0
if(secondFraction.getNumerator()==0)
throw new ArithmeticException("Second fraction is 0");
// a/b / c/d is (ad)/(bc)
int resultDen;
int resultNum;
resultNum = this.getNumerator()*secondFraction.getDenominator();
resultDen = this.getDenominator()*secondFraction.getNumerator();
Fraction divideResult = new Fraction(resultNum, resultDen);
divideResult.reduceToLowestTerms();
return divideResult;
} // end divide
public Fraction getReciprocal()
{
// return ArithmeticException if secondFraction is 0
if(this.getNumerator()==0)
throw new ArithmeticException("Reciprocal is not valid");
return new Fraction(this.den, this.num);
} // end getReciprocal
public boolean equals(Object other)
{
// implement this method!
Fraction secondFraction = (Fraction) other;
if(this.num==secondFraction.getNumerator() && this.den==secondFraction.getDenominator()){
return true;
}
else
return false;
} // end equals
public int compareTo(Fraction other)
{
// implement this method!
double fraction = (double)num/(double)den;
double otherFraction = (double)other.getNumerator()/(double)other.getDenominator();
if(otherFraction==fraction)
return 0;
else
return (int) (otherFraction-fraction);
} // end compareTo
public String toString()
{
if(den==1){
return String.valueOf(num);
}
else
return num + "/" + den;
} // end toString
/** Task: Reduces a fraction to lowest terms. */
//-----------------------------------------------------------------
// private methods start here
//-----------------------------------------------------------------
/**
* This method sets the numerator and denominator.
* @param numerator
* @param denominator
* @throws ArithmeticException if denominator is 0
*/
private void setFraction(int numerator, int denominator)
{
// return ArithmeticException if initialDenominator is 0
if(denominator==0)
throw new ArithmeticException("Denominator can't be 0");
// handle negative denominators to make it easy
if(denominator<0){
//swap the negative sign of denominator to numerator
this.den = Math.abs(denominator);
this.num = -numerator;
}
else{
this.num = numerator;
this.den = denominator;
}
} // end setFraction
private void reduceToLowestTerms()
{
// implement this method!
//
// Outline:
// compute GCD of num & den
int gcd = greatestCommonDivisor(num, den);
// greatestCommonDivisor works for + numbers.
// So, you should eliminate - sign
gcd = Math.abs(gcd);
// then reduce numbers : num/GCD and den/GCD
this.num = num/gcd;
this.den = den/gcd;
} // end reduceToLowestTerms
/** Task: Computes the greatest common secondFraction of two integers.
* @param integerOne an integer
* @param integerTwo another integer
* @return the greatest common divisor of the two integers */
private int greatestCommonDivisor(int integerOne, int integerTwo)
{
int result;
if (integerOne % integerTwo == 0)
result = integerTwo;
else
result = greatestCommonDivisor(integerTwo, integerOne % integerTwo);
return result;
} // end greatestCommonDivisor
public static void main(String[] args) {
Fraction fraction1 = new Fraction(2, 5);
Fraction fraction2 = new Fraction(16, 22); // will be reduced to lowest terms
// Just a check
System.out.println("Fraction 1 is: "+fraction1);
System.out.println("Fraction 3 is: "+fraction2);
System.out.println("Sum of the two fractions: "+fraction1.add(fraction2));
System.out.println("Difference (fraction1-fraction2): "+fraction1.subtract(fraction2));
System.out.println("Product of the two fractions: "+fraction1.multiply(fraction2));
System.out.println("fraction1/fraction2: "+fraction1.divide(fraction2));
}
} // end Fraction
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started