Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Guide To Client Server Databases

Authors: Joe Salemi

2nd Edition

1562763105, 978-1562763107

More Books

Students also viewed these Databases questions

Question

What is the purpose of a competitor analysis?

Answered: 1 week ago

Question

Explain the steps involved in training programmes.

Answered: 1 week ago

Question

3. What may be the goal of the team?

Answered: 1 week ago

Question

Is how things are said consistent with what is said?

Answered: 1 week ago

Question

Has the priority order been provided by someone else?

Answered: 1 week ago