Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask Programming Language Expert

Write a fraction class whose objects will represent fractions. You should provide the following member functions:

Two constructors, a default constructor which assigns the value 0 to the fraction, and a constructor that takes two parameters. The first parameter will represent the initial numerator of the fraction, and the second parameter will represent the initial denominator of the fraction.

Arithmetic operations that add, subtract, multiply, and divide fractions. These should be implemented as value returning functions that return a fraction object. They should be named addedTo, subtract, multipliedBy, and dividedBy.

A boolean operation named isEqualTo that compares two fraction objects for equality.

An output operation named print that displays the value of a fraction object on the screen in the form numerator/denominator.

Your class should have exactly two data members, one to represent the numerator of the fraction being represented, and one to represent the denominator of the fraction being represented.

I am providing a client program for you below. You should copy and paste this into a file and use it as your client program. The output that should be produced when the provided client program is run with your class is also given below, so that you can check your results. Since you are not writing the client program, you are not required to include comments in it.

I strongly suggest that you design your class incrementally. For example, you should first implement only the constructors and the output function, and then test what you have so far. Once this code has been thoroughly debugged, you should add additional member functions, testing each one thoroughly as it is added. You might do this by creating your own client program to test the code at each stage; however, it would probably be better to use the provided client program and comment out code that relates to member functions that you have not yet implemented.

As you can see from the sample output given below, you are not required to change improper fractions into mixed numbers for printing. Just print it as an improper fraction. You are, however, required to reduce fractions, as illustrated in the sample output. Make sure that your class will reduce ANY fraction, not just the fractions that are tested in the provided client program. Fractions should not be simply reduced upon output, they should be stored in reduced form at all times. In other words, you should ensure that all fraction objects are reduced before the end of any member function. You are also not required to deal with negative numbers, either in the numerator or the denominator.

You must create your own algorithm for reducing fractions. Don't look up an already existing algorithm for reducing fractions or finding GCF. The point here is to have you practice solving the problem on your own. In particular, don't use Euclid's algorithm. Don't worry too much about efficiency, just create something of your own that works correctly on ANY fraction.

Before you submit your assignment, make sure to carefully read section 1D of the Style Conventions, "Commenting in Classes".

Here is the client program.

#include

#include "fraction.h"

using namespace std;

int main()

{

fraction f1(9,8);

fraction f2(2,3);

fraction result;

cout << "The result starts off at ";

result.print();

cout << endl;

cout << "The product of ";

f1.print();

cout << " and ";

f2.print();

cout << " is ";

result = f1.multipliedBy(f2);

result.print();

cout << endl;

cout << "The quotient of ";

f1.print();

cout << " and ";

f2.print();

cout << " is ";

result = f1.dividedBy(f2);

result.print();

cout << endl;

cout << "The sum of ";

f1.print();

cout << " and ";

f2.print();

cout << " is ";

result = f1.addedTo(f2);

result.print();

cout << endl;

cout << "The difference of ";

f1.print();

cout << " and ";

f2.print();

cout << " is ";

result = f1.subtract(f2);

result.print();

cout << endl;

if (f1.isEqualTo(f2)){

cout << "The two fractions are equal." << endl;

} else {

cout << "The two fractions are not equal." << endl;

}

const fraction f3(12, 8);

const fraction f4(202, 303);

result = f3.multipliedBy(f4);

cout << "The product of ";

f3.print();

cout << " and ";

f4.print();

cout << " is ";

result.print();

cout << endl;

}

This client should produce the output shown here:

The result starts off at 0/1

The product of 9/8 and 2/3 is 3/4

The quotient of 9/8 and 2/3 is 27/16

The sum of 9/8 and 2/3 is 43/24

The difference of 9/8 and 2/3 is 11/24

The two fractions are not equal.

The product of 3/2

Programming Language, Programming

  • Category:- Programming Language
  • Reference No.:- M92024900

Have any Question?


Related Questions in Programming Language

Structs and enumsoverviewin this task you will create a

Structs and Enums Overview In this task you will create a knight database to help Camelot keep track of all of their knights. Instructions Lets get started. 1. What the topic 5 videos, these will guide you through buildi ...

Task - hand execution of arraysoverviewin this task you

Task - Hand Execution of Arrays Overview In this task you will demonstrate how arrays work by hand executing a number of small code snippets. Instructions Watch the Hand Execution with Arrays video, this shows how to ste ...

Task arrays and structsoverviewin this task you will

Task: Arrays and Structs Overview In this task you will continue to work on the knight database to help Camelot keep track of all of their knights. We can now add a kingdom struct to help work with and manage all of the ...

Assignment - horse race meetingthe assignment will assess

Assignment - Horse Race Meeting The Assignment will assess competencies for ICTPRG524 Develop high level object-oriented class specifications. Summary The assignment is to design the classes that are necessary for the ad ...

Assignment - haskell program for regular expression

Assignment - Haskell Program for Regular Expression Matching Your assignment is to modify the slowgrep.hs Haskell program presented in class and the online notes, according to the instructions below. You may carry out th ...

1 write a function named check that has three parameters

1. Write a function named check () that has three parameters. The first parameter should accept an integer number, andthe second and third parameters should accept a double-precision number. The function body should just ...

Task working with arraysoverviewin this task you will

Task: Working with Arrays Overview In this task you will create a simple program which will create and work with an array of strings. This array will then be populated with values, printed out to the console, and then, w ...

Background informationthis assignment tests your

Background Information This assignment tests your understanding of and ability to apply the programming concepts we have covered throughout the unit. The concepts covered in the second half of the unit build upon the fun ...

Php amp session managment assignment -this assignment looks

PHP & SESSION MANAGMENT ASSIGNMENT - This assignment looks at using PHP for creating cookies and session management. Class Exercise - Web Project: Member Registration/Login This exercise will cover adding data connectivi ...

Assignment - proposal literature review research method1

Assignment - Proposal, Literature Review, Research Method 1. Abstract - Summary of the knowledge gap: problems of the existing research - Aim of the research, summary of what this project is to achieve - Summary of the a ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As