Lab Assignment #3

Overview

You’ll be working in pairs: two people to one machine. You just need to login as ONE of you.

Login: login to the machine in CentOS (a version of Linux).

Part A: Fix a while loop to modify a picture with grayscale.

Part B: Fix a for loop to modify a picture with grayscale.

Quiz: Each person will individually take a short quiz.

Part A

Double click on your home directory (second icon from the top). From inside the window that opens, click on the box with the down arrow in the bottom left hand corner. Then click on the second item from the bottom – it should say cs8f. That will open a new window. Scroll to the bottom of the alphabetic list of icons to find the public directory icon.

Double click on public. You will find a Lab3 folder. Double click on that and you will find a file called Lab3A.java. We want to copy this file into your home/LABS/bookClasses folder. Go back to your home folder window, then double click on LABS, then double click on bookClasses. Back in the public/Lab3 folder, right click on Lab3A.java, select copy and then right click and select paste in the open bookClasses folder (NOT the one in the public folder). Inside the public/Lab3 folder, OPEN the file simpleGrayscale.txt (use TextEditor to edit this file). Leave this txt file open for now. You can now close your window to the public folder.

Run DrJava by double clicking on the white and blue icon that says "DrJava". Select “Close” if prompted about a new beta release. Close any files listed in the Files pane on the left.

We want to open the Lab3A.java file in DrJava. To do that from DrJava go to File, then Open… The window should automatically go to your home/LABS/bookClasses folder. If not, surf there. Double click on the file Lab3A.java

Follow the same steps in #4 above to open the Picture.java file in DrJava. Copy and paste the java method in simpleGrayscale.txt (that you left open in step 2) into Picture.java file you have open in DrJava. Be sure to place this method at the end of the Picture.java file you have open in DrJava.

To get you started, we made a version of simpleGrayscale() using a while loop. We’ll find that while loops can let us do more creative things with our pictures than for-each loops.

However – we didn’t do a good job of writing this method – there are some things WRONG.

Read the code and discuss it with your partner. See if you can identify any program/logic errors – BUT DON’T FIX THEM YET!

Compile Picture.java

What is the first error you see? Write it down.

What does that error mean? (ask if you don't know)

Fix the code so that the index variable is declared -- and it should start at the first pixel -- pixel 0. Compile the code -- is should now compile.

Go back to Lab3A.java and compile and run the program. When asked for a file, browse to Desktop, then to mediasources, then select a .jpg image of your choice, like for example: beach.jpg.

Hmmm…. It does not seem to work at all!

***********Hit the Reset button next to Compile ***************** Hitting reset will end your program.

What’s happening? Something is still wrong with our code, but we didn’t get any compile errors. Do you have any ideas?

When we have code that "doesn’t seem to finish/terminate" our problem is usually with a loop. It may be that we have an "infinite loop".

DEBUG: Let’s test to see if we have an infinite loop. Our while loop is supposed to loop over all the pixels – with the value of the integer index taking on values 0,1,2,3,4, and so on, up to the last pixel in the picture pixelArray (pixelArray.length-1).

We’ve included a line in the loop that is commented out (e.g. starts with //). Uncomment that line (remove the //), compile the code.

Next you will run the code BUT PLAN TO HIT RESET QUICKLY. Run the code (when you run, you need to make sure that Lab3A.java is the file open in your window) AND WATCH THE INTERACTIONS PANE AT THE BOTTOM. HIT RESET.

What did you see? What do you think is happening?

Fix the code to keep it from looping infinitely. This involves adding a line at the bottom of the loop to increment the index variable each time through the loop.

Compile and Run the code.

HIT RESET. Man – those print statements are really making things go slow! We can tell that our fix to make the index variable update (increase by one) each time through the loop is working. So let’s comment the print statement back out so it doesn’t get executed. (Put a //) in front of the System.out line.

If it works, you will see a gray-scale version of the picture you selected, and you are done!

Part B

Open the Lab3 folder in the public folder (PartA Step1-2). Open simpleGrayscaleVersion2.txt (right click and open in text editor). Copy the method simpleGrayscaleVersion2() and paste it in the end of your open Picture.java (in the DrJava appliction; be sure to paste before the last }).

We are providing you some code that is a "Picture method" - it’s an operation you can call on a Picture (a message you can send to a Picture object). As usual, it may have some problems with it as we’ll discover in a minute.

In order to test this simpleGrayscaleVersion2(), we need to make an "application" that calls the method (sends this message) on a Picture, so we can test it (and fix it). Since we have little time in lab, copy from the public/Lab3 directory the file called Lab3B.java into your home/LABS/bookclasses folder. Here you are copying an entire file (right click copy, right click paste in the right window). (Ask if you need help).

Open Lab3B.java in DrJava and read the code in it. What is it doing? (ask if any part of it confuses you). Specifically, how many Picture objects are there in this method and what are they called? Write down your answer.

How will it know to look in the type Picture to find the code to execute simpleGrayscaleVersion2()? Write down your answer. (ask if you don't know)

Save and compile Lab3B.java

Run Lab3B and pick any picture you like out of the Desktop/mediasources folder. If you don't know what to pick, try beach.jpg

Identify for yourself which window (Picture) is being drawn by which call to the show() method (if you aren't sure, ask).

Did it work? No! The picture is not completely gray. Read the simpleGrayscaleVersion2() code CAREFULLY with your brain in DEBUGGING MODE. What’s the problem? (write it down).

What pixels were actually being set to gray with that code? Write down your answer.

When is the code index++ executed? Write down your answer.

HINT: If you can’t find the bug, uncomment the print statement in the loop which will print out the value of index with each iteration.

Remove the last line in the loop body (index = index + 1;) to make the code work correctly. Compile and run to verify that you get a grayscale picture now.

Potential Quiz Question: Let’s say you didn’t remove the index = index + 1; line of code at the bottom of the loop body. Is there another piece of code you could remove to make the code work (still using a for loop)?

Review: Do You Really Understand It?

(you may do this after the lab)

What if I change the first line in the for loop to read:

pixelObj = pixelArray[foo];

What changes would you need to make your code in order for it to compile and run correctly? (ask if you don't know)

What are the three parts of the for loop structure [in the for(...)]. How many times is each executed? When is each part executed?

Logout

Click on System > Log Out cs8fzz... > Log Off

Complete Your Lab Quiz

See the instructor to get your individual lab quiz.The quizzes are open book & note, but closed Dr Java and partner.

Powered by Create your own unique website with customizable templates.