A rational number is of the form ab where a and b are intege

A rational number is of the form a/b, where a and b are integers, and b is not equal 0. Develop and test a class for processing rational numbers.
Details:
Your program should have 3 files: a driver file to test the operations, a header file for the class definition and any operator overloads you need, and an implementation file with the definitions of the items in the header file.

The class should read and display all rational numbers in the form a/b,
except when b is 1, then it should just display a

The operations that should be implemented for the rational numbers are
Operator Example Result
Addition 3/8 + 1/6 13/24
Subtraction 3/8 – 1/6 5/24
Multiplication 3/8 * 1/6 1/16
Division 3/8 / 1/6 9/4
Invert 3/8 I 8/3
Mixed fraction 8/3 M 2 and 2/3
Reduce 6/8 R 3/4
Greatest Common Divisor 6/8 G 2
Lowest Common Denominator 1/6 L 3/8 24
Less than 1/6 < 3/8 True
Less than or equal to 1/6 <= 3/8 True
Greater Than 1/6 > 3/8 False
Greater than or equal 1/6 >= 3/8 False
Equal to 3/8 = 9/24 True
  

You must have internal documentation:

Solution

Single responsibility principle

The Rational class does these things:

Represent a rational number composed of numerator and denominator

Normalize the sign of numerator and denominator to canonical form (keep sign in numerator)

Perform operations on rational numbers: add, subtract and others

Calculate an approximation to a double

Calculate highest common factor

Some of the operations fit nicely in the Rational class. I suggest moving the calculations outside to a different class are not essential for the functioning of Rational, and weaken the cohesion of the other methods of the class.

Unit testing

You have unit tests. But I recommend using JUnit4 instead of JUnit3. Changing is as simple as adding @Testannotation for the test cases, and changing the imports.

Naming

The constructor takes numerator and denominator, but inside the class you\'re using numand den. I suggest to spell out properly numeratorand denominator inside the class too.

RationalTest.java

A rational number is of the form a/b, where a and b are integers, and b is not equal 0. Develop and test a class for processing rational numbers. Details: Your

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site