Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ Complete the declaration of the following IMMUTABLE class named Fraction. Each instance of the class contains a rational number represented as a fraction containing

C++

Complete the declaration of the following IMMUTABLE class named Fraction. Each instance of the class contains a rational number represented as a fraction containing an integer numerator over an integer denominator. The instance methods implement common arithmetic operations on fractions. Since the class is immutable it has no modifying methods. All arithmetic operations return new Fraction objects as their results without modifying the target nor the argument Fraction.

Your job consists of completing the following 5 tasks:

Complete the empty percentage instance method Complete the empty addInverseOf instance method Complete the empty squareRoot instance method Complete the empty divide instance method Complete the empty powerOf instance method

#include #include using namespace std; class Fraction { private: float numerator; float denominator; public: Fraction(float num, float den) { numerator = num; denominator = den; } float getNumerator() { return numerator; } float getDenominator() { return denominator; } /* * Returns the percentage equivalent of the target fraction. * HINT: Check the percentage formula. Use the floor method, * you may review how the floor works by viewing the API. * Note that the tests expect values that are whole numbers. */ int percentage(); /* * Return a NEW fraction representing the addition of the target fraction and the inverse * of the argument fraction. The resulting fraction may not be expressed in minimal terms. * You must create a NEW fraction object and use getNumerator & getDenominator. */ Fraction addInverseOf(Fraction f2); /* * Return a NEW fraction representing the sqrt of the target object. * The resulting fraction may not be expressed in minimal terms. * You must create a NEW fraction object. */ Fraction squareRoot(); /* * Returns a NEW fraction representing the division of the target fraction * by the argument fraction. Remember that a/b divided by c/d is equivalent * to a/b times d/c. The resulting fraction may not be expressed in minimal * terms. You must create a NEW fraction object and able to use getNumerator * and getDenominator for the tests */ Fraction divide(Fraction f2); /* * Return a NEW fraction representing the power of n of the target fraction. * The resulting fraction may not be expressed in minimal terms. * You must create a NEW fraction object. */ Fraction powerOf(int n); };

1. Finish the percentage instance method.

Test Result
Fraction f1(2, 10); cout << f1.percentage() << endl;
20
Fraction f1(59, 73); cout << f1.percentage() << endl;
80
Fraction f1(19, 38); cout << f1.percentage() << endl;
50
Fraction f1(12, 16); cout << f1.percentage() << endl;
75

#include "Fraction.h" /* * Return the percentage equivalent of the target fraction. * * HINT: Check the percentage formula. Use the floor method, * you may review how floor works by viewing the C++ Library/API. * For example: https://en.cppreference.com/w/cpp/numeric/math * * Note that the tests expect and display values that are whole numbers. * * NOTE: The display of the results are already handled by the test cases. * You just need to return the int result. */ int Fraction::percentage() { // ADD YOUR CODE/SOLUTION BELOW return -1; // DUMMY RETURN, USE FLOOR. }

2. Finish the addInverseOf instance method.

Test Result
Fraction f1(1, 2); Fraction f2(5, 9); Fraction result = f1.addInverseOf(f2); cout << "The new numerator is " << result.getNumerator() << endl; cout << "The new denominator is " << result.getDenominator() << endl;
The new numerator is 23 The new denominator is 10
Fraction f1(21, 13); Fraction f2(8, 7); Fraction result = f1.addInverseOf(f2); cout << "The new numerator is " << result.getNumerator() << endl; cout << "The new denominator is " << result.getDenominator() << endl;
The new numerator is 259 The new denominator is 104
Fraction f1(3, 172); Fraction f2(19, 144); Fraction result = f1.addInverseOf(f2); cout << "The new numerator is " << result.getNumerator() << endl; cout << "The new denominator is " << result.getDenominator() << endl;
The new numerator is 24825 The new denominator is 3268 

#include "Fraction.h"

/* * Return a NEW fraction representing the addition of the target fraction and the inverse * of the argument fraction. The resulting fraction may not be expressed in minimal terms. * You must create a NEW fraction object and use getNumerator & getDenominator. * * NOTE: The display of the results are already handled by the test cases. * You just need to return the new Fraction. */ Fraction Fraction::addInverseOf(Fraction f2) { return f2; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Database Management

Authors: Jeff Hoffer, Ramesh Venkataraman, Heikki Topi

12th edition

133544613, 978-0133544619

More Books

Students also viewed these Databases questions

Question

How effective is the work of PLCs?

Answered: 1 week ago