Question: Hi I need help with the following question: 2. Chapter 8: Programming Project #1 Rational Number a. Download the attached RationalNumber.java (The file is at

Hi I need help with the following question:

2. Chapter 8: Programming Project #1 Rational Number a. Download the attached RationalNumber.java (The file is at the bottom) b. Background: a rational number represents a fraction, e.g. 3 / 5. Here, 3 is the numerator, and 5 is the denominator; both are integers (can be either positive or negative). The denominator cannot be 0. c. Implement two constructors: public RationalNumber(int numerator, int denominator) It constructs a new rational number. You can assume that the denominator will never be 0. public RationalNumber() It constructs a new rational number to represent 0 / 1 d. Implement the two accessors: public int getDenominator() It returns this rational numbers denominator. For example, for 3 / 5, it returns 5. public int getNumerator() It returns this rational numbers numerator. For example, for 3 / 5, it returns 3. e. Implement the toString method: public String toString() -- It returns a String representation of this rational number, such as 3/5. -- If the denominator is 1, omit it. For example, for 4 / 1, it returns 4. -- If the numerator is 0, omit denominator. For example, for 0 / 8, it returns 0. -- If both of the numerator and the denominator are negative, return positive. For example, for -3 / -5, it returns 3/5. CS 210 W. Li Assignment 8 -- If the numerator is positive and the denominator is negative, return the negative sign in front of the numerator. For example, for 3 / -5, it returns -3/5 -- You do NOT need to worry about reduced form (e.g. for 3 / 6, just returns 3/6 is good enough, no need to return 1/2). f. Implement the methods to add, subtract, multiply and divide another rational number. Here is the method signature for add: public RationalNumber add(RationalNumber numberToAdd) In the method, you should compute and return the result for the addition. For example, if r1 is 3 / 5, r2 is -1 / 5, r1.add(r2) should return a value of 2 / 5. Whether you choose to update the value of r1 is your choice -- in other words, it wont affect your grade, as long as the value returned from the method is correct. Again, you dont need to worry about reduced form.

public class RationalNumber { // TODO: implement the following methods // public RationalNumber(int numerator, int denominator) { // } // public RationalNumber() { // } // public int getDenominator() { // } // public int getNumerator() { // } // public String toString() { // } // public RationalNumber add(RationalNumber other) { // } // public RationalNumber subtract(RationalNumber other) { // } // public RationalNumber multiply(RationalNumber other) { // } // public RationalNumber divide(RationalNumber other) { // } } 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!