Write a fraction class whose objects will represent fraction

Assignment Help Programming Languages
Reference no: EM131270170

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

Reference no: EM131270170

Questions Cloud

Related to product design and capacity determination : Should a firm attempt to have fewer or more suppliers? What are the advantages and disadvantages of each approach? How is process selection related to product design and capacity determination?
Explain how the attentional blink relates to attention : Explain how the attentional blink relates to attention. Analyze how the variation in time influences the probability of seeing the second target letter. Explain the circumstances under which the attentional blink can be eliminated.
Find the producers surplus at a price level : Find the demand at a price of 52.50 cents per pound.- Find the producers surplus (to the nearest dollar) at a price level of 52.50 cents per pound.
Determining the two-part tariff : As the owner of a tennis club in a wealty community, you must decide on membership dues and fees for court time. Based on historical data, you understand that there are two types of tennis players in your community. "Serious" players with demand
Write a fraction class whose objects will represent fraction : 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.
Explain the pricing policy taken by apple : Describe what do you understand by the term "Cost plus Pricing "and highlight any limitations this may have. How does this differ from "Target Costing "pricing - Describe the type of pricing policy being applied by Hewlett- Packard in regard to the..
Is the anger usually directed at individual people : When researching this topic, some questions to consider in your response may include: Do your results parallel those of James Averill (see textbook), who found that most people become at least mildly angry several times a week, and sometimes sever..
Define your internal and external customers and suppliers : Briefly describe your work position and define your internal and external customers and suppliers. What are the four key roles that expatriates typically play? Why do a high percentage of expatriates fail abroad? Discuss two advantages and two disadv..
Write a program that inputs the number of hours worked : Write a program that inputs (from the user) the number of hours worked and hourly pay rate for employees and outputs their total pay. The program should process an arbitrary number of employees.

Reviews

Write a Review

Programming Languages Questions & Answers

  Simulation of checking tic-tac-toe board

Simulation of checking tic-tac-toe board for wins by counting X and O in rows, columns and diagonals - You must use loops, as demonstrated in class, to count the X and O in each direction.

  Create block using a loop to determine number of items

Create a block using a loop that will determine the number of items that can be purchased based on the price of the item and the total amount available to spend.

  Program that will allow a user to input list of their family

Problem Statement: Design a program that will allow a user to Input a list of their family members along with their age and state where they reside. Determine and output the average age of their family and output the names of anyone who lives in T..

  Determine the purpose of using javascriptr on a website

write a 200- to 300-word short-answer response to the followingwhat is the purpose of using javascriptr on a website?

  Program to print a business travel expenses

Write a program to print a business travel expenses attachment for an income tax return.

  Critical review of recent segment disclosures

Assignment will require explanation and critical review of recent segment disclosures provided by Qantas Limited in its 2015 Annual Report.

  Linux directory log

Linux Directory Log, Directions: As you complete each of the following steps, keep track of what occurs at each point, including what you type, the output given, and if you experience any errors. Record the occurrences in the following matrix.

  Find the date format is valid or not

Display the date and message that indicates whether it is valid. If it is not valid, also display a message explaining why it is not valid.

  Program to checks to see if the value of a is valid

Checks to see if the value of a is 0; if it is, the code prints the message "no solution for a=0" and returns from the function.

  Write program that simulate the operation of bank account

Your task is to write a program (detailed specifications below) that will simulate the operation of an ATMYenabled bank account with multiple linked cards. In other words, there will be several individuals who can access an account, each with his/..

  What default link state and visited link state

what default link state and visited link state are,on a CSS style sheet. And how to put them on the stlye sheet.

  Write a bioperl-based script

Write a BioPerl-based script ("pick-by-id.pl") to extract sequences by id. It will emulate the "blastdbcmd -db -entry" command. Use "GBB.pep" and "BBA15" as the two arguments.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd