SlideShare a Scribd company logo
1 of 21
Download to read offline
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() + 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)
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)
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
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
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);
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)
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 );
} // 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
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
* @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() + 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)
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)
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
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
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);
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)
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 );
} // 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
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)

More Related Content

Similar to Interfacepackage PJ1; public interface SimpleFractionInterface.pdf

public class Point {   Insert your name here    private dou.pdf
public class Point {    Insert your name here    private dou.pdfpublic class Point {    Insert your name here    private dou.pdf
public class Point {   Insert your name here    private dou.pdfanandshingavi23
 
Fraction.h #include iostream #ifndef FRACTION #define FR.pdf
Fraction.h #include iostream #ifndef FRACTION #define FR.pdfFraction.h #include iostream #ifndef FRACTION #define FR.pdf
Fraction.h #include iostream #ifndef FRACTION #define FR.pdfravikapoorindia
 
operating system Linux,ubuntu,Mac#include stdio.h #include .pdf
operating system Linux,ubuntu,Mac#include stdio.h #include .pdfoperating system Linux,ubuntu,Mac#include stdio.h #include .pdf
operating system Linux,ubuntu,Mac#include stdio.h #include .pdfaquazac
 
fraction_math.c for Project 5 Program Design fraction.pdf
fraction_math.c for Project 5  Program Design  fraction.pdffraction_math.c for Project 5  Program Design  fraction.pdf
fraction_math.c for Project 5 Program Design fraction.pdfanjanadistribution
 
Please read the comment ins codeExpressionTree.java-------------.pdf
Please read the comment ins codeExpressionTree.java-------------.pdfPlease read the comment ins codeExpressionTree.java-------------.pdf
Please read the comment ins codeExpressionTree.java-------------.pdfshanki7
 
write the To Dos to get the exact outputNOte A valid Fraction .pdf
write the To Dos to get the exact outputNOte A valid Fraction .pdfwrite the To Dos to get the exact outputNOte A valid Fraction .pdf
write the To Dos to get the exact outputNOte A valid Fraction .pdfjyothimuppasani1
 
Please do Part A, Ill be really gratefulThe main.c is the skeleto.pdf
Please do Part A, Ill be really gratefulThe main.c is the skeleto.pdfPlease do Part A, Ill be really gratefulThe main.c is the skeleto.pdf
Please do Part A, Ill be really gratefulThe main.c is the skeleto.pdfaioils
 
mainpublic class AssignmentThree {    public static void ma.pdf
mainpublic class AssignmentThree {    public static void ma.pdfmainpublic class AssignmentThree {    public static void ma.pdf
mainpublic class AssignmentThree {    public static void ma.pdffathimafancyjeweller
 
Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...Mario Fusco
 
can someone fix the errors in this code- the name needs to be Fraction.pdf
can someone fix the errors in this code- the name needs to be Fraction.pdfcan someone fix the errors in this code- the name needs to be Fraction.pdf
can someone fix the errors in this code- the name needs to be Fraction.pdfvinaythemodel
 
C++ AssignmentPlease read all the requirements and the overloading.pdf
C++ AssignmentPlease read all the requirements and the overloading.pdfC++ AssignmentPlease read all the requirements and the overloading.pdf
C++ AssignmentPlease read all the requirements and the overloading.pdflakshmijewellery
 
Here is the code with comments to solve the question. Please do rate.pdf
Here is the code with comments to solve the question. Please do rate.pdfHere is the code with comments to solve the question. Please do rate.pdf
Here is the code with comments to solve the question. Please do rate.pdfangelfragranc
 
C# Program. Create a Windows application that has the functionality .pdf
C# Program. Create a Windows application that has the functionality .pdfC# Program. Create a Windows application that has the functionality .pdf
C# Program. Create a Windows application that has the functionality .pdffathimalinks
 
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docxPROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docxamrit47
 
PROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdf
PROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdfPROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdf
PROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdfclimatecontrolsv
 
CSC-1106 Homework 09 (Class and static methods) Due Tuesday, Novembe.pdf
CSC-1106 Homework 09 (Class and static methods) Due Tuesday, Novembe.pdfCSC-1106 Homework 09 (Class and static methods) Due Tuesday, Novembe.pdf
CSC-1106 Homework 09 (Class and static methods) Due Tuesday, Novembe.pdfmarketing413921
 
ANSimport java.util.Scanner; class Bina_node { Bina_node .pdf
ANSimport java.util.Scanner; class Bina_node { Bina_node .pdfANSimport java.util.Scanner; class Bina_node { Bina_node .pdf
ANSimport java.util.Scanner; class Bina_node { Bina_node .pdfanukoolelectronics
 

Similar to Interfacepackage PJ1; public interface SimpleFractionInterface.pdf (20)

public class Point {   Insert your name here    private dou.pdf
public class Point {    Insert your name here    private dou.pdfpublic class Point {    Insert your name here    private dou.pdf
public class Point {   Insert your name here    private dou.pdf
 
Fraction.h #include iostream #ifndef FRACTION #define FR.pdf
Fraction.h #include iostream #ifndef FRACTION #define FR.pdfFraction.h #include iostream #ifndef FRACTION #define FR.pdf
Fraction.h #include iostream #ifndef FRACTION #define FR.pdf
 
operating system Linux,ubuntu,Mac#include stdio.h #include .pdf
operating system Linux,ubuntu,Mac#include stdio.h #include .pdfoperating system Linux,ubuntu,Mac#include stdio.h #include .pdf
operating system Linux,ubuntu,Mac#include stdio.h #include .pdf
 
fraction_math.c for Project 5 Program Design fraction.pdf
fraction_math.c for Project 5  Program Design  fraction.pdffraction_math.c for Project 5  Program Design  fraction.pdf
fraction_math.c for Project 5 Program Design fraction.pdf
 
Please read the comment ins codeExpressionTree.java-------------.pdf
Please read the comment ins codeExpressionTree.java-------------.pdfPlease read the comment ins codeExpressionTree.java-------------.pdf
Please read the comment ins codeExpressionTree.java-------------.pdf
 
write the To Dos to get the exact outputNOte A valid Fraction .pdf
write the To Dos to get the exact outputNOte A valid Fraction .pdfwrite the To Dos to get the exact outputNOte A valid Fraction .pdf
write the To Dos to get the exact outputNOte A valid Fraction .pdf
 
Please do Part A, Ill be really gratefulThe main.c is the skeleto.pdf
Please do Part A, Ill be really gratefulThe main.c is the skeleto.pdfPlease do Part A, Ill be really gratefulThe main.c is the skeleto.pdf
Please do Part A, Ill be really gratefulThe main.c is the skeleto.pdf
 
mainpublic class AssignmentThree {    public static void ma.pdf
mainpublic class AssignmentThree {    public static void ma.pdfmainpublic class AssignmentThree {    public static void ma.pdf
mainpublic class AssignmentThree {    public static void ma.pdf
 
Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...
 
Stack prgs
Stack prgsStack prgs
Stack prgs
 
can someone fix the errors in this code- the name needs to be Fraction.pdf
can someone fix the errors in this code- the name needs to be Fraction.pdfcan someone fix the errors in this code- the name needs to be Fraction.pdf
can someone fix the errors in this code- the name needs to be Fraction.pdf
 
C++ aptitude
C++ aptitudeC++ aptitude
C++ aptitude
 
C++ AssignmentPlease read all the requirements and the overloading.pdf
C++ AssignmentPlease read all the requirements and the overloading.pdfC++ AssignmentPlease read all the requirements and the overloading.pdf
C++ AssignmentPlease read all the requirements and the overloading.pdf
 
Here is the code with comments to solve the question. Please do rate.pdf
Here is the code with comments to solve the question. Please do rate.pdfHere is the code with comments to solve the question. Please do rate.pdf
Here is the code with comments to solve the question. Please do rate.pdf
 
C# Program. Create a Windows application that has the functionality .pdf
C# Program. Create a Windows application that has the functionality .pdfC# Program. Create a Windows application that has the functionality .pdf
C# Program. Create a Windows application that has the functionality .pdf
 
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docxPROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
 
PROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdf
PROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdfPROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdf
PROBLEM STATEMENTIn this assignment, you will complete DoubleEnde.pdf
 
CSC-1106 Homework 09 (Class and static methods) Due Tuesday, Novembe.pdf
CSC-1106 Homework 09 (Class and static methods) Due Tuesday, Novembe.pdfCSC-1106 Homework 09 (Class and static methods) Due Tuesday, Novembe.pdf
CSC-1106 Homework 09 (Class and static methods) Due Tuesday, Novembe.pdf
 
C++ TUTORIAL 4
C++ TUTORIAL 4C++ TUTORIAL 4
C++ TUTORIAL 4
 
ANSimport java.util.Scanner; class Bina_node { Bina_node .pdf
ANSimport java.util.Scanner; class Bina_node { Bina_node .pdfANSimport java.util.Scanner; class Bina_node { Bina_node .pdf
ANSimport java.util.Scanner; class Bina_node { Bina_node .pdf
 

More from sutharbharat59

1. Translation program#includestdio.h#includeconio.h#incl.pdf
1. Translation program#includestdio.h#includeconio.h#incl.pdf1. Translation program#includestdio.h#includeconio.h#incl.pdf
1. Translation program#includestdio.h#includeconio.h#incl.pdfsutharbharat59
 
1A. The acronym DBLC is used to name the Database Life Cycle.The DBL.pdf
1A. The acronym DBLC is used to name the Database Life Cycle.The DBL.pdf1A. The acronym DBLC is used to name the Database Life Cycle.The DBL.pdf
1A. The acronym DBLC is used to name the Database Life Cycle.The DBL.pdfsutharbharat59
 
In chemistry, hybridisation (or hybridization) is.pdf
                     In chemistry, hybridisation (or hybridization) is.pdf                     In chemistry, hybridisation (or hybridization) is.pdf
In chemistry, hybridisation (or hybridization) is.pdfsutharbharat59
 
RPS 19 stands for ribosomal protein S19. This proteins is synthesise.pdf
RPS 19 stands for ribosomal protein S19. This proteins is synthesise.pdfRPS 19 stands for ribosomal protein S19. This proteins is synthesise.pdf
RPS 19 stands for ribosomal protein S19. This proteins is synthesise.pdfsutharbharat59
 
y^(iv)-4y=0taking 4y to RHS we gety^(iv) = 4yy^(iii) = integra.pdf
y^(iv)-4y=0taking 4y to RHS we gety^(iv) = 4yy^(iii) = integra.pdfy^(iv)-4y=0taking 4y to RHS we gety^(iv) = 4yy^(iii) = integra.pdf
y^(iv)-4y=0taking 4y to RHS we gety^(iv) = 4yy^(iii) = integra.pdfsutharbharat59
 
 Fe(s) + Mn2+(aq) Fe2+(aq) + Mn(s)Yes this reaction is spon.pdf
 Fe(s) + Mn2+(aq)  Fe2+(aq) + Mn(s)Yes this reaction is spon.pdf Fe(s) + Mn2+(aq)  Fe2+(aq) + Mn(s)Yes this reaction is spon.pdf
 Fe(s) + Mn2+(aq) Fe2+(aq) + Mn(s)Yes this reaction is spon.pdfsutharbharat59
 
When Na2SO3 is dissolved in water, Na+ and SO32- ions are formedN.pdf
When Na2SO3 is dissolved in water, Na+ and SO32- ions are formedN.pdfWhen Na2SO3 is dissolved in water, Na+ and SO32- ions are formedN.pdf
When Na2SO3 is dissolved in water, Na+ and SO32- ions are formedN.pdfsutharbharat59
 
To convert html code into Java servlet you have to write all html ta.pdf
To convert html code into Java servlet you have to write all html ta.pdfTo convert html code into Java servlet you have to write all html ta.pdf
To convert html code into Java servlet you have to write all html ta.pdfsutharbharat59
 
Therapsids is a group of synapsidsAdaptation1.Four limbs extent.pdf
Therapsids is a group of synapsidsAdaptation1.Four limbs extent.pdfTherapsids is a group of synapsidsAdaptation1.Four limbs extent.pdf
Therapsids is a group of synapsidsAdaptation1.Four limbs extent.pdfsutharbharat59
 
They trade away higher fecundity for future reproduction.Being ite.pdf
They trade away higher fecundity for future reproduction.Being ite.pdfThey trade away higher fecundity for future reproduction.Being ite.pdf
They trade away higher fecundity for future reproduction.Being ite.pdfsutharbharat59
 
The statements that are consistent with facts known about membrane a.pdf
The statements that are consistent with facts known about membrane a.pdfThe statements that are consistent with facts known about membrane a.pdf
The statements that are consistent with facts known about membrane a.pdfsutharbharat59
 
The implementation algorithm whose amortized complexity is O (1) to .pdf
The implementation algorithm whose amortized complexity is O (1) to .pdfThe implementation algorithm whose amortized complexity is O (1) to .pdf
The implementation algorithm whose amortized complexity is O (1) to .pdfsutharbharat59
 
The correct matches are as discussed below(a) A (Glycoprotiens ar.pdf
The correct matches are as discussed below(a) A (Glycoprotiens ar.pdfThe correct matches are as discussed below(a) A (Glycoprotiens ar.pdf
The correct matches are as discussed below(a) A (Glycoprotiens ar.pdfsutharbharat59
 
Step1 Moles of He = 504.0026Step2 Moles of Xe = 50131.29Step3 .pdf
Step1 Moles of He = 504.0026Step2 Moles of Xe = 50131.29Step3 .pdfStep1 Moles of He = 504.0026Step2 Moles of Xe = 50131.29Step3 .pdf
Step1 Moles of He = 504.0026Step2 Moles of Xe = 50131.29Step3 .pdfsutharbharat59
 
SKIN SENSORY ORGANThe human skin is the external covering of the .pdf
SKIN SENSORY ORGANThe human skin is the external covering of the .pdfSKIN SENSORY ORGANThe human skin is the external covering of the .pdf
SKIN SENSORY ORGANThe human skin is the external covering of the .pdfsutharbharat59
 
a) FlaseIn terms of trasing volume it is by far the largest market.pdf
a) FlaseIn terms of trasing volume it is by far the largest market.pdfa) FlaseIn terms of trasing volume it is by far the largest market.pdf
a) FlaseIn terms of trasing volume it is by far the largest market.pdfsutharbharat59
 
tested on EclipseRainfall.javaimpo.pdf
tested on EclipseRainfall.javaimpo.pdftested on EclipseRainfall.javaimpo.pdf
tested on EclipseRainfall.javaimpo.pdfsutharbharat59
 
Ques-1Part-a Mitosis it is a somatic cell division in an organi.pdf
Ques-1Part-a Mitosis it is a somatic cell division in an organi.pdfQues-1Part-a Mitosis it is a somatic cell division in an organi.pdf
Ques-1Part-a Mitosis it is a somatic cell division in an organi.pdfsutharbharat59
 
Proportional tax is a tax where the rate of tax is fixed. It is a pr.pdf
Proportional tax is a tax where the rate of tax is fixed. It is a pr.pdfProportional tax is a tax where the rate of tax is fixed. It is a pr.pdf
Proportional tax is a tax where the rate of tax is fixed. It is a pr.pdfsutharbharat59
 
POS system is a networked invironment where a main computer linked t.pdf
POS system is a networked invironment where a main computer linked t.pdfPOS system is a networked invironment where a main computer linked t.pdf
POS system is a networked invironment where a main computer linked t.pdfsutharbharat59
 

More from sutharbharat59 (20)

1. Translation program#includestdio.h#includeconio.h#incl.pdf
1. Translation program#includestdio.h#includeconio.h#incl.pdf1. Translation program#includestdio.h#includeconio.h#incl.pdf
1. Translation program#includestdio.h#includeconio.h#incl.pdf
 
1A. The acronym DBLC is used to name the Database Life Cycle.The DBL.pdf
1A. The acronym DBLC is used to name the Database Life Cycle.The DBL.pdf1A. The acronym DBLC is used to name the Database Life Cycle.The DBL.pdf
1A. The acronym DBLC is used to name the Database Life Cycle.The DBL.pdf
 
In chemistry, hybridisation (or hybridization) is.pdf
                     In chemistry, hybridisation (or hybridization) is.pdf                     In chemistry, hybridisation (or hybridization) is.pdf
In chemistry, hybridisation (or hybridization) is.pdf
 
RPS 19 stands for ribosomal protein S19. This proteins is synthesise.pdf
RPS 19 stands for ribosomal protein S19. This proteins is synthesise.pdfRPS 19 stands for ribosomal protein S19. This proteins is synthesise.pdf
RPS 19 stands for ribosomal protein S19. This proteins is synthesise.pdf
 
y^(iv)-4y=0taking 4y to RHS we gety^(iv) = 4yy^(iii) = integra.pdf
y^(iv)-4y=0taking 4y to RHS we gety^(iv) = 4yy^(iii) = integra.pdfy^(iv)-4y=0taking 4y to RHS we gety^(iv) = 4yy^(iii) = integra.pdf
y^(iv)-4y=0taking 4y to RHS we gety^(iv) = 4yy^(iii) = integra.pdf
 
 Fe(s) + Mn2+(aq) Fe2+(aq) + Mn(s)Yes this reaction is spon.pdf
 Fe(s) + Mn2+(aq)  Fe2+(aq) + Mn(s)Yes this reaction is spon.pdf Fe(s) + Mn2+(aq)  Fe2+(aq) + Mn(s)Yes this reaction is spon.pdf
 Fe(s) + Mn2+(aq) Fe2+(aq) + Mn(s)Yes this reaction is spon.pdf
 
When Na2SO3 is dissolved in water, Na+ and SO32- ions are formedN.pdf
When Na2SO3 is dissolved in water, Na+ and SO32- ions are formedN.pdfWhen Na2SO3 is dissolved in water, Na+ and SO32- ions are formedN.pdf
When Na2SO3 is dissolved in water, Na+ and SO32- ions are formedN.pdf
 
To convert html code into Java servlet you have to write all html ta.pdf
To convert html code into Java servlet you have to write all html ta.pdfTo convert html code into Java servlet you have to write all html ta.pdf
To convert html code into Java servlet you have to write all html ta.pdf
 
Therapsids is a group of synapsidsAdaptation1.Four limbs extent.pdf
Therapsids is a group of synapsidsAdaptation1.Four limbs extent.pdfTherapsids is a group of synapsidsAdaptation1.Four limbs extent.pdf
Therapsids is a group of synapsidsAdaptation1.Four limbs extent.pdf
 
They trade away higher fecundity for future reproduction.Being ite.pdf
They trade away higher fecundity for future reproduction.Being ite.pdfThey trade away higher fecundity for future reproduction.Being ite.pdf
They trade away higher fecundity for future reproduction.Being ite.pdf
 
The statements that are consistent with facts known about membrane a.pdf
The statements that are consistent with facts known about membrane a.pdfThe statements that are consistent with facts known about membrane a.pdf
The statements that are consistent with facts known about membrane a.pdf
 
The implementation algorithm whose amortized complexity is O (1) to .pdf
The implementation algorithm whose amortized complexity is O (1) to .pdfThe implementation algorithm whose amortized complexity is O (1) to .pdf
The implementation algorithm whose amortized complexity is O (1) to .pdf
 
The correct matches are as discussed below(a) A (Glycoprotiens ar.pdf
The correct matches are as discussed below(a) A (Glycoprotiens ar.pdfThe correct matches are as discussed below(a) A (Glycoprotiens ar.pdf
The correct matches are as discussed below(a) A (Glycoprotiens ar.pdf
 
Step1 Moles of He = 504.0026Step2 Moles of Xe = 50131.29Step3 .pdf
Step1 Moles of He = 504.0026Step2 Moles of Xe = 50131.29Step3 .pdfStep1 Moles of He = 504.0026Step2 Moles of Xe = 50131.29Step3 .pdf
Step1 Moles of He = 504.0026Step2 Moles of Xe = 50131.29Step3 .pdf
 
SKIN SENSORY ORGANThe human skin is the external covering of the .pdf
SKIN SENSORY ORGANThe human skin is the external covering of the .pdfSKIN SENSORY ORGANThe human skin is the external covering of the .pdf
SKIN SENSORY ORGANThe human skin is the external covering of the .pdf
 
a) FlaseIn terms of trasing volume it is by far the largest market.pdf
a) FlaseIn terms of trasing volume it is by far the largest market.pdfa) FlaseIn terms of trasing volume it is by far the largest market.pdf
a) FlaseIn terms of trasing volume it is by far the largest market.pdf
 
tested on EclipseRainfall.javaimpo.pdf
tested on EclipseRainfall.javaimpo.pdftested on EclipseRainfall.javaimpo.pdf
tested on EclipseRainfall.javaimpo.pdf
 
Ques-1Part-a Mitosis it is a somatic cell division in an organi.pdf
Ques-1Part-a Mitosis it is a somatic cell division in an organi.pdfQues-1Part-a Mitosis it is a somatic cell division in an organi.pdf
Ques-1Part-a Mitosis it is a somatic cell division in an organi.pdf
 
Proportional tax is a tax where the rate of tax is fixed. It is a pr.pdf
Proportional tax is a tax where the rate of tax is fixed. It is a pr.pdfProportional tax is a tax where the rate of tax is fixed. It is a pr.pdf
Proportional tax is a tax where the rate of tax is fixed. It is a pr.pdf
 
POS system is a networked invironment where a main computer linked t.pdf
POS system is a networked invironment where a main computer linked t.pdfPOS system is a networked invironment where a main computer linked t.pdf
POS system is a networked invironment where a main computer linked t.pdf
 

Recently uploaded

Contoh Aksi Nyata Refleksi Diri ( NUR ).pdf
Contoh Aksi Nyata Refleksi Diri ( NUR ).pdfContoh Aksi Nyata Refleksi Diri ( NUR ).pdf
Contoh Aksi Nyata Refleksi Diri ( NUR ).pdfcupulin
 
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community PartnershipsSpring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community Partnershipsexpandedwebsite
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsSandeep D Chaudhary
 
How to Send Pro Forma Invoice to Your Customers in Odoo 17
How to Send Pro Forma Invoice to Your Customers in Odoo 17How to Send Pro Forma Invoice to Your Customers in Odoo 17
How to Send Pro Forma Invoice to Your Customers in Odoo 17Celine George
 
SPLICE Working Group: Reusable Code Examples
SPLICE Working Group:Reusable Code ExamplesSPLICE Working Group:Reusable Code Examples
SPLICE Working Group: Reusable Code ExamplesPeter Brusilovsky
 
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...Nguyen Thanh Tu Collection
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽中 央社
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project researchCaitlinCummins3
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSAnaAcapella
 
An overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismAn overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismDabee Kamal
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxannathomasp01
 
Rich Dad Poor Dad ( PDFDrive.com )--.pdf
Rich Dad Poor Dad ( PDFDrive.com )--.pdfRich Dad Poor Dad ( PDFDrive.com )--.pdf
Rich Dad Poor Dad ( PDFDrive.com )--.pdfJerry Chew
 
UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024Borja Sotomayor
 
e-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopale-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi RajagopalEADTU
 
Observing-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxObserving-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxAdelaideRefugio
 
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...Nguyen Thanh Tu Collection
 
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...Gary Wood
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfPondicherry University
 

Recently uploaded (20)

Contoh Aksi Nyata Refleksi Diri ( NUR ).pdf
Contoh Aksi Nyata Refleksi Diri ( NUR ).pdfContoh Aksi Nyata Refleksi Diri ( NUR ).pdf
Contoh Aksi Nyata Refleksi Diri ( NUR ).pdf
 
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community PartnershipsSpring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
How to Send Pro Forma Invoice to Your Customers in Odoo 17
How to Send Pro Forma Invoice to Your Customers in Odoo 17How to Send Pro Forma Invoice to Your Customers in Odoo 17
How to Send Pro Forma Invoice to Your Customers in Odoo 17
 
SPLICE Working Group: Reusable Code Examples
SPLICE Working Group:Reusable Code ExamplesSPLICE Working Group:Reusable Code Examples
SPLICE Working Group: Reusable Code Examples
 
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
 
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽會考英聽
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
 
An overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismAn overview of the various scriptures in Hinduism
An overview of the various scriptures in Hinduism
 
Supporting Newcomer Multilingual Learners
Supporting Newcomer  Multilingual LearnersSupporting Newcomer  Multilingual Learners
Supporting Newcomer Multilingual Learners
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
Rich Dad Poor Dad ( PDFDrive.com )--.pdf
Rich Dad Poor Dad ( PDFDrive.com )--.pdfRich Dad Poor Dad ( PDFDrive.com )--.pdf
Rich Dad Poor Dad ( PDFDrive.com )--.pdf
 
UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024
 
e-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopale-Sealing at EADTU by Kamakshi Rajagopal
e-Sealing at EADTU by Kamakshi Rajagopal
 
Observing-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxObserving-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptx
 
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
 
Including Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdfIncluding Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdf
 
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
 
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdfFICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
FICTIONAL SALESMAN/SALESMAN SNSW 2024.pdf
 

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)