In: Computer Science
IN JAVA
Rational Numbers
Create a Rational number class in the same style as the Complex number class created in class. That is, implement the following methods:
You must also provide a Main class and main method to fully test your Rational number class.
==================================================================
Program code to copy:
RationalNumber.java
/**
* RationalNumber.java
* Java class to represent one rational number with a numerator and
denominator.
*/
public class RationalNumber {
/* Class instance variables or data fields */
private int numerator, denominator;
/**
* Constructor: Sets up the rational number by ensuring
a nonzero denominator
* and making only the numerator signed.
* @param numer
* @param denom
*/
public RationalNumber(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();
}
/**
* Accessor method for field numerator
* @return numerator of this rational number.
*/
public int getNumerator() {
return numerator;
}
/**
* Accessor method for field denominator
* @return denominator of this rational number.
*/
public int getDenominator() {
return denominator;
}
/* Method to find reciprocal of this rational number.
*/
public RationalNumber reciprocal() {
return new
RationalNumber(denominator, numerator);
}
/**
* Method to add this rational number to the one passed
as a parameter.
* A common denominator is found by multiplying the
individual denominators.
* @param op2
* @return addition of two rational numbers.
*/
public RationalNumber add(RationalNumber op2) {
int commonDenominator = denominator
* op2.getDenominator();
int numerator1 = numerator *
op2.getDenominator();
int numerator2 = op2.getNumerator()
* denominator;
int sum = numerator1 +
numerator2;
return new RationalNumber(sum,
commonDenominator);
}
/**
* Method to Subtract the rational number passed as a
parameter
* from this rational number.
* @param op2
* @return difference of two rational numbers.
*/
public RationalNumber subtract(RationalNumber op2)
{
int commonDenominator = denominator
* op2.getDenominator();
int numerator1 = numerator *
op2.getDenominator();
int numerator2 = op2.getNumerator()
* denominator;
int difference = numerator1 -
numerator2;
return new
RationalNumber(difference, commonDenominator);
}
/**
* Method to Multiply this rational number by the one
passed as a parameter.
* @param op2
* @return product of two rational numbers
*/
public RationalNumber multiply(RationalNumber op2)
{
int numer = numerator *
op2.getNumerator();
int denom = denominator *
op2.getDenominator();
return new RationalNumber(numer,
denom);
}
/**
* Divides this rational number by the one passed as a
parameter
* by multiplying by the reciprocal of the second
rational.
* @param op2
* @return division of two rational numbers.
*/
public RationalNumber divide(RationalNumber op2)
{
return
multiply(op2.reciprocal());
}
/**
* Determines if this rational number is equal to the
one passed
* as a parameter. Assumes they are both reduced.
* @param op2
* @return true / false indicating equality of two
rational numbers
*/
public boolean equals(RationalNumber op2) {
return (numerator ==
op2.getNumerator() && denominator ==
op2.getDenominator());
}
/**
* @return rational number in string format
*/
public String toString() {
String result;
if (numerator == 0)
result =
"0";
else if (denominator == 1)
result =
numerator + "";
else
result =
numerator + "/" + denominator;
return result;
}
/**
* Method to minimize the rational number
* 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;
}
}
/**
* Method to find the greatest common divisor of the
two
* positive parameters. Uses Euclid's algorithm.
* @param num1
* @param num2
* @return greatest common divisor of the two positive
parameters.
*/
private int gcd(int num1, int num2) {
while (num1 != num2)
if (num1 >
num2)
num1 = num1 - num2;
else
num2 = num2 - num1;
return num1;
}
} /* End of class RationalNUmber */
TestRationalNumber.java
/* Java class to test RationalNumber class */
public class TestRationalNumber {
/* Main or Driver method to test RationalNumber
class methods */
public static void main(String[] args) {
/* Creating instances of
RationalNumber class */
RationalNumber rn1 = new
RationalNumber(10, 4);
RationalNumber rn2 = new
RationalNumber(11, 6);
/* Displaying rational numbers in
string format */
System.out.println("Rational
Number1: " + rn1);
System.out.println("Rational
Number2: " + rn2);
/* Displaying manipulations with
rational numbers */
System.out.println();
System.out.println("Sum: "+
rn1.add(rn2));
System.out.println("Difference: "+
rn1.subtract(rn2));
System.out.println("Product: "+
rn1.multiply(rn2));
System.out.println("Division:
"+rn1.divide(rn2));
} /* End of main method */
} /* End of TestRationalNumber class */
-------------------------------------------------------
Program code screenshot:
RationalNumber.java
TestRationalNumber.java
-----------------------------------------------------------
Sample Output:
==================================================================
Hope it will helpfull and do commets for any additional info if needed. Thankyou
==================================================================