Question

When we test your Fraction.java we will use the given FractionTester.java file. Your Fraction.java file -MUST-...

When we test your Fraction.java we will use the given FractionTester.java file. Your Fraction.java file -MUST- work perfectly with the supplied FractionTester.java file. You may NOT modify the FractionTester to make your Fraction class work right.

Starter Fraction File: Fraction.java ADD METHODS TO THIS FILE. HAND IN THIS FILE ONLY.

Given/Completed Tester File: FractionTester.java DO NOT MODIFY. DO NOT HAND IN.

You are to add the following methods to the given Fraction.java file

public Fraction add( Fraction other) returns a Fraction that is the sum of the two Fractions.

public Fraction subtract( Fraction other) returns a Fraction that is the difference between this Fraction minus the other Fraction.

public Fraction multiply( Fraction other) returns a Fraction that is the product of the two Fractions.

public Fraction divide( Fraction other) returns a Fraction that is the quotient of the two Fractions.

public Fraction reciprocal() returns a Fraction that is the reciprocal of this Fractions.

private void reduce() Does not return a Fraction. It just modifies this Fraction by reducing it to its lowest form.

You must keep every Fraction reduced at all times. Every new fraction constructed it must be reduced before the constructor exits. No Fraction at any time may be stored in a form other than its reduced form. This makes the outputted value of the Fraction consistent in all cases.

All methods except reduce() must be a single return statement of the form:
return new Fraction(...); -OR- return (a call to another Fraction method)

/*
        FractionTester.java  A program that declares Fraction variables
        We will test your Fraction class on THIS tester.
*/
public class FractionTester
{
        public static void main( String args[] )
        {
                // use the word Fraction as if were a Java data type
                Fraction f1 = new Fraction( 44,14 ); // reduces to 22/7
                System.out.println( "f1=" + f1 ); // should output 22/7

                Fraction f2 = new Fraction( 21,14 ); // reduces to 3/2
                System.out.println( "f2=" + f2 ); // should output 3/2

                System.out.println( f1.add( f2 ) ); // should output 65/14
                System.out.println( f1.subtract( f2 ) ); // should output 23/14
                System.out.println( f1.multiply( f2 ) ); // should output 33/7
                System.out.println( f1.divide( f2 ) ); // should output 44/21
                System.out.println( f1.reciprocal() ); // should output 7/22

        } // END main
} // EOF
/* Fraction.java  A class (data type) definition file
   This file just defines what a Fraction is
   This file is NOT a program
   ** data members are PRIVATE
   ** method members are PUBLIC
*/
public class Fraction
{
        private int numer;
        private int denom;

        // ACCESSORS
        public int getNumer()
        {
                return numer;
        }
        public int getDenom()
        {
                return denom;
        }
        public String toString()
        {
                return numer + "/" + denom;
        }

        // MUTATORS
        public void setNumer( int n )
        {
                numer = n;
        }
        public void setDenom( int d )
        {
                if (d!=0)
                        denom=d;
                else
                {
                        // error msg OR exception OR exit etc.
                }
        }

        // DEFAULT CONSTRUCTOR - no args passed in
        public Fraction(  )
        {
                this( 0, 1 ); // "this" means call a fellow constructor
        }

        // 1 arg CONSTRUCTOR - 1 arg passed in
        // assume user wants whole number
        public Fraction( int n )
        {
                this( n, 1 ); // "this" means call a fellow constructor

        }

        // FULL CONSTRUCTOR - an arg for each class data member
        public Fraction( int n, int d )
        {
                // you could calc the gcd(n,d) here and then divide both by gcd in the setter calls
                setNumer(n); // i.e. setNumer( n/gcd );
                setDenom(d); // same here
        }

        // COPY CONSTRUCTOR - takes ref to some already initialized Fraction object
        public Fraction( Fraction other )
        {
                this( other.numer, other.denom ); // call my full C'Tor with other Fraction's data
        }

        private void reduce()
        {
                // reduces this fraction to lowest form
        }
}// EOF
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Fraction.java

