lab07, CISC106, Fall 2006

 

Special Note

Some tasks scheduled for lab07 have been postponed to lab08

In lecture, I had indicated that in lab07 we would be developing our lab06 software a bit further by

We are still going to do those things—but in lab08 instead of lab07. The reasons are these:

Overview

This week's lab provides extra practice with the idea of a "design recipe" introduced in lecture of Monday October 16:

It also introduces a concept that we have covered in lecture, but not yet in lab:

Prerequisites

Before starting this lab:

Step-by-Step Instructions

Step 1: Preparing your lab07 directory, and copying files you will need

Step 1a: Create a new subdirectory for lab07

Create a new subdirectory ~/cisc106/lab07, and make that your working directory.

If you are not sure how to do this, then review the instructions from lab03, steps 1a through 1d.

Step 1b: Copy the files needed for this week's lab

There are two ways to copy the files for this week's lab. Read over both methods before deciding which one to use.

  1. Download them from the web page http://www.udel.edu/CIS/106/pconrad/06F/labs/lab07
  2. If you are working on strauss, copy them directly from the directory /www/htdocs/CIS/106/pconrad/06F/labs/lab07
Copying all the files at once (if you are on strauss)

Assuming that you are already in ~/cisc106/lab07 as your current working directory, the following command will copy all the files you need.

