Write a RationalNumber class having public methods for public RationalNumber();
ID: 3625638 • Letter: W
Question
Write a RationalNumber class having public methods for
public RationalNumber(); //Initializes to 0/1
public RationalNumber(int n, int d); //Initializes to n/d
public RationalNumber(RationalNumber x); //Initializes to x's values
public void add(RationalNumber other) //this = this + other
public void subtract(RationalNumber other) //this = this - other
public void multiply(RationalNumber other) // this = this * other
public void divide(RationalNumber other) //this = this / this
public String toString() //Creates string representation
public boolean equals(RationalNumber other) //Is this == other?
Any methods not listed above and all data fields should be private.
- r.equals(s) should return true if r and s have the same value.
- toString should create a String representation of the rational, in reduced form. If a number is negative the string should begin with -. If it is a whole number (including zero) it should be shown as over 1. For example. If I initialize with n=4 and d=-2, toString should return "-2/1".
- If d=0 is passed to the constructor, throw an IllegalArgumentException.
Explanation / Answer
import java.util.Scanner; // Needed for Scanner class
import java.io.*; // Needed for file classes
import java.text.DecimalFormat;
public class Rational
{
private int numerator, denominator;
// no-argument constructor
public Rational()
{
numerator = 1;
denominator = 1;
}
// initialize numerator part to n and denominator part to d
public Rational( int theNumerator, int theDenominator )
{
try
{
numerator = theNumerator;
if(theDenominator==0)
throw new IllegalArgumentException("Zero value at constructor");
}
catch(IllegalArgumentException e)
System.out.println(e.getMessage());
denominator = theDenominator;
reduce();
}
// add two Rational numbers
public Rational sum( Rational right )
{
int resultDenominator = denominator * right.denominator;
int resultNumerator = numerator * right.denominator +
right.numerator * denominator;
return new Rational( resultNumerator, resultDenominator );
}
// subtract two Rational numbers
public Rational subtract( Rational right )
{
int resultDenominator = denominator * right.denominator;
int resultNumerator = numerator * right.denominator -
right.numerator * denominator;
return new Rational( resultNumerator, resultDenominator );
}
// multiply two Rational numbers
public Rational multiply( Rational right )
{
return new Rational( numerator * right.numerator,
denominator * right.denominator );
}
// divide two Rational numbers
public Rational divide( Rational right )
{
return new Rational( numerator * right.denominator,
denominator * right.numerator );
}
// reduce the fraction
private void reduce()
{
int gcd = 0;
int smaller;
if ( numerator < denominator )
smaller = numerator;
else
smaller = denominator;
for ( int divisor = smaller; divisor >= 2; divisor-- )
{
if ( numerator % divisor == 0 && denominator % divisor == 0 )
{
gcd = divisor;
break;
}
}
if ( gcd != 0 )
{
numerator /= gcd;
denominator /= gcd;
}
}
// return String representation of a Rational number
public String toRationalString()
{
return numerator + "/" + denominator;
}
// return true if equal Rational numbers
public boolean isEqual( Rational r1 )
{
if( numerator ==r1.numerator &&denominator==r1.denominator)
return true;
else
return false;
}
} // end class Rational
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.