Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please answer. Thank you. Define a class RationalNumber that represents rational numbers and a few simple operations on them, as indicated below. A rational number

Please answer. Thank you.

image text in transcribed

image text in transcribed

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, where p and q are two integers, +0. The class should extend the abstract class java.lang.Number and hence, should implement all its abstract methods. 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 imple- mentation 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 Rational Number(int, int) for creating a rational number of the form , given p and q. The program should handle cases when q = 0 by throwing a suitable unchecked exception. An operation void simplify() for simplifying a rational number of the form to its simplest form , where r = alon) and s = d... The function replaces the receiving rational number with its simplified form. An operation void add (Rational Number), which adds the rational number given as parameter to the one on which this method is invoked (the receiving rational number). This operation modifies the receiving rational number with the result of the addition. Recall that the sum of rational numbers and is the rational number (P*8)+("*9) in q*s simplified form. An operation void multiply (RationalNumber), which multiplies the rational num- ber given as parameter to the one on which this method is invoked (the receiving rational number). This operation modifies the receiving rational number with the result of the multiplication in 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 example, calling the function double Value() on a rational number of the form should return the value 0.333. float floatValue(), returns a float which represents the rational number in deci- mal 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 example, calling the function intValue() on a rational number of the form should return the value 1. int longValue(), returns a long which represents the integer part of the decimal representation of the rational number

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_2

Step: 3

blur-text-image_3

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 Systems Design Implementation And Management

Authors: Peter Rob, Carlos Coronel

3rd Edition

0760049041, 978-0760049044

More Books

Students also viewed these Databases questions

Question

4. Repeat the last exercise using the data for group 2.

Answered: 1 week ago