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

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.