Question
Question: Implement MixedNumber class FRACTION.JAVA package PJ1; public class Fraction implements FractionI... implement MixedNumber class FRACTION.JAVA package PJ1; public class Fraction implements FractionInterface, Comparable {
Question: Implement MixedNumber class FRACTION.JAVA package PJ1; public class Fraction implements FractionI...
implement MixedNumber class
FRACTION.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; } } }
fractioninterface.java
/* This file specifies methods for FractionInterface */ /* Do not modify this file! */
package PJ1;
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 */
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 */
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 */
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 * @throw Project1Exception if secondFraction is 0 */ public FractionInterface divide(FractionInterface secondFraction);
}
MixedNumber.java
/** ********************************************************************************* * * 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. Must use the given Fraction and FractionInterface to store fraction part * 2. Implement interfaces: MixedNumberInterface and Comparable (i.e. compareTo()) * 3. Implement methods equals() and toString() from class Object * 4. Must work for both positive and negative mixed numbers * 5. Must reduce to mixed number to lowest term, e.g. 3 22/4 --> 8 1/2 * 6. Must reduce result mixed number to lowest term for operations * add(), subtract(), multiply() and divide(), e.g. see test cases * 7. For input 2 3/-10 & 2 -3/-10, must convert them to * -2 3/10 & 2 3/10 respectively (see Hint 2. below) * 8. Must throw only Project1Exception in case of errors * 9. Must not add new or modify existing data fields * 10.Must not add new or modify existing public methods * 11.May add private methods * * More info: * * 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. The following private methods will be useful * * // Private method: reduce a MixedNumber object to lowest term * // E.g. -3 50/7 --> -10 1/7; 3 25/10 --> 5 1/2 * private void reduceToLowestForm() * * // Private method: convert a MixedNumber to new Fraction object * // object. E.g. -7 1/7 --> -50/7; 3 1/8 --> 25/8 * private FractionInterface getFractionalEquivalent() * * 3. Use "this" to access this object if it is needed * * 4. You need to downcast reference parameter FractionInterface to Fraction if * you want to use it as Fraction. * * 5. More Fraction class info * * It works for both positive and negative fractions * For input 3/-10 & -3/-10, they are converted to -3/10 & 3/10 respectively * All fraction objects are reduced to lowest term, e.g. 10/20 --> 1/2 * Additional methods in Fraction: * * public Fraction() // default value: 0/1 * public Fraction(int num, int den) * public boolean equals(Object other) * public int compareTo(Fraction other) * public String toString() // e.g. "3/5" or "-10/11" * ************************************************************************************/
package PJ1;
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 integer; // whole number portion >= 0 private FractionInterface fraction; // fractional portion in lowest terms;
// Methods:
public MixedNumber() { setMixedNumber(0, 0, 1); } // end default constructor
public MixedNumber(int integerPart, int fractionNumerator, int fractionDenominator) { setMixedNumber(integerPart, fractionNumerator, fractionDenominator); } // 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
public void setMixedNumber(int integerPart, int fractionNumerator, int fractionDenominator) { // 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 wth the correct sign return 0; // change this } // end getInteger
public FractionInterface getFractionPart() { // retrieve fraction portion, assume + value return fraction; } // end getFraction
public MixedNumberInterface add(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 subtract(MixedNumberInterface operand) { // add statements // convert MixedNumber object to Fraction object // Use Fraction's substract() 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 multiply(MixedNumberInterface multiplier) { // add statements // convert MixedNumber object to Fraction object // Use Fraction's multiply() 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 multiply
public MixedNumberInterface divide(MixedNumberInterface divisor) { // add statements // convert MixedNumber object to Fraction object // Use Fraction's divide() method to obtain Fraction result // convert result to a new lowest term MixedNumber object // hint: return new MixedNumber(0,result); // check for exception cases 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 equals
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
// Private method: reduce a MixedNumber object to lowest term // E.g. -3 50/7 --> -10 1/7; 3 25/10 --> 5 1/2 private void reduceToLowestForm() { // add statements }
// Private method: convert a MixedNumber to new Fraction object // object. E.g. -7 1/7 --> -50/7; 3 1/8 --> 25/8 private FractionInterface getFractionalEquivalent() { // add statements return null; // changeit }
// You may add more private methods
} // end MixedNumber
mixednumberinterface.java
/** * This file specifies methods for MixedNumberInterface. * 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 add(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 subtract(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 multiply(MixedNumberInterface multiplier);
/** 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 divide(MixedNumberInterface divisor);
}
Project1exception.java
/************************************************************************************ * Do not modify this file. * Project1Exception class. *************************************************************************************/
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
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