Question

In: Computer Science

C++ Question1. Create a class called Rantional for performing arithmatic with fractions. Then write a program...

C++

Question1. Create a class called Rantional for performing arithmatic with fractions. Then write a program to test your class. Use integer variables to represent the private data of the class, meaning 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 contain default values in

case no initializers are provided and should store the fraction in reduced form. For example fraction 2/4 would be stored in the object as 1 for the numerator and 2 for the denominator.
Note: Remember that denominator cannot be 0.
Provide public member functions that perform each of the following tasks:
a) add -- Adds 2 rational numbers. Result should be stored in reduced form.
b) subtract -- Subtracts 2 rational numbers. Store result in reduced form.
c) multiply -- Multiplies 2 rational numbers. Store result in reduced form.
Write a main() function to test above functionalites.

Solutions

Expert Solution

Question1 : Create a class called Rantional for performing arithmatic with fractions.

Provide public member functions that perform each of the following tasks:
a) add -- Adds 2 rational numbers. Result should be stored in reduced form.
b) subtract -- Subtracts 2 rational numbers. Store result in reduced form.
c) multiply -- Multiplies 2 rational numbers. Store result in reduced form.
Write a main() function to test above functionalites.

PROGRAM:

#include "Rational.h"
#include <iostream>


int main(){
   Rational r1(3,4);
   r1.print();
   r1.printFloat();
   std::cout << "=============================" <<std::endl;

   Rational r2(5,6);
   r2.print();
   r2.printFloat();
   std::cout << "=============================" <<std::endl;

   std::cout <<"The addition is: " << std::endl;
   Rational ans = r1.add(r2);
   ans.print();
   ans.printFloat();
   std::cout << "=============================" <<std::endl;
   std::cout <<"The multiplication is: " << std::endl;
   ans = r1.mult(r2);
   ans.print();
   ans.printFloat();
   std::cout << "=============================" <<std::endl;
return 0;
};


Related Solutions

Create a c++ class called Fraction in which the objects will represent fractions. Remember:   do not...
Create a c++ class called Fraction in which the objects will represent fractions. Remember:   do not reduce fractions, do not use "const," do not provide any constructors, do not use three separate files. You will not receive credit for the assignment if you do any of these things. In your single file, the class declaration will come first, followed by the definitions of the class member functions, followed by the client program. It is required that you provide these member...
The following program will be written in JAVA. Create a class called complex performing arithmetic with...
The following program will be written in JAVA. Create a class called complex performing arithmetic with complex numbers. Write a program to test your class.                         Complex numbers have the form:                         realPart + imaginaryPart * i                                               ___                         Where i is sqrt(-1)                                                 Use double variables to represent data of the class. Provide a constructor that enables an object of this class to be initialized when it is declared. The constructor should contain default values in...
Must be coded in C#. 10.8 (Rational Numbers) Create a class called Rational for performing arithmetic...
Must be coded in C#. 10.8 (Rational Numbers) Create a class called Rational for performing arithmetic with fractions. Write an app 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 1/2 and would be stored in the object...
********************C# C# C#******************** Part A: Create a project with a Program class and write the following...
********************C# C# C#******************** Part A: Create a project with a Program class and write the following two methods(headers provided) as described below: 1. A Method, public static int InputValue(int min, int max), to input an integer number that is between (inclusive) the range of a lower bound and an upper bound. The method should accept the lower bound and the upper bound as two parameters and allow users to re-enter the number if the number is not in the range...
in C++ Requirements: Write a program that creates a new class called Customer. The Customer class...
in C++ Requirements: Write a program that creates a new class called Customer. The Customer class should include the following private data: name - the customer's name, a string. phone - the customer's phone number, a string. email - the customer's email address, a string. In addition, the class should include appropriate accessor and mutator functions to set and get each of these values. Have your program create one Customer objects and assign a name, phone number, and email address...
Write a program that meets the following requirements: Cat Class Create a class called Cat which...
Write a program that meets the following requirements: Cat Class Create a class called Cat which has only the following instance variables: - name - breed - number of legs - year born Create the no-argument constructor Create the constructor which uses all fields as parameters Write the getter and setter methods for all instance variables Override the toString method using the example shown above There should be NO main method in the Cat class. CatTester Class Create a class...
Write a program in C++ that calculates the sum of two fractions. The program should ask...
Write a program in C++ that calculates the sum of two fractions. The program should ask for the numerators and denominators of two fractions and then output the sum of the two fractions. You will need to write four functions for this program, one to read the inputted data, one to calculate the sum of the two fractions, one to find the Greatest Common Divider (GCD) between the numerator and denominator and a function that will display the end result....
Java programming Question1 You will create a full program called LetterGrade that will have the user...
Java programming Question1 You will create a full program called LetterGrade that will have the user enter in 2 test scores as double values. It will compute the average and then using else..if statements it will display the letter grade based on my grading scale listed in the course outline.
1. create a class called ArrayStack that is a generic class. Create a main program to...
1. create a class called ArrayStack that is a generic class. Create a main program to read in one input file and print out the file in reverse order by pushing each item on the stack and popping each item off to print it. The two input files are: tinyTale.txt and numbers.txt. Rules: You cannot inherit the StackofStrings class. 2. Using your new ArrayStack, create a new class called RArrayStack. To do this, you need a) remove the capacity parameter...
1. create a class called ArrayStack that is a generic class. Create a main program to...
1. create a class called ArrayStack that is a generic class. Create a main program to read in one input file and print out the file in reverse order by pushing each item on the stack and popping each item off to print it. The two input files are: tinyTale.txt and numbers.txt. Rules: You cannot inherit the StackofStrings class. 2. Using your new ArrayStack, create a new class called RArrayStack. To do this, you need a) remove the capacity parameter...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT