CS24, Fall 2016

Lab02:
Define, implement, apply a C++ class


Goals for this lab

By the time you have completed this lab, you should be able to

We assume you already know everything that was covered in Lab01, and we will not repeat instructions given there for basic operations.

Step by Step Instructions

Step 0: Get together with your lab partner (chosen last week)

Choose who will be the pilot for the first part of the lab. The pilot should sit down in front of the computer now. The navigator gets a chair and sits next to the pilot. You should exchange roles after awhile, before the pilot gets tired, and before the navigator gets bored or distracted.

If your regular partner is more than 5 minutes late, ask the TA to pair you with someone else for this week.

Step 1: Create a directory for this lab in the pilot's account

Log onto the pilot's account. If the pilot's account is not working, allow the navigator to log in instead. You will (both) work in this account for the rest of the lab.

Open a terminal window. As a reminder, that's the Application Menu, then System Tools, then Terminal Window.

Create a ~/cs24/lab02 directory and make it your current directory:

mkdir ~/cs24/lab02
cd ~/cs24/lab02

Step 2: Study a non-OO program, and copy an incomplete OO conversion

In the rest of this lab, you will finish writing a C++ program that uses an object-oriented (OO) approach to solve exactly the same problems that are solved by rugfit1.cpp - but first study this program to understand the problems and their non-OO solutions:

Here is a sample run of the program (user input is bold like usual):

-bash-4.3$ ./rugfit1
enter width and length of floor: 10.5 15
enter width and length of rug: 13.2 7.9
floor area: 157.5
rug area: 104.28
leftover rug area: 0
empty floor area: 53.22

Your revision of this program should operate exactly the same way.

Now save a copy of this skeleton of rugfit2.cpp in the first pilot's lab02 directory to prepare for the next step. No need to copy/paste - use cp to get a copy from the class account as follows:

cp ~cs24/labs/lab02/rugfit2.cpp ~/cs24/lab02/

Step 3: Know what it means to design an OO program

An experienced OO programmer would frown at the sight of variable names like floorWidth and floorLength, and would absolutely cringe at then seeing names like rugWidth and rugLength. Such a programmer's object-oriented training would scream out the need for objects named floor and rug, each with its own width and length attributes. And although this programmer would appreciate the procedural abstraction of an area function, he or she would prefer to let the floor and rug objects calculate their own areas. In response, the OO programmer probably would decide to write a class that can represent either a floor or a rug, or any other rectangle for that matter. Then he would use objects of this class to solve problems - maybe even future problems the programmer is not facing yet.

Here are the steps necessary to achieve such an object-oriented solution:

  1. Write a class definition for class Rectangle. This definition includes declarations for private instance variables to store a width and a height, and declarations for public methods that can be used by programs that that need Rectangle objects. This class is the abstraction (the ADT).
  2. Define (a.k.a. implement) the methods of class Rectangle that were only declared in the class definition. These method definitions use a special syntax that includes a "scope resolution operator" (::) to tie them to the Rectangle class. These methods implement the abstraction.
  3. Create and use Rectangle objects to solve problems, often just in a main function. This main function applies the abstraction.

Discuss the meaning of these steps with your lab partner, to make sure you both understand (at least generally) what you are to do, and hopefully gain an appreciation for why you might want to do it that way.

Step 4: Complete rugfit2.cpp

First you should study the parts of rugfit2.cpp that are complete. It consists of three main parts - and in later labs you will normally store such parts in separate files: (1) the abstraction - class Rectangle is defined; (2) the implementation - the methods of class Rectangle are defined (a.k.a. implemented); and (3) the application - the main function is defined. Your job involves additions to each of these parts.

Now it is time to edit the program with emacs or another editor of your choice. Lab01 had a link to some emacs help if you need it.

emacs rugfit2.cpp

Make the following edits, then save, and quit the editor. Maybe you want to switch roles between pilot and navigator about now (but stay logged into the original pilot's account).

  1. Fix the comment at the top to show your names and the date you are doing this lab. By the way, this instruction always applies whether or not we remind you to do it in future labs or programming projects - always type your name(s) and the date at the top of all your programs.
  2. In the definition of class Rectangle: declare a function named area that will take no arguments and will return a double value. Make the function const - meaning it promises not to change the object on which it is called - just like the getWidth and getLength functions that are already declared in the skeleton.
  3. Scroll down past the end of the class definition and past the existing definitions of the constructor and four standard get and set methods, and then define the area method you declared above. Look at the other method definitions as examples for how to do it - notice they use the scope resolution operator to identify their connection to a particular class, as in Rectangle::setLength which identifies it as pertaining to class Rectangle. Return the value of length times width.
  4. In the main function: prompt the user for the width and length of the rug, read those two values from the user, and then reset the width and length of the Rectangle object named rug with the user's dimensions (using the rug's setWidth and setLength methods, of course). As a reminder, you use the object's name, the dot operator, and the name of the method to do such things. For example, if we wanted to find the value of the floor's width, we could do so by using the getWidth method as follows:
    floor.getWidth()
  5. Also in main: change the two assignment statements for floorArea and rugArea to use the area method for each of the floor and rug objects.

Step 5: Compile and run the program to test it

Use make to compile your program. Then run it to make sure everything works. Here is a test of our solution:

-bash-4.3$ make rugfit2
g++     rugfit2.cpp   -o rugfit2
-bash-4.3$ ./rugfit2
enter width and length of floor: 10 11.5
enter width and length of rug: 8 15
floor area: 115
rug area: 120
leftover rug area: 5
empty floor area: 0

If errors occur: read the error messages and try to figure out what needs changing. Don't just randomly make changes, but instead really think about the problem and how to fix it. Ask the TA for help only if you are truly stumped, but give it at least 5-10 minutes worth of study first.

Step 6: Submit rugfit2.cpp, and verify your work

Submit Lab02 at https://submit.cs.ucsb.edu/, or use the following command from a CS terminal:

~submit/submit -p 548 rugfit2.cpp
Be sure to wait for the results of the 4 simple tests.

If you are working with a partner, be sure that both partners' names are in a comment at the top of the source code file, and be sure to properly form a group for this project in the submit.cs system.

Don't leave early though ... see challenge problems below.


Evaluation and Grading

Each student must accomplish the following to earn full credit [50 total points] for this lab:


Optional Extra Challenge

DO NOT CHANGE rugfit2.cpp to do these challenge problems, and DO NOT resubmit Lab02. Instead you should create copies that have different names. The problems below are just for practice - you will not turn in any of your solutions.

Prepared by Michael Costanzo. Thanks to Diana Franklin for ideas.