Prepare a a simple class fraction

Assignment Help C/C++ Programming
Reference no: EM13780039

Work on the following exercises in the sequence indicated.

Logging On. Log on with your username and password. If you experience any difficulty, let the lab instructor know immediately. Insist that your problem be fixed in the beginning of this lab.

The topic of this lab is definition of user-defined classes, and the programming with objects.

You may think of object simply as variables that are of a type that happens to be a userdefined class. In this lab you will define a class Fraction which will allow us to represent numeric values as fractions, e.g., 1/2, 5/3, 7/21, etc.

Exercise 1: A simple class Fraction. Open a new file fraction.cpp and define a class

Fraction as suggested by the following "skeleton" class:
class Fraction
{
public:
// constructor that initializes numerator and denominator;
// allow only positive fractions, and do not allow 0 for
// the denominator;
Fraction(int n, int d)
{
// ...
}
// set function: sets numerator and denominator to values passed;
// allow only positive fractions, and do not allow 0 for
// the denominator;
void set(int n, int d)
{
// ...
}
// get function: returns value of the numerator;
int get_numer()
{
// ...
}
1// get function: returns values of the denominator;
int get_denom()
{
// ...
}
// prints the fraction in form of x/y;
void print()
{
// ...
}
private:
// two integer data members, one for numerator, one for
// denominator;
// ...
};Test your class with the following int main() function:
int main()
{
int a, b;
cout << endl;
cout << "Enter the numerator and denominator of your first fraction: ";
cin >> a >> b;
Fraction frac1(a,b);
cout << endl;
cout << "Enter the numerator and denominator of your second fraction: ";
cin >> a >> b;
Fraction frac2(a,b);
cout << endl;
cout << "Your fractions are: ";
frac1.print();
cout << " and ";
frac2.print();
cout << endl << endl;
return 0;
}

Do not forget all necessary #include statements and using namespace std; Compile and test for a variety of inputs.

Exercise 2.: Normalizing fractions. Enhance your class Fraction by adding the capability to convert a fraction into its normalized form. A fraction is normalized when its its numerator and denominator have taken on the smallest possible integer values that, as fraction, represent the same value as the original fraction. For example, fraction 3/15 is normalized to 1/5. In general, a fraction is normalized by dividing the numerator and denominator by their greatest common factor (which is 3 for the example).

The following recursive function is the quickest way to write a function that will, for any two integers, determine the greatest common factor.

// greatest common factor;
int gcf(int a, int b)
{
if (a % b == 0)
return b;
else
return gcf(b, a % b);
}
Since we have not (yet) formally discussed recursion in the lecture, you may simply take this function for granted. Add this function to your file fraction.cpp. Make sure that either the prototype, or the entire function definition appears before class Fraction.

Within class Fraction add a member function
void normalize()
{
// ...
}
with its body implementing the normalization. In order to test your new member function, add the following lines to the end of your int main() in Exercise 1:
frac1.normalize();
frac2.normalize();
cout << "They are normalized: ";
frac1.print();
cout << " and ";
frac2.print();
cout << endl << endl;

Compile and test with a few example inputs. Verify that your fractions are being normalized, and realize that some fractions may already be in this from the start (and normalizing will not change them).

Exercise 3.: Adding fractions. Enhance your class Fraction even further by adding a member function that allows the addition of two /tt Fraction objects. Given two fractions, frac1 and frac2, you should be able to add both with a member function call Fraction sum = frac1.add(frac2);

Notice how frac1, is plays the role of the calling object, and frac2 is the argument object of the .add member function call.

Recall the mathematical operations to add two fractions. The following steps are easiest to implement:
                                    x*z + w*y
x/y + w/z = NORMALIZE(-------------)
                                        y*z
Add a member function add to class Fraction:
Fraction add(Fraction other)
{
// ...
}
Test the new member function by adding to your int main() the following lines of code:
Fraction sum = frac1.add(frac2);
cout << "Result of adding both: ";
sum.print();
cout << endl;
Compile and test with a few examples.

Reference no: EM13780039

Questions Cloud

Global strategy-multi-domestic or transnational strategy : The text describes 3 international strategies that firms deploy in order to enter foreign markets: Global Strategy, Multi-domestic Strategy, or Transnational Strategy. Pick one of these strategies, and pick a company that you think utilizes such a st..
What are the pros and cons of that strategy : Why are some U.S. companies, such as Eaton, reincorporating in foreign countries, such as to Dublin, Ireland, as did Eaton? What are the pros and cons of that strategy?
During the mortgage crisis of 2008-2009 : During the mortgage crisis of 2008-2009, there was a proposal to suspend mark-to-market accounting. Why was this proposal brought up and why was it ultimately not accepted?
Write each of the given in a correct form : Select the correct statement. Write each of the following in a correct form without using any negative exponents:
Prepare a a simple class fraction : Web tickets are purchased on the internet. Web tickets purchased a week in advance of the event are £30 and those purchased less than a week in advance are £40.
Healthcare systems in the united states : Research the ethical issues of reforming the healthcare systems in the United States and at least one other country using your textbook, the Argosy University online library resources, and the Internet.
Terrorist attack against the united states : Terrorist Attack against the United States, Address the elements listed below that are specific the following attack:
Describe what sort of search you would conduct to help you : Describe what sort of search you would conduct to help you in responding to this student and why. What search engine(s) or database(s) might you use and why?
Write a program that reads in from a ?le : Write a program that reads in from a ?le a starting month name, an ending month name, and then the monthly rainfall for each month during that period.

Reviews

Write a Review

C/C++ Programming Questions & Answers

  Create program that uses functions and reference parameters

Create program that uses functions and reference parameters, and asks user for the outside temperature.

  Write a program using vectors and iterators

Write a program using vectors and iterators that allows a user to maintain a personal list of DVD titles

  Write the code required to analyse and display the data

Calculate and store the average for each row and column. Determine and store the values for the Average Map.

  Write a webservices application

Write a webservices application that does a simple four function calculator

  Iimplement a client-server of the game

Iimplement a client-server version of the rock-paper-scissors-lizard-Spock game.

  Model-view-controller

Explain Model-View-Controller paradigm

  Design a nested program

How many levels of nesting are there in this design?

  Convert celsius temperatures to fahrenheit temperatures

Write a C++ program that converts Celsius Temperatures to Fahrenheit Temperatures.

  Evaluate and output the value in the given base

Write C program that will input two values from the user that are a Value and a Base with which you will evaluate and output the Value in the given Base.

  Design a base class shape with virtual functions

Design a base class shape with virtual functions

  Implementation of classes

Implementation of classes Chart and BarChart. Class barChart chould display a simple textual representation of the data

  Technical paper: memory management

Technical Paper: Memory Management, The intent of this paper is to provide you with an in depth knowledge of how memory is used in executing, your programs and its critical support for applications.

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