Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

public class Fraction { private int numerator; private int denominator; public void setNumerator(int v) { numerator = v; } public void setDenominator(int v) { denominator

public class Fraction {

private int numerator; private int denominator; public void setNumerator(int v) { numerator = v; } public void setDenominator(int v) { denominator = v; } @Override public String toString() { return numerator+"/"+denominator; } }

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.

Then add a private method simplify() to your Fraction class that converts a fraction to its simplest form. For example, the fraction 20 / 60 should be stored in the class instance variables as 1 / 3 (i.e. numerator = 1, denominator = 3). N.B. you will need a method to determine the Greatest Common Divisor (GCD). Remember, both of these methods (simplify and gcd) must be private. As these methods are private, client programs cannot access them. So, how are they to be used? Given their purpose, it would mean that any Fraction class method that modifies the instance variables (e.g.: add, ICT167 Principles of Computer Science 2 constructor, set) should call the simplify() method to reduce the instance variables to their minimum values. Thus, these methods are used only for housekeeping; they are not to be used by client programs.

Make sure you understand how the following GCD algorithm work before implementing it.

// function gcd; two integer parameters

int gcd(int n1, int n2)

// local variable c

int c

loop while ( n1 != 0 AND n2 != 0 )

c = n2

n2 = n1 % n2 // modulus operator

n1 = c

return (n1 + n2)

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

Database Programming With Visual Basic .NET

Authors: Carsten Thomsen

2nd Edition

1590590325, 978-1590590324

More Books

Students also viewed these Databases questions

Question

4-6 Is there a digital divide? If so, why does it matter?

Answered: 1 week ago