Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

//******************************************************************** // RationalNumber2.java Author: Lewis/Loftus // // Solution to Programming Project 7.4 //******************************************************************** public class RationalNumber2 implements Comparable { private int numerator, denominator; private final

image text in transcribed

//********************************************************************

// 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

{

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;

}

}

2. Implement Programming Project 7.4 p. 375. Modify the RationalNumber class so that it implements the Comparable interface. To perform the comparison, compute an equivalent floating point value from the numerator and denomi- nator for both RationalNumber objects, then compare them using a tolerance value of 0.0001. Write a main driver to test your modifications. PP 7.4 Use this folder PP7.4-1.zip and complete the code. You can find the place in the code to complete under the comment /"YOUR SOLUTION GOES HERE

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning Apache Cassandra Development

Authors: Vivek Mishra

1st Edition

1484201426, 9781484201428

More Books

Students also viewed these Databases questions