..ooo T-Mobile 10:09 PM @ 14% ps://moodle.Oakland.edL] Write a class for rationa
ID: 3869900 • Letter: #
Question
..ooo T-Mobile 10:09 PM @ 14% ps://moodle.Oakland.edL] Write a class for rational numbers. Each object in the class should have two integers values that define the rational numbers: the numerator and the denominator. For example, the fraction 5/6 would have a numerator of 5 and a denominator of 6. Include a constructor with two arguments that can used to set the numerator and denominator (forbidding zero in the denominator). Also provide a no- arguments constructor that has zero for the numerator and 1 for the Include a method that prints a rational number to System.out in a normal form (so that the denominator is as small as possible). Note that the numerator and denominator (or both) may contain a minus sign, but when a rational number is printed, the denominator should never include a minus sign. So if the numerator is 1 and the denominator is -2,then the printing method should print-1/2 Include a function to normalize the values stored so that, after normalization, the denominator is positive and as small as possible. For example, after normalization, 4-8 would be represented as -1/2. Write static methods for the usual arithmetic operators to provide addition, subtraction, multiplication, and division of two rational numbers. Write static Boolean methods to provide the usual comparison operations to allow comparison of two rational numbers Hints: Two rational numbers a/b and c/d are equal if a'd equals c"b. For positive rational numbers, a/b is less than c/d provided that a'd is less than c'b.Explanation / Answer
Given below is the java class for representing Rational and a test program. Hope it helps. If it did, please do rate the answer. Thank you.
Rational.java
public class Rational {
private int numerator;
private int denominator;
public Rational()
{
set(0, 1);
}
public Rational(int num, int denom)
{
set(num, denom);
}
private void set(int num, int denom)
{
numerator = num;
denominator = denom;
if(denominator == 0)
{
System.out.println("Denominator is zero. Setting it 1");
denominator = 1;
}
normalize();
}
//converts the rational into simplest form by dividing both numerator and denominator by their GCD
private void normalize()
{
if(denominator < 0) //if denominator is -ve, move the -ve sign to numerator
{
denominator = -denominator;
numerator = -numerator;
}
int gcd = GCD(numerator, denominator);
numerator /= gcd;
denominator /= gcd;
}
public static Rational add(Rational first, Rational second)
{
//make the denominators same
int resDenominator = first.denominator * second.denominator;
//now adjust the numerators accoringly using the multiplying factor
int resNumerator = first.numerator * second.denominator + second.numerator * first.denominator;
return new Rational(resNumerator, resDenominator);
}
public static Rational sub(Rational first, Rational second)
{
//make the denominators same
int resDenominator = first.denominator * second.denominator;
//now adjust the numerators accoringly using the multiplying factor
int resNumerator = first.numerator * second.denominator - second.numerator * first.denominator;
return new Rational(resNumerator, resDenominator);
}
public static Rational mul(Rational first, Rational second)
{
int resNumerator = first.numerator * second.numerator;
int resDenominator = first.denominator * second.denominator;
return new Rational(resNumerator, resDenominator);
}
public static Rational div(Rational first, Rational second)
{
int resNumerator = first.numerator * second.denominator;
int resDenominator = first.denominator * second.numerator;
return new Rational(resNumerator, resDenominator);
}
public static boolean equals(Rational first, Rational second)
{
if(first.numerator * second.denominator == first.denominator * second.numerator)
return true;
else
return false;
}
public static boolean lessThan(Rational first, Rational second)
{
if(first.numerator * second.denominator < first.denominator * second.numerator)
return true;
else
return false;
}
public static boolean greaterThan(Rational first, Rational second)
{
if(first.numerator * second.denominator > first.denominator * second.numerator)
return true;
else
return false;
}
public String toString()
{
return numerator + "/" + denominator;
}
//returns the Greatest common divisor i.e the biggest number dividing both the numbers, which will be used to normalize the fraction
private static int GCD(int num1, int num2)
{
int i = 1;
int gcd = 1;
//convert both to +ve
if(num1 < 0)
num1 = -num1;
if(num2 < 0)
num2 = -num2;
//find the biggest number dividin both numbers with 0 remainder
while( i <= num1 && i <= num2)
{
if(num1 % i == 0 && num2 %i == 0)
gcd = i;
i++;
}
return gcd;
}
}
RationalTest.java
public class RationalTest {
public static void main(String[] args) {
Rational f1 = new Rational(2, 3);
Rational f2 = new Rational(3, 6);
System.out.println("f1 = " + f1);
System.out.println("f2 = " + f2);
System.out.println("f1 + f2 = " + f1 + " + " + f2 + " = " + Rational.add(f1, f2));
System.out.println("f1 - f2 = " + f1 + " - " + f2 + " = " + Rational.sub(f1, f2));
System.out.println("f1 * f2 = " + f1 + " * " + f2 + " = " + Rational.mul(f1, f2));
System.out.println("f1 / f2 = " + f1 + " / " + f2 + " = " + Rational.div(f1, f2));
if(Rational.equals(f1, f2))
System.out.println(f1 + " is equal to " + f2);
else if(Rational.lessThan(f1, f2))
System.out.println(f1 + " is less than " + f2);
else
System.out.println(f1 + " is greater than " + f2);
}
}
output
f1 = 2/3
f2 = 1/2
f1 + f2 = 2/3 + 1/2 = 7/6
f1 - f2 = 2/3 - 1/2 = 1/6
f1 * f2 = 2/3 * 1/2 = 1/3
f1 / f2 = 2/3 / 1/2 = 4/3
2/3 is greater than 1/2
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.