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
- Right click on the Desktop and Select "Open a Terminal"
- Paste the following lines of code into the terminal
- The terminal should print five files after the last command: Lab3a.java Lab3b.java Picture.java simpleGrayscale.txt simpleGrayscaleVersion2.txt
- Open your Lab3a.java file in DrJava. Do this by going (in the DrJava window) to File, Open, navigating to your Lab3 folder and then clicking on the file Lab3a.java
- Open the files Picture.java and simpleGrayScale.txt in the same manner as above. Note that for simpleGrayScale.txt, you may have to click the "Files of Type:" dropdown and select "All Files"
- Copy and paste the java method in simpleGrayscale.txt into Picture.java file. Be sure to place this method at the end of the Picture.java file you have open in DrJava, but before the last "}"
- 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 did not do a good job of writing this method, and there are some ERRORS.
- 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!
- cd ../public/Lab3
- make directories
- make install
- cd
- cd Lab3
- mv Lab3a.java.empty Lab3a.java
- mv Lab3b.java.empty Lab3b.java
- mv Picture.java.empty Picture.java
- ls
Part B
- Open simpleGrayscaleVersion2.txt in Dr. Java(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). The application that we are going to use is Lab3b.java
- 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 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)?
- 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.
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?
Invite Your Partner for PSA4 and Logout
Click on System > Log Out cs8wzz... > 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.
The quizzes are open book & note, but closed Dr Java and partner.