Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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
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
Step: 1
3a Heres a Java class called Rational that implements the Comparable interface and performs arithmetic with positive rational numbers csharp Edit Full ...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started