Complete the code where it says \"YOUR SOLUTION GOES HERE\": //*****************
ID: 3791746 • Letter: C
Question
Complete the code where it says "YOUR SOLUTION GOES HERE":
//********************************************************************
// RationalNumber2.java Author: Lewis/Loftus
//
// Solution to Programming Project 7.4
//********************************************************************
public class RationalNumber2 implements Comparable
{
private int numerator, denominator;
private final double TOLERANCE = 0.0001;
//-----------------------------------------------------------------
// Sets up the rational number by ensuring a nonzero denominator
// and making only the numerator signed.
//-----------------------------------------------------------------
public RationalNumber2(int numer, int denom)
{
if (denom == 0)
denom = 1;
// Make the numerator "store" the sign
if (denom < 0)
{
numer = numer * -1;
denom = denom * -1;
}
numerator = numer;
denominator = denom;
reduce();
}
//-----------------------------------------------------------------
// Returns the numerator of this rational number.
//-----------------------------------------------------------------
public int getNumerator()
{
return numerator;
}
//-----------------------------------------------------------------
// Returns the denominator of this rational number.
//-----------------------------------------------------------------
public int getDenominator()
{
return denominator;
}
//-----------------------------------------------------------------
// Returns the reciprocal of this rational number.
//-----------------------------------------------------------------
public RationalNumber2 reciprocal()
{
return new RationalNumber2(denominator, numerator);
}
//-----------------------------------------------------------------
// Adds this rational number to the one passed as a parameter.
// A common denominator is found by multiplying the individual
// denominators.
//-----------------------------------------------------------------
public RationalNumber2 add(RationalNumber2 op2)
{
int commonDenominator = denominator * op2.getDenominator();
int numerator1 = numerator * op2.getDenominator();
int numerator2 = op2.getNumerator() * denominator;
int sum = numerator1 + numerator2;
return new RationalNumber2(sum, commonDenominator);
}
//-----------------------------------------------------------------
// Subtracts the rational number passed as a parameter from this
// rational number.
//-----------------------------------------------------------------
public RationalNumber2 subtract(RationalNumber2 op2)
{
int commonDenominator = denominator * op2.getDenominator();
int numerator1 = numerator * op2.getDenominator();
int numerator2 = op2.getNumerator() * denominator;
int difference = numerator1 - numerator2;
return new RationalNumber2(difference, commonDenominator);
}
//-----------------------------------------------------------------
// Multiplies this rational number by the one passed as a
// parameter.
//-----------------------------------------------------------------
public RationalNumber2 multiply(RationalNumber2 op2)
{
int numer = numerator * op2.getNumerator();
int denom = denominator * op2.getDenominator();
return new RationalNumber2(numer, denom);
}
//-----------------------------------------------------------------
// Divides this rational number by the one passed as a parameter
// by multiplying by the reciprocal of the second rational.
//-----------------------------------------------------------------
public RationalNumber2 divide(RationalNumber2 op2)
{
return multiply(op2.reciprocal());
}
//-----------------------------------------------------------------
// Determines if this rational number is equal to the one passed
// as a parameter. Assumes they are both reduced.
//-----------------------------------------------------------------
public boolean isLike(RationalNumber2 op2)
{
return ( numerator == op2.getNumerator() &&
denominator == op2.getDenominator() );
}
//-----------------------------------------------------------------
// Returns this rational number as a string.
//-----------------------------------------------------------------
public String toString()
{
String result;
if (numerator == 0)
result = "0";
else
if (denominator == 1)
result = numerator + "";
else
result = numerator + "/" + denominator;
return result;
}
//-----------------------------------------------------------------
// Compares this RationalNumber2 object to the parameter.
//-----------------------------------------------------------------
public int compareTo(Object obj)
{
// YOUR SOLUTION GOES HERE
}
//-----------------------------------------------------------------
// Reduces this rational number by dividing both the numerator
// and the denominator by their greatest common divisor.
//-----------------------------------------------------------------
private void reduce()
{
if (numerator != 0)
{
int common = gcd(Math.abs(numerator), denominator);
numerator = numerator / common;
denominator = denominator / common;
}
}
//-----------------------------------------------------------------
// Computes and returns the greatest common divisor of the two
// positive parameters. Uses Euclid's algorithm.
//-----------------------------------------------------------------
private int gcd(int num1, int num2)
{
while (num1 != num2)
if (num1 > num2)
num1 = num1 - num2;
else
num2 = num2 - num1;
return num1;
}
}
Explanation / Answer
RationalNumber2.java
public class RationalNumber2 implements Comparable {
private int numerator, denominator;
private final double TOLERANCE = 0.0001;
// -----------------------------------------------------------------
// Sets up the rational number by ensuring a nonzero denominator
// and making only the numerator signed.
// -----------------------------------------------------------------
public RationalNumber2(int numer, int denom) {
if (denom == 0)
denom = 1;
// Make the numerator "store" the sign
if (denom < 0) {
numer = numer * -1;
denom = denom * -1;
}
numerator = numer;
denominator = denom;
reduce();
}
// -----------------------------------------------------------------
// Returns the numerator of this rational number.
// -----------------------------------------------------------------
public int getNumerator() {
return numerator;
}
// -----------------------------------------------------------------
// Returns the denominator of this rational number.
// -----------------------------------------------------------------
public int getDenominator() {
return denominator;
}
// -----------------------------------------------------------------
// Returns the reciprocal of this rational number.
// -----------------------------------------------------------------
public RationalNumber2 reciprocal() {
return new RationalNumber2(denominator, numerator);
}
// -----------------------------------------------------------------
// Adds this rational number to the one passed as a parameter.
// A common denominator is found by multiplying the individual
// denominators.
// -----------------------------------------------------------------
public RationalNumber2 add(RationalNumber2 op2) {
int num1 = numerator;
int denom1 = denominator;
int num2 = op2.getNumerator();
int denom2 = op2.getDenominator();
int num3 = (num1 * denom2) + (num2 * denom1);
int denom3 = denom1 * denom2;
RationalNumber2 r1 = new RationalNumber2(num3, denom3);
return r1;
}
// -----------------------------------------------------------------
// Subtracts the rational number passed as a parameter from this
// rational number.
// -----------------------------------------------------------------
public RationalNumber2 subtract(RationalNumber2 op2) {
int commonDenominator = denominator * op2.getDenominator();
int numerator1 = numerator * op2.getDenominator();
int numerator2 = op2.getNumerator() * denominator;
int difference = numerator1 - numerator2;
return new RationalNumber2(difference, commonDenominator);
}
// -----------------------------------------------------------------
// Multiplies this rational number by the one passed as a
// parameter.
// -----------------------------------------------------------------
public RationalNumber2 multiply(RationalNumber2 op2) {
int numer = numerator * op2.getNumerator();
int denom = denominator * op2.getDenominator();
return new RationalNumber2(numer, denom);
}
// -----------------------------------------------------------------
// Divides this rational number by the one passed as a parameter
// by multiplying by the reciprocal of the second rational.
// -----------------------------------------------------------------
public RationalNumber2 divide(RationalNumber2 op2) {
return multiply(op2.reciprocal());
}
// -----------------------------------------------------------------
// Determines if this rational number is equal to the one passed
// as a parameter. Assumes they are both reduced.
// -----------------------------------------------------------------
public boolean isLike(RationalNumber2 op2) {
return (numerator == op2.getNumerator() && denominator == op2
.getDenominator());
}
// -----------------------------------------------------------------
// Returns this rational number as a string.
// -----------------------------------------------------------------
public String toString() {
String result;
if (numerator == 0)
result = "0";
else if (denominator == 1)
result = numerator + "";
else
result = numerator + "/" + denominator;
return result;
}
// -----------------------------------------------------------------
// Compares this RationalNumber2 object to the parameter.
// -----------------------------------------------------------------
public int compareTo(Object obj) {
RationalNumber2 a=(RationalNumber2)obj;
int thisRational = numerator * a.getDenominator();
int otherRational = a.getNumerator() * denominator;
if (thisRational < otherRational){
return -1;
}else if (thisRational > otherRational ){
return 1;
}else{
return 0;
}
}
// -----------------------------------------------------------------
// Reduces this rational number by dividing both the numerator
// and the denominator by their greatest common divisor.
// -----------------------------------------------------------------
public void reduce() {
if (numerator != 0) {
int common = gcd(Math.abs(numerator), denominator);
numerator = numerator / common;
denominator = denominator / common;
}
}
// -----------------------------------------------------------------
// Computes and returns the greatest common divisor of the two
// positive parameters. Uses Euclid's algorithm.
// -----------------------------------------------------------------
private int gcd(int num1, int num2) {
// % is modulus which is the remainder of a division
// base case
if ((num1 % num2) == 0) {
return num2;
}
// recursive case
else {
return gcd(num2, num1 % num2);
}
}
}
_______________________
Test.java
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Scanner sc1 = new Scanner(System.in);
while (true) {
//Getting the First Rational number From the user
System.out.println(":: Enter Rational No 1 ::");
System.out.print("Enter Numerator =");
int num1 = sc.nextInt();
System.out.print("Enter Denominator =");
int denom1 = sc.nextInt();
//Getting the second Rational number From the user
System.out.println(":: Enter Rational No 2 ::");
System.out.print("Enter Numerator =");
int num2 = sc.nextInt();
System.out.print("Enter Denominator =");
int denom2 = sc.nextInt();
//Creating the objects to Rational Class
RationalNumber2 r = new RationalNumber2(num1, denom1);
RationalNumber2 r1 = new RationalNumber2(num2, denom2);
System.out.println(" ");
RationalNumber2 rc=r.reciprocal();
System.out.println(rc.toString()+" is the reciprocal and reduced form of (" +num1+"/"+denom1+")" );
// Reducing the Rational Numbers
System.out.println("** Rational Reduced Forms **");
r.reduce();
System.out.println("(" +num1+"/"+denom1+") reduced to :" + r.toString());
r1.reduce();
System.out.println("(" +num2+"/"+denom2+") reduced to :" + r1.toString());
System.out.println(" ** Adding two Rational Numbers **");
// Addition of two Rational Numbers
RationalNumber2 r11=r.add(r1);
System.out.println("(" + r.toString() + ") +" + "(" + r1.toString()+ ") = " + r11.toString());
System.out.println(" ** Subtracting two Rational Numbers **");
RationalNumber2 r12=r.subtract(r1);
System.out.println("(" + r.toString() + ") -" + "(" + r1.toString()+ ") = " + r12.toString());
System.out.println(" ** Multiplying two Rational Numbers **");
RationalNumber2 r13=r.multiply(r1);
System.out.println("(" + r.toString() + ") *" + "(" + r1.toString()+ ") = " + r13.toString());
System.out.println(" ** Dividing two Rational Numbers **");
RationalNumber2 r14=r.divide(r1);
System.out.println("(" + r.toString() + ") /" + "(" + r1.toString()+ ") = " + r14.toString());
System.out.println(" ** Comparing two Rational Numbers **");
// Comparing Two Rational Numbers
int com = r.compareTo(r1);
if (com == -1)
System.out.println("(" + r.toString() + ") less than (" + r1.toString()+ ")");
else if(com == 1)
System.out.println("(" + r.toString() + ") greater than (" + r1.toString()+ ")");
else if(com==0)
System.out.println("(" + r.toString() + ") equal to (" + r1.toString()+ ")");
System.out.print("Do you want to continue(Y/N):");
char c = sc1.next(".").charAt(0);
if (c == 'Y' || c == 'y')
continue;
else {
System.out.println(":: Program Exit ::");
break;
}
}
}
}
__________________________
Output:
:: Enter Rational No 1 ::
Enter Numerator =5
Enter Denominator =25
:: Enter Rational No 2 ::
Enter Numerator =25
Enter Denominator =125
5 is the reciprocal and reduced form of (5/25)
** Rational Reduced Forms **
(5/25) reduced to :1/5
(25/125) reduced to :1/5
** Adding two Rational Numbers **
(1/5) +(1/5) = 2/5
** Subtracting two Rational Numbers **
(1/5) -(1/5) = 0
** Multiplying two Rational Numbers **
(1/5) *(1/5) = 1/25
** Dividing two Rational Numbers **
(1/5) /(1/5) = 1
** Comparing two Rational Numbers **
(1/5) equal to (1/5)
Do you want to continue(Y/N):y
:: Enter Rational No 1 ::
Enter Numerator =3
Enter Denominator =81
:: Enter Rational No 2 ::
Enter Numerator =27
Enter Denominator =243
27 is the reciprocal and reduced form of (3/81)
** Rational Reduced Forms **
(3/81) reduced to :1/27
(27/243) reduced to :1/9
** Adding two Rational Numbers **
(1/27) +(1/9) = 4/27
** Subtracting two Rational Numbers **
(1/27) -(1/9) = -2/27
** Multiplying two Rational Numbers **
(1/27) *(1/9) = 1/243
** Dividing two Rational Numbers **
(1/27) /(1/9) = 1/3
** Comparing two Rational Numbers **
(1/27) less than (1/9)
Do you want to continue(Y/N):n
:: Program Exit ::
____________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.