Question: Define a class RationalNumber that represents rational numbers and a few simple operations on them, as indicated below. A rational number can be expressed as

Define a class RationalNumber that represents rational numbers and a few simple operations on them, as indicated below. A rational number can be expressed as the fraction p q , where p and q are two integers, q != 0. The class should extend the abstract class java.lang.Number and hence, should implement all its abstract functions. This assignment requires that a number of operations be supported. They are divided into three groups. The first is a set of basic operations, second group consists of implementation of the functions of the abstract class java.lang.Number and the third group is slightly more challenging and addresses binary periodic numbers.

2.1 Basic operations

The following operations should be supported:

A constructor RationalNumber(int, int) for creating a rational number of the form p q , given p and q. The program should handle cases when q = 0.

An operation void simplify() for simplifying a rational number of the form p q to its simplest form r s , where r = p gcd(p,q) and s = q gcd(p,q) . The function replaces the receiving rational number with its si

An operation void add(RationalNumber), which adds two rational numbers, one is the rational number that receives the message and the other rational number is given as a parameter. This operation modifies the receiving rational number with the result of the addition. Sum of rational number p q and rational number r s is the rational number (ps)+(rq) qs in the simplified form.

An operation void multiply(RationalNumber), which multiplies two rational numbers, one is the rational number that receives the message and the other rational number is given as a parameter. It modifies the receiving rational number with the result of the multiplication. The resulting rational number should be in its simplified form

2.2 Implementing java.lang.Number functions

double doubleValue(), returns a double which represents the rational number in decimal form upto 3 decimal places(truncate if the result has more than 3 decimal places). For eg : Calling the function doubleValue() on a rational number of the form 1/3 should return the value 0.333.

float floatValue(), returns a float which represents the rational number in decimal form upto 3 decimal places(truncate if the result has more than 3 decimal places).

int intValue(), returns an int which represents the integer part of the decimal representation of the rational number. For eg : Calling the function intValue() on a rational number of the form 6/5 should return the value 1.

int longValue(), returns a long which represents the integer part of the decimal representation of the rational number.

2.3 Binary Periodic Number

A binary periodic number is a binary decimal number consisting of a periodic/recurring decimal part. 100.01 101 is an example of a binary periodic number. Here, 100 is called the characteristic, 01 is called the anti-period, 101 is called the period. Hence, a binary periodic number can be represented as characteristic.antiperiod period where characteristic, antiperiod, period can be empty, but a string where all of them are empty is not valid. Examples of valid strings are .10 001, 10.1, 10. 101, . 01. But . is an invalid string. A binary periodic number can be represented as a rational number by applying the following rules :

The characteristic is evaluated as starting from right to left multiply each digit by the powers of 2. In this example, 1 2 2 + 0 2 1 + 0 2 0 = 4

The anti-period is evaluated as starting from left to right divide by the non-zero powers of 2. In this example , 0 (21) + 1 (22) = 1 4

The period is evaluated as starting from right to left multiply each digit by the powers of 2. Then,divide the result by 2l 1 where l is the length of the period. In this example, 101 evaluates to 5, length(period) = 3. Hence result is 5 (231) = 5 7 2

The rational number which represents the binary periodic number is obtained by adding the value of characteristic, anti-period and period. In this example 100.01101 evaluates to 4 + 1 4 + 5 7 = 139 28 .The rational number should be in the simplifed form. Implement the following : A constructor RationalNumber(String s) where string s is of format characteristic.antiperiod period. The constructor should parse the string s and convert the binary periodic number to a rational number.

Hint : Char Java.lang.String.charAt(int index) returns the char value at the specified index

Char java.lang.String.substring(int beginIndex, int endIndex) returns a new string that is a substring of this string.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!