Program Names: Rational.java and RationalDriver.java Define a class for rational
ID: 3766507 • Letter: P
Question
Program Names: Rational.java and RationalDriver.java
Define a class for rational numbers. A rational number is a number that can be represented as the quotient of two integers. For example, ½, ¾, and so forth are all rational numbers (by ½ and so forth, we mean the mathematical meaning of the fraction, not the integer division this expression would produce in a Java program.) Represent rational numbers as two values of type int, one for the numerator, and one for the denominator. Your class should have two instance variables of type int. The name of the class is Rational. Include a default constructor and an overloaded constructor with two arguments that are used to set the instance variables of an object of the Rational class to any two integer values. Note that the numerator, the denominator, or both may be negative.
You should include a method to normalize the sign of the rational number so that the denominator is always positive and the numerator is either positive of negative. For example, after normalization, 4/-8 would be the same as -4/8.
Define the following methods (using the names in parenthesis) for addition (add), subtraction (subtract), multiplication (multiply), and division (divide) of objects of your class, Rational. Each of these methods must accept a Rational object as a single parameter. Arithmetic operations on fractions are defined by the following rules:
a/b + c/d = (ad + bc) / bd
a/b –c/d = (ad-bc) / bd
a/b * c/d = ac/db
(a/b) / (c/d) = ad / bc, where c/d 0
These are instance methods with a single argument that DO NOT RETURN A VALUE. The result is a change in the state of the value of the data members of the calling object. For example, the add method (for addition) has a calling object and one argument. Therefore, if rationalNum1 has a numerator value of 1 and a denominator value of 2 and rationalNum2 has a numerator value of 1 and a denominator value of 4, the method call,
rationalNum1.add(rationalNum2);
changes the values of the instance variables of rationalNum1 so they represent the result of adding rationalNum2 to the original version of rationalNum1 (numerator is 3 and denominator is 4).
Define observer (getter) and mutator (setter) methods. In addition, define the following method:
public boolean equals (object other)
public String toString()
Hint: Two rational numbers a/b and c/d are equal if a*d equals c*b. Guidelines for overriding the equals method:
Use the == operator to check if the argument is a reference to itself.
Use the instanceof operator to check if the argument has the correct data type.
Cast the argument to the correct type.
For each significant data member, test for equality.
Test these three properties:
-Is it symmetric?
-Is it transitive?
-Is it consistent?
Write a second class called RationalDriver containing a main method. Your method will allow the user to thoroughly test all of the methods in your Rational class. The main method should take keyboard input fom the user; instantiate two Rational objects, and then ask the user which operations to perform. Allow the user to continue performing operations on rational numbers until the user elects to quit. For example:
Enter the numerator for rational number #1:
Enter the denominator for rational number #1:
Enter the numerator for rational number #2:
Enter the denominator for rational number #2:
Enter the corresponding number for the desired action,
1. Addition
2. Subtraction
3.Multiplication
4.Division
5.Test for Equality
6.Change 1st rational number
7.Change 2nd rational number
8.Exit
Develop this program in stages:
Default and Parameterized Constructors
toString() to display fraction - print out fraction to test it
Normalize fraction after constructor executes
Implement method – print out results
Repeat... for the remaining project requirements
Loop to allow multiple rational number to be input
Your file names should be Rational.java and RationalDriver.java
Explanation / Answer
public class Rational
{
//variable numer is used for numerator and denomin used for denominator
private int numer, denomin;
public Rational (int numer, int denom)
{
if (denom == 0)
denom = 1;
if (denom < 0)
{
numer = numer * -1;
denom = denom * -1;
}
numer = numer;
denomin = denom;
reduce();
}
//getnumer method is used for numerator
public int getnumer ()
{
return numer;
}
//getdenomin method is used for denominator
public int getdenomin ()
{
return denomin;
}
public Rational multiply (RationalNumber op2)
{
int numer = numer * op2.getnumer();
int denom = denomin * op2.getdenomin();
return new Rational (numer, denom);
}
public Rational add (Rational op2)
{
int commondenomin = denomin * op2.getdenomin();
int numer1 = numer * op2.getdenomin();
int numer2 = op2.getnumer() * denomin;
int sum = numer1 + numer2;
return new Rational(sum, commondenomin);
}
public Rational subtract (Rational op2)
{
int commondenomin = denomin * op2.getdenomin();
int numer1 = numer * op2.getdenomin();
int numer2 = op2.getnumer() * denomin;
int difference = numer1 - numer2;
return new Rational (difference, commondenomin);
}
public Rational divide (Rational op2)
{
return multiply (op2.reciprocal());
}
public boolean equals (RationalNumber op2)
{
return ( numer == op2.getnumer() &&
denomin == op2.getdenomin() );
}
public String toString ()
{
String result;
if (numer == 0)
result = "0";
else
if (denomin == 1)
result = numer + "";
else
result = numer + "/" + denomin;
return result;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.