Design a fraction class. The class should have 2 data members to represent the n
ID: 673793 • Letter: D
Question
Design a fraction class. The class should have 2 data members to represent the numerator and denominator. Both of these numbers should obviously be integers. It should be able to display a fraction and include an operator for performing multiplication of fractions.
Construct the fraction class with 3 constructors. The first is the default. The second constructor should allow us to create a fraction that represents a whole number. The third constructor should allow us to specify the numerator and denominator of a new fraction object. The public functions should include functions for respectively getting the numerator and denominator, setting the respective numerator and denominator, multiplying fractions, and displaying the fraction.
You should then use this class in a program that multiplies fractions. (Hint: you can create 3 fraction objects f1, f2, and f3. You can use the public function readFraction that reads 2 fractions f1 and f2. You can then use the statement f3 = f1.multiply(f2) to calculate f1 * f2 and assign the result to f3.)
The function's input prompts should be "Enter 1st fraction" and "Enter 2nd fraction" where the user enters the appropriate fractions. In addition, the program needs to display the result of the multiplication operation in the readable format:
3/4 * 5/6 = 15/24
This result should be displayed on one line.
Test the function with the fractions 3/4 and 5/6 and also with 1/2 and 1. (Note that your result will not provide the fraction in reduced form and you do not have to write code to reduce the fraction. Also note that we have not restricted a fraction with a denominator of 0, but this would be a nice addition to your program.)
Write a program with a class called CheapWatch that provides the time of day in a program. Three unsigned integers should be used to represent time:
The first integer should represent the hour.
The second integer should represent the minutes.
The third integer should represent the seconds.
Include in the program a type conversion constructor that converts a long integer representing the number of seconds from midnight into your three-integer (hour, minute, second) representation. For example, if the long integer 18637 should convert to 5:10:37, you may use the following formula:
Elapsed seconds = (hours * 3600) + (minutes * 60) + seconds.
Use military time. For example, 3:40 PM is represented as 15:40:00.
(Hint: Design the CheapWatch class where the 3 integers representing time are to be treated as private data members. Include a standard constructor, a type conversion constructor, and a function that shows the time as public function members. In your design, identify with a comment the constructor member function. Ask the user to enter the number of seconds after midnight; output the seconds converted to military time. Make sure the output is formatted properly with appropriate manipulators, formatting flags and formatting functions.)
Explanation / Answer
#include <iostream>
using namespace std;
class Fraction
{
private:
int numerator, denominator;
public:
Fraction()
{
numerator = 1;
denominator = 1;
}
Fraction(int n, int d)
{
numerator = n;
if (d==0)
{
cout << "ERROR: ATTEMPTING TO DIVIDE BY ZERO" << endl;
exit(0);
}
else
denominator = d;
}
Fraction Sum(Fraction otherFraction)
{
int n = numerator*otherFraction.denominator+otherFraction.numerator*denominator;
int d = denominator*otherFraction.denominator;
return Fraction(n/gcd(n,d),d/gcd(n,d));
}
Fraction Difference(Fraction otherFraction)
{
int n = numerator*otherFraction.denominator-otherFraction.numerator*denominator;
int d = denominator*otherFraction.denominator;
return Fraction(n/gcd(n,d),d/gcd(n,d));
}
Fraction Product(Fraction otherFraction)
{
int n = numerator*otherFraction.numerator;
int d = denominator*otherFraction.denominator;
return Fraction(n/gcd(n,d),d/gcd(n,d));
}
Fraction Division(Fraction otherFraction)
{
int n = numerator*otherFraction.denominator;
int d = denominator*otherFraction.numerator;
return Fraction(n/gcd(n,d),d/gcd(n,d));
}
int gcd(int n, int d)
{
int remainder;
while (d != 0)
{
remainder = n % d;
n = d;
d = remainder;
}
return n;
}
void show()
{
if (denominator == 1)
cout << numerator << endl;
else
cout << numerator << "/" << denominator << endl;
}
};
int main()
{
Fraction a(1,2);
Fraction b(1,4);
Fraction c;
c = a.Sum(b);
c.show();
c = a.Difference(b);
c.show();
c = a.Product(b);
c.show();
c = a.Division(b);
c.show();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.