Ask C/C++ Expert


Home >> C/C++

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.

C/C++, Programming

  • Category:- C/C++
  • Reference No.:- M91350200
  • Price:- $40

Guranteed 36 Hours Delivery, In Price:- $40

Have any Question?


Related Questions in C/C++

Question 1find the minimum and maximum of a list of numbers

Question: 1. Find the Minimum and Maximum of a List of Numbers: 10 points File: find_min_max.cpp Write a program that reads some number of integers from the user and finds the minimum and maximum numbers in this list. Th ...

Software development fundamentals assignment 1 -details amp

Software Development Fundamentals Assignment 1 - Details & Problems - In this assignment, you are required to answer the short questions, identify error in the code, give output of the code and develop three C# Console P ...

What are the legal requirements with which websites must

What are the legal requirements with which websites must comply in order to meet the needs of persons with disabilities? Why is maximizing accessibility important to everyone?

There are several ways to calculate the pulse width of a

There are several ways to calculate the pulse width of a digital input signal. One method is to directly read the input pin and another method (more efficient) is to use a timer and pin change interrupt. Function startTi ...

Assignment word matchingwhats a six-letter word that has an

Assignment: Word Matching What's a six-letter word that has an e as its first, third, and fifth letter? Can you find an anagram of pine grave. Or how about a word that starts and ends with ant (other than ant itself, of ...

1 implement the binary search tree bst in c using the node

1. Implement the Binary Search Tree (BST) in C++, using the Node class template provided below. Please read the provided helper methods in class BST, especially for deleteValue(), make sure you get a fully understanding ...

Assign ment - genetic algorithmin this assignment you will

ASSIGN MENT - GENETIC ALGORITHM In this assignment, you will use your C programming skills to build a simple Genetic Algorithm. DESCRIPTION OF THE PROGRAM - CORE REQUIREMENTS - REQ1: Command-line arguments The user of yo ...

Project - space race part a console Project - Space Race Part A: Console Implementation

Project - Space Race Part A: Console Implementation INTRODUCTION This assignment aims to give you a real problem-solving experience, similar to what you might encounter in the workplace. You have been hired to complete a ...

Why do researcher drop the ewaste and where does it end

Why do researcher drop the ewaste and where does it end up?

  • 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