# Enumerations, Classes and Multiple-file Programs

The Exam Preparation Exercises (EPE) and Programming Warm-up Exercises (PWE) must be word processed. Documentation, including preconditions and postconditions, is not required for the EPEs and PWEs.

Dale Chapter 10 |
Dale Chapter 11 |

EPE 15-18 | EPE 4, 8, 9, 14-17 |

PWE 13 | Apartment Locator Program |

Rational Number Program |

## Apartment Locator Program

**Problem**

Write a program that asks the user for the number of bedrooms and the price
range for an apartment that they would like to rent. The program displays all of
the apartments in the data file that match the criteria.

**Specifications**

Use a struct type to store and process the apartment data: the apartment name,
number of bedrooms, and monthly rent. Use the apartments.dat file provided
online in the Resources link. For every apartment that matches the search
criteria, display its name and price. If no apartments match, then display an
appropriate message.

Specifications

Use a struct type to store and process the apartment data: the apartment name,
number of bedrooms, and monthly rent. Use the apartments.dat file provided
online in the Resources link. For every apartment that matches the search
criteria, display its name and price. If no apartments match, then display an
appropriate message.

## Rational Number Program

**Problem**

A rational number is one that can be expressed as a fraction whose numerator and
denominator are integers. Examples of rational numbers are 0.75 or 3/4 and 1.125
or 9/8. The value of π is not a rational number because it cannot be accurately
expressed as the ratio of two integers. Working with rational numbers in the
floating-point representation can yield imprecise results. Therefore, we will
develop a class that allows us to work with them as two integer values.

A rational number is one that can be expressed as a fraction whose numerator and
denominator are integers. Examples of rational numbers are 0.75 or 3/4 and 1.125
or 9/8. The value of π is not a rational number because it cannot be accurately
expressed as the ratio of two integers. Working with rational numbers in the
floating-point representation can yield imprecise results. Therefore, we will
develop a class that allows us to work with them as two integer values.

In order to give you a little experience with team programming, this one program (not any of the above exercises) may be designed, coded, tested and debugged with a single partner. You may turn in just one copy of the program with the two team members clearly identified in the initial program documentation. To get full credit, the team program must include the extra member function on the back side. Those working individually do not need to complete the extra function.

**Specifications**

Design, implement and test a Rational class that represents one rational number
as a pair of integers (no other data members in the class). The class must be
implemented using a specification file (e.g. rational.h) and an implementation
file (e.g. rational.cpp). In these two Bruce Purcell © 2005-2006 CIS10Lab6.doc
3/14/09

files, use the labels public, private and const as appropriate. Negative
fractions are to be designated by a negative numerator; denominators should
always be positive. All operations must be done using only integer values.

The class should provide the following eight operations:

• A default constructor that initializes the numerator to
0 and the denominator to 1.

• An input function that prompts the user to enter an integer numerator and an
integer denominator for the object’s data members. Have the user to enter the
fraction until the denominator is > 0. No other data validation is required.
Parameters should not be used.

• Four arithmetic functions for adding, subtracting, multiplying, and dividing
two rational objects. The left operand of the arithmetic operation is the
rational object that calls the function and the right operand is the rational
object that is passed as a parameter. These are value-returning observer
functions that return a Rational result. See the Add function in money.cpp. Be
sure these functions handle negative fractions correctly.

• An equality function that returns a bool value after comparing two Rational
objects. For example, the fractions 1/2 and 3/6 should return true.

• An output function that displays the value of a Rational object in the form of
a fraction (numerator / denominator). No other text is displayed and no other
member function displays results.

The client code needs to do the following:

• Declare some rational objects.

• The first rational object (left operand) calls its input function to get its
numerator and denominator.

• The second rational object (right operand) also gets its numerator and
denominator.

• Inside of a loop, display a menu of program choices to the user, get his or
her selection, and implement it. The menu has the following seven choices (list
one choice per line):

o display the sum of the two fractions

o display the difference

o display the product

o display the quotient

o display whether the fractions are equal

o input two more rational numbers

o exit

• Use a switch statement to implement the seven choices and handle errors. The
menu system should be bulletproof: the program should handle any data entered
for the menu choice, display an appropriate error message, and ask for the
user’s selection again.

• Some sample code to display the sum of two fractions:

rationalResult = rationalNum1.AddedTo(rationalNum2);

rationalNum1.Write();

cout << “ + “;

rationalNum2.Write();

cout << “ = “;

rationalResult.Write();

cout << endl;

3/4 + -4/3 = -7/12

• As before, it is important to identify cohesive tasks in the client code to
write as separate functions below the main function.

The client file only requires initial program documentation. The specification and implementation files require the Comments in Class Specification and Implementation files specified in the Homework Procedures handout (pages 1 & 2). An incomplete set of documentation is in your textbook on pages 566 – 573.

**Extra member function for teams**

Write an additional Rational operation that reduces a fraction to its simplest
terms by finding the greatest common divisor. For example, 12/4 would become
3/1. The operation should work with negative fractions. All results should now
display in the simplest terms.

**Turn in**

The program code for the client, specification, and implementation files.

Output showing all the features of your program using simple fractions, one of
which is negative.