lab01 : Define implement and apply a C++ class

num ready? description assigned due
lab01 true Define implement and apply a C++ class Tue 04/03 11:00AM Mon 04/09 11:59PM

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 Lab00, and we will not repeat instructions given there for basic operations. This assignment must be completed individually.

Step by Step Instructions

Step 1a: Do some initial ONE-TIME git configurations

   git config --global user.name "Alex Triton"

   git config --global user.email "atriton@cs.ucsb.edu"
	git clone git@github.com:ucsb-cs24-s18/cs24-s18-starter-code.git

Note that this repo contains the starter code for all labs and programming assignments (although only the code for lab01 is up to date). So, you don’t have to repeat the above step in subsequent labs.

Step 1b: Create a new repo and clone it on your local machine

submit

	cd ~/cs24
	git clone git@github.com:ucsb-cs24-s18/lab00_jgaucho.git
	cd lab00_jgaucho

You will write all the code for this assignment in this directory

Step 1c: Copy the starter code to your repo

Copy the starter code to your git repo directory

	cp ../cs24-s18-starter-code/lab01/* ./

You should see two files in your current directory: rugfit1.cpp and rugfit2.cpp

Step 1d: Push your code to github

In furture labs we will refer to all the steps in this section as “push your code to github”. What we really mean is that you sync up a current version of your code with your repo on https://github.com. Follow these steps to sync the current version of your code:

	git status

You should see the following output:

On branch master
Your branch is up to date with 'origin/master'.

Untracked files:
  (use "git add <file>..." to include in what will be committed)

	rugfit1.cpp
	rugfit2.cpp

nothing added to commit but untracked files present (use "git add" to track)
	git add rugfit1.cpp rugfit2.cpp

OR

	git add .

Type git status again and you should see that two files appear in green. This means they are ready to be “saved” locally as a new version of your project.

	git commit -m "Initial version of lab01"
	git push origin master

Navigate to your repo and refresh your browser. You should see the two new files that you added to your repo appear in your repo online.

Your code is now accessible to the CS24 teaching staff for feedback. This is the primary reason we are using github in this class. As you write new code in your local repo, repeat the steps in this section to sync your changes with your repo on github.

Step 3: Study a non-OO program

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 :

-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. You will make the revision using the provided skeleton code in rugfit2.cpp

Step 4: 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:

Step 5: 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. Lab00 had a link to some emacs help if you need it.

emacs rugfit2.cpp

Step 6: 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 a TA or tutor for help only if you are truly stumped, but give it at least 5-10 minutes worth of study first.

Step 7: Upload your code to github one last time

Hopefully you remembered to sync your local changes to github often as you were completing the assignment.

If you forgot to do so, be sure to follow the steps in section 1d (above) to upload your final code to github

Step 8: Submit your code to gradescope

Go to our class site on www.gradescope.com. Navigate to the assignment for this lab and submit your code via github just like you did in lab00.

You should see a score of 100/100 for this lab.

Step 9: Lab check off

You may now attempt the optional extra challenge or work on pa01 (and then return to the challenge problems)

Optional Extra Challenge

DO NOT CHANGE rugfit2.cpp to do these challenge problems, and DO NOT resubmit Lab01. 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:

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).

Push your code to github, but do not resubmit to gradescope. Attempting the extra credit portion of the lab earns you star points which work in magical ways.