Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

q1: This question involves a Fraction class.It is a class that can handles rational numbers with the four basic arithmetic operations for fractions (add, subtract,

q1:

This question involves a Fraction class.It is a class that can handles rational numbers with the four basic arithmetic operations for fractions (add, subtract, multiply, divide), reduce the result, and then display the three fractions in the following format:

1/2 + 1/3 = 5/6

For this question the Fraction will be limited to addition only.The result of the addition needs to be reduced and that can be done with a provided getGCF method that returns the Greatest Common Factor (GCF) of two integer attributes of the Fraction class. The incomplete Fraction class is shown below.

class Fraction

{

public Fraction (int n, int d)

{

num = n;

den = d;

}

public void add (Fraction fract1, Fraction fract2)

{

/*To be completed in Part (a)*/

}

/**Postcondition: returns the GCF of n1 and n2. */

public static int getGCF (int n1, int n2)

{

/* Can be used in writing method add */

}

public String tostring()

{

/*To be completed in Part (b)*/

}

Part (a).

Method add is not a return method.It is a void method that can be used in a program statement, such as fract3.add (fract1, fract2).The results of the addition of objects fract1 and fract2, which are a numerator (num) and a denominator (den), need to be stored in the fract3 object.

Complete method add.This is meant to be rational number addition.It is not meant to be decimal number computation, such as 0.25 + 0.50 = 0.75.It is meant to be 1/4 + 1/2 = 3/4.

Method add is not a return method.It is a void method that is to be used in a program statement. The result of adding fractions from object fract1 and fract2 are expected bereduced.You must use method getGCF, which was described previously.

/**Precondition: fract1 and fract2 are objects with attributes initialized.

*Postcondition: the results of adding fract1 to fract2 is stored in "this".

*/

public void add (Fraction fract1, Fraction fract2)

Part (b)

Complete method toString below.

/**Precondition: Attributes num and den are initialized, or altered, in this Fraction object.

*Postcondition: returns a String in the num / den format, such as 3/4

*/

public String toString ()

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

Students also viewed these Programming questions