public class Fraction {
   private int numer;
private int denom;

// ACCESSORS
public int getNumer()
{
return numer;
}
public int getDenom()
{
return denom;
}
public String toString()
{
return numer + "/" + denom;
}

// MUTATORS
public void setNumer( int n )
{
numer = n;
}
public void setDenom( int d )
{
if (d!=0)
denom=d;
else
{
throw new IllegalArgumentException("** Denominator Must not be Zero **");
}
}

// DEFAULT CONSTRUCTOR - no args passed in
public Fraction( )
{
this( 0, 1 ); // "this" means call a fellow constructor
}

// 1 arg CONSTRUCTOR - 1 arg passed in
// assume user wants whole number
public Fraction( int n )
{
this( n, 1 ); // "this" means call a fellow constructor

}

// FULL CONSTRUCTOR - an arg for each class data member
public Fraction( int n, int d )
{
   int gcd = this.gcd(n, d);
       // if GCD is negative, change to positive
       if (gcd < 0)
       {
       gcd = -gcd;
       }
setNumer(n/gcd); // i.e. setNumer( n/gcd );
setDenom(d/gcd); // same here
}

// COPY CONSTRUCTOR - takes ref to some already initialized Fraction object
public Fraction( Fraction other )
{
this( other.numer, other.denom ); // call my full C'Tor with other Fraction's data
}

private void reduce()
{
   // determine the greatest common divisor
       int gcd = this.gcd(numer, denom);
       // if GCD is negative, change to positive
       if (gcd < 0)
       {
       gcd = -gcd;
       }
       // divide gcd into both numerator and denominator
       numer = numer / gcd;
       denom = denom / gcd;   
}


//Method which will find Greatest Common Divisor
private Integer gcd(Integer a, Integer b)
{
   // % is modulus which is the remainder of a division
   // base case
   if ((a % b) == 0) {
   return b;
   }
   // recursive case
   else {
   return gcd(b, a % b);
   }
   }
  
//This method is used to add two Fractions
public Fraction add(Fraction f)
{
  

int a=this.numer;
int b=this.denom;
int c=f.numer;
int d=f.denom;
int num=(a*d + b*c);
int den=(b*d);
   Fraction frac=new Fraction(num, den);
frac.reduce();
  
return frac;

}
//This method is used to subtract two Fractions
public Fraction subtract(Fraction f)
{
   int a=this.numer;
int b=this.denom;
int c=f.numer;
int d=f.denom;
int num=(a*d - b*c);
int den=(b*d);
    Fraction frac=new Fraction(num, den);
frac.reduce();
return frac;
}

//This method is used to multiply two Fractions
public Fraction multiply(Fraction f)
{
   int a=this.numer;
int b=this.denom;
int c=f.numer;
int d=f.denom;
int num=(a*c);
int den=(b*d);
    Fraction frac=new Fraction(num, den);
frac.reduce();
return frac;

}

//This method is used to Divide two Fractions
public Fraction divide(Fraction f)
{
   int a=this.numer;
int b=this.denom;
int c=f.numer;
int d=f.denom;
int num=(a*d);
int den=(c*b);
   Fraction frac=new Fraction(num, den);
frac.reduce();
return frac;
}


//This method is used to find the reciprocal of a Fraction
public Fraction reciprocal()
{
   Fraction Frac=new Fraction(denom,numer);
   reduce();
   return Frac;
}
}

___________________

FractionTester.java

public class FractionTester
{
public static void main( String args[] )
{
// use the word Fraction as if were a Java data type
Fraction f1 = new Fraction( 44,14 ); // reduces to 22/7
System.out.println( "f1=" + f1 ); // should output 22/7

Fraction f2 = new Fraction( 21,14 ); // reduces to 3/2
System.out.println( "f2=" + f2 ); // should output 3/2

System.out.println( f1.add( f2 ) ); // should output 65/14
System.out.println( f1.subtract( f2 ) ); // should output 23/14
System.out.println( f1.multiply( f2 ) ); // should output 33/7
System.out.println( f1.divide( f2 ) ); // should output 44/21
System.out.println( f1.reciprocal() ); // should output 7/22

}
}

_______________________

Output:

f1=22/7
f2=3/2
65/14
23/14
33/7
44/21
7/22

________Thank You

