Project 1: A Bear Class

American black bears live in North America, ranging from Canada to Mexico. Black bear adult males weigh about 300 to 400 pounds. Because of their smaller size, they can climb trees. Typically these bears are black in color but coat colors range from white, blond, cinnamon, or light brown to dark chocolate brown or to jet black, with many intermediate variations existing. Black bears eat mostly vegetation, such as berries, but sometimes eat fish and other animals. [1]

Grizzly bears are larger, with adult males weighing between 800 and 1200 pounds on average. Their fur color ranges from blond to nearly black but is typically brown. They also live in the north, many in Alaska and the northern parts of Asia. Their diet is omnivorous, including berries, fish, and moose. [2]

Polar bears inhabit arctic regions of the world, such as northern Canada and Alaska. Their fur color is transparent but looks white because of the surrounding snow and ice. Polar bears are very large, with adult males weighing an average of 1000 pounds. The most carnivorous of the bear family, their diet consists of primarily of seals. However, they also eat other foods such as birds, reindeer, and even other polar bears when seals are not available. [3]

Project Specifications

Write a declaration and a definition for a class named Bear along with a short main() function to test the class code.

You must name the source code file bear.cpp and include all your code in this single file.

Be careful of the spelling, including capitalization, as you will lose points for a misspelled name. Naming is important in programming.

Have no user input for this project. Testing code for the program is hard-coded in main().

Do NOT use any keyboard input streams, like cin, in this project. If you have a cin statement in your code, you will get a low score.

The Bear class must contain exactly (no more and no less than) three member variables declared private:

string type

int weight

string color

Write function declarations (prototypes) inside the class and separately define (implement) all the functions outside the class declaration (curly braces of the class).

Write a default constructor that sets the type to "Black" and the weight to 350.

Write a three-parameter constructor that sets the type, weight and color member variables to the parameter values. The parameters must be coded in the order of type, weight and color.

Write one "get" function for each of the member variables that returns the variable's current value. The name of the function must follow the naming standards for "get" functions, which is the name of the variable with the prefix word "get".

All non-mutator functions must include the keyword const as discussed in lesson 9.2.4 under Accessor Functions and in the textbook on pages 402-403 under the section on, "const Correctness".

In your main() function, construct three Bear objects, one of each type. Then write a story that calls all the member functions of the class at least one time. For example:

***My Bear Stories***
Once when I was hiking a saw a brown mother bear with two cubs.
I knew that all Grizzly bears are dangerous, especially mother
bears if they are protecting their cubs. My companion and I slowly
backed away and the bears went the other way.
When I was in Glacier Park a few years, one of the rangers was biking
and came upon a bear. The bear killed him.
I have never seen a Black bear but know they mostly eat vegetation.
Even so, at 350 pounds, be careful and carry your bear mace when
hiking in bear country.
Polar bears are endangered because of global warming. At the zoo a
Polar bear gets a meal of fortified meat, dog kibble and fish.
The Polar bear eats the #dog kibble#. Yum!

In the above example story, the text in italics shows the output of member functions calls. Your program does NOT print the characters using italics. You can make up your own bear story.

After displaying the story, exit the program.

Submit this project with the rest of the assignment as described in Deliverables.

References and More Information

Project 2: A Classy Person

When we declare a class, we create a model of an object in software. To be effective, we create an abstraction of what we are modeling. Abstraction is the reduction of a concept or idea to the most simple or basic of shared characteristics. Once we have determined the essential characteristics, we provide ways to generalize the class. Generalization is about developing a general solution that works for a range of similar problems. Once we develop a class, we may create many objects from the class.

In this project, we create a simple abstraction of a person. We will declare a programming interface and define the functions for that interface. In addition we will write a short main() function to test our interface.

Project Specifications

Write a declaration and a definition for a class named Person along with a short main() function to test the class code.

Name the source code file person.cpp and include all your code in this single file.

Be careful of the spelling, including capitalization, as you will lose points for a misspelled name. Naming is important in programming.

Have no user input for this project. Testing code for the program is hard-coded in main().

Do NOT use any keyboard input streams, like cin, in this project. If you have a cin statement in your code, you will get a low score.

The Person class must contain exactly (no more and no less than) three member variables declared private:

string name

int age

double income

Write the following function declarations (prototypes) inside the class braces and separately define (implement) the functions outside the class declaration (curly braces of the class):

