Create a class called Rational to implement rational numbers with the following
ID: 3684245 • Letter: C
Question
Create a class called Rational to implement rational numbers with the following public member functions. Constructors: Rational(). and Rational(double top, double bottom) Member functions: void print () - Prints top/bottom using cout. Rational add(const Rational &secondRational;) Overloaded addition operator: Rational operator* (const Rational &secondRational;) Implement the dass inline including all constructors, create all necessary private variables member functions, overloaded operators as outlined above Note: no need to separate implementation from interface. A rational number is a number with a numerator a and a denominator b in the form a/b. You can assume positive rational numbers only. The following equations can be used to perform the indicated operation on rational numbers addition: a/b + c/d = (a*d + c*b)/(b*d) You do NOT need to reduce the resulting rational numbers. For example, you do not need to reduce 4/6 to 2/3. To test your implementation you will need to create a main, construct two rational numbers 3/7 and 25/9, display each of them, add both numbers and display the result.Explanation / Answer
Rational program
------------------------------
Rational.java
// This a a class that performs arithmetic with fractions
// Represents a mathematical fraction, consisting of a
// numerator and a denominator, both of which are integers.
public class Rational {
// Instance variables
private int numerator; // Numerator of fraction
private int denominator; // Denominator of fraction
// Constructs a fraction with the specified numerator and denominator
public Rational(int num, int denom)
{
numerator = num;
denominator = denom;
}
// Constructs a fraction with the specified numerator and a denominator of 1
public Rational(int num)
{
this(num, 1);
}
// Constructs a fraction with a numerator of 0 and a denominator of 1
public Rational()
{
this(0, 1);
}
// Add other rational number and this rational number and return new Rational number. Do not change this rational number
public Rational add(Rational f)
{
int num = numerator * f.denominator +
f.numerator * denominator;
int denom = denominator * f.denominator;
return new Rational(num, denom);
}
// getDenominator returns the denominator of this fraction
public int getDenominator()
{
return denominator;
}
// getNumerator returns the numerator of this fraction
public int getNumerator()
{
return numerator;
}
//Subtract other rational number from this rational number and return new rational number. Do not change this rational number.
public Rational subtract(Rational f)
{
int num = numerator * f.denominator -
f.numerator * denominator;
int denom = denominator * f.denominator;
return new Rational(num, denom);
}
/print rational values
public void print()
{
System.out.println("numerator=" + this.numerator);
System.out.println("denominator=" + this.denominator);
}
// Converts this fraction into a float value
public void printfloat()
{
double f = (double) numerator / denominator;
System.out.println("rational number =" +f);
}
}
TestRational.java
// This a program that tests the Rational class.
public class TestRational
{
//main
public static void main(String[] args)
{
Rational r1 = new Rational(4, 5);
System.out.println();
r1.print();
r1.printfloat();
Rational r2 = new Rational(7, 8);
System.out.println();
r2.print();
r2.printfloat();
Rational r3 = r1.add(r2);
System.out.println();
r3.print();
r3.printfloat();
Rational r4 = r1.subtract(r2);
System.out.println();
r4.print();
r4.printfloat();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.