## Question

Implementation Specifications

This section will detail the rational class interface and the recommendations and requirements that should be adhered to in your implementation.

3.1 Definitions

For this assignment, as well as mathematically, a rational number is defined as a fraction in which the numerator and denominator are integers.

However, the denominator cannot be zero as it leads to an undefined expression and, specifically, an Arithmetic Exception in this program.

In this program, a rational number will always be in simplest terms; that is, 8/6 shall be immediately reduced to 4/3.

In order to simplify properly you will have to implement the Euclidean Algorithm to determine the greatest common divisor of the two numbers.

3.2 Requirements

To ensure consistency among all implementations there are some requirements that all implementations must maintain:

• Your implementation should reflect the definition of a rational number at all times.

• Your Rational class is a Comparable since it implements the Comparable interface.

• A long (integer) used to represent both the numerator and denominator.

• The Rational class must be able to handle both positive and negative numbers.

• The rational number must be in simplest form after every operation.

• All methods that have an object parameter must be able to handle an input of null..

3.3 Recommendations

There are deviations in program implementation that are acceptable and will not impact the overall functionality of the Rational class.

• The denominator should be initialized to a sensible and consistent value; the numerator shall be initialized to zero.

• The denominator should always be positive leaving the numerator to indicate the sign of the number (positive or negative).

• The rational number must be in simplest form after every operation.

3.4 Rational Member Methods

There are many methods that one would expect to be supported in a Rational class.

This section will describe the interface of the Rational class.

Unless specified, you will have to implement all of the described methods.

3.4.1 Constructors

• Rational()

o Description: constructs a Rational initializing the value to 0.

• Rational(long a)

o Parameters: a – the default value for the Rational number.

o Description: constructs a Rational initializing the value to 1 a.

• Rational(long a, long b) throws ArithmeticException

o Parameters: a – an integer specifying the initial value of the numerator. b – an integer specifying the initial value of the denominator.

o Description: constructs a Rational number initializing the object to a b.

3.4.2 Accessor Methods

• long getNumerator()

o Description: returns the current value of the numerator.

o Returns: as an accessor method, it only returns the current value of the numerator.

• long getDenominator()

o Description: returns the current value of the denominator.

o Returns: as an accessor method, it only returns the current value of the denominator.

3.4.3 Methods Implementing Mathematical Operations

With each of the following methods, if the input object is null, you may treat the input as the value zero (0).

• Rational add(Rational r)

o Parameters: – the rational number to be added to this rational.

o Description: adds and this, returning a new object with the reduced sum. A common denominator is required to complete this operation.

o Returns: returns a new object with the reduced sum.

• Rational subtract(Rational r)

o Parameters: – the rational number to be subtracted from this rational.

o Description: subtracts from this, returning a new object with the reduced difference. A common denominator is required to complete this operation.

o Returns: returns a new object with the reduced difference.

• Rational multiply(Rational r)

o Parameters: – the rational number to be multiplied with this rational.

o Description: multiplies with this, returning a new object with the reduced product.

o Returns: returns a new object with the reduced product.

• Rational divide(Rational r) throws ArithmeticException

o Parameters: – the rational number that is to divide this rational.

o Description: divides this by, returning a new object with the reduced quotient.

o Returns: returns a new object with the reduced quotient.

3.4.4 The Greatest Common Divisor Algorithm

• private long gcd(long p, long q)

o Parameters: p – an integer. q – an integer.

o Description: determines the greatest common divisor of the two input integers according to the Euclidean Algorithm; a quick implementation is easily available online.

o Returns: the (positive) GCD of the two input integers.

o Notes: It is easier to implement this method if both integers are positive values.

3.4.5 Supplied Methods Besides the interface specification described above, there are other methods that have been provided; feel free to use them as required in your implementation and testing.

• String toString()

o Description: provides a means of viewing the values contained within the Rational object.

o Returns: a String representation of this rational value.

o Notes: It is important that this method remain unmodified since it will be used for evaluation purposes. It has been provided for debugging purposes.

• int compareTo(Object obj)

o Parameters: obj – an object to compare against this rational value.

o Description: determines whether the content of obj is smaller (or larger) than this rational value; the result returned is compliant with the Comparable interface that the Rational class implements.

o Returns: -1, 0, or 1 when this object is less than, equal, or greater than obj.

o Notes: It is important that this method remain unmodified since it will be used for evaluation purposes. It has been provided for debugging purposes.

• boolean equals(Object obj)

o Parameters: obj – an object to compare against this rational value.

o Description: determines whether the content of obj equals this rational value.

o Returns: TRUE or FALSE whether obj is an instance of class Rational and is equal to the rational value represented in this rational value.

o Notes: It is important that this method remain unmodified since it will be used for evaluation purposes. It has been provided for debugging purposes.

## Solution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

import java.lang.Math.*;public class Rational implements Comparable

{

private long _numer;

private long _denom;

public Rational()

{

_numer = 0;

_denom = 1;

}

//

public Rational(long a)

{

_numer = a;

_denom = 1;

}

//

public Rational(long a, long b) throws ArithmeticException

{

long t;

t = (long) (a/b); // to throw the exception

long g = gcd(Math.abs(a), Math.abs(b));

_numer = a/g;

_denom = b/g;

if (_denom < 0) {

_denom *= -1;

_numer *= - 1;

}

}

//

public long getDenominator()

{

return _denom;

}...

By purchasing this solution you'll be able to access the following files:

solution.zip.