Hi I need help with the following question: 2. Chapter 8: Programming Project #1
ID: 3676032 • Letter: H
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 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 number’s denominator. For example, for 3 / 5, it returns 5.
public int getNumerator()
It returns this rational number’s 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 won’t
affect your grade, as long as the value returned from the method is correct.
Again, you don’t need to worry about reduced form.
Explanation / Answer
public class RationalNumber {
// TODO: implement the following methods
int numerator;
int denominator;
public RationalNumber(int numerator, int denominator) {
if (denominator == 0)
denominator = 1;
// Make the numerator "store" the sign
if (denominator < 0) {
numerator = numerator * -1;
denominator = denominator * -1;
}
this.numerator = numerator;
this.denominator = denominator;
}
public RationalNumber() {
numerator = 0;
denominator = 1;
}
public int getDenominator() {
return denominator;
}
public int getNumerator() {
return numerator;
}
public String toString() {
String result;
if (numerator == 0)
result = "0";
else if (denominator == 1)
result = numerator + "";
else
result = numerator + "/" + denominator;
return result;
}
public RationalNumber add(RationalNumber other) {
int commonDenominator = denominator * other.getDenominator();
int numerator1 = numerator * other.getDenominator();
int numerator2 = other.getNumerator() * denominator;
int sum = numerator1 + numerator2;
return new RationalNumber(sum, commonDenominator);
}
public RationalNumber subtract(RationalNumber other) {
int commonDenominator = denominator * other.getDenominator();
int numerator1 = numerator * other.getDenominator();
int numerator2 = other.getNumerator() * denominator;
int difference = numerator1 - numerator2;
return new RationalNumber(difference, commonDenominator);
}
public RationalNumber multiply(RationalNumber other) {
int numer = numerator * other.getNumerator();
int denom = denominator * other.getDenominator();
return new RationalNumber(numer, denom);
}
public RationalNumber divide(RationalNumber other) {
return multiply(other.reciprocal());
}
public RationalNumber reciprocal() {
return new RationalNumber(denominator, numerator);
}
private void reduce() {
if (numerator != 0) {
int common = gcd(Math.abs(numerator), denominator);
numerator = numerator / common;
denominator = denominator / common;
}
}
private int gcd(int num1, int num2) {
while (num1 != num2)
if (num1 > num2)
num1 = num1 - num2;
else
num2 = num2 - num1;
return num1;
}
public static void main(String[] args) {
RationalNumber rationalNumber1 = new RationalNumber(5, 2);
RationalNumber rationalNumber2 = new RationalNumber(2, 3);
System.out.println("Addition of r1 and r2:"
+ rationalNumber1.add(rationalNumber2));
System.out.println("Subtraction of r1 and r2:"
+ rationalNumber1.subtract(rationalNumber2));
}
}
OUTPUT:
Addition of r1 and r2:19/6
Subtraction of r1 and r2:11/6