the answer of Write a class for rational numbers. A rational number is quotient of two integers, such as 2/3 or 1/2. Your class should have three constructors. One constructor takes no argument, and initializes the rotational number to 0. Another constructor takes a numerator only (the denominator is assumed to be 1). The third constructor takes both a numerator and denominator. Your class should have methods for adding, subtracting, multiplying and dividing rational numbers.

Dani AI

Generated

As and asked, post your attempt so helpers can point out mistakes. Below is a compact, working approach you can study and adapt — it shows the three constructors you need, clean normalization (reduce to lowest terms, keep denominator positive), and the four arithmetic operations implemented immutably. Use this as a reference, then paste your code if something still fails.

The class keeps num and den private and final, normalizes in the constructors using Euclid’s GCD, and throws on a zero denominator. Arithmetic methods compute with long intermediates (to reduce but not eliminate overflow risk) and return new reduced Rational objects. equals and hashCode compare reduced forms so 1/2.equals(2/4) is true.

public final class Rational {
    private final int num;
    private final int den;

    public Rational() { this(0, 1); }
    public Rational(int numerator) { this(numerator, 1); }

    public Rational(int numerator, int denominator) {
        if (denominator == 0) throw new IllegalArgumentException("Denominator must not be zero");
        if (denominator < 0) { numerator = -numerator; denominator = -denominator; }
        int g = gcd(numerator, denominator);
        this.num = numerator / g;
        this.den = denominator / g;
    }

    private static int gcd(int a, int b) {
        a = Math.abs(a); b = Math.abs(b);
        if (a == 0) return b == 0 ? 1 : b;
        while (b != 0) { int t = a % b; a = b; b = t; }
        return a;
    }

    public Rational add(Rational r) {
        long n = (long) this.num * r.den + (long) r.num * this.den;
        long d = (long) this.den * r.den;
        return new Rational((int) n, (int) d);
    }

    public Rational subtract(Rational r) {
        long n = (long) this.num * r.den - (long) r.num * this.den;
        long d = (long) this.den * r.den;
        return new Rational((int) n, (int) d);
    }

    public Rational multiply(Rational r) {
        long n = (long) this.num * r.num;
        long d = (long) this.den * r.den;
        return new Rational((int) n, (int) d);
    }

    public Rational divide(Rational r) {
        if (r.num == 0) throw new ArithmeticException("Divide by zero");
        long n = (long) this.num * r.den;
        long d = (long) this.den * r.num;
        return new Rational((int) n, (int) d);
    }

    @Override public boolean equals(Object o) {
        if (o == this) return true;
        if (!(o instanceof Rational)) return false;
        Rational r = (Rational) o;
        return this.num == r.num && this.den == r.den;
    }

    @Override public int hashCode() { return 31 * num + den; }
    @Override public String toString() { return num + "/" + den; }
}

Quick tips: always check for zero denominator, normalize the sign to the numerator, and reduce with GCD. Watch out for integer overflow when numerators/denominators get large — use BigInteger for production-level safety. , post your code and any specific failing input and someone will point out precise fixes.

Recommended Answers

All 2 Replies

What have you done so far?

If you haven't, then start on something. We are not here to do your homework. We are here to help you in case you get stuck.

There are lots of people here who will freely give their time to help you become the best Java programmer you can be. There's nobody here who is interested in helping you cheat or doing your homework for you.

DaniWeb Member Rules (which you agreed to when you signed up) include:
"Do provide evidence of having done some work yourself if posting questions from school or work assignments"
http://www.daniweb.com/community/rules

Post what you have done so far and someone will help you from there.

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.