Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need to write a program that adds fractions. This implements another class cal

ID: 3639729 • Letter: I

Question

I need to write a program that adds fractions. This implements another class called FractionInterface and Comparable. This class would be used by another file to do the actual operations. Any help would be appreciated.

package PJ1;

public class Fraction implements FractionInterface, Comparable<Fraction>
{
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 initialNumerator, int initialDenominator)
{
// implement this method!
} // end constructor

public Fraction(int wholeInteger)
{
// implement this method!
} // end constructor

public void setFraction(int newNumerator, int newDenominator)
{
// return ArithmeticException if initialDenominator is 0
// implement this method!
} // end setFraction

public int getNumerator()
{
// implement this method!
return 0;
} // end getNumerator

public int getDenominator()
{
// implement this method!
return 0;
} // end getDenominator

public char getSign()
{
// implement this method!
return 0;
} // end getSign

public void switchSign()
{
// implement this method!
} // change setSign

public FractionInterface add(FractionInterface operand)
{
// a/b + c/d is (ad + cb)/(bd)
// implement this method!
return null;
} // end add

public FractionInterface subtract(FractionInterface operand)
{
// a/b - c/d is (ad - cb)/(bd)
// implement this method!
return null;
} // end subtract

public FractionInterface multiply(FractionInterface multiplier)
{
// a/b * c/d is (ac)/(bd)
// implement this method!
return null;
} // end multiply

public FractionInterface divide(FractionInterface divisor)
{
// return ArithmeticException if divisor is 0
// a/b / c/d is (ad)/(bc)
// implement this method!
return null;
} // end divide

public FractionInterface getReciprocal()
{
// return ArithmeticException if divisor is 0
// implement this method!
return null;
} // end getReciprocal


public boolean equals(Object other)
{
// implement this method!
return false;
} // end equals


public int compareTo(Fraction other)
{
// implement this method!
return 0;
} // end compareTo


public String toString()
{
return num + "/" + den;
} // end toString


/** Task: Reduces a fraction to lowest terms. */

//-----------------------------------------------------------------
// private methods start here
//-----------------------------------------------------------------

private void reduceToLowestTerms()
{
// implement this method!
//
// Outline:
// compute GCD of numerator & denominator
// greatestCommonDivisor works for + numbers.
// So, you should eliminate - sign
// then reduce numeratorbers : numerator/GCD and denominator/GCD
} // end reduceToLowestTerms

/** Task: Computes the greatest common divisor 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


//-----------------------------------------------------------------
// Simple test driver is provided here

public static void main(String[] args)
{
FractionInterface firstOperand = null;
FractionInterface secondOperand = null;
FractionInterface result = null;

Fraction nineSixteenths = new Fraction(9, 16); // 9/16
Fraction Fraction(1, 4); // 1/4

// 7/8 + 9/16
firstOperand = new Fraction(7, 8);
result = firstOperand.add(nineSixteenths);
System.out.println("The sum of " + firstOperand + " and " +
nineSixteenths + " is " + result);

// 9/16 - 7/8
firstOperand = nineSixteenths;
secondOperand = new Fraction(7, 8);
result = firstOperand.subtract(secondOperand);
System.out.println("The difference of " + firstOperand +
" and " + secondOperand + " is " + result);

// 15/-2 * 1/4
firstOperand.setFraction(15, -2);
result = firstOperand.multiply(oneFourth);
System.out.println("The product of " + firstOperand +
" and " + oneFourth + " is " + result);

// (-21/2) / (3/7)
firstOperand.setFraction(-21, 2);
secondOperand.setFraction(3, 7);
result = firstOperand.divide(secondOperand);
System.out.println("The quotient of " + firstOperand +
" and " + secondOperand + " is " + result);

// -21/2 + 7/8
firstOperand.setFraction(-21, 2);
secondOperand.setFraction(7, 8);
result = firstOperand.add(secondOperand);
System.out.println("The sum of " + firstOperand +
" and " + secondOperand + " is " + result);

System.out.println();

// equality check
if (firstOperand.equals(firstOperand))
System.out.println("Identity of fractions OK");
else
System.out.println("ERROR in identity of fractions");

secondOperand.setFraction(-42, 4);
if (firstOperand.equals(secondOperand))
System.out.println("Equality of fractions OK");
else
System.out.println("ERROR in equality of fractions");

// comparison check
Fraction first = (Fraction)firstOperand;
Fraction second = (Fraction)secondOperand;

if (first.compareTo(second) == 0)
System.out.println("Fractions == operator OK");
else
System.out.println("ERROR in fractions == operator");

second.setFraction(7, 8);
if (first.compareTo(second) < 0)
System.out.println("Fractions < operator OK");
else
System.out.println("ERROR in fractions < operator");

if (second.compareTo(first) > 0)
System.out.println("Fractions > operator OK");
else
System.out.println("ERROR in fractions > operator");

System.out.println();

try {
Fraction a1 = new Fraction(1, 0);
}
catch ( ArithmeticException arithmeticException )
{
System.err.printf( " Exception: %s ", arithmeticException );
} // end catch

try {
Fraction a2 = new Fraction();
Fraction a3 = new Fraction(1, 2);
a3.divide(a2);
}
catch ( ArithmeticException arithmeticException )
{
System.err.printf( " Exception: %s ", arithmeticException );
} // end catch



} // end main
} // end Fraction

Explanation / Answer

Hi! Cramster Terms & Conditions were changed recently (I hope you have read them). Now, experts have to send the answers to askers' inbox after you rate them. That is because of people copying from older threads which have public answers. Hence we can only send the answer after you rate. So please give me a Lifesaver rating and I'll send the solution to your inbox or e-mail. You've got no other option to get the answer, because no one can give you the answer over here. Even you might be banned for rating a user that gave the answer directly on your question. You need not worry as I have the solution ready in my notebook. Hope you rate me :)