Interface
package PJ1;
public interface SimpleFractionInterface
{
/** Task: Sets a fraction to a given value.
* @param num is the integer numerator
* @param den is the integer denominator
* @throw ArithmeticException if denominator is 0 */
public void setSimpleFraction(int num, int den);
/** Task: convert a fraction to double value
* @return the double floating point value of a fraction */
public double toDouble();
/** Task: Adds two fractions.
* @param secondFraction is a fraction that is the second operand of the addition
* @return a fraction which is the sum of the invoking fraction and the secondFraction */
public SimpleFractionInterface add(SimpleFractionInterface secondFraction);
/** Task: Subtracts two fractions.
* @param secondFraction a fraction that is the second operand of the subtraction
* @return a fraction which is the difference of the invoking fraction and the second operand */
public SimpleFractionInterface subtract(SimpleFractionInterface secondFraction);
/** Task: Multiplies two fractions.
* @param secondFraction a fraction that is the second operand of the multiplication
* @return a fraction which is the product of the invoking fraction and the secondFraction*/
public SimpleFractionInterface multiply(SimpleFractionInterface secondFraction);
/** Task: Divides two fractions.
* @param secondFraction a fraction that is the second operand of the division
* @return a fraction which the quotient of the invoking fraction and the secondFraction
* @throw FractionException if secondFraction is 0 */
public SimpleFractionInterface divide(SimpleFractionInterface secondFraction);
/** Task: Get\'s the fraction\'s reciprocal
* @return the reciprocal of the invoking fraction
* @throw FractionException if the new number with denominator is 0*/
public SimpleFractionInterface getReciprocal();
}
exception class
package PJ1;
public class SimpleFractionException extends RuntimeException
{
public SimpleFractionException()
{
this(\"\");
}
public SimpleFractionException(String errorMsg)
{
super(errorMsg);
}
}
class with main method
package PJ1;
public class SimpleFraction implements SimpleFractionInterface, Comparable
{
// integer numerator and denominator
private int num;
private int den;
public SimpleFraction()
{
setSimpleFraction(0,1);// default numbers
} // end of the default constructor
public SimpleFraction(int num, int den)
{
setSimpleFraction(num, den);//
} // end constructor
public void setSimpleFraction(int num, int den)
{
if (den == 0)
throw new SimpleFractionException(\"denominator cannot be 0\");
else{
this.num= num;
this.den= den;
}
reduceSimpleFractionToLowestTerms();
} // end setSimpleFraction
public double toDouble()
{
// return double floating point value
return (double) num/den;
} // end toDouble
public SimpleFractionInterface add(SimpleFractionInterface secondFraction)
{
SimpleFraction secondFraction2;
secondFraction2 = (SimpleFraction) secondFraction;
// a/b + c/d is (ad + cb)/(bd)
int newNum = num*secondFraction2.getDen() + sec.
Interfacepackage PJ1; public interface SimpleFractionInterface.pdf
1. Interface
package PJ1;
public interface SimpleFractionInterface
{
/** Task: Sets a fraction to a given value.
* @param num is the integer numerator
* @param den is the integer denominator
* @throw ArithmeticException if denominator is 0 */
public void setSimpleFraction(int num, int den);
/** Task: convert a fraction to double value
* @return the double floating point value of a fraction */
public double toDouble();
/** Task: Adds two fractions.
* @param secondFraction is a fraction that is the second operand of the addition
* @return a fraction which is the sum of the invoking fraction and the secondFraction */
public SimpleFractionInterface add(SimpleFractionInterface secondFraction);
/** Task: Subtracts two fractions.
* @param secondFraction a fraction that is the second operand of the subtraction
* @return a fraction which is the difference of the invoking fraction and the second operand */
public SimpleFractionInterface subtract(SimpleFractionInterface secondFraction);
/** Task: Multiplies two fractions.
* @param secondFraction a fraction that is the second operand of the multiplication
* @return a fraction which is the product of the invoking fraction and the secondFraction*/
public SimpleFractionInterface multiply(SimpleFractionInterface secondFraction);
/** Task: Divides two fractions.
* @param secondFraction a fraction that is the second operand of the division
* @return a fraction which the quotient of the invoking fraction and the secondFraction
* @throw FractionException if secondFraction is 0 */
public SimpleFractionInterface divide(SimpleFractionInterface secondFraction);
/** Task: Get's the fraction's reciprocal
* @return the reciprocal of the invoking fraction
* @throw FractionException if the new number with denominator is 0*/
public SimpleFractionInterface getReciprocal();
}
exception class
2. package PJ1;
public class SimpleFractionException extends RuntimeException
{
public SimpleFractionException()
{
this("");
}
public SimpleFractionException(String errorMsg)
{
super(errorMsg);
}
}
class with main method
package PJ1;
public class SimpleFraction implements SimpleFractionInterface, Comparable
{
// integer numerator and denominator
private int num;
private int den;
public SimpleFraction()
{
setSimpleFraction(0,1);// default numbers
} // end of the default constructor
public SimpleFraction(int num, int den)
{
setSimpleFraction(num, den);//
} // end constructor
public void setSimpleFraction(int num, int den)
{
if (den == 0)
throw new SimpleFractionException("denominator cannot be 0");
else{
this.num= num;
3. this.den= den;
}
reduceSimpleFractionToLowestTerms();
} // end setSimpleFraction
public double toDouble()
{
// return double floating point value
return (double) num/den;
} // end toDouble
public SimpleFractionInterface add(SimpleFractionInterface secondFraction)
{
SimpleFraction secondFraction2;
secondFraction2 = (SimpleFraction) secondFraction;
// a/b + c/d is (ad + cb)/(bd)
int newNum = num*secondFraction2.getDen() + secondFraction2.getNum()*den;
int newDen = den*secondFraction2.getDen();
// return result which is a new SimpleFraction object
return new SimpleFraction(newNum,newDen);
} // end add
public SimpleFractionInterface subtract(SimpleFractionInterface secondFraction)
{
SimpleFraction secondFraction2;
secondFraction2 = (SimpleFraction) secondFraction;
// a/b - c/d is (ad - cb)/(bd)
int newNum = num*secondFraction2.getDen()-secondFraction2.getNum()*den;
int newDen = den*secondFraction2.getDen();
// return result which is a new SimpleFraction object
return new SimpleFraction(newNum,newDen);
} // end subtract
public SimpleFractionInterface multiply(SimpleFractionInterface secondFraction)
{
SimpleFraction secondFraction2;
secondFraction2 = (SimpleFraction) secondFraction;
// a/b * c/d is (ac)/(bd)
4. int newNum = num*secondFraction2.getNum();
int newDen = den*secondFraction2.getDen();
// return result which is a new SimpleFraction object
return new SimpleFraction(newNum,newDen);
} // end multiply
public SimpleFractionInterface divide(SimpleFractionInterface secondFraction)
{
SimpleFraction secondFraction2;
secondFraction2 = (SimpleFraction) secondFraction;
// a/b / c/d is (ad)/(bc)
// return SimpleFractionException if secondFraction is 0
if (secondFraction2.getNum() == 0)
throw new SimpleFractionException("The second fraction cannot be 0");
int newNum= num*secondFraction2.getDen();
int newDen= den*secondFraction2.getNum();
// return result which is a new SimpleFraction object
return new SimpleFraction(newNum,newDen);
} // end divide
public SimpleFractionInterface getReciprocal()
{
SimpleFraction r;
// return SimpleFractionException if secondFraction is 0
if(den == 0)
throw new SimpleFractionException("The second fraction cannot be 0");
else
r = new SimpleFraction(den,num);
// return result which is a new SimpleFraction object
return r;
} // end getReciprocal
public boolean equals(Object other)
{
boolean result = false;
if(other instanceof SimpleFraction){
SimpleFraction otherSimple = (SimpleFraction) other;
if(compareTo(otherSimple) == 0)
5. result = true;
}
return result;
} // end equals
public int compareTo(SimpleFraction other)
{
if(toDouble() == other.toDouble())
return 0;
else if(toDouble() > other.toDouble())
return 1;
else
return -1;
} // end compareTo
public String toString()
{
return num + "/" + den;
} // end toString
/** Task: Reduces a fraction to lowest terms. */
// private methods start here
private int getNum(){
return num;
}
private int getDen(){
return den;
}
private void reduceSimpleFractionToLowestTerms()
{
// Outline:
// compute GCD of num & den
// GCD works for + numbers.
// So, you should eliminate - sign
// then reduce numbers : num/GCD and den/GCD
int sign = 1; //1= positive numbers; -1 = negative numbers
6. if(num < 0){ //number negative
sign = -1*sign;
num = num*-1;
}
if (den < 0){
sign= -1*sign;
den = den*-1;
}
int gcd= GCD(num,den);
num= (num/gcd)*sign;
den= den/gcd;
} // end reduceSimpleFractionToLowestTerms
/** 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 GCD(int integerOne, int integerTwo)
{
int result;
if (integerOne % integerTwo == 0)
result = integerTwo;
else
result = GCD(integerTwo, integerOne % integerTwo);
return result;
} // end GCD
//-----------------------------------------------------------------
// Simple test is provided here
public static void main(String[] args)
{
SimpleFractionInterface firstOperand = null;
SimpleFractionInterface secondOperand = null;
SimpleFractionInterface result = null;
double doubleResult = 0.0;
SimpleFraction nineSixteenths = new SimpleFraction(9, 16); // 9/16
SimpleFraction oneFourth = new SimpleFraction(1, 4); // 1/4
7. System.out.println(" ========================================= ");
// 7/8 + 9/16
firstOperand = new SimpleFraction(7, 8);
result = firstOperand.add(nineSixteenths);
System.out.println("The sum of " + firstOperand + " and " +
nineSixteenths + " is tt" + result);
System.out.println("tExpected result :tt23/16 ");
// 9/16 - 7/8
firstOperand = nineSixteenths;
secondOperand = new SimpleFraction(7, 8);
result = firstOperand.subtract(secondOperand);
System.out.println("The difference of " + firstOperand +
" and " + secondOperand + " is t" + result);
System.out.println("tExpected result :tt-5/16 ");
// 15/-2 * 1/4
firstOperand = new SimpleFraction(15, -2);
result = firstOperand.multiply(oneFourth);
System.out.println("The product of " + firstOperand +
" and " + oneFourth + " is t" + result);
System.out.println("tExpected result :tt-15/8 ");
// (-21/2) / (3/7)
firstOperand = new SimpleFraction(-21, 2);
secondOperand= new SimpleFraction(3, 7);
result = firstOperand.divide(secondOperand);
System.out.println("The quotient of " + firstOperand +
" and " + secondOperand + " is t" + result);
System.out.println("tExpected result :tt-49/2 ");
// -21/2 + 7/8
firstOperand = new SimpleFraction(-21, 2);
secondOperand= new SimpleFraction(7, 8);
result = firstOperand.add(secondOperand);
System.out.println("The sum of " + firstOperand +
" and " + secondOperand + " is tt" + result);
System.out.println("tExpected result :tt-77/8 ");
// 0/10, 5/(-15), (-22)/7
firstOperand = new SimpleFraction(0, 10);
8. doubleResult = firstOperand.toDouble();
System.out.println("The double floating point value of " + firstOperand + " is t" +
doubleResult);
System.out.println("tExpected result ttt0.0 ");
firstOperand = new SimpleFraction(1, -3);
doubleResult = firstOperand.toDouble();
System.out.println("The double floating point value of " + firstOperand + " is t" +
doubleResult);
System.out.println("tExpected result ttt-0.333333333... ");
firstOperand = new SimpleFraction(-22, 7);
doubleResult = firstOperand.toDouble();
System.out.println("The double floating point value of " + firstOperand + " is t" +
doubleResult);
System.out.println("tExpected result ttt-3.142857142857143");
System.out.println(" ========================================= ");
firstOperand = new SimpleFraction(-21, 2);
System.out.println("First = " + firstOperand);
// equality check
System.out.println("check First equals First: ");
if (firstOperand.equals(firstOperand))
System.out.println("Identity of fractions OK");
else
System.out.println("ERROR in identity of fractions");
secondOperand = new SimpleFraction(-42, 4);
System.out.println(" Second = " + secondOperand);
System.out.println("check First equals Second: ");
if (firstOperand.equals(secondOperand))
System.out.println("Equality of fractions OK");
else
System.out.println("ERROR in equality of fractions");
// comparison check
SimpleFraction first = (SimpleFraction)firstOperand;
SimpleFraction second = (SimpleFraction)secondOperand;
System.out.println(" check First compareTo Second: ");
if (first.compareTo(second) == 0)
9. System.out.println("SimpleFractions == operator OK");
else
System.out.println("ERROR in fractions == operator");
second = new SimpleFraction(7, 8);
System.out.println(" Second = " + second);
System.out.println("check First compareTo Second: ");
if (first.compareTo(second) < 0)
System.out.println("SimpleFractions < operator OK");
else
System.out.println("ERROR in fractions < operator");
System.out.println(" check Second compareTo First: ");
if (second.compareTo(first) > 0)
System.out.println("SimpleFractions > operator OK");
else
System.out.println("ERROR in fractions > operator");
System.out.println(" =========================================");
System.out.println(" check SimpleFractionException: 1/0");
try {
SimpleFraction a1 = new SimpleFraction(1, 0);
System.out.println("Error! No SimpleFractionException");
}
catch ( SimpleFractionException fe )
{
System.err.printf( "Exception: %s ", fe );
} // end catch
System.out.println("Expected result : SimpleFractionException! ");
System.out.println(" check SimpleFractionException: division");
try {
SimpleFraction a2 = new SimpleFraction();
SimpleFraction a3 = new SimpleFraction(1, 2);
a3.divide(a2);
System.out.println("Error! No SimpleFractionException");
}
catch ( SimpleFractionException fe )
{
System.err.printf( "Exception: %s ", fe );
10. } // end catch
System.out.println("Expected result : SimpleFractionException! ");
} // end main
} // end SimpleFraction
output:
run:
=========================================
The sum of 7/8 and 9/16 is 23/16
Expected result : 23/16
The difference of 9/16 and 7/8 is -5/16
Expected result : -5/16
The product of -15/2 and 1/4 is -15/8
Expected result : -15/8
The quotient of -21/2 and 3/7 is -49/2
Expected result : -49/2
The sum of -21/2 and 7/8 is -77/8
Expected result : -77/8
The double floating point value of 0/1 is 0.0
Expected result 0.0
The double floating point value of -1/3 is -0.3333333333333333
Expected result -0.333333333...
The double floating point value of -22/7 is -3.142857142857143
Expected result -3.142857142857143
=========================================
First = -21/2
check First equals First:
Identity of fractions OK
Second = -21/2
check First equals Second:
Equality of fractions OK
check First compareTo Second:
SimpleFractions == operator OK
Second = 7/8
check First compareTo Second:
SimpleFractions < operator OK
11. check Second compareTo First:
SimpleFractions > operator OK
=========================================
check SimpleFractionException: 1/0
Exception: PJ1.SimpleFractionException: denominator cannot be 0
Expected result : SimpleFractionException!
check SimpleFractionException: division
Exception: PJ1.SimpleFractionException: The second fraction cannot be 0
Expected result : SimpleFractionException!
BUILD SUCCESSFUL (total time: 3 seconds)
Solution
Interface
package PJ1;
public interface SimpleFractionInterface
{
/** Task: Sets a fraction to a given value.
* @param num is the integer numerator
* @param den is the integer denominator
* @throw ArithmeticException if denominator is 0 */
public void setSimpleFraction(int num, int den);
/** Task: convert a fraction to double value
* @return the double floating point value of a fraction */
public double toDouble();
/** Task: Adds two fractions.
* @param secondFraction is a fraction that is the second operand of the addition
* @return a fraction which is the sum of the invoking fraction and the secondFraction */
public SimpleFractionInterface add(SimpleFractionInterface secondFraction);
/** Task: Subtracts two fractions.
* @param secondFraction a fraction that is the second operand of the subtraction
* @return a fraction which is the difference of the invoking fraction and the second operand */
public SimpleFractionInterface subtract(SimpleFractionInterface secondFraction);
/** Task: Multiplies two fractions.
* @param secondFraction a fraction that is the second operand of the multiplication
12. * @return a fraction which is the product of the invoking fraction and the secondFraction*/
public SimpleFractionInterface multiply(SimpleFractionInterface secondFraction);
/** Task: Divides two fractions.
* @param secondFraction a fraction that is the second operand of the division
* @return a fraction which the quotient of the invoking fraction and the secondFraction
* @throw FractionException if secondFraction is 0 */
public SimpleFractionInterface divide(SimpleFractionInterface secondFraction);
/** Task: Get's the fraction's reciprocal
* @return the reciprocal of the invoking fraction
* @throw FractionException if the new number with denominator is 0*/
public SimpleFractionInterface getReciprocal();
}
exception class
package PJ1;
public class SimpleFractionException extends RuntimeException
{
public SimpleFractionException()
{
this("");
}
public SimpleFractionException(String errorMsg)
{
super(errorMsg);
}
}
class with main method
package PJ1;
public class SimpleFraction implements SimpleFractionInterface, Comparable
{
// integer numerator and denominator
private int num;
private int den;
public SimpleFraction()
{
13. setSimpleFraction(0,1);// default numbers
} // end of the default constructor
public SimpleFraction(int num, int den)
{
setSimpleFraction(num, den);//
} // end constructor
public void setSimpleFraction(int num, int den)
{
if (den == 0)
throw new SimpleFractionException("denominator cannot be 0");
else{
this.num= num;
this.den= den;
}
reduceSimpleFractionToLowestTerms();
} // end setSimpleFraction
public double toDouble()
{
// return double floating point value
return (double) num/den;
} // end toDouble
public SimpleFractionInterface add(SimpleFractionInterface secondFraction)
{
SimpleFraction secondFraction2;
secondFraction2 = (SimpleFraction) secondFraction;
// a/b + c/d is (ad + cb)/(bd)
int newNum = num*secondFraction2.getDen() + secondFraction2.getNum()*den;
int newDen = den*secondFraction2.getDen();
// return result which is a new SimpleFraction object
return new SimpleFraction(newNum,newDen);
} // end add
public SimpleFractionInterface subtract(SimpleFractionInterface secondFraction)
{
14. SimpleFraction secondFraction2;
secondFraction2 = (SimpleFraction) secondFraction;
// a/b - c/d is (ad - cb)/(bd)
int newNum = num*secondFraction2.getDen()-secondFraction2.getNum()*den;
int newDen = den*secondFraction2.getDen();
// return result which is a new SimpleFraction object
return new SimpleFraction(newNum,newDen);
} // end subtract
public SimpleFractionInterface multiply(SimpleFractionInterface secondFraction)
{
SimpleFraction secondFraction2;
secondFraction2 = (SimpleFraction) secondFraction;
// a/b * c/d is (ac)/(bd)
int newNum = num*secondFraction2.getNum();
int newDen = den*secondFraction2.getDen();
// return result which is a new SimpleFraction object
return new SimpleFraction(newNum,newDen);
} // end multiply
public SimpleFractionInterface divide(SimpleFractionInterface secondFraction)
{
SimpleFraction secondFraction2;
secondFraction2 = (SimpleFraction) secondFraction;
// a/b / c/d is (ad)/(bc)
// return SimpleFractionException if secondFraction is 0
if (secondFraction2.getNum() == 0)
throw new SimpleFractionException("The second fraction cannot be 0");
int newNum= num*secondFraction2.getDen();
int newDen= den*secondFraction2.getNum();
// return result which is a new SimpleFraction object
return new SimpleFraction(newNum,newDen);
} // end divide
public SimpleFractionInterface getReciprocal()
{
SimpleFraction r;
// return SimpleFractionException if secondFraction is 0
if(den == 0)
15. throw new SimpleFractionException("The second fraction cannot be 0");
else
r = new SimpleFraction(den,num);
// return result which is a new SimpleFraction object
return r;
} // end getReciprocal
public boolean equals(Object other)
{
boolean result = false;
if(other instanceof SimpleFraction){
SimpleFraction otherSimple = (SimpleFraction) other;
if(compareTo(otherSimple) == 0)
result = true;
}
return result;
} // end equals
public int compareTo(SimpleFraction other)
{
if(toDouble() == other.toDouble())
return 0;
else if(toDouble() > other.toDouble())
return 1;
else
return -1;
} // end compareTo
public String toString()
{
return num + "/" + den;
} // end toString
/** Task: Reduces a fraction to lowest terms. */
// private methods start here
private int getNum(){
return num;
}
16. private int getDen(){
return den;
}
private void reduceSimpleFractionToLowestTerms()
{
// Outline:
// compute GCD of num & den
// GCD works for + numbers.
// So, you should eliminate - sign
// then reduce numbers : num/GCD and den/GCD
int sign = 1; //1= positive numbers; -1 = negative numbers
if(num < 0){ //number negative
sign = -1*sign;
num = num*-1;
}
if (den < 0){
sign= -1*sign;
den = den*-1;
}
int gcd= GCD(num,den);
num= (num/gcd)*sign;
den= den/gcd;
} // end reduceSimpleFractionToLowestTerms
/** 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 GCD(int integerOne, int integerTwo)
{
int result;
if (integerOne % integerTwo == 0)
result = integerTwo;
else
17. result = GCD(integerTwo, integerOne % integerTwo);
return result;
} // end GCD
//-----------------------------------------------------------------
// Simple test is provided here
public static void main(String[] args)
{
SimpleFractionInterface firstOperand = null;
SimpleFractionInterface secondOperand = null;
SimpleFractionInterface result = null;
double doubleResult = 0.0;
SimpleFraction nineSixteenths = new SimpleFraction(9, 16); // 9/16
SimpleFraction oneFourth = new SimpleFraction(1, 4); // 1/4
System.out.println(" ========================================= ");
// 7/8 + 9/16
firstOperand = new SimpleFraction(7, 8);
result = firstOperand.add(nineSixteenths);
System.out.println("The sum of " + firstOperand + " and " +
nineSixteenths + " is tt" + result);
System.out.println("tExpected result :tt23/16 ");
// 9/16 - 7/8
firstOperand = nineSixteenths;
secondOperand = new SimpleFraction(7, 8);
result = firstOperand.subtract(secondOperand);
System.out.println("The difference of " + firstOperand +
" and " + secondOperand + " is t" + result);
System.out.println("tExpected result :tt-5/16 ");
// 15/-2 * 1/4
firstOperand = new SimpleFraction(15, -2);
result = firstOperand.multiply(oneFourth);
System.out.println("The product of " + firstOperand +
" and " + oneFourth + " is t" + result);
System.out.println("tExpected result :tt-15/8 ");
// (-21/2) / (3/7)
firstOperand = new SimpleFraction(-21, 2);
secondOperand= new SimpleFraction(3, 7);
18. result = firstOperand.divide(secondOperand);
System.out.println("The quotient of " + firstOperand +
" and " + secondOperand + " is t" + result);
System.out.println("tExpected result :tt-49/2 ");
// -21/2 + 7/8
firstOperand = new SimpleFraction(-21, 2);
secondOperand= new SimpleFraction(7, 8);
result = firstOperand.add(secondOperand);
System.out.println("The sum of " + firstOperand +
" and " + secondOperand + " is tt" + result);
System.out.println("tExpected result :tt-77/8 ");
// 0/10, 5/(-15), (-22)/7
firstOperand = new SimpleFraction(0, 10);
doubleResult = firstOperand.toDouble();
System.out.println("The double floating point value of " + firstOperand + " is t" +
doubleResult);
System.out.println("tExpected result ttt0.0 ");
firstOperand = new SimpleFraction(1, -3);
doubleResult = firstOperand.toDouble();
System.out.println("The double floating point value of " + firstOperand + " is t" +
doubleResult);
System.out.println("tExpected result ttt-0.333333333... ");
firstOperand = new SimpleFraction(-22, 7);
doubleResult = firstOperand.toDouble();
System.out.println("The double floating point value of " + firstOperand + " is t" +
doubleResult);
System.out.println("tExpected result ttt-3.142857142857143");
System.out.println(" ========================================= ");
firstOperand = new SimpleFraction(-21, 2);
System.out.println("First = " + firstOperand);
// equality check
System.out.println("check First equals First: ");
if (firstOperand.equals(firstOperand))
System.out.println("Identity of fractions OK");
else
System.out.println("ERROR in identity of fractions");
19. secondOperand = new SimpleFraction(-42, 4);
System.out.println(" Second = " + secondOperand);
System.out.println("check First equals Second: ");
if (firstOperand.equals(secondOperand))
System.out.println("Equality of fractions OK");
else
System.out.println("ERROR in equality of fractions");
// comparison check
SimpleFraction first = (SimpleFraction)firstOperand;
SimpleFraction second = (SimpleFraction)secondOperand;
System.out.println(" check First compareTo Second: ");
if (first.compareTo(second) == 0)
System.out.println("SimpleFractions == operator OK");
else
System.out.println("ERROR in fractions == operator");
second = new SimpleFraction(7, 8);
System.out.println(" Second = " + second);
System.out.println("check First compareTo Second: ");
if (first.compareTo(second) < 0)
System.out.println("SimpleFractions < operator OK");
else
System.out.println("ERROR in fractions < operator");
System.out.println(" check Second compareTo First: ");
if (second.compareTo(first) > 0)
System.out.println("SimpleFractions > operator OK");
else
System.out.println("ERROR in fractions > operator");
System.out.println(" =========================================");
System.out.println(" check SimpleFractionException: 1/0");
try {
SimpleFraction a1 = new SimpleFraction(1, 0);
System.out.println("Error! No SimpleFractionException");
}
catch ( SimpleFractionException fe )
{
20. System.err.printf( "Exception: %s ", fe );
} // end catch
System.out.println("Expected result : SimpleFractionException! ");
System.out.println(" check SimpleFractionException: division");
try {
SimpleFraction a2 = new SimpleFraction();
SimpleFraction a3 = new SimpleFraction(1, 2);
a3.divide(a2);
System.out.println("Error! No SimpleFractionException");
}
catch ( SimpleFractionException fe )
{
System.err.printf( "Exception: %s ", fe );
} // end catch
System.out.println("Expected result : SimpleFractionException! ");
} // end main
} // end SimpleFraction
output:
run:
=========================================
The sum of 7/8 and 9/16 is 23/16
Expected result : 23/16
The difference of 9/16 and 7/8 is -5/16
Expected result : -5/16
The product of -15/2 and 1/4 is -15/8
Expected result : -15/8
The quotient of -21/2 and 3/7 is -49/2
Expected result : -49/2
The sum of -21/2 and 7/8 is -77/8
Expected result : -77/8
The double floating point value of 0/1 is 0.0
Expected result 0.0
The double floating point value of -1/3 is -0.3333333333333333
Expected result -0.333333333...
The double floating point value of -22/7 is -3.142857142857143
21. Expected result -3.142857142857143
=========================================
First = -21/2
check First equals First:
Identity of fractions OK
Second = -21/2
check First equals Second:
Equality of fractions OK
check First compareTo Second:
SimpleFractions == operator OK
Second = 7/8
check First compareTo Second:
SimpleFractions < operator OK
check Second compareTo First:
SimpleFractions > operator OK
=========================================
check SimpleFractionException: 1/0
Exception: PJ1.SimpleFractionException: denominator cannot be 0
Expected result : SimpleFractionException!
check SimpleFractionException: division
Exception: PJ1.SimpleFractionException: The second fraction cannot be 0
Expected result : SimpleFractionException!
BUILD SUCCESSFUL (total time: 3 seconds)