Add a comment
Know the answer?
Add Answer to:
When we test your Fraction.java we will use the given FractionTester.java file. Your Fraction.java file -MUST-...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Lab 1.java only Goal: This lab will give you experience with defining and using classes and...

    Lab 1.java only Goal: This lab will give you experience with defining and using classes and fields, and with conditionals and recursive functions. Getting Started --------------- Read the Fraction.java class into a text editor and compile it filling in the command javac -g Fraction.java. The program should compile without errors. In a shell window, run the program using "java Fraction". The program should run, although it will print fractions in a non-reduced form, like 12/20. Part I: Constructors (1 point)...

  • Rational will be our parent class that I included to this post, Implement a sub-class MixedRational....

    Rational will be our parent class that I included to this post, Implement a sub-class MixedRational. This class should Implement not limited to: 1) a Constructor with a mathematically proper whole, numerator and denominator values as parameters. 2) You will override the: toString, add, subtract, multiply, and divide methods. You may need to implement some additional methods, enabling utilization of methods from rational. I have included a MixedRational class with the method headers that I used to meet these expectations....

  • Need help with Java for Fraction exercise

    Add another public method called add to your Fraction class. This method adds another fraction to the ‘calling object’. Thus, the method will take a Fraction class object as a parameter, add this parameter fraction to the calling object (fraction), and return a Fraction object as a result. HINT: We can use cross multiplication to determine the numerator of the resultant Fraction. The denominator of the resultant Fraction is simply the multiplication of the denominators of the two other Fractions.Add...

  • Refer to this header file: // Fraction class // This class represents a fraction a /...

    Refer to this header file: // Fraction class // This class represents a fraction a / b class Fraction { public: // Constructors Fraction(); // sets numerator to 1 and denominator to 1 Fraction(int num, int denom); // Setters void setNumerator(int num); void setDenominator(int denom); // getters int getNumerator()const {return num;} int getDenominator()const {return denom;} double getDecimal(){return static_cast<double> num / denom;} private: int num, denom; }; 1.Write the code for the non-member overloaded << operator that will display all of...

  • In this assignment you are required to complete a class for fractions. It stores the numerator...

    In this assignment you are required to complete a class for fractions. It stores the numerator and the denominator, with the lowest terms. It is able to communicate with iostreams by the operator << and >>. For more details, read the header file. //fraction.h #ifndef FRACTION_H #define FRACTION_H #include <iostream> class fraction { private: int _numerator, _denominator; int gcd(const int &, const int &) const; // If you don't need this method, just ignore it. void simp(); // To get...

  • I need help with the following Java code Consider a class Fraction of fractions. Each fraction...

    I need help with the following Java code Consider a class Fraction of fractions. Each fraction is signed and has a numerator and a denominator that are integers. Your class should be able to add, subtract, multiply, and divide two fractions. These methods should have a fraction as a parameter and should return the result of the operation as a fraction. The class should also be able to find the reciprocal of a fraction, compare two fractions, decide whether two...

  • 13.21 Lab: Rational class This question has been asked here before, but every answer I have...

    13.21 Lab: Rational class This question has been asked here before, but every answer I have tested did not work, and I don't understand why, so I'm not able to understand how to do it correctly. I need to build the Rational.cpp file that will work with the main.cpp and Rational.h files as they are written. Rational Numbers It may come as a bit of a surprise when the C++ floating-point types (float, double), fail to capture a particular value...

  • java only no c++ Write a Fraction class whose objects will represent fractions. You should provide...

    java only no c++ Write a Fraction class whose objects will represent fractions. You should provide the following class methods: Two constructors, a parameter-less constructor that assigns the value 0 to the Fraction, and a constructor that takes two parameters. The first parameter will represent the initial numerator of the Fraction, and the second parameter will represent the initial denominator of the Fraction. Arithmetic operations that add, subtract, multiply, and divide Fractions. These should be implemented as value returning methods...

  • Need help with following assignment written in Java. For this assignment, you’re going to create...

    Need help with following assignment written in Java. For this assignment, you’re going to create a new class named “Fraction” for managing rational numbers (fractions). (You should not submit a main program with the Fraction class; however, you'll definitely want to write one for your own testing purposes.) Details are as follows: CLASS DEFINITION CONSTRUCTORS: Fraction(int num,int denom) num/denom creates a new number whose value is Fraction(int num) METHODS (assume x is a Fraction): creates a new number whose value...

  • C++ CODE /* This is program project 2 on page 695. * Before you begin the...

    C++ CODE /* This is program project 2 on page 695. * Before you begin the project, please read the project description * on page 695 first. * * Author: Your Name * Version: Dates */ #include <iostream> #include <cmath> #include <cassert> using namespace std; class Fraction { public: // constructor Fraction(int a, int b); // generate a fraction which is a/b Fraction(int a); // generate a fraction which is a/1 Fraction(); // generate a fraction which is 0/1. i.e...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
Active Questions
ADVERTISEMENT