BIG C++
Cay Horstmann & Timothy Budd

Laboratory Notebook
Chapter 26 – An Introduction to Design Patterns


Your name:

Your email address:

Your student ID number:

Once this form has been customized for your institution, you can use this button to send your lab work. Be sure to read the instructions before starting your work.


Lab Objectives

To gain experience with

 

        the advantages of iterators

        the pattern concept

        several common design patterns: ITERATOR, ADAPTER, TEMPLATE METHOD, STRATEGY, and COMPOSITE

        patterns used in the standard C++ library

        putting patterns to work in a complex program

 


 

P1. Preliminary Programming

 

Assume you are writing software for a restaurant. In preparation for this task and subsequent problems within this lab, you will need to implement two classes. One for representing the nutritional value of an item of food and the other for representing a drink. First, create a base class called FoodItem. This class should contain a number of items such as calorie count, amount of fat, amount of sodium, and amount of carbohydrates. Include constructors and accessor functions as indicated by the class definition provided below. Also, include a single function called printNutritionLabel(). This function should neatly print out a nutrition label for the food to the screen. The foodName variable should store the name of the food represented by the object. (i.e., french fries, hamburger, etc.).

class FoodItem

{

public:

FoodItem(string foodName, double iSodium, double iFat,

double iCarbohydrates, double iSugar, int iCalories);

string getFoodName();

int getNumCalories();

double getGramsSodium();

double getGramsFat();

double getGramsSugar();

double getGramsCarbohydrates();

private:

string foodName;

int numCalories;

double gramsSodium;

double gramsFat;

double gramsSugar;

double gramsCarbohydrates;

};

 

Now, implement a class called DrinkItem. A drink item is very similar to a FoodItem. Implement the DrinkItem class according to the definition supplied below.

 

class DrinkItem

{

public:

DrinkItem(string drinkName, int iCalories, double iCarbs,

double iSugar);

string getFoodName();

int getNumCalories();

double getGramsCarbohydrates();

double getGramsSugar();

private:

string foodName;

int numCalories;

double gramsCarbohydrates;

double gramsSugar;

};

 

Supply your answer in the space below

 


 

R1. Adapter Patterns

You will now get some practice in writing adapter patterns. A drink may be considered a type of food. Therefore, it may be useful for you to create an adapter for converting a DrinkItem to a FoodItem. Define and implement such as class in the space below.

Submit your answer in the space below


 

P2. Testing

Write a short main() function to test the three classes written in the previous problems within this lab. You should create both drink and food items and test your adapter class for converting from drink to food.

Provide all code in the space below.


 

R2. Template Patterns

In this problem, you will gain a better understanding of the template pattern. In the template pattern, a base class implements an algorithm using a combination of primitive functions implemented within a derived class. To illustrate, you must first add two member functions to your FoodItem class. The first is called printPrice(). This function will print the price of the food item to the screen. To determine the price to display, a second function is required, called computePrice(). The function definitions are provided below. The computePrice() function should be implemented with a dummy implementation.

virtual double FoodItem::computePrice();

void FoodItem::printPrice();

 

Now, implement a class called SellableFoodItem that is derived from FoodItem. This class should implement the printPrice() function and define an extra private variable called basePrice. The variable basePrice will represent the lowest possible price of the food item. Your job is to compute the final price of the FoodItem according to the following formula: Total = basePrice + 10.00*(-.00694x^2 + .16666*x) where x is the time of day in 24 hour format. In this format, midnight is 0 and 11:59pm is 23.98. This formula adjusts the price of the item so that, at lunchtime (noon), the price is ten dollars higher than the base price. The price falls of in parabolic shape for the rest of the day. Your computePrice() function should read the time of day from the computer system and compute the appropriate price. The class definition is provided below.

 

class SellableFoodItem : public FoodItem

{

public:

SellableFoodItem(string iFoodName, double iSodium, double iFat, double iCarbohydrates, double iSugar, int iCalories,double iBasePrice);

virtual double computePrice();

private:

double basePrice;

};

Provide your answer in the space below.


 

P3. More Testing

 

Modify your main() function from Problem P2 such that it tests the new functionality.

 

Insert all code in the space below.


 

R3. Explanation

In Problem R2, you implemented a dummy implementation for computePrice() within the FoodItem class. Why was this necessary?

Comment out the definition and dummy implementation of the function. Will the software still compile?

You had a choice when implementing the dummy implementation. More specifically, you had to return a dummy double value. What was your choice of return value? Justify your answer.


 

P4. Composite Classes

 

In this problem, you will learn to program with the composite pattern. Define and implement a ValueMeal class that can store a number of FoodItem objects as specified by the constructor. The ValueMeal class is a composite class. The ValueMeal should contain a function called printNutritionLabel() that adds up the various nutrition fields from each class that ValueMeal is composed of and then prints this totaled nutrition label neatly to the screen.

 


Do not forget to send your answers when you are finished.