In Java define a class for rational numbers. A rational number is a number that
ID: 3641274 • Letter: I
Question
In 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, 1/2, 3/4, 64/2, and so forth are all rational numbers. (By 1/2 and so forth, we mean the everyday 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. Call the class Rational. Include a constructor with two arguments that can be used to set the instance variables of an object to any values. Also include a constructor that has only a single parameter of type int; call the single parameter wholeNumber and define the constructor so that the object will be intialized to the rational number wholeNumber/1. Also include a noargument constructor that initializes an object to 0 (that is, to 0/1). Note that the numerator, the denominator, or both may contain a minus sign. Define methods for addition, subtraction, multiplication, and division of objects of your class Rational. These methods should be static methods that each have 2 parameters of type Rational and return a value of type Rational. For example, Rational.add(r1,r2) will return the result of adding the 2 rational numbers (2 objects of the class Rational, r1, and r2). Define accesor and mutator methods as well as the methods equals and toString. You should include a method to normalize the sign of the rational number so that the denominator is positive and the numerator is either positive or negative. For example, after normalization, 4/-8 would be represented the same as -4/8. Also write a test program to test your class. Hint: 2 rational numbers a/b and c/d are equal if a*d equals c*b.Explanation / Answer
I included the driver file below Rational.java separated by dashes. Make sure you save them as seperate files with the appropriate names for them to work correctly. I didn't know whether or not you wanted the user to be able to interact with the program in your test file, so I made it user-interactive just to be safe. Rational.java is listed first which is the Rational class and RationalTest.java is the driver class which is listed after Rational.java. Hope this is what you needed!
---------------------------------------------------------------------------------------------
// File Rational.java
public class Rational
{
//-------------------------
// instance variables
//-------------------------
private int numerator, denominator;
//-------------------------------
// Constructor with two arguments
//-------------------------------
public Rational(int n, int d)
{
numerator = n;
denominator = d;
}
//------------------------------
// Constructor with one argument
//------------------------------
public Rational(int wholeNumber)
{
numerator = wholeNumber;
denominator = 1;
}
//------------------------------
// Constructor with no arguments
//------------------------------
public Rational()
{
numerator = 0;
denominator = 1;
}
//-------------------------
// .equals() method
//-------------------------
public boolean equals(Rational r2)
{
boolean flag = false;
if (this == r2)
{
flag = true;
}
else if (r2 instanceof Rational)
{
if(this.numerator == r2.numerator && this.denominator == r2.denominator)
{
flag = true;
}
}
else
{
flag = false;
}
return flag;
}
//--------------------------------
// .getNumerator() accessor method
//--------------------------------
public int getNumerator()
{
return numerator;
}
//--------------------------------
// .getDenominator() accessor method
//--------------------------------
public int getDenominator()
{
return denominator;
}
//--------------------------------
// .setNumerator() mutator method
//--------------------------------
public void setNumerator(int n)
{
numerator = n;
}
//--------------------------------
// .setDenominator() mutator method
//--------------------------------
public void setDenominator(int d)
{
denominator = d;
}
//-----------------
// Addition method
//-----------------
public static Rational add(Rational r1, Rational r2)
{
if (r1.equals(r2))
{
Rational r3 = new Rational((r1.getNumerator())*2, r1.getDenominator());
return r3;
}
else
{
Rational r3 = new Rational();
r3.numerator = ((r1.getNumerator()*r2.getDenominator())+(r2.getNumerator()+r1.getDenominator()));
r3.denominator = (r1.getDenominator()*r2.getDenominator());
return r3;
}
}
//-------------------
// Subtraction method
//-------------------
public static Rational subtract(Rational r1, Rational r2)
{
if (r1.equals(r2))
{
Rational r3 = new Rational();
return r3;
}
else
{
Rational r3 = new Rational();
r3.numerator = ((r1.getNumerator()*r2.getDenominator())-(r2.getNumerator()+r1.getDenominator()));
r3.denominator = (r1.getDenominator()*r2.getDenominator());
return r3;
}
}
//----------------------
// Multiplication method
//----------------------
public static Rational multiply(Rational r1, Rational r2)
{
Rational r3 = new Rational();
r3.numerator = (r1.getNumerator() * r2.getNumerator());
r3.denominator = (r1.getDenominator() * r2.getDenominator());
return r3;
}
//-----------------
// Division method
//-----------------
public static Rational divide(Rational r1, Rational r2)
{
Rational r3 = new Rational();
r3.numerator = (r1.getNumerator() * r2.getDenominator());
r3.denominator = (r1.getDenominator() * r2.getNumerator());
return r3;
}
//--------------------------
// Sign normalization method
//--------------------------
public static Rational normalizeSign(Rational r1)
{
if(r1.numerator > 0 && r1.denominator < 0)
{
r1.numerator = -(r1.getNumerator());
r1.denominator = Math.abs(r1.getDenominator());
}
else if (r1.numerator < 0 && r1.denominator < 0)
{
r1.numerator = Math.abs(r1.getNumerator());
r1.denominator = Math.abs(r1.getDenominator());
}
return r1;
}
//-------------------
// .toString() method
//-------------------
public String toString()
{
return (numerator + "/" + denominator);
}
}
-------------------------------------------------------------------------------------
//File: RationalTest.java
import java.util.Scanner;
public class RationalTest
{
public static void main(String[] args)
{
int numerator, denominator, wholeNumber;
Scanner scan = new Scanner(System.in);
//------------------------------
// Two argument constructor test
//------------------------------
System.out.print("Enter numerator: ");
numerator = scan.nextInt();
System.out.print("Enter denominator: ");
denominator = scan.nextInt();
Rational r1 = new Rational(numerator, denominator);
//--------------------------------------------------
// getNumerator() and getDenominator() method tests
//--------------------------------------------------
System.out.println("The numerator you entered was: " + r1.getNumerator());
System.out.println("The denominator you entered was: " + r1.getDenominator());
System.out.println();
//--------------------------------------------------
// setNumerator() and setDenominator() method tests
//--------------------------------------------------
System.out.print("Enter a new numerator: ");
numerator = scan.nextInt();
System.out.print("Enter a new denominator: ");
denominator = scan.nextInt();
r1.setNumerator(numerator);
r1.setDenominator(denominator);
System.out.println("The new numerator you entered was: " + r1.getNumerator());
System.out.println("The new denominator you entered was: " + r1.getDenominator());
System.out.println();
//------------------------------
// One argument constructor test
//------------------------------
System.out.print("Enter a whole number: ");
wholeNumber = scan.nextInt();
Rational r2 = new Rational(wholeNumber);
//------------------------------
// No argument constructor test
//------------------------------
Rational r3 = new Rational();
//-----------------------------
// normalizeSign() method test
//-----------------------------
Rational.normalizeSign(r1);
//------------------------------
// .equals() method test
//------------------------------
if(r1.equals(r2))
{
System.out.println(r1 + " equals " + r2);
}
else
{
System.out.println(r1 + " does not equal " + r2);
}
System.out.println();
//------------------------------
// .add() method test
//------------------------------
System.out.println("Time to do some math...");
System.out.print("Enter the first numerator: ");
numerator = scan.nextInt();
System.out.print("Enter the first denominator: ");
denominator = scan.nextInt();
r1.setNumerator(numerator);
r1.setDenominator(denominator);
Rational.normalizeSign(r1);
System.out.print("Enter the second numerator: ");
numerator = scan.nextInt();
System.out.print("Enter the second denominator: ");
denominator = scan.nextInt();
r2.setNumerator(numerator);
r2.setDenominator(denominator);
Rational.normalizeSign(r2);
r3 = Rational.add(r1, r2);
System.out.println(r1 + " plus " + r2 + " equals " + r3);
//------------------------------
// .subtract() method test
//------------------------------
r3 = Rational.subtract(r1, r2);
System.out.println(r1 + " minus " + r2 + " equals " + r3);
//------------------------------
// .multiply() method test
//------------------------------
r3 = Rational.multiply(r1, r2);
System.out.println(r1 + " times " + r2 + " equals " + r3);
//------------------------------
// .divide() method test
//------------------------------
r3 = Rational.divide(r1, r2);
System.out.println(r1 + " divided by " + r2 + " equals " + r3);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.