>> !cp /www/htdocs/CIS/106/pconrad/06F/labs/lab07/* .
>>

There are additional notes about this command at lab04, step 1b if you need to review how this command works.

Step 2: Examining the example programs

As in past weeks, there is nothing to turn in for Step 2

The work you do in step 2 of this lab is only for practice and learning. There is nothing to turn in from this step of the lab.

But don't skip this step.

By now, I'm guessing you already know why. (Look back at lab04 step 2, lab05 step 2, and lab06 step 2 if you still need this pounded into your head.)

Step 2a: Another example M-file that defines a function: areaOfRect.m

In lecture on October 16, we developed two files: areaOfDisc.m and areaOfRing.m to illustrate the Design Recipe idea.

In the lab07 directory, there is one more example: areaOfRect.m

Take a look at this M-file. Note that since it is an M-file that defines a function, to try it out, you can't just type:

>> areaOfRect  

Instead, you have to type something like this:

>> areaOfRect(8,3)  

That's because the areaOfRect function expects two input arguments (also called actual parameters). We can see this in the function header, which is on the first line of the M-file:

  function area = areaOfRect(length,width)

In addition, I've provided two other M-files, both of which are ordinary M-files (your textbook calls these "script" files). These files don't define a function, so you can just type their names at the MATLAB prompt. These files are:

Try running these files. Then look at the code inside.

Step 2b: The testAreaOfRect.m file—an example of regression testing

The testAreaOfRect.m file is an example of what is called regression testing. Regression testing is used to make sure that as we make changes to software, that we don't accidentally break something (causing it to "get worse instead of better", i.e."regress").

The testAreaOfRect.m file calls the areaOfRect function twice, and checks that the value returned is the value we expect. Note that we use exactly the same examples as the ones in the H1 comment of the areaOfRect.m file. Every time we run testAreaOfRect.m, it tells us whether areaOfRect.m is working properly or not.

The computeAreaOfRect.m file—an example of an interactive driver

The computeAreaOfRect.m file is an example of an interactive driver. That is, it is a program that asks the user for input, computes a result, and displays the result. But note: the actual computation is done inside the file areaOfRect.m by the areaOfRect() function—the formula "area equals length times width" never appears in the computeAreaOfRect.m file itself.

For a formula as simple as "area equals length times width", you might say that this is overkill—and I wouldn't disagree.

However, this program is meant to serve as an example, and as a teaching tool—to help you see that an interactive driver uses a function you've already written. In later examples, the function will be one that is more complicated, and the benefit of splitting it out into a separate file will be more clear.

Furthermore, once we develop and test the areaOfRect() function, we can use it with other functions as well.

In fact, in a later stage in this lab, you'll do exactly that—you'll develop a function pricePerSquareUnitRectangle() that takes length, width and price, and computes the price per square unit (e.g. the price per square inch of a rectangular pizza, or the price per square foot of a rectangular apartment or storage unit).

This function will take three input arguments: length, width and price, and produce one output argument, the pricePerSquareUnit. In the body of the function you'll be able to use areaOfRect() to do part of the computation.

You'll also be asked to develop a regression testing file, and a couple of interactive drivers for your new function, similar to the ones illustrated here. So be sure you understand these files before moving to the next step.

Step 2c: Looking at the myMin.m and testMyMin.m files

Look at the files myMin.m and testMyMin.m:

In this case, there is no interactive test—asking the user to input a vector interactively is beyond the scope of the MATLAB features we've covered so far, so we'll skip that for now.

In a later stage, you'll be asked to develop your own files myMax.m and testMyMax.m which are similar to these files, except that myMax.m computes the maximum element in a vector.

If you have questions, Your TA and I will freely explain how these files work, and how one might come up with these from scratch.

However, we'll be a lot less free with the explanation of how to do Steps 3 and 4! That is for you to figure out on your own. So ask questions at step 2 if you don't understand. Once you do, you are ready to tackle some problems on your own.

Step 3: A function to compute price per square unit of a rectangle

Step 3a: Design the file pricePerSquareUnit.m

Write an M file pricePerSquareUnit.m, using the "Design Recipe" steps:

  1. identify the contract (what type of data the function consumes, and what it produces) and write the function header and the H1 comment in the M-file.
  2. come up with at least two examples, and document them in the H1 comment
  3. develop the body of the function
  4. test the function on the examples from your H1 comment

When your function is complete and it tests ok, continue to the next step:

Step 3b: Write a regression test script: testPricePerSquareUnit.m

Using the example programs testMyMin.m and testAreaOfRect.m as a model, write a regression test script for pricePerSquareUnit.m. Use your examples that you came up with in step 2 of the design recipe.

Refine your M files pricePerSquareUnit.m and testPricePerSquareUnit.m until all the tests pass.

Step 3c: Write an interactive driver rectPizza.m

Using the example program computeAreaOfRect.m as a model, write an interactive driver for pricePerSquareUnit.m.

Important Instructions (deductions will be made for failing to follow these instructions)

Step 3d: Write a second interactive driver rectRoom.m

Now, write another interactive driver that uses your pricePerSquareUnit() function to compute the price per square foot of a rectangular apartment or storage room.

Important Instructions (deductions will be made for failing to follow these instructions)

If you are doing it right, step 3c and 3d should be almost identical

I mention this, because sometimes if something is too easy, students think "I must be doing this wrong". No, this is actually very simple. I'm requiring you to do it anyway—because if you actually do it, it is more likely to sink in.

 

Step 4: Create M-files to define and test a function myMax

Step 4a: Design an M-file myMax.m

Use the Design Recipe to design a function myMax(). This function should take a vector of numbers (row or column) and return the maximum number in that vector. It should operate in a manner similar to the myMin.m function provided in the lab07 directory, which you may use as a model.

Note: Don't use the built-in MATLAB max function

Note that there is a built-in MATLAB function called max(). You may not use that function to do this assignment—one of the learning goals here is for you to know how to compute a minimum or a maximum when you do NOT have a built-in min or max function already available.

Step 4b: Design an M-file testMyMax.m to do regression testing for myMax.m

Following the steps outlined previously develop a regression testing file called testMyMax.m to do regression testing on the function you developed in step 4a.

If you aren't sure what to do, review steps 2b, 2c and 3b.

Step 5: Create a diary file lab07.txt to show your work.

Once the M-files developed for this lab work, create a diary file called lab07.txt in which you type out each of the M files you developed in steps 3 and 4, and also demonstrate that they work properly.

Step 6: Upload and Submit your work on WebCT

You need to submit seven files in all—the six M-files, and then the one diary file lab07.txt.

As the number of files is growing, as part of next week's lab, we'll cover how to create a .zip file containing all the files other than your diary file so that you only have to upload two files: a zip file and a diary file.

But, for this week, unless your TA tells you otherwise, please do not create a zip file, tar file, rar file, or anything else—it will be easier if everyone does the work in a consistent way.


That's it for lab07!


Grading

  1. pricePerSquareUnit.m (20 pts)
  2. testPricePerSquareUnit.m (10 pts)
  3. rectPizza.m (10 pts)
  4. rectRoom.m (10 pts)
  5. myMax.m (20 pts)
  6. testMyMax.m (10 pts)

End of lab07 for CISC106, Fall 2006 (100 pts)