Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have a solution for my java coding question but I don't understand what is happening. Please explain line by line what is happening in

I have a solution for my java coding question but I don't understand what is happening.

Please explain line by line what is happening in the following code.

I really want to understand everything going on in the code so be very descriptive.

Here is the question:

image text in transcribed

image text in transcribed

Here is the solution:

import java.util.*;

public class Rational { private long numerator; private long denominator; public Rational(long numerator, long denominator) { if (denominator == 0) { throw new IllegalArgumentException("Denominator cannot be zero"); } long gcd = gcd(numerator, denominator); this.numerator = numerator / gcd; this.denominator = denominator / gcd; } public Rational plus(Rational b) { long num = this.numerator * b.denominator + b.numerator * this.denominator; long den = this.denominator * b.denominator; return new Rational(num, den); } public Rational minus(Rational b) { long num = this.numerator * b.denominator - b.numerator * this.denominator; long den = this.denominator * b.denominator; return new Rational(num, den); } public Rational times(Rational b) { long num = this.numerator * b.numerator; long den = this.denominator * b.denominator; return new Rational(num, den); } public Rational divides(Rational b) { long num = this.numerator * b.denominator; long den = this.denominator * b.numerator; return new Rational(num, den); } public boolean equals(Rational that) { return this.numerator == that.numerator && this.denominator == that.denominator; } public String toString() { return numerator + "/" + denominator; } private static long gcd(long p, long q) { if (q == 0) return p; long r = p % q; return gcd(q, r); } public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter numerator of first rational number: "); long num1 = input.nextLong(); System.out.print("Enter denominator of first rational number: "); long den1 = input.nextLong(); Rational r1 = new Rational(num1, den1); System.out.print("Enter numerator of second rational number: "); long num2 = input.nextLong(); System.out.print("Enter denominator of second rational number: "); long den2 = input.nextLong(); Rational r2 = new Rational(num2, den2); System.out.println("First rational number: " + r1.toString()); System.out.println("Second rational number: " + r2.toString()); System.out.println("Sum: " + r1.plus(r2).toString()); System.out.println("Difference: " + r1.minus(r2).toString()); System.out.println("Product: " + r1.times(r2).toString()); System.out.println("Quotient: " + r1.divides(r2).toString()); System.out.println("Are they equal? " + r1.equals(r2)); } }

1.2.16 Rational numbers. Implement an immutable data type Rationa 1 for rational numbers that supports addition, subtraction, multiplication, and division. You do not have to worry about testing for overflow (see EXERCISE 1.2.17), but use as instance variables two long values that represent the numerator and denominator to limit the possibility of overflow. Use Euclid's algorithm (see page 4) to ensure that the numerator and denominator never have any common factors. Include a test client that exercises all of your methods. Euclid's Algorithm: public static int gcd( int p, int q ) \{ if (q==0) return p; int r=p%q; return gcd(q,r); \}

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

More Books

Students also viewed these Databases questions