Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

IN JAVA package PJ1; public class Fraction implements FractionInterface, Comparable { private int num; private int den; public Fraction() { setFraction(0, 1); } public Fraction(int

IN JAVA

package PJ1;

public class Fraction implements FractionInterface, Comparable { private int num; private int den; public Fraction() { setFraction(0, 1); } public Fraction(int num, int den) { setFraction(num, den); } public void setFraction(int num, int den) { this.num = num; if (den == 0) throw new Project1Exception("Denominator is 0"); this.den= den; adjustSigns(); reduceFractionToLowestTerms(); } public char getSign() { char sign; if (num >= 0) sign = '+'; else sign = '-'; return sign; } public void setSign(char sign) { num= Math.abs(num); den= Math.abs(den); if (sign == '-') num= -num; } public int getNumerator() { return num; } public int getDenominator() { return den; } public FractionInterface add(FractionInterface secondFraction) { Fraction op = (Fraction)secondFraction; int newNumerator = num * op.den + op.num * den; int newDenominator = den * op.den; Fraction r = new Fraction(newNumerator, newDenominator); return r; } public FractionInterface subtract(FractionInterface secondFraction) { Fraction op = (Fraction)secondFraction; int newNumerator = num * op.den - op.num * den; int newDenominator = den * op.den; Fraction r = new Fraction(newNumerator, newDenominator); return r; } public FractionInterface multiply(FractionInterface secondFraction) { Fraction mult = (Fraction)secondFraction; int newNumerator = num * mult.num; int newDenominator = den * mult.den; Fraction r = new Fraction(newNumerator, newDenominator); return r; } public FractionInterface divide(FractionInterface secondFraction) { Fraction div = (Fraction)secondFraction; if (div.num == 0) throw new Project1Exception("Divisor is 0"); int newNumerator = num * div.den; int newDenominator = den * div.num; Fraction r = new Fraction(newNumerator, newDenominator); return r; } public boolean equals(Object other) { boolean result; if ((other == null) || (getClass() != other.getClass())) result = false; else { Fraction otherFraction = (Fraction)other; int one = num * otherFraction.den; int two = den * otherFraction.num; result = (one == two); } return result; } public int compareTo(Fraction other) { int result = 0; int one = num * other.den; int two = den * other.num; if (one < two) result = -1; else if (one > two) result = +1; return result; } public String toString() { return num + "/" + den; } private void reduceFractionToLowestTerms() { int gcd = GCD(Math.abs(num), Math.abs(den)); num = num / gcd; den = den / gcd; } private int GCD(int integerOne, int integerTwo) { int result; if (integerOne % integerTwo == 0) result = integerTwo; else result = GCD(integerTwo, integerOne % integerTwo); return result; } private void adjustSigns() { if (((num > 0) && (den < 0)) || ((num < 0) && (den < 0))) { num = -num; den = -den; } } }

package PJ1; /* This file specifies methods for FractionInterface */ /* Do not modify this file! */

public interface FractionInterface { /** Task: Sets "this" fraction to a given value. * @param num is the integer numerator * @param den is the integer denominator * @throw Project1Exception if denominator is 0 */ public void setFraction(int num, int den);

/** Task: Gets the fraction's numerator. * @return the fraction's numerator */ public int getNumerator();

/** Task: Gets the fraction's denominator. * @return the fraction's denominator */ public int getDenominator();

/** Task: Gets the fraction's sign. * @return the fraction's sign */ public char getSign();

/** Task: Sets the numerator's sign to the fraction's sign, * and sets the denominator's sign to +. * @param sign a character that represents the fraction's sign */ public void setSign(char sign);

/** Task: Adds two fractions. * @param secondFraction is a fraction that is the second operand of the addition * @return a new fraction which is the sum of "this" fraction and the secondFraction * Note: do not reduce the returning fraction */ public FractionInterface add(FractionInterface secondFraction);

/** Task: Subtracts two fractions. * @param secondFraction a fraction that is the second operand of the subtraction * @return a new fraction which is the difference of "this" fraction and the second operand * Note: do not reduce the returning fraction */ public FractionInterface subtract(FractionInterface secondFraction);

/** Task: Multiplies two fractions. * @param secondFraction a fraction that is the second operand of the multiplication * @return a new fraction which is the product of "this" fraction and the secondFraction * Note: do not reduce the returning fraction */ public FractionInterface multiply(FractionInterface secondFraction);

/** Task: Divides two fractions. * @param secondFraction a fraction that is the second operand of the division * @return a new fraction which the quotient of "this" fraction and the secondFraction * Note: do not reduce the returning fraction * @throw Project1Exception if secondFraction is 0 */ public FractionInterface divide(FractionInterface secondFraction);

}

package PJ1;

/** ********************************************************************************* * * This class represents a mixed number which consist of sign (+ or -),integer * and fraction parts of a number. Example: -10 3/5, 0 1/2, -0 3/4, 4 5/6 * * Requirements: * 1. Implement interfaces: MixedNumberInterface and Comparable (i.e. compareTo()) * 2. Implement methods equals() and toString() from class Object * 3. Must work for both positive and negative mixed numbers * Example: -3 5/6, -3 -5/-6, 0 -4/5, 0 4/-5 * are valid mixed numbers, all with sign '-' * -3 -5/6, 3 -4/5, 3 4/-5 are invalid mixed numbers * 4. Must reduce to mixed number to lowest term, e.g. -3 14/4 --> -6 1/2 * 5. Must reduce result mixed number to lowest term for operations * add, subtract, multiply and divide, e.g. see test cases * 6. For input such as -2 -3/-10, 2 -3/-10 and 0 -4/-5 must convert them to * -2 3/10, 2 3/10 and 0 4/5 respectively * 7. Must throw only Project1Exception in case of errors or invalid mixed numbers * 8. Must not add new or modify existing data fields * 9. Must not add new public methods * 10.May add private methods * * Hints: * * 1. You need to downcast reference parameter MixedNumberInterface to * MixedNumber if you want to use it as MixedNumber. * See add, subtract, multiply and divide methods * * 2. You need to downcast reference parameter FractionInterface to Fraction if * you want to use it as Fraction. * * 3. Use "this" to access this object if it is needed * * 4. Use given Fraction class to simplify MixedNumber class implementations * 4.1 Fraction class always reduce fraction object to lowest term. * 12/8 --> 3/2 * 4.2 Fraction class always set denominator to > 0 and * numerator to +/- values. Example: 3/-2 or -3/2 --> -3/2 * 4.3 Look at Fraction interface for operations * 4.4 Additional Operations: * * public Fraction() * public Fraction(int num, int den) * public boolean equals(Object other) * public int compareTo(Fraction other) * public String toString() * ************************************************************************************/

public class MixedNumber implements MixedNumberInterface, Comparable { // Fields:

// both integer and fraction parts are forced to >= 0 // sign of a mixed number is stored as '+' or '-' private char sign; // '+' or '-' private int intPart; // whole number portion >= 0 private FractionInterface fracPart; // fraction portion in lowest terms >= 0

// Methods:

public MixedNumber() { setMixedNumber(0, 0, 1); } // end default constructor

public MixedNumber(int integerPart, int fracPartNumerator, int fracPartDenominator) { setMixedNumber(integerPart, fracPartNumerator, fracPartDenominator); } // end constructor

public MixedNumber(int integerPart, FractionInterface fractionPart) { setMixedNumber(integerPart, fractionPart); } // end constructor

public void setMixedNumber(int integerPart, FractionInterface fractionPart) { // add statements // set this object to the given values // make sure to reduce to lowest term // check for exception cases } // end setMixedNumber

// check for exception cases public void setMixedNumber(int integerPart, int fracPartNumerator, int fracPartDenominator) { // add statements // set this object to the given values // make sure to reduce to lowest term // check for exception cases } // end setMixedNumber

public int getIntegerPart() { // add statements // retrieve integer portion with correct (+ or -) sign return 0; } // end getInteger

public FractionInterface getFractionPart() { // add statements // retrieve fraction portion, always + sign return null; } // end getFraction

public MixedNumberInterface addMixedNumber(MixedNumberInterface operand) { // add statements // convert MixedNumber object to Fraction object // Use Fraction's add() method to obtain Fraction result // convert result to a new lowest term MixedNumber object // hint: return new MixedNumber(0,result); return null; // change it } // end add

public MixedNumberInterface subtractMixedNumber(MixedNumberInterface operand) { // add statements // convert MixedNumber object to Fraction object // Use Fraction's substrct() method to obtain Fraction result // convert result to a new lowest term MixedNumber object // hint: return new MixedNumber(0,result); return null; // change it } // end subtract

public MixedNumberInterface multiplyMixedNumber(MixedNumberInterface operand) { // add statements // convert MixedNumber objects to Fraction objects // Use Fraction's multiply() method to obtain Fraction result // convert result to lowest term MixedNumber object // hint: return new MixedNumber(0,result); return null; // change it } // end multiply

public MixedNumberInterface divideMixedNumber(MixedNumberInterface operand) { // add statements // convert MixedNumber objects to Fraction objects // Use Fraction's divide() method to obtain Fraction result // convert result to lowest term MixedNumber object // hint: return new MixedNumber(0,result); return null; // change it } // end divide

public boolean equals(Object other) { // add statements // possible solution: // convert MixedNumber objects to Fraction objects // Use Fraction's equals() method to obtain boolean result return false; // change it } // end if

public int compareTo(MixedNumber other) { // add statements // possible solution: // convert MixedNumber objects to Fraction objects // Use Fraction's compareTo() method to obtain result return 0; // change it } // end compareTo

public String toString() { // possible solution: // together with sign, integer and Fraction's toString() method // to obtain string value // add statements return null; // change it } // end toString

// Useful private methods:

// reduce this MixedNumber object to lowest term MixedNumber // object. E.g. 0 -50/7 --> -7 1/7; 4 25/8 --> 7 1/8 private void reduceToLowestForm() { // add statements } // end reduceToLowestForm

// convert this MixedNumber object to a new Fraction object // object. E.g. -7 1/7 --> -50/7; 3 1/8 --> 25/8 private FractionInterface getFractionalEquivalent() { // add statements return null; } // end getFractionalEquivalent

} // end MixedNumber

/** * This file specifies methods for MixedNumberInterface. * All result mixed numbers must be reduced to lowest term * Do not modify this file! */

package PJ1;

public interface MixedNumberInterface { /** Task: Sets a MixedNumber to a given value. * @param integerPart the whole number * @param fractioPart the fraction */ public void setMixedNumber(int integerPart, FractionInterface fractionPart);

/** Task: Sets a MixedNumber to a given value. * @param integerPart the whole number * @param fractionNumerator the integer numerator * @param fractionDenominator the integer denominator */ public void setMixedNumber(int integerPart, int fractionNumerator, int fractionDenominator);

/** Task: Gets the MixedNumber's whole part. * @return the MixedNumber's whole part */ public int getIntegerPart();

/** Task: Gets the MixedNumber's fraction * @return the MixedNumber's fraction */ public FractionInterface getFractionPart();

/** Task: Adds two MixedNumbers. * @param operand a MixedNumber that is the second operand of the addition * @return the sum of the invoking MixedNumber and the second operand */ public MixedNumberInterface addMixedNumber(MixedNumberInterface operand);

/** Task: Subtracts two MixedNumbers. * @param operand a MixedNumber that is the second operand of the subtraction * @return the difference of the invoking MixedNumber and the second operand */ public MixedNumberInterface subtractMixedNumber(MixedNumberInterface operand);

/** Task: Multiplie two MixedNumbers. * @param operand a MixedNumber that is the second operand of the multiplication * @return the product of the invoking MixedNumber and the second operand */ public MixedNumberInterface multiplyMixedNumber(MixedNumberInterface operand);

/** Task: Divides two MixedNumbers. * @param operand a MixedNumber that is the second operand of the division * @return the quotient of the invoking MixedNumber and the second operand */ public MixedNumberInterface divideMixedNumber(MixedNumberInterface operand);

}

/************************************************************************************ * Do not modify this file Project1Exception class. * It is used by Fraction and MixedNumber classes *************************************************************************************/

package PJ1;

public class Project1Exception extends RuntimeException { public Project1Exception() { this(""); }

public Project1Exception(String errorMsg) { super(errorMsg); }

}

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

Students also viewed these Databases questions

Question

Understand the different approaches to job design. page 167

Answered: 1 week ago