Question

In: Computer Science

ASAP (Use BigInteger for the Rational class) Redesign and implement the Rational class in Listing 13.13...

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.

Solutions

Expert Solution

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());

}

}


Related Solutions

(Enable Rectangle comparable) Rewrite the Rectangle class in Listing 13.3 to extend GeometricObject and implement the...
(Enable Rectangle comparable) Rewrite the Rectangle class in Listing 13.3 to extend GeometricObject and implement the Comparable interface. Override the equals method in the Object class. Two Rectangle objects are equal if their areas are the same. Draw the UML diagram that involves Rectangle, GeometricObject, and Comparable. Also include explain paragraph.
9.6 (Rational Class) Create a class called Rational (separate the files as shown in the chapter)...
9.6 (Rational Class) Create a class called Rational (separate the files as shown in the chapter) for performing arithmetic with fractions. Write a program to test your class. Use integer variables to represent the private data of the class-the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it's declared. The constructor should contain default values in case no initializers are provided and should store the fraction in reduced form. For...
Use the Heap class provided to implement a sort routine in a Main class where the...
Use the Heap class provided to implement a sort routine in a Main class where the user enters a series of values, each value is then pushed onto a heap, then the values are printed out in ascending order. public class Heap { public static final int SIZE = 1025; public Heap() { elt = new Element[SIZE]; lastLoc = 0; } public void push(String k, Object o) { if (!fullCheck()) { lastLoc++; elt[lastLoc] = new Element(k,o); int loc = lastLoc;...
Write a rational number class in C++. Recall a rational number is a rational number, composed...
Write a rational number class in C++. Recall a rational number is a rational number, composed of two integers with division indicated. The division is not carried out, it is only indicated, as in 1/2, 2/3, 15/32. You should represent rational numbers using two int values, numerator and denominator. A principle of abstract data type construction is that constructors must be present to create objects with any legal values. You should provide the following two constructors: one constructor to make...
Overview For this assignment, implement and use the methods for a class called Seller that represents...
Overview For this assignment, implement and use the methods for a class called Seller that represents information about a salesperson. The Seller class Use the following class definition: class Seller { public: Seller(); Seller( const char [], const char[], const char [], double ); void print(); void setFirstName( const char [] ); void setLastName( const char [] ); void setID( const char [] ); void setSalesTotal( double ); double getSalesTotal(); private: char firstName[20]; char lastName[30]; char ID[7]; double salesTotal; };...
Overview For this assignment, implement and use the methods for a class called Seller that represents...
Overview For this assignment, implement and use the methods for a class called Seller that represents information about a salesperson. The Seller class Use the following class definition: class Seller { public: Seller(); Seller( const char [], const char[], const char [], double ); void print(); void setFirstName( const char [] ); void setLastName( const char [] ); void setID( const char [] ); void setSalesTotal( double ); double getSalesTotal(); private: char firstName[20]; char lastName[30]; char ID[7]; double salesTotal; };...
Define a class called Rational for rational numbers. Arational number is a number that can...
Define a class called Rational for rational numbers. A rational number is a number that can be represented as the quotient of two integers. For example, /2, 3/4, 64/2, and so forth are all rational numbers.Represent rational numbers as two private values of type int, numfor the numerator and den for the denominator. The class has a default constructor with default values (num = 0,den = 1), a copy constructor, an assignment operator and three friend functions for operator overloading...
C++ make a rational class that includes these members for rational -private member variables to hold...
C++ make a rational class that includes these members for rational -private member variables to hold the numerator and denominator values -a default constructor -an overloaded constructor that accepts 2 values for an initial fraction -member fractions add(), sub(), mul(), div(), less(), eq(), and neq() (less should not return true if the object is less than argument) -a member function that accepts an argument of type of ostream that writes the fraction to that open output stream do not let...
1. You are given Stack.java, an interface class for Stacks. /* Use an array to implement...
1. You are given Stack.java, an interface class for Stacks. /* Use an array to implement the Stack.java in a class called ArrayStack.java that uses Generics. Write a main function that creates two stacks, one containing 10 Integers and a different one containing 10 Strings, and reverses there contents using a method called reverse that takes as a paramater a Generic array. You will need to implement all the methods of Stack.java as well as create a toString method in...
Must be coded in C#. 10.8 (Rational Numbers) Create a class called Rational for performing arithmetic...
Must be coded in C#. 10.8 (Rational Numbers) Create a class called Rational for performing arithmetic with fractions. Write an app to test your class. Use integer variables to represent the private instance variables of the class—the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it’s declared. The constructor should store the fraction in reduced form. The fraction 2/4 is equivalent to 1/2 and would be stored in the object...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT