#### Question Details

[solution]: Define a class of Fractions Consider ordinary fractions like 3 /

Answer download

More Details:

Define a class of Fractions

Consider ordinary fractions like ^{3}/_{4} and ^{-1}/_{2}. A fraction is the ratio of two integers: a numerator and a denominator. Create a user-defined type for fractions by defining a class Fraction. The class should supply a set of necessary operations on fractions

- addition
- subtraction
- multiplication
- division

and should hide implementation details of data representation and internal manipulations.

Remember that a class encapsulates data structures with access and manipulation procedures. In designing a class, an important task is to decide on the internal data representation which is isolated from the outside view. Member methods will keep the data representation consistent. Outside methods are not allowed to create or modify the internal structures directly. They may do this by calling methods in the class.

Here are some (internal) representation considerations to guide your design:

- A fraction is essentially a pair of integers callednumeratoranddenominator.
- The numerator carries the sign of the fraction and can be positive, negative, or zero.
- The denominator is kept positive and can never be zero.
- Fractions with the same value may have different representations (for example
^{1}/_{2},^{2}/_{4},^{3}/_{6}). In our representation let's decide for equal fractions to have the same numerator and the same denominator. That means that all fractions must be reduced to their lowest terms. (A data representation in which all equal quantities are represented uniquely is known as a*canonical*representation. Keeping fractions canonical is desirable and we should enforce this.) The canonical representation of 0 (zero) as a fraction is^{0}/_{1}.

Your

Fractionclass can be designed to enforce such conventions, not just to have them as principles that one can choose to follow or ignore. This is one big advantage of object-oriented programming. The canonical representation conventions should be enforced by the

Fractionconstructor.

The constructor should take the two arguments n and d and construct a fraction ^{n}/_{d} out of it. The denominator of the resulting Fraction should not be zero or negative. The fraction should be reduced by removing the gcd (greatest common divisor) between the numerator and the denominator. You should define a member method gcd that computes the gratest common divisor of two numbers and make the constructor use it to create canonical Fractions. gcd should be implemented as a classwide method since it is independent of Fraction instances. It should be declaredprivate because it does not contribute to the public interface of the Fraction class.

gcd is described in your text on page 149.

The operations for fractions listed above should implement binary operations. In other words we are going to define methods such that a Fraction can be added to, subtracted from, multiplied with, or divided by another Fraction. The result should be a Fraction.

You should also define a few predicates.

Each Fraction that you create should be able to respond to the following questions:

- isZero()
- isInt()
- equals(anotherFraction)
- greaterThan(anotherFraction)

You should also add a

toStringmethod to

Fraction(an instance method) that returns a

Stringrepresentation of a

Fraction. It must be an instance method as it is

Testing your Fractions:

// TestFrac.java class TestFrac { public static void main(String[] args) { Fraction x = new Fraction( 1, 20); Fraction u = new Fraction(-1, 60); Fraction v = new Fraction( 1, 30); Fraction y; y = x.plus(u).minus(v); // in one step! System.out.println( x + " + " + u + " - " + v + " = " + y); } }

For the ambitious:

Add a private method lcm to the class Fraction that takes two ints and returns their least common multiple. (For example, lcm(15, 12) is 60. Hint: use gcd). Modify minus and plus in Fraction to use lcm.

**Solution details:**

Answered

QUALITY

Approved

ANSWER RATING

This question was answered on: * Dec 18, 2020 *

* * Solution~00031148133715.zip (25.37 KB)

This attachment is locked

We have a ready expert answer for this paper which you can use for in-depth understanding, research editing or paraphrasing. You can buy it or order for a fresh, original and plagiarism-free copy (Deadline assured. Flexible pricing. TurnItIn Report provided)

##### Pay using PayPal (No PayPal account Required) or your credit card . All your purchases are securely protected by .

#### About this Question

STATUSAnswered

QUALITYApproved

DATE ANSWEREDDec 18, 2020

EXPERTTutor

ANSWER RATING

#### GET INSTANT HELP/h4>

We have top-notch tutors who can do your essay/homework for you at a reasonable cost and then you can simply use that essay as a template to build your own arguments.

You can also use these solutions:

- As a reference for in-depth understanding of the subject.
- As a source of ideas / reasoning for your own research (if properly referenced)
- For editing and paraphrasing (check your institution's definition of plagiarism and recommended paraphrase).

#### NEW ASSIGNMENT HELP?

### Order New Solution. Quick Turnaround

Click on the button below in order to Order for a New, Original and High-Quality Essay Solutions.
New orders are original solutions *and precise to your writing instruction requirements. Place a New Order using the button below.*

WE GUARANTEE, THAT YOUR PAPER WILL BE WRITTEN FROM SCRATCH AND WITHIN YOUR SET DEADLINE.