CSI Lab 05

Tuesday, February 12th

Objectives:


Introduction

The material in this week's lab is based heavily on the material in sections 4.3 of your textbook. At times, we will ask you to work very closely with your book. At other points, we will ask you to use what you have just done to write some code of your own. If you get stuck, feel free to refer to the appropriate pages in your textbook. However, please pay attention to the specific directions in this lab, as some of the activities will ask you to solve a problem in a radically different manner than that used in your book.

As in the past, the instructions are contained on this website, but the answers or signatures that you are required to obtain should be completed on the paper Activity Report which will be provided to you in lab.


Activity A : Copy a picture into another picture.

Download the following files into your mediasources directory (or copy them from the "common_mediasources" directory on the p drive)

Section 4.3.1 talks about copying an image into another image. Recipe 23 shows you how to do this by copying the image of "Barb" into the blank image.

Using Recipe 23 as a model, create a method in the Picture class called copyLogo() which copies the small logo file onto the blank picture. This should require almost no changes.  However, please recall that your author tends to skip the last row and last column.  Make sure you make the appropriate change to copy the entire picture.

[SIG1] When you have your version of copyLogo() working, demonstrate this to a TA.


Activity B : Reworking the nested loop to create a "cleaner" method

In addition to the problem of skipping the last row and last column, I have another problem with Recipe 23. The code that they are using is 100% legal python code, but it just seems overly confusing for what should be a simple method.  I actually find the use of both targetX and sourceX to be confusing when it need not be.

[Q1] When the outer loop is first run, what is the value of these two variables?

[Q2] When the outer loop runs a second time, what is the value of these two variables?

[Q3] When the outer loop runs for a twentieth time, what is the value of these two variables?

Notice that these two variables are ALWAYS in step with each other. In fact, notice that sourceY and targetY have the exact same relationship. That being the case, we really only need one variable for x, and one variable for y, not two each.

Copy the copyLogo() method into a method called copyLogoV2().  Refactor copyLogoV2() to replace sourceX and targetX with a single variable called 'x' Replace all references to these variables with a reference to this single variable.

Perform a comparable operation for sourceY and targetY.

[SIG2]  When you have completed this replacement, compile and test your copyLogoV2() method.  Demo your working code for a TA.


Activity C : But what if we don't want it in the upper left hand corner?

Recipe 25 in your textbook assumes that you don't want to put the copy of Barb (in our case, the Logo) in the upper left hand corner, but instead, want to apply an offset of 100 pixels on each side of the image.  As you did in Activity B, use Recipe 25 as a model and create a copyLogoPartway() method so that it copies the logo onto the picture, applying an offset of 100 pixels.

[SIG3] When you have this newer version of copyLogoPartway() working, demonstrate this to a TA.


Activity D : Removing the extra variables

Recipe 25 suffers the same problem that Recipe 23 suffered - it uses extra variables to do what should be a fairly simply thing.

As you did in Activity B, notice the references to two "counting" variables - sourceX and targetX

[Q4] When the outer loop is first run, what is the value of each of these two variables?

[Q5] When the outer loop runs a second time, what is the value of each of these two variables?

[Q6] When the outer loop runs for a twentieth time, what is the value of each of these two variables?

[Q7]  What do you notice about the relationship between these two variables?

[Q8]  What could you do if you wanted to eliminate all references to targetX from the structure of your outer loop?  What about removing targetY from the structure of the inner loop?

[SIG4]  TRY IT!  Create a copy of this method called copyLogoPartwayV2().  See if you can eliminate all references to targetX and targetY.


Activity E : Making a new photo by copying from two different photos

Find two photos that you would like to "merge."  For example, you might decide to put your head on the body of TC.  Or you might decide to put Barack Obama playing poker with Sarah Palin.  Or you might decide to put my face as the basketball being shot by Adam Koch (a member of the UNI basketball team)).

Create a method called mergePhotos() that copies pieces of these two photos and copies them onto a third photo.

[SIG5]  Show this to me when you get done (I want to see what you come up with!)