Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Background: Project #6 requires you to enhance the Fraction Class, we have been studying in class. You are to add several methods as described below.

Background:

Project #6 requires you to enhance the Fraction Class, we have been studying in class. You are to add several methods as described below.

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.

********************************************************************************

/* 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 

**************************************************************************************************

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

************************************************************************************************

/* 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 

****************************************************************************************************************

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)

Correct Output:

f1=22/7

f2=3/2

65/14

23/14

33/7

44/21

7/22

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

PC Magazine Guide To Client Server Databases

Authors: Joe Salemi

1st Edition

156276070X, 978-1562760700

More Books

Students also viewed these Databases questions

Question

1. What is Ebola ? 2.Heart is a muscle? 3. Artificial lighting?

Answered: 1 week ago

Question

a. Do team members trust each other?

Answered: 1 week ago

Question

How do members envision the ideal team?

Answered: 1 week ago

Question

Has the team been empowered to prioritize the issues?

Answered: 1 week ago