0
import java.util.Scanner;

public class Rational {
    private int numerator;
    private int denominator;

    public Rational() {
        numerator = 1;
        denominator = 2;
    }
    public Rational(int numerator, int denominator) {
        this.numerator = numerator;
        this.denominator = denominator;
    }
    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;
    }
    public Rational add(Rational r) {
        int  num = numerator * r.denominator + r.numerator * denominator;
        int denom = denominator * r.denominator;
        return new Rational(num, denom);
    }
    public Rational subtract(Rational r) {
        int num = numerator * r.denominator - r.numerator * denominator;
        int denom = denominator  * r.denominator;
        return new Rational(num, denom);
    }
    public Rational multiply(Rational r) {
        int num = numerator * r.numerator;
        int denom = denominator * r.denominator;
        return  new Rational(num, denom);
    }
    public Rational divide(Rational r) {
        int num = numerator / r.numerator;
        int denom = denominator / r.denominator;
        return new Rational(num, denom);
    }
    public String toString() {
        return "(" + numerator + "/" + denominator + ")";
    }
}
class RationalTest {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        Rational a;
        Rational b;

        System.out.println("Enter numerator for the first rational number: ");
        int numa = input.nextInt();
        System.out.println("Enter a non-zero denominator for the first rational number: ");
        int denoma = input.nextInt();
        System.out.println("Enter a numerator for the second rational number: ");
        int numb = input.nextInt();
        System.out.println("Enter a non-zero denominator for the second rational number: ");
        int denomb = input.nextInt();

        a = new Rational(numa, denoma);
        b = new Rational(numb, denomb);

        System.out.println("First rational number is: " + a);
        System.out.println("Second rational number is: " + b);
        System.out.println("Addition of the rational number is: " + a.add(b));
        System.out.println("Subtraction of the rational number is: " + a.subtract(b));
        System.out.println("Multiplication of the rational number is: " + a.multiply(b));
        System.out.println("Division of the rational number is: " + a.divide(b));
    }
}

The insctructions say:
The constructor should store the fraction in reduced form.
a) Add two Rational numbers: The result of the addition should be stored in reduced
form.
b) Subtract two Rational numbers: The result of subtraction should be stored in reduced
form.
c) Multiply two Rational numbers: The result of the multiplication should be stored in
reduced form.
d) Divide two Rational numbers: The result of the division should be stored in reduced
form.
e) Return a String representation of Rational number in the form

2
Contributors
2
Replies
40
Views
2 Weeks
Discussion Span
Last Post by JamesCherrill
0

A bit of clarification that i left out, i don't understand where to implement the reduction or how to use GCD in java

0

The constructor should store the fraction in reduced form.

So you should perform the reduction in the constructor.

Google Euclidean algorithm for a good way to compute the GCD

Edited by JamesCherrill

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.