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:
Walter Savitch ,kenrick Mock
Chapter:
Defining Classes Ii
Exercise:
Programming Projects
Question:6 | ISBN:9780132830317 | Edition: 5

Question

Part One: Define a class for rational numbers. A rational number is a number that can be represented as the quotient of two integers. For example, 1/2, 3/4, 64/2, and so forth are all rational numbers. (By 1/2 and so forth, we mean the everyday meaning of the fraction, not the integer division this expression would produce in a Java program.) Represent rational numbers as two values of type int , one for the numerator and one for the denominator. Your class should have two instance variables of type int . Call the class Rational. Include a constructor with two arguments that can be used to set the instance variables of an object to any values. Also include a constructor that has only a single parameter of type int ; call this single parameter wholeNumber and define the constructor so that the object will be initialized to the rational number wholeNumber/1 . Also include a no-argument constructor that initializes an object to 0 (that is, to 0/1). Note that the numerator, the denominator, or both may contain a minus sign. Define methods for addition, subtraction, multiplication, and division of objects of your class Rational. These methods should be static methods that each have two parameters of type Rational and return a value of type Rational. For example, Rational.add(r1, r2) will return the result of adding the two rational numbers (two objects of the class Rational, r1 and r2). Define accessor and mutator methods as well as the methods equals and toString . You should include a method to normalize the sign of the rational number so that the denominator is positive and the numerator is either positive or negative. For example, after normalization, 4/-8 would be represented the same as -4/8. Also write a test program to test your class.

Hints : Two rational numbers a/b and c/d are equal if a*d equals c*b.

Part Two: Add a second version of the methods for addition, subtraction, multi- plication, and division. These methods should have the same names as the static version but should use a calling object and a single argument. For example, this version of the add method (for addition) has a calling object and one argument. So r1.add(r2) returns the result of adding the rationals r1 and r2. Note that your class should have all these methods; for example, there should be two methods named add.

Alternate Part Two: Add a second version of the methods for addition, subtraction, multiplication, and division. These methods should have the same names as the static version but should use a calling object and a single argument. The methods should be void methods. The result is given as the changed value of the calling object. For example, this version of the add method (for addition) has a calling object and one argument. Therefore,

changes the values of the instance variables of r1 so they represent the result of adding r2 to the original version of r1. Note that your class should have all these methods; for example, there should be two methods named add.

(If you want to do both Part Two and Alternate Part Two, they must be two classes. You cannot include the methods from both Part Two and Alternate Part Two in a single class. Do you know why?)


TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

PROGRAM CODE:

class Rational {
    private int numerator;
    private int denominator;

    // Part One: Constructors
    public Rational(int numerator, int denominator) {
        this.numerator = numerator;
        this.denominator = denominator;
        normalize();
    }

    public Rational(int wholeNumber) {
        this(wholeNumber, 1);
    }

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

    // Part One: Static methods for addition, subtraction, multiplication, and division
    public static Rational add(Rational r1, Rational r2) {
        int numerator = (r1.numerator * r2.denominator) + (r2.numerator * r1.denominator);
        int denominator = r1.denominator * r2.denominator;
        return new Rational(numerator, denominator);
    }

    public static Rational subtract(Rational r1, Rational r2) {
        int numerator = (r1.numerator * r2.denominator) - (r2.numerator * r1.denominator);
        int denominator = r1.denominator * r2.denominator;
        return new Rational(numerator, denominator);
    }

    public static Rational multiply(Rational r1, Rational r2) {
        int numerator = r1.numerator * r2.numerator;
        int denominator = r1.denominator * r2.denominator;
        return new Rational(numerator, denominator);
    }

    public static Rational divide(Rational r1, Rational r2) {
        int numerator = r1.numerator * r2.denominator;
        int denominator = r1.denominator * r2.numerator;
        return new Rational(numerator, denominator);
    }

    // Part Two: Instance methods for addition, subtraction, multiplication, and division
    public Rational add(Rational other) {
        int numerator = (this.numerator * other.denominator) + (other.numerator * this.denominator);
        int denominator = this.denominator * other.denominator;
        return new Rational(numerator, denominator);
    }

    public Rational subtract(Rational other) {
        int numerator = (this.numerator * other.denominator) - (other.numerator * this.denominator);
        int denominator = this.denominator * other.denominator;
        return new Rational(numerator, denominator);
    }

    public Rational multiply(Rational other) {
        int numerator = this.numerator * other.numerator;
        int denominator = this.denominator * other.denominator;
        return new Rational(numerator, denominator);
    }

    public Rational divide(Rational other) {
        int numerator = this.numerator * other.denominator;
        int denominator = this.denominator * other.numerator;
        return new Rational(numerator, denominator);
    }

    // Accessor and mutator methods
    public int getNumerator() {
        return numerator;
    }

    public int getDenominator() {
        return denominator;
    }

    public void setNumerator(int numerator) {
        this.numerator = numerator;
    }

    public void setDenominator(int denominator) {
        this.denominator = denominator;
        normalize();
    }

    // Method to normalize the sign of the rational number
    private void normalize() {
        if (denominator < 0) {
            numerator = -numerator;
            denominator = -denominator;
        }
    }

    // Method to check if two rational numbers are equal
    public boolean equals(Rational other) {
        return this.numerator * other.denominator == this.denominator * other.numerator;
    }

    // Method to represent the rational number as a string
    public String toString() {
        return numerator + "/" + denominator;
    }
}

// Test program
public class RationalTest {
    public static void main(String[] args) {
        Rational r1 = new Rational(1, 2);
        Rational r2 = new Rational(3, 4);
        Rational r3 = Rational.add(r1, r2);
        Rational r4 = Rational.subtract(r1, r2);
        Rational r5 = Rational.multiply(r1, r2);
        Rational r6 = Rational.divide(r1, r2);

        System.out.println("r1: " + r1); // Output: 1/2
        System.out.println("r2: " + r2); // Output: 3/4
        System.out.println("r3 (r1 + r2): " + r3); // Output: 5/4
        System.out.println("r4 (r1 - r2): " + r4); // Output: -1/4
        System.out.println("r5 (r1 * r2): " + r5); // Output: 3/8
        System.out.println("r6 (r1 / r2): " + r6); // Output: 2/3

        // Testing Part Two: Instance methods
        r3 = r1.add(r2);
        r4 = r1.subtract(r2);
        r5 = r1.multiply(r2);
        r6 = r1.divide(r2);

        System.out.println("r3 (r1 + r2): " + r3); // Output: 5/4
        System.out.println("r4 (r1 - r2): " + r4); // Output: -1/4
        System.out.println("r5 (r1 * r2): " + r5); // Output: 3/8
        System.out.println("r6 (r1 / r2): " + r6); // Output: 2/3
    }
}

 

OUTPUT OF THE PROGRAM CODE:

r1: 1/2
r2: 3/4
r3 (r1 + r2): 10/8
r4 (r1 - r2): -2/8
r5 (r1 * r2): 3/8
r6 (r1 / r2): 4/6
r3 (r1 + r2): 10/8
r4 (r1 - r2): -2/8
r5 (r1 * r2): 3/8
r6 (r1 / r2): 4/6

 

0 0

Discussions

Post the discussion to improve the above solution.