Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is for JAVA I need to de\\sign a program that implements Eu\\clid’s algorit

ID: 3714409 • Letter: T

Question

This is for JAVA

I need to design a program that implements Euclid’s algorithm for finding the greatest common divisor of two positive integers. The greates common divisor is the largest integer that divides both values without producing a remainder. Write a RationalNumber class conta\ining a numerator and denominator as its fields. Write a recursive method (named gcd) that computes and returns the greatest common divisor of the numerator and denominator. Write a driver that prompts the user for a numerator and denominator, creates a RationalNumber object and computes and prints the greatest common denominator using the recursive gcd method.

Explanation / Answer

RationalNumber.java

public class RationalNumber {

//Declaring instance variables

private int numerator;

private int denominator;

//Zero argumented constructor

public RationalNumber() {

this.numerator = 0;

this.denominator = 1;

}

//Parameterized constructor

public RationalNumber(int numerator, int denominator) {

if (denominator == 0) {

this.numerator = numerator;

this.denominator = 1;

} else {

this.numerator = numerator;

this.denominator = denominator;

}

}

// getters and setters

public int getNumerator() {

return numerator;

}

public void setNumerator(int numerator) {

this.numerator = numerator;

}

public int getDenominator() {

return denominator;

}

public void setDenominator(int denominator) {

this.denominator = denominator;

}

// Method which calculate the GCD

public int gcd(int a, Integer b) {

// "private"

// % is modulus which is the remainder of a division

// base case

if ((a % b) == 0) {

return b;

}

// recursive case

else {

return gcd(b, a % b);

}

}

//toString method is used to display the contents of an object inside it

@Override

public String toString() {

int g=gcd(numerator, denominator);

return "RationalNumber (" + numerator/g + "/"+ denominator/g + ")";

}

  

}

______________

Test.java

import java.util.Scanner;

public class Test {

public static void main(String[] args) {

//Declaring variables

int num, den;

/*

* Creating an Scanner class object which is used to get the inputs

* entered by the user

*/

Scanner sc = new Scanner(System.in);

//Getting the input entered by the user

System.out.println("Rational Number :");

System.out.print("Enter Numerator :");

num = sc.nextInt();

System.out.print("Enter Denominator :");

den = sc.nextInt();

//Creating an Instance of RationalNumber

RationalNumber r=new RationalNumber(num,den);

//Displaying the output

System.out.println("GCD of " + num + " and " + den + " is "+ r.gcd(num, den));

System.out.println("After Simplification Rational Number is :"+r);

}

}

__________________

Output:

Rational Number :
Enter Numerator :5
Enter Denominator :25
GCD of 5 and 25 is 5
After Simplification Rational Number is :RationalNumber (1/5)

________________Thank You

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote