Part B - Foundations

Compound Types

Workshop 2

In this workshop, you are to define a compound type with private data and public member functions.

Learning Outcomes

Upon successful completion of this workshop, you will have demonstrated the abilities

• to design a compound type
• to privatize data within instances of that type
• to access data within an instance of the compound type through public member functions

Fraction Simplifier - All Groups

Design and code a compound type named Fraction that has three member functions named

• void set(int n, int d)
• void simplify()
• void display() const

The set() function accepts the numerator and denomenator from the client and stores simplified values in a Fraction object.  The simplify() function simplifies the numerator and denominator, if possible, in a Fraction object.  The display() query displays the fraction stored in a Fraction object on standard output.

The main program that uses your compound type contains the following code.  The output from this program looks something like that shown on the right.

 ``` // Workshop 2 - Fraction Simplifier // w2.cpp #include using namespace std; #include "Fraction.h" int main() { Fraction fraction; int num, den; cout << "Fraction Simplifier" << endl; cout << "===================" << endl; cout << "Numerator : "; cin >> num; cout << "Denominator : "; cin >> den; cout << endl; fraction.set(num, den); fraction.display(); cout << endl; } ``` ``` Fraction Simplifier =================== Numerator: 4 Denominator: 16 1 / 4 ```

Place your definition of the Fraction type in a header file named Fraction.h.  Place your member function definitions in an implementation file named Fraction.cpp

For a complete solution you need to design an algorithm that simplifies a fraction.  Test several fractions to ensure that your algorithm works for all cases.  Include negative as well as zero values in your test cases.

Typescript - Group X only

On matrix, create a typescript of your complete solution using the following commands:

 ``` + At the prompt, type: script w2.txt + At the prompt, type: whoami + At the prompt, type: cat w2.cpp Fraction.h Fraction.cpp  + At the prompt, type: g++ -o w2 w2.cpp Fraction.cpp + At the prompt, type: w2 + At the input prompt, enter some test data + At the prompt, type: w2 + At the input prompt, enter some other test data + At the prompt, type: w2 + At the input prompt, enter some other test data + At the prompt, type: w2 + At the input prompt, enter some other test data + At the prompt, type: exit ```

These commands will produce a file named w2.txt.

For submission instructions, see the SUBMISSION section below.

Unit Tests - Group Y

• int numerator() const
• int denominator() const

The numerator() query returns the numerator stored in a Fraction object.  The denomenator() query returns the denomenator stored in a Fraction object.

The main program that tests your compound type contains the following code.  The output from this program looks something like that shown on the right.

 ``` // Workshop 2 - Fraction Simplifier // w2.cpp #include using namespace std; #include "Fraction.h" int unitTests(Fraction* f) { cout << "Fraction Simplifier Tests" << endl;  cout << "=========================" << endl; // fill in code for unit tests } int main() { Fraction fraction; bool passed; passed = unitTests(&fraction); if (passed) cout << "Passed All Tests" << endl; else cout << "Failed Tests" << endl; } ``` ``` Fraction Simplifier Tests  ========================= Test 1 Passed Input: 4/16 Output: 1/4 Test 2 Passed Input: -4/16 Output: -1/4 Test 3 Passed Input: 0/16 Output: 0 Test 4 Passed Input: 0/0 Output: undefined Test 5 Passed Input: 4/0 Output: undefined Passed All Tests ```

Place your definition of the Fraction type in a header file named Fraction.h.  Place your member function definitions in an implementation file named Fraction.cpp

Typescript

On matrix, create a typescript of your complete solution using the following commands:

 ``` + At the prompt, type: script w2.txt + At the prompt, type: whoami + At the prompt, type: cat w2.cpp Fraction.h Fraction.cpp  + At the prompt, type: g++ -o w2 w2.cpp Fraction.cpp + At the prompt, type: w2 + At the prompt, type: exit ```

These commands will produce a file named w2.txt.

SUBMISSION

Moodle

• Select W2 under Assignments and Tasks
• Write a short note to your instructor
• press "Edit"
• press "Save Changes"
• When ready to submit, press "Send for Marking"

Other Submissions  