Question: implement a Java class to represent a rational number. Mathematically (and for this assignment), a rational number is defined as a fraction in which the
implement a Java class to represent a rational number. Mathematically (and for this assignment), a rational number is defined as a fraction in which the numerator and denominator are integers with the denominator being non-zero.
Requirements
To ensure consistency among solutions each implementation must implement the following requirements.
Your implementation should reflect the definition of a simplified rational number at all times.
A long (integer) is 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; that is, 8/6 shall be immeditly reduced to 4/3. In order to simplify a rational number properly, use the the Euclidean Algorithm to determine the greatest common divisor of the two numbers.
All methods that have an object as parameter must be able to handle an input of null.
By definition, the denominator of a fraction cannot be zero since it leads to an undefined value. In Java, this division by zero results in an ArithmeticException; the Rational class must throw an ArithmeticException when division by zero is attempted.
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 class shall reside in the default package.
Methods
There are many methods that one would expect to be supported in a Rational class; unless specified, you will have to implement all of the described methods.
Constructors
Rational() o Description: constructs a Rationalinitializing th evalue to 0.
Rational(long a) o Parameters: a the default value for the Rational number.
o Description: constructs a Rational initializing the value to 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.
Accessors
o Description: constructs a Rational number initializing the object to a . b
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.
Mathematical Operations
With each of the following methods, if the input object r is null, you may treat the input as the value zero (0).
Rational add(Rational r)
o Parameters: rthe rational number to be added to this rational.
o Description: adds r 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: r the rationa lnumber to be subtracted from this rational. o Description: subtracts r 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: r the rational number to be multiplied with this rational. o Description: multiplies r 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: r the rational number that is to divide this rational. o Description: divides this by r, returning a new object with the reduced quotient.
o Returns: returns a new object with the reduced quotient.
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)GCDofthetwoinputintegers.
o Notes: It is easier to implement this method if both integers are positive values.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
