public class Rational
{
// PUT PRIVATE DATA FIELDS HERE
/**
* The default constructor for objects of class Rational. Creates the rational number 1.
*/
public Rational()
{
// ADD CODE TO THE CONSTRUCTOR
}
/**
* The alternate constructor for objects of class Rational. Creates a rational number equivalent to n/d.
* @param n The numerator of the rational number.
* @param d The denominator of the rational number.
*/
public Rational(int n, int d)
{
// ADD CODE TO THE ALTERNATE CONSTRUCTOR
}
/**
* Get the value of the Numerator
*
* @return the value of the numerator
*/
public int getNumerator()
{
// CHANGE THE RETURN TO SOMETHING APPROPRIATE
return 0;
}
/**
* Get the value of the Denominator
*
* @return the value of the denominator
*/
public int getDenominator()
{
// CHANGE THE RETURN TO SOMETHING APPROPRIATE
return 0;
}
/**
* Negate a rational number r
*
* @return a new rational number that is negation of this number -r
*/
public Rational negate()
{
// CHANGE THE RETURN TO SOMETHING APPROPRIATE
return null;
}
/**
* Invert a rational number r
*
* @return a new rational number that is 1/r.
*/
public Rational invert()
{
// CHANGE THE RETURN TO SOMETHING APPROPRIATE
return null;
}
/**
* Add two rational numbers
*
* @param other the second argument of the add
* @return a new rational number that is the sum of this and the other rational
*/
public Rational add(Rational other)
{
// ADD NEW CODE AND CHANGE THE RETURN TO SOMETHING APPROPRIATE
return null;
}
/**
* Subtract a rational number t from this one r
*
* @param other the second argument of subtract
* @return a new rational number that is r-t
*/
public Rational subtract(Rational other)
{
// CHANGE THE RETURN TO SOMETHING APPROPRIATE
return null;
}
/**
* Multiply two rational numbers
*
* @param other the second argument of multiply
* @return a new rational number that is the sum of this object and the other rational.
*/
public Rational multiply(Rational other)
{
// ADD NEW CODE AND CHANGE THE RETURN TO SOMETHING APPROPRIATE
return null;
}
/**
* Divide this rational number r by another one t
*
* @param other the second argument of divide
* @return a new rational number that is r/t
*/
public Rational divide(Rational other)
{
// CHANGE THE RETURN TO SOMETHING APPROPRIATE
return null;
}
/**
* Put the rational number in normal form where the numerator
* and the denominator share no common factors. Guarantee that only the numerator
* is negative.
*
*/
private void normalize()
{
// ADD CODE TO NORMALIZE THE RATIONAL NUMBER
}
/**
* Recursively compute the greatest common divisor of two positive integers
*
* @param a the first argument of gcd
* @param b the second argument of gcd
* @return the gcd of the two arguments
*/
private int gcd(int a, int b)
{
int result = 0;
if(a<b)
result = gcd(b,a);
else if(b==0)
result = a;
else
{
int remainder = a % b;
result = gcd(b, remainder);
}
return result;
}
}class Rational
{
// PUT PRIVATE DATA FIELDS HERE
private int numerator;
private int denominator;
/**
* The default constructor for objects of class Rational. Creates
the rational number 1.
*/
public Rational()
{
// ADD CODE TO THE CONSTRUCTOR
numerator = 0;
denominator = 1;
}
/**
* The alternate constructor for objects of class Rational. Creates
a rational number equivalent to n/d.
* @param n The numerator of the rational number.
* @param d The denominator of the rational number.
*/
public Rational(int n, int d)
{
// ADD CODE TO THE ALTERNATE CONSTRUCTOR
numerator = n;
denominator = d;
}
/**
* Get the value of the Numerator
*
* @return the value of the numerator
*/
public int getNumerator()
{
// CHANGE THE RETURN TO SOMETHING APPROPRIATE
return numerator;
}
/**
* Get the value of the Denominator
*
* @return the value of the denominator
*/
public int getDenominator()
{
// CHANGE THE RETURN TO SOMETHING APPROPRIATE
return denominator;
}
/**
* Negate a rational number r
*
* @return a new rational number that is negation of this number
-r
*/
public Rational negate()
{
// CHANGE THE RETURN TO SOMETHING APPROPRIATE
return new Rational(-this.numerator,this.denominator);
}
/**
* Invert a rational number r
*
* @return a new rational number that is 1/r.
*/
public Rational invert()
{
// CHANGE THE RETURN TO SOMETHING APPROPRIATE
int temp = numerator;
numerator = denominator;
denominator = temp;
return new Rational(numerator,denominator);
}
/**
* Add two rational numbers
*
* @param other the second argument of the add
* @return a new rational number that is the sum of this and the
other rational
*/
public Rational add(Rational other)
{
// ADD NEW CODE AND CHANGE THE RETURN TO SOMETHING
APPROPRIATE
int num, den;
den = denominator *
other.getDenominator();
num = numerator *
other.getDenominator() +other.getNumerator() * denominator;
Rational r = new Rational (num,
den);
r.normalize();
return r;
}
/**
* Subtract a rational number t from this one r
*
* @param other the second argument of subtract
* @return a new rational number that is r-t
*/
public Rational subtract(Rational other)
{
// CHANGE THE RETURN TO SOMETHING APPROPRIATE
int num, den;
den = denominator *
other.getDenominator();
num = numerator *
other.getDenominator() - other.getNumerator() * denominator;
Rational r = new Rational (num,
den);
r.normalize();
return r;
}
/**
* Multiply two rational numbers
*
* @param other the second argument of multiply
* @return a new rational number that is the sum of this object and
the other rational.
*/
public Rational multiply(Rational other)
{
// ADD NEW CODE AND CHANGE THE RETURN TO SOMETHING
APPROPRIATE
int num, den;
den = denominator *
other.getDenominator();
num = numerator *
other.getNumerator();
Rational r = new Rational (num,
den);
r.normalize();
return r;
}
/**
* Divide this rational number r by another one t
*
* @param other the second argument of divide
* @return a new rational number that is r/t
*/
public Rational divide(Rational other)
{
// CHANGE THE RETURN TO SOMETHING APPROPRIATE
int num, den;
den = denominator *
other.getNumerator();
num = numerator *
other.getDenominator();
Rational r = new Rational (num,
den);
r.normalize();
return r;
}
/**
* Put the rational number in normal form where the numerator
* and the denominator share no common factors. Guarantee that only
the numerator
* is negative.
*
*/
private void normalize()
{
// ADD CODE TO NORMALIZE THE RATIONAL NUMBER
int factor;
factor = gcd(numerator,
denominator);
numerator = numerator /
factor;
denominator = denominator /
factor;
}
/**
* Recursively compute the greatest common divisor of two positive
integers
*
* @param a the first argument of gcd
* @param b the second argument of gcd
* @return the gcd of the two arguments
*/
private int gcd(int a, int b)
{
int result = 0;
if(a<b)
result = gcd(b,a);
else if(b==0)
result = a;
else
{
int remainder = a % b;
result = gcd(b, remainder);
}
return result;
}
}
class RationalTest
{
public static void main (String[] args)
{
Rational r1 = new
Rational(4,5);
Rational r2 = new
Rational(3,4);
Rational sum = new
Rational();
sum = r1.add(r2);
System.out.println(" Sum of r1 and
r2 = "+sum.getNumerator()+"/"+sum.getDenominator());
Rational sub = new
Rational();
sub = r1.subtract(r2);
System.out.println(" Subtraction of
r2 from r1 = "+sub.getNumerator()+"/"+sub.getDenominator());
Rational mul = new
Rational();
mul = r1.multiply(r2);
System.out.println(" Multiplication
of r1 and r2 =
"+mul.getNumerator()+"/"+mul.getDenominator());
Rational div = new
Rational();
div = r1.divide(r2);
System.out.println(" Division of r1
and r2 = "+div.getNumerator()+"/"+div.getDenominator());
Rational inv = new
Rational();
inv = r1.invert();
System.out.println("Inverse of r1 :
"+inv.getNumerator()+"/"+inv.getDenominator());
Rational neg = new
Rational();
neg = r2.negate();
System.out.println("Negation of r2
: "+neg.getNumerator()+"/"+neg.getDenominator());
}
}
Output:
Sum of r1 and r2 = 31/20 Subtraction of r2 from r1 = 1/20 Multiplication of r1 and r2 = 3/5 Division of r1 and r2 = 16/15 Inverse of r1 : 5/4 Negation of r2 : -3/4
Do ask if any doubt. Please upvote.
public class Rational { // PUT PRIVATE DATA FIELDS HERE /** * The default constructor for...
Rational will be our parent class that I included to this post, Implement a sub-class MixedRational. This class should Implement not limited to: 1) a Constructor with a mathematically proper whole, numerator and denominator values as parameters. 2) You will override the: toString, add, subtract, multiply, and divide methods. You may need to implement some additional methods, enabling utilization of methods from rational. I have included a MixedRational class with the method headers that I used to meet these expectations....
Create a fraction class. This will have two attributes, a numerator and a denominator, both int. This class will have constructors accessors and mutators (for the attributes) a toString method which will allow us to print the fraction in the form 3/4 an Add method so we can add two fractions a subtract method (subtracts one fraction from the other) a multiply method (multiply two fractions) a divide method (divides one fraction by the other) DO NOT (for now) worry...
Adapt your Rational class : public class Rational { private int num; private int denom; public Rational() { num = 0; denom = 1; } public Rational(int num, int denom) { this.num = num; this.denom = denom; } int getNum() { return num; } int getDenom() { return denom; } public Rational add(Rational rhs) { return new Rational(num * rhs.denom + rhs.num * denom, denom * rhs.denom); } public Rational subtract(Rational rhs) { return new Rational(num * rhs.denom - rhs.num...
I need help with the following Java code Consider a class Fraction of fractions. Each fraction is signed and has a numerator and a denominator that are integers. Your class should be able to add, subtract, multiply, and divide two fractions. These methods should have a fraction as a parameter and should return the result of the operation as a fraction. The class should also be able to find the reciprocal of a fraction, compare two fractions, decide whether two...
(Rational Numbers) Create a class called Rational for performing arithmetic with fractions. Write a program to test your class. Use integer variables to represent the private instance variables of the class- the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it's declared. The constructor should store the fraction in reduced form. The fraction 2/4 is equivalent to h and would be stored in the object as 1 in the numerator...
C++ I am using visual studio for it. Make a copy of the Rational class you created in the previous Lab. Modify the class. Replace all your mathematical, input, and output functions with overloaded operators. Overload the following 12 operators: + - * / < > = = ! = <= >= >> << In order to test your class in your main function, prompt the user for a numerator and a denominator for your first object. Repeat the prompt...
Rational Number *In Java* A rational number is one that can be expressed as the ratio of two integers, i.e., a number that can be expressed using a fraction whose numerator and denominator are integers. Examples of rational numbers are 1/2, 3/4 and 2/1. Rational numbers are thus no more than the fractions you've been familiar with since grade school. Rational numbers can be negated, inverted, added, subtracted, multiplied, and divided in the usual manner: The inverse, or reciprocal of...
Header file for the Rational class:
#ifndef RATIONAL_H
#define RATIONAL_H
class Rational
{
public:
Rational( int = 0, int = 1 ); // default constructor
Rational addition( const Rational & ) const; // function
addition
Rational subtraction( const Rational & ) const; // function
subtraction
Rational multiplication( const Rational & ) const; // function
multi.
Rational division( const Rational & ) const; // function
division
void printRational () const; // print rational format
void printRationalAsDouble() const; // print rational as...
13.21 Lab: Rational class This question has been asked here before, but every answer I have tested did not work, and I don't understand why, so I'm not able to understand how to do it correctly. I need to build the Rational.cpp file that will work with the main.cpp and Rational.h files as they are written. Rational Numbers It may come as a bit of a surprise when the C++ floating-point types (float, double), fail to capture a particular value...
Can somebody help me with this assignment. I will highly
appreciate. Also, please display the output as well. I need to use
JAVA to to write the program.
This is the sample output provided by my
professor.
HOME WORK: due 09.17.2019 (Rational Numbers) Create a class called Rational for performing arithmetic with fractions. Write a program to test your class. Use integer variables to represent the private instance variables of the class- the numerator and the denominator. Provide a constructor...