Lab Assignment #4
Overview
- You’ll be working in pairs: two people to one machine. You just need to login as ONE of you. Either person will do.
- Login: login to the machine in CentOS.
- Part A: Nested loops: Figuring out how for loop iterators interact.
- Part B: Explore off-by-one errors with a simple collage.
- 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 last statement should print out four files: Lab4A.java Lab4B.java Picture.java createForLab4B.txt
- Open Lab4A.java using Dr. Java and examine the code.
- This code will let you play around with nested for loops, which we’ll be using to modify pictures. This code will help you understand about how nested loops can access pixels in images in various ways.
- THIS CODE IS NOT COMPLETE. There are bits you have to FILL IN for it to compile and run
- The goal of this code is to allow you to see the order in which it loops over the indexes rows and cols. Right now the number of times it should loop is not set
- Replace the text that says FILLIN so that the code loops over 3 rows and 2 columns (that is 3 times for outer loop and 2 times for inner loop).
- Compile (if you get errors, fix them and ask if you have questions) and run the code. The System.out.println() code causes things to be output in the Console window. The "\n" represents a newline. That is why an empty line can be seen between every two lines.
- Variable total keeps track of the total number of times the body of the inner loop is repeated. The total value should be the product of number of columns and number of the rows
- Copy down the table below and fill in your output – check it with another team:
Round:
Row: Column:
Round:
Row: Column:
Round:
Row: Column:
Round:
Row: Column:
Round:
Row: Column:
Round:
Row: Column: - …If we were to indicate with a drawing the "order" of accessing pixels, it would zig zag across the image from left to right and top to bottom -- first across the top row, then across the second row, etc.
- Do you think you can explain why? You should be able to specify the "indexes" of each of the boxes and discuss based on the order in which the rows and cols variables change. Try to explain it to your partner and have your partner explain it to you.
- Let’s modify the code again. This time, make the code loop over 4 columns and 3 rows. Before you run the code, in DrJava go to Tools->Interactions & Console->Clear Console, so that you can easily see the output of your next Run. Draw a diagram like the one above that shows the order in which the rows and columns were looped over.
QUESTION: Why is the cols loop inside the loop controlling the rows? - We don’t always have to loop over the rows in a picture. We could loop "down the columns" – then loop over each column from left to right
- Modify the code to do this. You have 2 hints
HINT 1: You don’t need to write ANY new code.
HINT 2: Your output should start off looking this way:
Round: 1
Row: 0 Column: 0
Round: 2
Row: 1 Column: 0 - Compile and run the code (clear the console first). Check that for each column, you loop down the column over all the indexes of the rows, and then you move on to the next column. Ask the instructor if you are not sure if what you have is right.
- Modify the code to do this. You have 2 hints
- Let’s modify the code again. This time, let’s create a condition that skips (bypasses) the inner loop sometimes. We can do this by creating an if condition inside the outer for loop but outside (before) the inner for loop. Write the bold lines of code below in the outer for loop. The if condition checks if the value of cols is even (i.e. if remainder of dividing cols by 2 is 0) and only executes the inner loop if this is true. In other words, it does not execute the inner loop if the value of cols is odd.
for (int cols = 0; cols < 4; cols++)
{
if (cols % 2 == 0)
{
for (int rows = 0; rows < 3; rows++)
{
++total;
System.out.println("Round: " + total);
System.out.println("Row: " + rows + " Column: " + cols + "\n");
}
}
}
Before running your code, what do you think will get printed now?
Run your code, what actually gets printed? Can you explain why?
What is the ending value of total ?
- cd ../public/Lab4
- make directories
- make install
- cd
- cd Lab4
- mv Lab4A.java.empty Lab4A.java
- mv Lab4B.java.empty Lab4B.java
- mv Picture.java.empty Picture.java
- ls
Part B
Switch navigator and driver!
- Open createForLab4B.txt in a text editor or Dr. Java (so you can copy and paste)
- Copy the method createForLab4B().
- In DrJava click File, Open and open Picture.java. Scroll to the end of the file. Paste the method you just copied at the end of Picture.java (but before the last }).
- In DrJava click File, Open and open Lab4B.java
- Before you get started, read and understand the code for Lab4B
- Start with the Lab4B.java file:
- COOL TRICK: To have line numbers shown on all your code, go to Edit, Preferences, Click on"Display Options" on left. Then check the button "Show all Line Numbers" then click on OK.
- Also cool, if you get a runtime error that has a line number in Picture.java, you can go to that line with Edit, "Go to line…".
- We’re really tired of having to surf to our mediasources directory to pick a File when sometimes we want to test multiple times with the same file input. We’ve used the setMediaPath method in FileChooser to set the media path.
- However, you must type in your specific login in the XX where it says ../ieng6/cs8w/cs8wXX.
- Then, (in lines 10 ,11, and 12) we’re able to “hardcode” the name of the file we want to open (we could also use pickAFile and it would immediately take us to our mediasources directory since we set the mediapath.)
- Start with the Lab4B.java file:
- The main program calls the method createForLab4B().
- What object is the calling object?
- What are the two arguments?
- What variable name do you use to "refer" to the calling object inside the createForLab4B method?
- What variable name do you use to refer to input2 inside the createForLab4B method?
- Read the code for createForLab4B(). Note: the first set of loops iterates over p1. The second set of loops iterates over p2.
- Based on the for loops shown, how would you describe what this code does? Try explaining it to your partner.
- Run and "Fix" the code:
- Run the code to confirm what this code does. Make sure you understand which pictures are being modified and which are not. If you have questions, ask.
- We want to get rid of that annoying white line between the two pictures – placing them exactly side-by-side
- Figure out the starting x and y coordinates where pixel (0,0) of p2 SHOULD be placed on the calling object (the background). You can figure this out "in the abstract" (using the equations for width and height) or you can uncomment the print line provided to help you figure it out.
starting X in calling object = starting Y in calling object = - Fix the starting target value so that it will copy Picture p2 "right next to" p1 (with no white line in between).
- Figure out the starting x and y coordinates where pixel (0,0) of p2 SHOULD be placed on the calling object (the background). You can figure this out "in the abstract" (using the equations for width and height) or you can uncomment the print line provided to help you figure it out.
Can You Do?
Do this after the lab
There are a number of ways you can play with this code to explore and develop your understanding. Don’t do this now, but later.
There are a number of ways you can play with this code to explore and develop your understanding. Don’t do this now, but later.
- Can you make the second picture appear BELOW the first picture?
- Can you make the second picture appear to the "right" AND "below" the first picture?
- Without making ANY change in the createForLab4B() method, can you make bird2 appear on the left and bird1 appear on the right?
Invite Your Partner for PSA5 and Log Out
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.