Question

In: Computer Science

Write a class named Rat (to simulate rational or fraction number) such that it works as...

Write a class named Rat (to simulate rational or fraction number) such that it works as in the main below.

int main()

{

Rat a(3, 4), b(1, 2);

a.print(); // output: 3/4

b.print(); // output: 1/2

Rat c = a.add(b);

c.print(); // output: 5/4

// Hint: 3/4 + 1/2 = 6/8 + 4/8 = 10/8 = 5/4

Rat d = a.multiply(b);

d.print(); // output: 3/8

// Hint: 3/4 x 1/2 = 3/8

return 0;

}

Solutions

Expert Solution

C++ code:

#include <bits/stdc++.h>
using namespace std; 

// Rat class
class Rat{
    public:
        int numerator;
        int denominator;
    
    // Constructor
    Rat(int x, int y){
        numerator = x;
        denominator = y;
    }

    // print the fraction
    void print(){
        cout << numerator << "/" << denominator << endl;
    }
    
    // add two fractional numbers
    Rat add(Rat x){
        int num = this->numerator * x.denominator + this->denominator * x.numerator;
        int den = this->denominator * x.denominator;
        int gcd = __gcd(num, den);
        num /= gcd;
        den /= gcd;

        Rat y(num, den);
        return y;
    }

    // multiply two fractional numbers
    Rat multiply(Rat x){
        int num = this->numerator * x.numerator;
        int den = this->denominator * x.denominator;
        int gcd = __gcd(num, den);
        num /= gcd;
        den /= gcd;

        Rat y(num, den);
        return y;
    }
};

// Main function
int main(){

    Rat a(3, 4), b(1, 2);
    a.print(); // output: 3/4
    b.print(); // output: 1/2

    Rat c = a.add(b);
    c.print(); // output: 5/4

    Rat d = a.multiply(b);
    d.print(); // output: 3/8
    
    return 0;
}

Please refer to the following pictures for the code:

Execution of the above code:


Related Solutions

Write a rational number class in C++. Recall a rational number is a rational number, composed...
Write a rational number class in C++. Recall a rational number is a rational number, composed of two integers with division indicated. The division is not carried out, it is only indicated, as in 1/2, 2/3, 15/32. You should represent rational numbers using two int values, numerator and denominator. A principle of abstract data type construction is that constructors must be present to create objects with any legal values. You should provide the following two constructors: one constructor to make...
Java Program to write a Fraction class that models a mathematical fraction. The fraction class needs...
Java Program to write a Fraction class that models a mathematical fraction. The fraction class needs to support simple arithmetic functions including taking the sum, difference, and the product of the division of the two. Do not include a main() method in the Fraction class. The Fraction class will implement the following class methods: Fraction add (Fraction f1, Fraction f2); // f1 + f2 and returns a new Fraction Fraction sub (Fraction f1, Fraction f2); // f1 - f2 and...
Define a class called Rational for rational numbers. Arational number is a number that can...
Define a class called Rational for rational numbers. A rational number is a number that can be represented as the quotient of two integers. For example, /2, 3/4, 64/2, and so forth are all rational numbers.Represent rational numbers as two private values of type int, numfor the numerator and den for the denominator. The class has a default constructor with default values (num = 0,den = 1), a copy constructor, an assignment operator and three friend functions for operator overloading...
Declare and define a class for a fraction number. A fraction in mathematics is defined as...
Declare and define a class for a fraction number. A fraction in mathematics is defined as a/b, where a and b are integers and called numerator and denominator. Requirements Task1    Define a fraction class that has the following member functions: constructor that initializes the fraction by default arguments. set function that sets the numerator of the fraction. set function that sets the denominator of the fraction. get function that returns the numerator of the fraction. get function that returns...
Write the form of the partial fraction decomposition of the rational expression. It is not necessary...
Write the form of the partial fraction decomposition of the rational expression. It is not necessary to solve for the constants.
IN JAVA Rational Numbers Create a Rational number class in the same style as the Complex...
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: constructor add sub mul div toString You must also provide a Main class and main method to fully test your Rational number class.
Write a class named ContactEntry that has fields for a person’s name, phone number and email...
Write a class named ContactEntry that has fields for a person’s name, phone number and email address. The class should have a no-arg constructor and a constructor that takes in all fields, appropriate setter and getter methods. Then write a program that creates at least five ContactEntry objects and stores them in an ArrayList. In the program create a method, that will display each object in the ArrayList. Call the method to demonstrate that it works. I repeat, NO-ARG constructors....
Partial Fractions: Problem 2 Use the method of partial fraction decomposition to write the following rational...
Partial Fractions: Problem 2 Use the method of partial fraction decomposition to write the following rational expression as the sum of simpler rational functions whose denominators are polynomials of degree 1. −20x+20/x^2−x−56=
Is rational number divided by an irrational number equal to irrational number or rational number?
Is rational number divided by an irrational number equal to irrational number or rational number? for example such as ( 5 / 2pi )
Given the main method of a driver class, write a Fraction class. Include the following instance...
Given the main method of a driver class, write a Fraction class. Include the following instance methods: add, multiply, print, printAsDouble, and a separate accessor method for each instance variable. Write a Fraction class that implements these methods: add ─ This method receives a Fraction parameter and adds the parameter fraction to the calling object fraction. multiply ─ This method receives a Fraction parameter and multiplies the parameter fraction by the calling object fraction. print ─ This method prints the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT