In: Computer Science
ASAP
(Use BigInteger for the Rational class) Redesign and implement the Rational class in Listing 13.13 using BigInteger for the numerator and denominator. Use the code at https://liveexample.pearsoncmg.com/test/Exercise13_15Test.txt to test your implementation. Here is a sample run: Sample Run Enter the first rational number: 3 454 Enter the second second number: 7 2389 3/454 + 7/2389 = 10345/1084606 3/454 - 7/2389 = 3989/1084606 3/454 * 7/2389 = 21/1084606 3/454 / 7/2389 = 7167/3178 7/2389 is 0.0029300962745918793 Class Name: Exercise13_15 If you get a logical or runtime error, please refer https://liveexample.pearsoncmg.com/faq.html.
code
solution
//output
//copyable code
// BigRational.java
import java.math.*;
public class BigRational extends Number implements Comparable
{
//create the biginteger
private BigInteger[] r1 = new BigInteger[2];
public BigRational()
{
//constructor
this(new BigInteger("0"), new BigInteger("1"));
}
public BigRational(BigInteger numertor, BigInteger denominator)
{
BigInteger data = data(numertor, denominator);
r1[0] = (denominator.compareTo(BigInteger.ZERO) > 0? BigInteger.ONE : new BigInteger("-1")).multiply(numertor.divide(data));
r1[1] = denominator.divide(data);
}
private static BigInteger data(BigInteger nn1_val, BigInteger dd1_val)
{
BigInteger n1val = nn1_val;
BigInteger n2val = dd1_val;
BigInteger data = BigInteger.ONE;
for (BigInteger kk1 = BigInteger.ONE; kk1.compareTo(n1val) <= 0 && kk1.compareTo(n2val) <= 0; kk1 = kk1.add(BigInteger.ONE))
{
if (n1val.remainder(kk1).compareTo(BigInteger.ZERO) == 0 && n2val.remainder(kk1).compareTo(BigInteger.ZERO) == 0)
data = kk1;
}
return data;
}
public BigInteger getNum()
{
return r1[0];
}
public BigInteger getdenominator()
{
return r1[1];
}
public BigRational addition(BigRational second_Rat)
{
BigInteger nn1_val = (r1[0].multiply(second_Rat.getdenominator())).add(r1[1].multiply(second_Rat.getNum()));
BigInteger dd1_val = r1[1].multiply(second_Rat.getdenominator());
return new BigRational(nn1_val, dd1_val);
}
public BigRational subtraction(BigRational second_Rat)
{
BigInteger nn1_val = (r1[0].multiply(second_Rat.getdenominator())).subtract(r1[1].multiply(second_Rat.getNum()));
BigInteger dd1_val = r1[1].multiply(second_Rat.getdenominator());
return new BigRational(nn1_val, dd1_val);
}
public BigRational multiplication(BigRational second_Rat)
{
BigInteger nn1_val = r1[0].multiply(second_Rat.getNum());
BigInteger dd1_val = r1[1].multiply(second_Rat.getdenominator());
return new BigRational(nn1_val, dd1_val);
}
public BigRational division(BigRational second_Rat)
{
BigInteger nn1_val = r1[0].multiply(second_Rat.getdenominator());
BigInteger dd1_val = r1[1].multiply(second_Rat.getNum());
return new BigRational(nn1_val, dd1_val);
}
//string function
@Override
//string function
public String toString()
{
if (r1[1].compareTo(BigInteger.ONE) == 0)
//return data
return r1[0] + "";
else
return r1[0] + "/" + r1[1];
}
//boolean function
@Override
//object value
public boolean equals(Object ot)
{
//subtract data
if (((this.subtraction((BigRational)(ot))).getNum()).compareTo(BigInteger.ZERO) == 0)
//true value
return true;
else
//false value
return false;
}
//float func
@Override
public float floatValue() {
//return vlue
return (float)doubleValue();
}
//double value function
@Override
//function
public double doubleValue()
{
return this.getNum().doubleValue() / this.getdenominator().doubleValue();
}
//long func
@Override
//override func
public long longValue()
{
//return result
return (long)doubleValue();
}
@Override
public int intValue()
{
return (int)doubleValue();
}
//function
@Override
//compare value
public int compareTo(Object o1)
{
BigInteger z1 = BigInteger.ZERO;
BigInteger nn1_val = this.subtraction((BigRational)o1).getNum();
if (nn1_val.compareTo(z1) > 0)
//return value
return 1;
else if (nn1_val.compareTo(z1) < 0)
return -1;
else
//return
return 0;
}
}
//Main.java
import java.math.BigInteger;
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter rational number1 with numerator and denominator seperated by a space: ");
String n1 = input.next();
String d1 = input.next();
System.out.print("Enter rational number2 with numerator and denominator seperated by a space: ");
String n2 = input.next();
String d2 = input.next();
BigRational r1 = new BigRational(new BigInteger(n1), new BigInteger(d1));
BigRational r2 = new BigRational(new BigInteger(n2), new BigInteger(d2));
System.out.println("Addition: "+r1 + " + " + r2 + " = " + r1.addition(r2));
System.out.println("Subtraction: "+r1 + " - " + r2 + " = " + r1.subtraction(r2));
System.out.println("Multiplication: "+r1 + " * " + r2 + " = " + r1.multiplication(r2));
System.out.println("Division: "+r1 + " / " + r2 + " = " + r1.division(r2));
System.out.println(r2 + " is " + r2.doubleValue());
}
}