Q/ When we represent a number such as 4 using floating point, we cannot store it
ID: 3581058 • Letter: Q
Question
Q/ When we represent a number such as 4 using floating point, we cannot store its exact value. 3
The new class Rational represent a rational number using two long integers. This rational number can be converted different number type such as int or double. When an array of this class is created, it can be sorted using Arrays.sort() method. In addition, this class could be stored in ArrayList<> and also can be sorted with the help of supporting class RationalCmp.
a/ Choose appropriate classes and interfaces to satisfy the above mentioned requirements
class Rational
extends ____ implements ____
{ ... }
class RationalCmp
implements ____
{
@Override
public int compare(____ x, ____ y) {
…
}
}
Explanation / Answer
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
class Rational extends Number implements Comparable<Rational> {
private long numer = 0;
private long denom = 1;
/**
* Default constructor
*/
public Rational() {
this(0, 1);
}
/**
* Paramaterized constructor
**/
public Rational(long numer, long denom) {
long gcd = calcuateGCD(numer, denom);
this.denom = Math.abs(denom) / gcd;
this.numer = ((denom > 0) ? 1 : -1) * numer / gcd;
}
/** This method will find GCD of two numbers */
private static long calcuateGCD(long numer, long denom) {
int gcd = 1;
long firstNum = Math.abs(numer);
long secondNum = Math.abs(denom);
for (int i = 1; i <= firstNum && i <= secondNum; i++) {
if (firstNum % i == 0 && secondNum % i == 0)
gcd = i;
}
return gcd;
}
/** getter methods */
public long getNumerator() {
return numer;
}
public long getDenominator() {
return denom;
}
/** Method to subtract rational number */
public Rational subtractRational(Rational rationalNumber) {
long n = numer * rationalNumber.getDenominator() - denom * rationalNumber.getNumerator();
long d = denom * rationalNumber.getDenominator();
return new Rational(n, d);
}
/**
* methods overrriden from Number class
*/
@Override
public int intValue() {
return (int)doubleValue();
}
@Override
public float floatValue() {
return (float)doubleValue();
}
@Override
public double doubleValue() {
return numer * 1.0 / denom;
}
@Override
public long longValue() {
return (long)doubleValue();
}
/**
* Implemented the compareTo method to compare rational numbers
*
*/
@Override
public int compareTo(Rational o) {
if (this.subtractRational(o).getNumerator() > 0)
return 1;
else if (this.subtractRational(o).getNumerator() < 0)
return -1;
else
return 0;
}
@Override
public String toString() {
if (denom == 1){
return numer + "";
}
else{
return numer + "/" + denom;
}
}
}
class RationalCmp implements Comparator<Rational>{
@Override
public int compare(Rational firstNumber, Rational secondNumber) {
return firstNumber.compareTo(secondNumber);
}
}
public class RationalDemo {
public static void main(String[] args) {
Rational firstNum=new Rational(1,2);
Rational secondNum=new Rational(1,4);
Rational thirdNum=new Rational(1,8);
Rational fourthNum=new Rational(1,16);
List<Rational>rationalNumberList=new ArrayList<>();
rationalNumberList.add(firstNum); //adding rational numbers to array list
rationalNumberList.add(thirdNum);
rationalNumberList.add(fourthNum);
rationalNumberList.add(secondNum);
System.out.println("Rational numbers without sorting : "+rationalNumberList); //printing the list
Collections.sort(rationalNumberList,new RationalCmp()); // sorting the list using RationalCmp comparator
System.out.println("Rational numbers with sorting : "+rationalNumberList);//printing the list
}
}
------------------------------------------------------------------output--------------------------------------------------------------------
Rational numbers without sorting : [1/2, 1/8, 1/16, 1/4]
Rational numbers with sorting : [1/16, 1/8, 1/4, 1/2]
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.