Question: I need help with the following Java code Consider a class Fraction of fractions. Each fraction is signed and has a numerator and a denominator

I need help with the following Java code

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.

At this stage, design, but do not implement, the class Fraction. Begin by listing all the needed operations. Then write a Java interface, named FractionInterface, that declares each public method. This interface needs to declare following methods: setFraction, getNumerator, getDenominator, add, subtract, multiply, divide, and getReciprocal. Include javadoc-style comments to specify each method.

Now, write a Java class Fraction that implements the FractionInterface you designed. Begin with reasonable constructors. Design and implement useful private methods, if needed, and include comments that specify them.

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 divisor. The greatest common divisor 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.

Finally, your Fraction class needs to extend java.lang.Number class and implement java.lang.Comparable interface (in addition to FractionInterface . It will also need to override the toString and equals methods of Object class.

This is what I have so far, i need help with the areas marked to do. As well as my lowestTerms method, this method is not working at all. One problem I am having is that some methods have to return a fraction but the answer should return as 0. For example, in the get reciprocal method, if the numerator is 0 the result of the method should be 0, but 0 is not a fraction so.

/**

* Fraction class that performs basic math operations such

* as add, subtract, multiply, and divide. The class can

* also get the reciprocal of a fraction, compare two fractions,

* determine if two fractions are equal, and obtain the string form

* of a fraction. The class can handle denominators of 0, negative

* numbers and improper fractions.

*

*/

public class Fraction extends java.lang.Number implements FractionInterface, java.lang.Comparable

{

/**

* Data field for the numerator

*/

private int numerator;

/**

* Data field for the denominator

*/

private int denominator;

/**

* No arg constructor that sets the numerator

* and denominator to 0.

*/

public Fraction()

{

this.numerator = 0;

this.denominator = 0;

}

/**

* Constructor that initializes the given numbers to the

* numerator and denominator for the fraction

* @param numerator

* @param denominator

*/

public Fraction(int givenNumerator, int givenDenominator)

{

setFraction(givenNumerator, givenDenominator);

}

/**

* Constructor that takes a fraction object as a parameter

* @param newFraction

*/

public Fraction (Fraction newFraction)

{

this.numerator = newFraction.numerator;

this.denominator = newFraction.denominator;

setFraction(numerator,denominator);

}

/**

* Sets a fraction

* @param numerator

* @param denominator

*/

public void setFraction(int numerator, int denominator)

{

this.numerator = numerator;

if (denominator == 0)

{

throw new IllegalFractionException("Denominator can not be zero");

}

else

{

this.denominator = denominator;

}

this.adjustSign();

this.lowestTerms();

}

/**

* Determines the greatest common divisor between two integers

* @param a

* @param b

* @return an integer that is the gcd

*/

private int gcd(int a, int b)

{

int result;

if (a % b == 0)

{

result = b;

}

else

{

result = gcd(b, a % b);

}

return result;

}

/**

* Puts the fraction into its lowest terms using the gcd method

* @return the lowest terms of a fraction

*/

private Fraction lowestTerms()

{

Fraction newFraction = new Fraction(numerator, denominator);

if (numerator == 0)

{

newFraction = new Fraction(numerator, denominator);

return newFraction;

}

else

{

int GCD = gcd(Math.abs(numerator), Math.abs(denominator));

numerator = this.getNumerator() / GCD;

denominator = this.getDenominator() / GCD;

newFraction = new Fraction(numerator, denominator);

}

return newFraction;

}

/**

* Obtains the numerator

* @return numerator

*/

public int getNumerator()

{

return numerator;

}

/**

* Obtains the denominator

* @return denominator

*/

public int getDenominator()

{

return denominator;

}

/**

* Sets the numerator to the given value

* @param newNumerator

* @return the value of the numerator

*/

public int setNumerator(int newNumerator)

{

this.numerator = newNumerator;

lowestTerms();

return numerator;

}

public int setDenominator(int newDenominator)

{

if (denominator == 0)

{

throw new IllegalFractionException("Denominator can not be zero");

}

else

{

this.denominator = newDenominator;

}

adjustSign();

lowestTerms();

return denominator;

}

/**

* Obtains the fraction's sign.

* @return the fraction's sign

*/

public char getSign()

{

char sign;

this.adjustSign();

if (numerator >= 0)

{

sign = '+';

}

else

{

sign = '-';

}

return sign;

}

/**

* Sets the numerator's sign to the fraction's sign

* and sets the denominator's sign to positive

*/

public void adjustSign()

{

if (denominator < 0)

{

numerator = numerator * -1;

denominator = denominator * -1;

}

else

{

return;

}

}

/**

* Adds two fractions together

* @param

*/

public FractionInterface add(FractionInterface otherFraction)

{

Fraction othFra = (Fraction) otherFraction;

Fraction sum;

this.adjustSign();

othFra.adjustSign();

this.lowestTerms();

othFra.lowestTerms();

if ((this.numerator == 0) && (othFra.numerator == 0))

{

sum = new Fraction(); //TODO should return 0

}

else

{

int newNumerator = this.numerator * othFra.denominator + othFra.numerator * this.denominator;

int newDenominator = this.denominator * othFra.denominator;

sum = new Fraction(newNumerator, newDenominator);

}

return sum;

}

/**

* Subtracts two fractions

* @param

*/

public FractionInterface subtract(FractionInterface otherFraction)

{

Fraction othFra = (Fraction) otherFraction;

Fraction difference;

this.adjustSign();

othFra.adjustSign();

this.lowestTerms();

othFra.lowestTerms();

if ((this.numerator == 0) && (othFra.numerator == 0))

{

difference = new Fraction(); //TODO

}

else

{

int newNumerator = this.numerator * othFra.denominator - othFra.numerator * this.denominator;

int newDenominator = this.denominator * othFra.denominator;

difference = new Fraction(newNumerator, newDenominator);

}

return difference;

}

/**

* Multiples two fractions together

* @param

*/

public FractionInterface multiply(FractionInterface otherFraction)

{

Fraction othFra = (Fraction) otherFraction;

Fraction product;

this.adjustSign();

othFra.adjustSign();

this.lowestTerms();

othFra.lowestTerms();

if ((this.numerator == 0) && (othFra.numerator == 0))

{

product = new Fraction(); //TODO

}

else

{

int newNumerator = this.numerator * othFra.numerator;

int newDenominator = this.denominator * othFra.denominator;

if (newNumerator == 0)

{

product = new Fraction(); //TODO

}

else

{

product = new Fraction(newNumerator, newDenominator);

}

}

return product;

}

/**

* Divides two fractions

* @param

*/

public FractionInterface divide(FractionInterface otherFraction)

{

Fraction othFra = (Fraction) otherFraction;

Fraction quotient;

this.adjustSign();

othFra.adjustSign();

this.lowestTerms();

othFra.lowestTerms();

if (othFra.numerator == 0)

{

throw new IllegalFractionException("Divisor is 0");

}

else if (this.numerator == 0)

{

quotient = new Fraction(); //TODO

}

else

{

int newNumerator = numerator * othFra.denominator;

int newDenominator = denominator * othFra.numerator;

quotient = new Fraction(newNumerator, newDenominator);

}

return quotient;

}

/**

* Obtains the reciprocal of a fraction

* @return

*/

public Fraction getReciprocal()

{

Fraction reciprocal;

if(numerator == 0)

{

reciprocal = new Fraction(); //TODO this should return 0, but since

//it has to return a fraction i dnt know what to do

}

else

{

reciprocal = new Fraction(denominator, numerator);

}

return reciprocal;

}

/**

* Compares two fractions together and returns 0 if the

* fractions are the same, return negative or positive if

* the fraction is smaller or larger than the other fraction

* @param obj

* @return integer that represents result

*/

@Override

public int compareTo(Fraction obj)

{

int result = 100;

this.adjustSign();

this.lowestTerms();

obj.adjustSign();

this.lowestTerms();

obj.lowestTerms();

if ((numerator == obj.numerator) && (denominator == obj.denominator))

{

result = 0;

}

else

{

int one = numerator * obj.denominator;

int two = obj.numerator * denominator;

result = one - two;

}

return result;

}

/**

* Determines if two fractions are equal to each other

* @param obj

* @return true if the fractions are equal

*/

@Override

public boolean equals(Object obj)

{

boolean result = false;

if(this == obj)

{

result = true;

}

if ((obj == null) || (obj.getClass() != this.getClass()))

{

result = false;

}

Fraction otherFraction = (Fraction) obj;

this.lowestTerms();

this.adjustSign();

otherFraction.lowestTerms();

otherFraction.adjustSign();

if ((numerator == otherFraction.numerator) &&

(denominator == otherFraction.denominator));

{

result = true;

}

return result;

}

/**

* Converts a Fraction to a string

* @return string form of a fraction

*/

@Override

public String toString()

{

this.adjustSign();

this.lowestTerms();

return this.getNumerator() + "/" + this.getDenominator();

}

/**

* This method returns the value of the specified number as a int

* @return the value as an int

*/

@Override

public int intValue()

{

int i = denominator;

if (denominator == 0)

{

i = 0;

}

else

{

i = (int) numerator / denominator;

}

return i;

}

/**

* This method returns the value of the specified number as a long

* @return the value as a long

*/

@Override

public long longValue()

{

long i = denominator;

if (denominator == 0)

{

i = 0;

}

else

{

i = (long) numerator / denominator;

}

return i;

}

/**

* This method returns the value of the specified number as a float

* @return the value as a float

*/

@Override

public float floatValue()

{

float i = denominator;

if (denominator == 0)

{

i = 0;

}

else

{

i = (float) numerator / denominator;

}

return i;

}

/**

* This method returns the value of the specified number as a double

* @return the value as a double

*/

@Override

public double doubleValue()

{

double i = denominator;

if (denominator == 0)

{

i = 0;

}

else

{

i = (double) numerator / denominator;

}

return i;

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!