SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Stuart Reges, Marty Stepp
Chapter:
Classes
Exercise:
Programming Projects
Question:1 | ISBN:9780136091813 | Edition: 2

Question

Write a class called RationalNumber that represents a fraction with an integer numerator and denominator.
A RationalNumber object should have the following methods:
public RationalNumber(int numerator, int denominator)
Constructs a new rational number to represent the ratio (numerator/denominator). The denominator cannot be 0,
so throw an IllegalArgumentException if 0 is passed.
public RationalNumber()
Constructs a new rational number to represent the ratio (0/1).
public int getDenominator()
Returns this rational number’s denominator value; for example, if the ratio is (3/5), returns 5.
public int getNumerator()
Returns this rational number’s numerator value; for example, if the ratio is (3/5), returns 3.
public String toString()
Returns a String representation of this rational number, such as "3/5". You may wish to omit denominators of 1,
returning "4" instead of "4/1".

An extra challenge would be to maintain your RationalNumber objects in reduced form, avoiding rational numbers such as 3/6 in favor of 1/2, or avoiding 2/–3 in favor of –2/3. Another possible extra feature would be methods to add, subtract, multiply, and divide two rational numbers.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

PROGRAM CODE:

import java.math.BigInteger;

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

    // Constructs a new rational number to represent the ratio (numerator/denominator)
    public RationalNumber(int numerator, int denominator) {
        if (denominator == 0) {
            throw new IllegalArgumentException("Denominator cannot be 0.");
        }
        this.numerator = numerator;
        this.denominator = denominator;
        simplify();
    }

    // Constructs a new rational number to represent the ratio (0/1)
    public RationalNumber() {
        this(0, 1);
    }

    // Returns this rational number's denominator value
    public int getDenominator() {
        return denominator;
    }

    // Returns this rational number's numerator value
    public int getNumerator() {
        return numerator;
    }

    // Returns a String representation of this rational number
    public String toString() {
        if (denominator == 1) {
            return Integer.toString(numerator);
        }
        return numerator + "/" + denominator;
    }

    // Simplifies the rational number to reduced form
    private void simplify() {
        int gcd = BigInteger.valueOf(numerator).gcd(BigInteger.valueOf(denominator)).intValue();
        numerator /= gcd;
        denominator /= gcd;
        if (denominator < 0) {
            numerator = -numerator;
            denominator = -denominator;
        }
    }
}

 

 

0 0

Discussions

Post the discussion to improve the above solution.