A default constructor that sets the numerical types to zero (0).

A three-parameter constructor that sets the name, age and income member variables to the parameter values. The parameters must be coded in the order of name, age and income.

One "get" function for each of the member variables that returns the variable's current value. The name of the function must follow the naming standards for "get" functions, which is the name of the variable with the prefix word "get".

One "set" function for each member variable that sets a new value. The name of the function must follow the naming standards for "set" functions, which is the name of the variable with the prefix "set".

Accessor function print() that has no parameters and returns no values, but displays the data of the object all on one line like this:

Emma Hacker 24 71916.00

Text must be left justified and numbers right justified with the correct number of decimal places. The data displayed in order are:

name

age

income

Do not add any extra member functions than the above.

Note: even though you must write the get functions, you may not need to actually call them for this assignment. We will be using the get functions in future assignments.

All non-mutator functions must include the keyword const as discussed in lesson 9.2.4 under Accessor Functions and in the textbook on pages 402-403 under the section on "const Correctness".

In the person.cpp file, use the specified main() function to do the following:

Construct at least the three Person objects.

Set values (name, age and income) for two objects using the three-argument constructor and for one object using the default constructor and "set" functions.

Display output for all 3 objects as shown in the Example Output specification.

Have no user input for this project. Instead use the above code in main() for testing. Do NOT use any keyboard input streams, like cin, in this project. If you have a cin statement in your code, you will get a low score.

Example Run: When you run the program you must see the following output without any user input.

Persons of Interest:
Name Age Income
Sophie Engineer 42 102280.00
Emma Hacker 24 71916.00
John Q Public 37 55775.42

You may change the spacing between columns but columns must line up as shown, with names left justified and numbers right justified with two decimal points.

Create the correct spacing for the output using the setw()formatting manipulator described on pages 49-51 (1/e: 53-55) of the textbook. In addition, you will need to use the left and right formatting manipulators. To use these manipulators, include the iomanip library. Align text or numbers in a setw() field using left or right to align the data. For example:

cout << setw(18) << left << name; // aligns data left in 18 character field

After displaying the output, exit the program.

Submit the source code file person.cpp with the rest of the assignment as described in Deliverables.

Extra Credit

The following are worth extra credit points if the main program works well:

Complete the assignment using pair programming. (2 points)

Add an extra member function eat() to the Bear class with the following signature: (2 points)

/**
The bear randomly chooses one of the offered foods to eat and
prints the food surrounded by # marks.
@param food An array of foods to eat.
@param size The number of elements in the array.
*/
void eat(string food[], int size) const;

Call the function at least once in your story. When called, the function selects one of the foods at random and prints the type of the bear and the name of the food consumed surrounded by # marks like:

Add a function named toString() to class Person that has no parameters and returns a string value containing all the data about the person. Call the function one time to show that it works. (2 points)

The function does NOT print any values and must NOT use cout. Instead, the function must return a string with one line of text like that shown below.

Emma Hacker 24 71916.24

Once returned, print the string within main() using code like:

cout << emma.toString() << endl;

Note that this extra credit will require you to convert numbers to strings. Research stringstream for the conversion and provide an attribution for the source of your stringstream information, either a URL or book and page number. No attribution means no credit.

Tutorial Lab

In preparation for next weeks lessons, complete the following:

Read the assigned reading in the textbook

Complete the Tutorial Exercises in CodeLab 9 before the specified due date.

Refer to the assigned reading for the next lesson to help you understand the problems. Also, you can use the online lecture notes for more information as the notes become available. You can look at solutions if you miss your first few attempts and are stuck by clicking the "Solution" tab.

Grading Criteria

The instructor will evaluate your assignment using the following criteria. Thus you should check your assignment against these criteria to maximize your score.

Each criteria represents a specific achievement of your assignment and has a scoring guide. The scoring guide explains the possible scores you can receive. Some scoring guides have a list of indicators. These indicators are a sign of meeting, or a symptom of not meeting, the specific criterion. Note that a single indicator may not always be reliable or appropriate in a given context. However, as a group, they show the condition of meeting the criterion.

You must submit all the files needed to complete your assignment. Your assignment must work as submitted. Remember to test and double check your files before submitting them. If you make a mistake, you can resubmit up to the deadline. If you resubmit, you must include all your assignment files in the last submission as Canvas hides prior submissions.