Create a class called Rational for performing arithmetic with fractions. Write a
ID: 3891773 • Letter: C
Question
Create a class called Rational for performing arithmetic with fractions. Write a program to test your class. Use integer values to represent the private instance variables of the class - the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it's declared. The constructor should store the fraction in reduced form. The fraction 2/4 is equivalent to 1/2 and would be stored in the object as 1 in the numerator and 2 in the denominator. Provide a no-argument constructor with default values in case no initializers are provided. You can assume all the rational numbers are positive numbers. Provide public methods that perform each of the following operations:
Add two Rational numbers: The result of the addition should be stored in reduced form. Implement this as a static method.
Subtract two Rational numbers: The result of the subtraction should be stored in reduced form. Implement this as a static method.
Multiply two Rational numbers: The result of the multiplication should be stored in reduced form. Implement this as a staticmethod.
Divide two Rational numbers: The result of the division should be stored in reduced form. Implement this as a static method.
Return a String representation of a Rational number in the form a/b, where a is the numerator and b is the denominator.
Return a String representation of a Rational number in floating-point format. Consider providing formatting capabilities that enable the user of the class to specify the number of digits of precision to the right of the decimal point.
Explanation / Answer
PROGRAM (JAVA)
import java.util.*;
class Rational
{
private int numer, denom;
public Rational(int num, int den)
{
if (den == 0)
den = 1;
if (den < 0)
{
num = num * -1;
den = den * -1;
}
numer = num;
denom = den;
reduce();
}
//To return numerator
public int getNumerator()
{
return numer;
}
//To return denominator
public int getDenominator()
{
return denom;
}
public Rational reciprocal()
{
return new Rational(denom, numer);
} //End reciprocal
//For adding rationals
public Rational add(Rational op2)
{
int commonDenominator = denom * op2.getDenominator();
int num1 = numer * op2.getDenominator();
int num2 = op2.getNumerator() * denom;
int sum = num1 + num2;
return new Rational(sum, commonDenominator);
} //End Addition
//For subtracting rationals
public Rational subtract(Rational op2)
{
int commonDenominator = denom * op2.getDenominator();
int num1 = numer * op2.getDenominator();
int num2 = op2.getNumerator() * denom;
int diff = num1 - num2;
return new Rational(diff, commonDenominator);
} //End Subtraction
public Rational multiply(Rational op2)
{
int num = numer * op2.getNumerator();
int den = denom * op2.getDenominator();
return new Rational(num, den);
} //End Multiply
//For Dividing rationals
public Rational divide(Rational op2)
{
return multiply(op2.reciprocal());
} //End Divide
public boolean equals(Rational op2)
{
return(numer == op2.getNumerator() && denom == op2.getDenominator() );
}
public boolean lessThan(Rational op2)
{
return(numer * op2.getDenominator() < denom * op2.getNumerator());
}
public boolean greaterThan(Rational op2)
{
return(numer * op2.getDenominator() > denom * op2.getNumerator());
}
public String toString()
{
String result;
if (numer == 0) result = "0";
else
if (denom == 1) result = numer + "";
else
result = numer + "/" + denom;
return result;
} //End toString
//Reduced form
private void reduce()
{
if (numer != 0)
{
int common = gcd(Math.abs(numer), denom);
numer = numer / common;
denom = denom / common;
}
} //End Reduce
private int gcd(int num1, int num2)
{
while (num1 != num2)
if (num1 > num2)
num1 = num1 - num2;
else
num2 = num2 - num1;
return num1;
} //End GCD
} //End Rational
class TestRational
{
// implement menu() method
public static int menu()
{
int ch;
System.out.println(" RATIONAL NUMBERS ARITHEMATIC OPERATIONS");
System.out.print("----------------------------------------- ");
System.out.println("1. ADDITION");
System.out.println("2. SUBTRACTION");
System.out.println("3. MULTIPLICATION");
System.out.println("4. DIVISION");
System.out.println("5. EXIT");
System.out.print("YOUR CHOICE: ");
Scanner scr=new Scanner(System.in);
ch=scr.nextInt();
return ch;
}
public static void main(String args[])
{
while(true)
{
Rational rat1=new Rational(2,4); //8,9
Rational rat2=new Rational(3,9); //1,9
String s1=rat1.toString();
String s2=rat2.toString();
int ch=menu();
switch(ch)
{
case 1: rat1=rat1.add(rat2);
System.out.println(" ADDITION of "+s1+" and "+s2+" Value is: "+rat1.toString()); break;
case 2: rat1=rat1.subtract(rat2);
System.out.println(" SUBTRACTION of "+s1+" and "+s2+" Value is: "+rat1.toString()); break;
case 3: rat1=rat1.multiply(rat2);
System.out.println(" MULTIPLY of "+s1+" and "+s2+" Value is: "+rat1.toString()); break;
case 4: rat1=rat1.divide(rat2);
System.out.println(" DIVISION of "+s1+" and "+s2+" Value is: "+rat1.toString()); break;
case 5: System.exit(0); break;
default: System.out.println("Wrong Choice! Try Again?");
}
}
}
}
OUTPUT
F:>javac TestRational.java
F:>java TestRational
RATIONAL NUMBERS ARITHEMATIC OPERATIONS
-----------------------------------------
1. ADDITION
2. SUBTRACTION
3. MULTIPLICATION
4. DIVISION
5. EXIT
YOUR CHOICE: 1
ADDITION of 1/2 and 1/3 Value is: 5/6
RATIONAL NUMBERS ARITHEMATIC OPERATIONS
-----------------------------------------
1. ADDITION
2. SUBTRACTION
3. MULTIPLICATION
4. DIVISION
5. EXIT
YOUR CHOICE: 2
SUBTRACTION of 1/2 and 1/3 Value is: 1/6
RATIONAL NUMBERS ARITHEMATIC OPERATIONS
-----------------------------------------
1. ADDITION
2. SUBTRACTION
3. MULTIPLICATION
4. DIVISION
5. EXIT
YOUR CHOICE: 3
MULTIPLY of 1/2 and 1/3 Value is: 1/6
RATIONAL NUMBERS ARITHEMATIC OPERATIONS
-----------------------------------------
1. ADDITION
2. SUBTRACTION
3. MULTIPLICATION
4. DIVISION
5. EXIT
YOUR CHOICE: 4
DIVISION of 1/2 and 1/3 Value is: 3/2
RATIONAL NUMBERS ARITHEMATIC OPERATIONS
-----------------------------------------
1. ADDITION
2. SUBTRACTION
3. MULTIPLICATION
4. DIVISION
5. EXIT
YOUR CHOICE: 5
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.