Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

For intro to java programming chapter 13 problem 15 LISTING 13.13 RATIONAL CLASS 13.15 (Use Biginteger for the Rational class) Redesign and implement the Rational

For intro to java programming chapter 13 problem 15 image text in transcribed

LISTING 13.13 RATIONAL CLASS

image text in transcribedimage text in transcribedimage text in transcribed

13.15 (Use Biginteger for the Rational class) Redesign and implement the Rational class in Listing 13.13using BigInteger for the numerator and denominator. Write a test program that prompts the user to enter two rational - numbers and display the results as shown in the following sample run: Enter the first rational number: 3 454 Enter the second second number: 7 2389 3/454 + 7/2389 = 10345/|084606 3/454-7/2389 = 3989/|084606 3/454 7/2389 21/1084606 3/454 / 7/2389 7167 / 3178 7/2389 is 0.0029300962745918793 1 public class Rational extends Number implements Comparable f 2 / Data fields for numerator and denominator private long numerator = 0; 4 private long denominator 1; 6 *Construct a rational with default properties */ 7 public Rational) 8 this(0, 1)i 10 11 *Construct a rational with specified numerator and denominator 12 public Rational(long numerator, long denominator) 13 long gcd -gcd (numerator, denominator); this.numerator(denominator > 0?1 :-1) numerator ged; this.denominatorMath.abs (denominator) / ged; 15 16 17 18 ** Find GCD of two numbers / 19 private static long gcd (long n, long d) 20 21 long nl Math.abs (n) long n2-Math.abs (d) i int ged -1; 23 24 25 26 27 28 29 30 31 32 /**Return numerator* 33 public long getNumerator) t gcd = k; return gcd; return numeratori 35 36 37 /** Return denominator/ 38 public long getDenominator) ( 39 40 return denominator; 42 *Add a rational number to this rational 43 public Rational add (Rational secondRational) f long n numerator secondRational.getDenominator() + 45 denominator *secondRational.getNumerator () 4 6 long d denominator * secondRational.getDenominator() return new Rational (n, d); 48h 4 9 50 I Subtract a rational number from this rational 51 public Rational subtract (Rational secondRational) long nnumerator *secondRational.getDenominator () 52 53 54 denominator secondRational.getNumerator () long d denominatorsecondRational.getDenominator () return new Rational (n, d); 56 58 Multiply a rational number by this rational / 59 public Rational multiply (Rational secondRational) long n = numerator * secondRational.getNumerator(); long d-denominator* secondRational.getDenominator (); return new Rational (n, d)i 60 62 63 64 65 * Divide a rational number by this rational/ 66 public Rational divide (Rational secondRational) 67 68 69 70 71 72 73 public String toString)t 74 75 76 long n numerator * secondRational.getDenominator (); long d -denominatorsecondRational.numerator; return new Rational (n, d)i 2Override if (denominator1) return numerator+ " else return numerator + "/" + denominator; 78 79 80 @Override // Override the equals method in the Object class 81 public boolean equals (Object other) 82 83 84 85 86 if ((this.subtract ( (Rational) (other))).getNumerator() 0) return true else return false; 88 Override // Implement the abstract intValue method in Number 89 public int intValue() 90 91 92 93 override// Implement the abstract floatValue method in Number 94 public float floatValue) f 95 96 97 98 override // Implement the doubleValue method in Number 99 public double doubleValue()f return (int)doubleValue()i return (float)doubleValue () 100 101 102 103 Override // Implement the abstract longValue method in Number 104 public long longValue(O 105 return numerator1.0/ denominator; return (long) doubleValue() 106 107 108 override// Implement the compareTo method in Comparable 109 public int compareTo (Rational o) t 110 if (this.subtract (o).getNumerator) >0) return 1; 112 113 114 115 116 else if (this.subtract (o).getNumerator) f 2 / Data fields for numerator and denominator private long numerator = 0; 4 private long denominator 1; 6 *Construct a rational with default properties */ 7 public Rational) 8 this(0, 1)i 10 11 *Construct a rational with specified numerator and denominator 12 public Rational(long numerator, long denominator) 13 long gcd -gcd (numerator, denominator); this.numerator(denominator > 0?1 :-1) numerator ged; this.denominatorMath.abs (denominator) / ged; 15 16 17 18 ** Find GCD of two numbers / 19 private static long gcd (long n, long d) 20 21 long nl Math.abs (n) long n2-Math.abs (d) i int ged -1; 23 24 25 26 27 28 29 30 31 32 /**Return numerator* 33 public long getNumerator) t gcd = k; return gcd; return numeratori 35 36 37 /** Return denominator/ 38 public long getDenominator) ( 39 40 return denominator; 42 *Add a rational number to this rational 43 public Rational add (Rational secondRational) f long n numerator secondRational.getDenominator() + 45 denominator *secondRational.getNumerator () 4 6 long d denominator * secondRational.getDenominator() return new Rational (n, d); 48h 4 9 50 I Subtract a rational number from this rational 51 public Rational subtract (Rational secondRational) long nnumerator *secondRational.getDenominator () 52 53 54 denominator secondRational.getNumerator () long d denominatorsecondRational.getDenominator () return new Rational (n, d); 56 58 Multiply a rational number by this rational / 59 public Rational multiply (Rational secondRational) long n = numerator * secondRational.getNumerator(); long d-denominator* secondRational.getDenominator (); return new Rational (n, d)i 60 62 63 64 65 * Divide a rational number by this rational/ 66 public Rational divide (Rational secondRational) 67 68 69 70 71 72 73 public String toString)t 74 75 76 long n numerator * secondRational.getDenominator (); long d -denominatorsecondRational.numerator; return new Rational (n, d)i 2Override if (denominator1) return numerator+ " else return numerator + "/" + denominator; 78 79 80 @Override // Override the equals method in the Object class 81 public boolean equals (Object other) 82 83 84 85 86 if ((this.subtract ( (Rational) (other))).getNumerator() 0) return true else return false; 88 Override // Implement the abstract intValue method in Number 89 public int intValue() 90 91 92 93 override// Implement the abstract floatValue method in Number 94 public float floatValue) f 95 96 97 98 override // Implement the doubleValue method in Number 99 public double doubleValue()f return (int)doubleValue()i return (float)doubleValue () 100 101 102 103 Override // Implement the abstract longValue method in Number 104 public long longValue(O 105 return numerator1.0/ denominator; return (long) doubleValue() 106 107 108 override// Implement the compareTo method in Comparable 109 public int compareTo (Rational o) t 110 if (this.subtract (o).getNumerator) >0) return 1; 112 113 114 115 116 else if (this.subtract (o).getNumerator)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases Illuminated

Authors: Catherine M Ricardo, Susan D Urban

3rd Edition

1284056945, 9781284056945

More Books

Students also viewed these Databases questions