For this programming assignment, you are to complete the Fraction class discusse
ID: 668675 • Letter: F
Question
For this programming assignment, you are to complete the Fraction class discussed in class and the provided partial test driver (both given below) for testing your class.
Modifications to Make
Modify the Fraction class as follows:
Modify setters so that they ignore inappropriate values (i.e., divide by zero)
Implement the equals() method inherited from the top-level Object class
Implement less than and greater than methods
Implement add, subtract, and multiply methods
Complete the test driver for your Fraction class to test the newly-added methods.
FRACTION CLASS
public class Fraction {
private int numer;
private int denom;
public Fraction() { // no-arg constructor
numer = 0;
denom = 1;
}
public Fraction(int numer, int denom) {
this.numer = numer;
this.denom = denom;
}
public Fraction(Fraction frac) { // copy constructor
numer = frac.getNumer();
denom = frac.getDenom();
}
// getters and setters
public int getNumer() {
return numer;
}
public void setNumer(int x) {
numer = x;
}
public int getDenom() {
return denom;
}
public void setDenom(int x) {
denom = x;
}
// Special Methods
public String toString() {
return numer + "/" + denom;
}
// Other Methods
public Fraction reduce() {
Fraction temp = new Fraction();
int GCD = gcd(numer, denom);
temp.setNumer(numer / GCD);
temp.setDenom(denom / GCD);
return temp;
}
// Private Methods
private int gcd(int n1, int n2)
{
int M, N, R;
if (n1 < n2)
{
N = n1;
M = n2;
}
else
{
N = n2;
M = n1;
}
R = M % N;
while (R != 0)
{
M = N;
N = R;
R = M % N;
}
return N;
}
}
FRACTION CLASS TEST DRIVER
public class FractionClassTestDriver {
public static void main(String[] args) {
// test constructors
Fraction frac0 = new Fraction();
System.out.println("TESTING NO-ARG CONSTRUCTOR");
System.out.println(“frac0: Result should be 0/1:”);
System.out.println("Numer = " + frac0.getNumer());
System.out.println("Denom = " + frac0.getDenom());
System.out.println("TESTING int/int CONSTRUCTOR");
Fraction frac1 = new Fraction(2,4);
System.out.println(“frac1: Result should be 2/4:”);
System.out.println("Numer = " + frac1.getNumer());
System.out.println("Denom = " + frac1.getDenom());
System.out.println("TESTING Fraction CONSTRUCTOR");
Fraction frac2 = new Fraction(frac1);
System.out.println(“frac2: Result should be 2/4:”);
System.out.println("Numer = " + frac2.getNumer());
System.out.println("Denom = " + frac2.getDenom());
System.out.println("TESTING COPY CONSTRUCTOR frac1 à frac2");
if (frac1.getNumer() == frac2.getNumer() &&
frac1.getDenom() == frac2.getDenom() &&
frac1 != frac2)
{
System.out.println("Copy constructor working");
}
else
System.out.println("PROBLEM with copy constructor");
// test equal method
System.out.println("TESTING EQUALITY OF frac1 and frac2 -");
System.out.println("SHOULD BE FOUND EQUAL:");
if (frac1.equals(frac2))
{
System.out.println("frac1 and frac2 found equal");
}
else
{
System.out.println("frac1 and frac2 NOT equal");
}
// test reduce method
System.out.println("TESTING reduce METHOD ON frac1");
Fraction reduced_frac1 = frac1.reduce();
System.out.println("Reduced frac1 = " + reduced_frac1);
// test getters and setters
frac2.setNumer(8);
frac2.setDenom(12);
System.out.println("Numer = " + frac2.getNumer());
System.out.println("Denom = " + frac2.getDenom());
// System.out.println("GCD of 2/4 = " + frac1.gcd(1,4));
}
* TO BE COMPLETED *
}
Explanation / Answer
another approach :
import java.io.*;
/** The Fraction class implements non-negative fractions, i.e., rational
* numbers.
*/
class Fraction {
/** Constructs a Fraction n/d.
* @param n is the numerator, assumed non-negative.
* @param d is the denominator, assumed positive.
*/
Fraction(int n, int d) {
numerator = n;
denominator = d;
}
/** Constructs a Fraction n/1.
* @param n is the numerator, assumed non-negative.
*/
public Fraction(int n) {
this(n,1);
}
/** Constructs a Fraction 0/1.
*/
public Fraction() {
numerator = 0;
denominator = 1;
}
/** Converts this fraction to a string format: "numerator/denominator."
* Fractions are printed in reduced form (part of your assignment is
* to make this statement true).
* @return a String representation of this Fraction.
*/
public String toString() {
int thisGcd = gcd(numerator, denominator);
return (numerator/thisGcd + "/" + denominator/thisGcd);
}
/** Calculates and returns the double floating point value of a fraction.
* @return a double floating point value for this Fraction.
*/
public double evaluate()
{
double n = numerator; // convert to double
double d = denominator;
return (n / d);
}
/** Add f2 to this fraction and return the result.
* @param f2 is the fraction to be added.
* @return the result of adding f2 to this Fraction.
*/
public Fraction add (Fraction f2) {
Fraction r = new Fraction((numerator * f2.denominator) +
(f2.numerator * denominator),
(denominator * f2.denominator));
return r;
}
/** Computes the greatest common divisor (gcd) of the two inputs.
* @param x is assumed positive
* @param y is assumed non-negative
* @return the gcd of x and y
*/
static private int gcd (int x, int y) {
/* Remove the following line. */
return 1;
}
/* private fields within a Fraction. */
private int numerator;
private int denominator;
/** Put the Fraction class through some test sequences.
* @param argv is not used.
*/
public static void main(String[] argv) {
/* Test all three contructors and toString. */
Fraction f0 = new Fraction();
Fraction f1 = new Fraction(3);
Fraction f2 = new Fraction(12, 20);
System.out.println(" Testing constructors (and toString):");
System.out.println("The fraction f0 is " + f0.toString());
System.out.println("The fraction f1 is " + f1); // toString is implicit
System.out.println("The fraction f2 is " + f2);
/* Test methods on Fraction: add and evaluate. */
System.out.println(" Testing add and evaluate:");
System.out.println("The floating point value of " + f1 + " is " +
f1.evaluate());
System.out.println("The floating point value of " + f2 + " is " +
f2.evaluate());
/*
Fraction sumOfTwo = _______________;
Fraction sumOfThree = ______________;
System.out.println("The sum of " + f1 + " and " + f2 + " is " + sumOfTwo);
System.out.println("The sum of " + f0 + ", " + f1 + " and " + f2 + " is "
+ sumOfThree);
*/
/* Test gcd function (static method). */
System.out.println(" Testing gcd:");
System.out.println("The gcd of 2 and 10 is: " + gcd(2, 10));
System.out.println("The gcd of 15 and 5 is: " + gcd(15, 5));
System.out.println("The gcd of 24 and 18 is: " + gcd(24, 18));
System.out.println("The gcd of 10 and 10 is: " + gcd(10, 10));
System.out.println("The gcd of 21 and 400 is: " + gcd(21, 400));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.