Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Assignment 3.1 [15 points] This assignment will not be graded for style issues. If your code works correctly, you'll get 15 points. Style will be

Assignment 3.1 [15 points]

This assignment will not be graded for style issues. If your code works correctly, you'll get 15 points. Style will be graded next week.

Here are the client program (https://hastebin.com/naqevuhuke.cpp) and correct output (https://hastebin.com/kivokebeci.vbs).

Write a class to handle objects of type Fraction. In its simplest form, a Fraction is just two integer values: a numerator and a denominator. Fractions can be negative, may be improper (larger numerator than denominator), and can be whole numbers (denominator of 1).

This is the first part of a two part assignment. Next week you will be making some refinements to the class that you create this week. For example, no documentation is required this week, but full documentation will be required next week. Also, I expect you to work with just a single file this week. You should begin by copy/pasting the provided client program (https://hastebin.com/naqevuhuke.cpp) into a file and add your class to the file. The class declaration will be first in the file, followed by the definitions of member functions, followed by the client code. Next week you will divide the program up into three files.

Your class should support the following operations on Fraction objects:

Construction of a Fraction from two, one, or zero integer arguments. If two arguments, they are assumed to be the numerator and denominator, just one is assumed to be a whole number, and zero arguments creates a zero Fraction. Use default parameters so that you only need a single function to implement all three of these constructors.

You should check to make sure that the denominator is not set to 0. The easiest way to do this is to use an assert statement: assert(inDenominator != 0); You can put this statement at the top of your constructor. Note that the variable in the assert() is the incoming parameter, not the data member. In order to use assert(), you must #include

For this assignment, you may assume that all Fractions are positive. We'll fix that next week.

Printing a Fraction to a stream with an overloaded << operator. Next week we will get fancy with this, but for now just print the numerator, a forward-slash, and the denominator. No need to change improper Fractions to mixed numbers, and no need to reduce.

All six of the relational operators (<, <=, >, >=, ==, !=) should be supported. They should be able to compare Fractions to other Fractions as well as Fractions to integers. Either Fractions or integers can appear on either side of the binary comparison operator. You should only use one function for each operator.

The four basic arithmetic operations (+, -, *, /) should be supported. Again, they should allow Fractions to be combined with other Fractions, as well as with integers. Either Fractions or integers can appear on either side of the binary operator. Only use one function for each operator.

Note that no special handling is needed to handle the case of dividing by a Fraction that is equal to 0. If the client attempts to do this, they will get a runtime error, which is the same behavior they would expect if they tried to divide by an int or double that was equal to 0.

The shorthand arithmetic assignment operators (+=, -=, *=, /=) should also be implemented. Fractions can appear on the left-hand side, and Fractions or integers on the right-hand side.

The increment and decrement (++, --) operators should be supported in both prefix and postfix form for Fractions. To increment or decrement a Fraction means to add or subtract (respectively) one (1).

Additional Requirements and Hints:

The name of your class must be "Fraction". No variations will work.

Use exactly two data members.

You should not compare two Fractions by dividing the numerator by the denominator. This is not guaranteed to give you the correct result every time. I would simply cross multiply and compare the products.

Don't go to a lot of trouble to find the common denominator (when adding or subtracting). Simply multiply the denominators together.

The last two bullets bring up an interesting issue: if your denominators are really big, multiplying them together (or cross multiplying) may give you a number that is too big to store in an int variable. This is called overflow. The rule for this assignment is: don't worry about overflow in these two situations.

My solution has 20 member functions (including friend functions). All of them are less than 4 lines long. I'm not saying yours has to be like this, but it shouldn't be way off.

Do not use as a resource a supplementary text or website if it includes a Fraction class (or rational or ratio or whatever).

Getting Started

Here are some suggestions for those of you who have trouble just figuring out where to start with assignment 1. Remember to use iterative development. That means start with the smallest, simplest subset of the final product that you can, make sure it works, and then start adding things to it one at a time (preferably the simple things first, if possible).

Start with just a default constructor and a stream insertion operator. For now, don't even worry about mixed numbers, just write the stream insertion operator so that it works with proper fractions. Test this out with a client program something like this:

int main(){ Fraction f1; cout << f1; } 

(You should get output of "0/1" because you should have initialized the fraction to 0/1 in your constructor.)

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

Databases Organizing Information Digital And Information Literacy

Authors: Greg Roza

1st Edition

1448805929, 978-1448805921

More Books

Students also viewed these Databases questions

Question

600 lb 20 0.5 ft 30 30 5 ft

Answered: 1 week ago