When we represent a number such as 4/3 using floating point, we cannot store its
ID: 3581038 • Letter: W
Question
When we represent a number such as 4/3 using floating point, we cannot store its exact value.
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
Code ---> http://txt.do/ddcxo
(b) Figure 1 shows the UML designing the class Rational. Complete the fowlloing program methods and submit the source code.
Figure 1: Picture below
Code ---> http://txt.do/ddcxc
(c) Using the implemented class Rational, execute the following test program and submit the output which is the same as the exhibit.
Code ---> http://txt.do/ddcxx
Explanation / Answer
package org.jay.sample;
import java.util.ArrayList;
import java.util.Arrays;
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 add rational numbers */
public Rational addRational(Rational rationalNumber) {
long n = numer * rationalNumber.getDenominator() + denom * rationalNumber.getNumerator();
long d = denom * rationalNumber.getDenominator();
return new Rational(n, d);
}
/** Method to multiply rational numbers */
public Rational multiplyRational(Rational rationalNumber) {
long n = numer * rationalNumber.getNumerator();
long d = denom * rationalNumber.getDenominator();
return new Rational(n, d);
}
/** Method to divide rational numbers */
public Rational divideRational(Rational rationalNumber) {
long n = numer * rationalNumber.getDenominator();
long d = denom * rationalNumber.numer;
return new Rational(n, d);
}
/** Method to subtract rational numbers */
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);
Rational[]rationalArray=new Rational[4]; //adding rational to array
rationalArray[0]=firstNum;
rationalArray[1]=thirdNum;
rationalArray[2]=fourthNum;
rationalArray[3]=secondNum;
System.out.println("Addition of "+firstNum+" and "+secondNum+" : "+firstNum.addRational(secondNum));
System.out.println("Division "+firstNum+" and "+secondNum+" : "+firstNum.divideRational(secondNum));
System.out.println("Multiplication "+firstNum+" and "+secondNum+" : "+firstNum.multiplyRational(secondNum));
System.out.println("Substraction "+firstNum+" and "+secondNum+" : "+firstNum.subtractRational(secondNum));
System.out.println("Rational numbers array without sorting"); //printing the array
for (int i = 0; i < rationalArray.length; i++) {
System.out.print(" "+rationalArray[i]);
}
Arrays.sort(rationalArray); // sorting the list using RationalCmp comparator
System.out.println(" Rational numbers array with sorting");//printing the array
for (int i = 0; i < rationalArray.length; i++) {
System.out.print(" "+rationalArray[i]);
}
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(" -----------------------------------------------------------------------------------");
System.out.println("Rational numbers list without sorting : "+rationalNumberList); //printing the list
Collections.sort(rationalNumberList,new RationalCmp()); // sorting the list using RationalCmp comparator
System.out.println("Rational numbers list with sorting : "+rationalNumberList);//printing the list
}
}
-----------------------------------------------------output---------------------------------------------------------------
Addition of 1/2 and 1/4 :3/4
Division 1/2 and 1/4 :2
Multiplication 1/2 and 1/4 :1/8
Substraction 1/2 and 1/4 :1/4
Rational numbers array without sorting
1/2 1/8 1/16 1/4
Rational numbers array with sorting
1/16 1/8 1/4 1/2
-----------------------------------------------------------------------------------
Rational numbers list without sorting : [1/2, 1/8, 1/16, 1/4]
Rational numbers list with sorting : [1/16, 1/8, 1/4, 1/2]
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.