Question: 3. Write a Java class called Rational for performing arithmetic with fractions. Use integer variables to represent the private instance variables of the class -

 3. Write a Java class called Rational for performing arithmetic with

3. Write a Java class called Rational for performing arithmetic with fractions. Use integer variables to represent the private instance variables of the class - the numerator and the denominator. Provide a constructor method that enables an object of this class to be initialised when it is declared e.g. the fraction 2/3 would be stored in the object as 2 in the numerator and 3 in the denominator. 4 MARKS Provide public methods for each of the following (a) Addition of two Rational numbers. The result should be stored in the target object e.g. if rl and r2 are objects of type Rational, calling rl.add(r2) would add the value of r2 to rl and then store the new value in rl. (Hint: adding 2/3 and 3/4 gives the result 1712). 7 MARKS (b) Multiplication of two Rational numbers. In the same way as for (a), the result should be stored in the target object. (Hint: multiplying 2/3 and 3/4 gives the result 6/12). 7 MARKS (c) Printing Rational numbers in the form a/b, where a is the numerator and b is the denominator 3 MARKS Finally, write a suitable driver program that could be used to test your class. 4 MARKS

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!