java program coding – www.savvyessaywriters.net
java program coding – www.savvyessaywriters.net
Problem
For this programming assignment, you are to implement the Fraction class design given below. You are also to develop an appropriate test driver that thoroughly tests the class.
Fraction Class Design
Implement the following class design.
public class Fraction {_x000D_
private int numer;_x000D_
private int denom;_x000D_
// alternate constructor (general fraction construction)
public Fraction(int numer, int denom) throws InvalidDenominatorException
// alternate constructor (unit fraction construction)
public Fraction(int denom) throws InvalidDenominatorException
// copy constructor_x000D_
public Fraction(Fraction otherFrac)_x000D_
// getters_x000D_
public int getNumer()_x000D_
public int getDenom()_x000D_
// standard methods_x000D_
public String toString()_x000D_
public boolean equals(Fraction rFrac)_x000D_
// arithmetic operators_x000D_
public Fraction abs()_x000D_
public Fraction add(Fraction rFrac)_x000D_
public Fraction sub(Fraction rFrac)_x000D_
public Fraction mult(Fraction rFrac)_x000D_
public Fraction div(Fraction rFrac)_x000D_
public Fraction invert()_x000D_
public double convertToDecimal()_x000D_
public Fraction reduce()_x000D_
{
Fraction temp = new Fraction(); int GCD = gcd(numer, denom);
temp.numer = numer / GCD; // allowed to access private vars of temp.denom = denom / GCD; // another object if same type as itself
return temp;_x000D_
}_x000D_
// relational operators_x000D_
public boolean lessThan(Fraction rFrac)_x000D_
public boolean greaterThan(Fraction rFrac)_x000D_
// additional operators_x000D_
public boolean reducedForm()_x000D_
public boolean properFrac()_x000D_
// Private Methods_x000D_
private int gcd(int n1, int n2) // Euclid’s Algorithm_x000D_
{
int M, N, R;
if (n1 < n2){_x000D_
N = n1;_x000D_
} M=n2;
else{N = n2;
M = n1; }
R = M % N;_x000D_
while (R != 0){_x000D_
M = N;
N = R;
} R=M%N;
return N; }
}
Note that objects of type Fraction are immutable. Also, the alternate constructor throws an InvalidDenominatorException if passed a denominator value of 0. This exception type is not predefined in Java, so you must define it as follow as another class in your Java project files:
public class InvalidDenominatorException extends RuntimeException { } // nothing to implement
This creates an exception of type RuntimeException. RuntimeExceptions do not need to be caught when thrown. However, your test driver should have the code for catching this exception where appropriate.
For example, if your test driver excutes the following,
Fraction frac1 = new Fraction(1,2);_x000D_
then there is no need to catch the possible exception, since it is clear that the denominator is not 0. However, if the values are input by the user, then there is a chance that a denominator value of 0 could be entered. Therefore, the code for catching the possible exception is needed,
int numer, denom;_x000D_ Fraction frac1;_x000D_ Scanner input = new Scanner(System.in);_x000D_ Boolean valid_input = false;_x000D_
while (!valid_input)_x000D_ try_x000D_
{
System.out.print(“Enter numerator <space> denominator: “); numer = input.nextInt();
denom = input.nextInt();
frac1 = new Fraction(numer,denom);_x000D_ } valid_input = true;_x000D_
catch (InvalidDenominatorException e)
{ System.out.println(“Denominator of Zero found – Please reenterâ€); c}atch (InputMismatchException e)
{ System.out.println(“Non-digit character found – Please reenterâ€);
e}tc.
Considerations
- You must use the method names given above, with the same number and types of parameters.
- Makes sure the equals method returns true for any two fractions that are arithmetically equal.
- The equals, lessThan and greaterThan methods must not alter the fractions being compared.
Savvy Essay Writers
