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.
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.
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
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/
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:
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).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.
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).
floor.getWidth()
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.
Submit Lab02 at https://submit.cs.ucsb.edu/, or use the following command from a CS terminal:
~submit/submit -p 548 rugfit2.cppBe 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.
Each student must accomplish the following to earn full credit [50 total points] for this lab:
This lab is due by 11:59 pm tonight.
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.
- The current program seems incomplete in a way. It says how much bare floor space might result or how much extra rug there is, but it does not match up the width and length dimensions in any way. A nicer program would say something like "cut X from the length to fit" or "add Y more rug to the width" for instance. It seems straightforward to find two new Rectangle objects - one for excess or lacking width, and one for excess or lacking length. Change the main function (in your new copy of the program) to work this way (no need to change anything else), or do the next challenge instead.
- Realize that without making any changes to class Rectangle or its implementation, you can use those parts to solve completely different problems just by changing main. For instance, first use
cp
to copy rugfit2.cpp to wallpaper.cpp, and then change main as follows:Notice how this problem shows that your abstraction is reusable, and it is why we normally store the abstraction in a separate file, so we can use it again and again to solve different problems.
- Create objects named wall and paper (in place of floor and rug, respectively), with user-entered dimensions for the wall and for a single strip of wallpaper.
- Then calculate how many strips of wallpaper are needed to cover the wall.
- Modify wallpaper.cpp (from the last challenge) to account for one or more windows and/or one or more doors - just create and use some more Rectangle objects of course.
- In case you need something more to do, why not improve class Rectangle a bit? For instance, you might want a rectangle to store the locational coordinates (x, y) of one of its corners. This might help you reuse the class in a graphical application someday, or as a way to determine whether or not two rectangles intersect one another in a different type of application. Specifically, do the following:
- Add two variables named x and y to class Rectangle's private section.
- Add the corresponding get and set method declarations to the public portion of class Rectangle: getX, getY, setX and setY. Note: usually we routinely provide such methods for private instance variables, unless there is a good reason not to provide such access (which does happen sometimes).
- Define the get and set methods outside the class definition - in the same way the other class methods are defined.
- Revise or replace the main function to test your new parts of class Rectangle.
- Already finished the previous challenge, and still got some time? Add an intersects and/or intersection method to class Rectangle. Then implement and test. Here are suggested method declarations:
bool intersects(Rectangle const &other) const; Rectangle intersection(Rectangle const &other) const;Both methods refer to another Rectangle object, and both methods promise not to change either object. The first one returns true or false, but the second one returns a Rectangle object that is the intersection (this second version presumably would return a 0-length and 0-width rectangle if the objects do not intersect).
Prepared by Michael Costanzo. Thanks to Diana Franklin for ideas.