When we test your Fraction.java we will use the given FractionTester.java file.
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.
Every Fraction must reduce 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.
/* 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 )
{
setNumer(n);
setDenom(d);
// call your reduce 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
JavaTester file
Solution
/* 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 who.
When we test your Fraction.java we will use the given FractionTester.pdf
1. When we test your Fraction.java we will use the given FractionTester.java file.
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.
Every Fraction must reduce 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.
/* 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()
2. {
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 )
{
setNumer(n);
setDenom(d);
// call your reduce here
}
// COPY CONSTRUCTOR - takes ref to some already initialized Fraction object
3. 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
JavaTester file
Solution
/* 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;
}
4. 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) {
setNumer(n);
setDenom(d);
// call your reduce 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
int GCD = gcd(getNumer(), getDenom());
setNumer(getNumer() / GCD);
setDenom(getDenom() / GCD);
}
// gcd : greatest common denom, used to reduce fractions
public int gcd(int n1, int n2) {
int M, N, R;
5. 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;
}
/**
* operation(+): addition between fractions
*
* @param b
* @return result of addition
*/
public Fraction add(Fraction b) {
int numer = (this.numer * b.denom) + (b.numer * this.denom);
int denom = this.denom * b.denom;
Fraction fraction = new Fraction(numer, denom);
fraction.reduce();
return fraction;
}
/**
* @param b
* @return
*/
public Fraction subtract(Fraction b) {
return add(new Fraction(-b.numer, b.denom));
}
/**
6. * method to multiply
*
* @param b
* @return
*/
public Fraction multiply(Fraction b) {
// check preconditions
if ((denom == 0) || (b.denom == 0))
throw new IllegalArgumentException("invalid denom");
// create new fraction to return as product
Fraction product = new Fraction();
// calculate product
product.numer = numer * b.numer;
product.denom = denom * b.denom;
// reduce the resulting fraction
product.reduce();
return product;
}
/**
* method to devide
*
* @param b
* @return
*/
public Fraction divide(Fraction b) {
// check preconditions
if ((denom == 0) || (b.numer == 0))
throw new IllegalArgumentException("invalid denom");
// create new fraction to return as result
Fraction result = new Fraction();
// calculate result
result.numer = numer * b.denom;
result.denom = denom * b.numer;
// reduce the resulting fraction
result.reduce();
return result;
7. }
public Fraction reciprocal() {
Fraction fraction = new Fraction(this.denom, this.numer);
fraction.reduce();
return fraction;
}
}// EOF
/*
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
OUTPUT:
f1=44/14
f2=21/14
65/14
23/14
33/7
44/21
7/22