Question: 3.a: Write a Java class called Rational for performing arithmetic with positive rational numbers. A positive rational no. is a number that can be expressed

3.a: Write a Java class called Rational for performing arithmetic with positive rational numbers. A positive rational no. is a number that can be expressed in the form of p/q, where p,q are positive integers with q !=0 and p and q have no common divisor. Make the class implement the comparable interface so that each object should be able to compare itself to another object of the same type based on ascending order. 5 MARKS

b: Write a Java program that uses an ArrayList to store a collection of 10 of the Rational objects defined in part a. Sort the ArrayList of Rational objects according to the Natural Order defined in the Comparable interface of the Rational class itself using the sort method provided in the collections framework and then finally print out the list. 8 MARKS

c: Sort the ArrayList of Rational objects again using a separate Comparator class (a class that implements java.util.Comparator) that sorts them in descending order i.e. the opposite of the Natural Order defined in the Rational class itself. 7 MARKS

d: Modify the program to use the built-in Collections.binarySearch() method to check if a particular fraction, inputted through the console, is contained within the list. Print out your results to the console. 5 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!