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.
(Implement a doubly linked list) The MyLinkedList class used in Listing 24.5 is a one-way directional...
(Implement a doubly linked list) The MyLinkedList class used in Listing 24.5 is a one-way directional linked list that enables one-way traversal of the list. Modify the Node class to add the new data field name previous to refer to the previous node in the list, as follows : public class Node { E element; Node next; Node previous; public Node(E e) { element = e; } } Implement a new class named TwoWayLinkedList that uses a doubly linked list...
Use Java programming to implement the following: Implement the following methods in the UnorderedList class for...
Use Java programming to implement the following: Implement the following methods in the UnorderedList class for managing a singly linked list that cannot contain duplicates. Default constructor Create an empty list i.e., head is null. boolean insert(int data) Insert the given data into the end of the list. If the insertion is successful, the function returns true; otherwise, returns false. boolean delete(int data) Delete the node that contains the given data from the list. If the deletion is successful, the...
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...
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...
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;...
Given the following UML class diagram, implement the class as described by the model. Use your...
Given the following UML class diagram, implement the class as described by the model. Use your best judgment when implementing the code inside of each method. +---------------------------------------------------+ | Polygon | +---------------------------------------------------+ | - side_count : int = 3 | | - side_length : double = 1.0 | +---------------------------------------------------+ | + Polygon() | | + Polygon(side_count : int) | | + Polygon(side_count : int, side_length : double) | | + getSides() : int | | + setSides(side_count : int) | |...
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...
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; };...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT