Lab Assignment #2
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 (a version of Linux), using your CSE 8A account (starts with cs8f)
- Part A: Multiple Turtles.
- Part B: Modify a Picture to make it all one color.
- 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 four files after the last command: Lab2a.java Lab2b.java Picture.java makeRed.txt
- Run Dr Java by double clicking on the white and blue icon on your Desktop that says DrJava.
- Open your Lab2a.java file in DrJava. Do this by going (in the DrJava window) to File, Open, and then clicking on the file Lab2a.java
- Read through the Lab2a.java file and talk with your partner about what it does. DON’T CHANGE ANYTHING. What do you think this code is going to do? (write down your answer)
- Compile: The compile step should translate the Lab2a.java file, which contains Java code which humans can understand, to a Java byte code file named Lab2a.class, which contains code that the computer can run.
- Click on the "Compile" Button in DrJava (left of Reset on the top - right side)
- AHA! This code does not compile. So, you will get more practice interpreting and fixing compiler errors. The compiler gives you two errors, and the first one like this (the one highlighted in yellow in the Compiler Output pane at the bottom):
File: /bookClasses/Lab2a.java [line: 22]
Error: /bookClasses/Lab2a.java:22: cannot find symbol
symbol: variable t5
location: class Lab2a
Here’s how to interpret this error:
The error is on line 22 on Lab2a.java (That’s what the "File:" and “line:” tells you)
It is complaining that it cannot find the definition of a symbol, the variable t5, that appears on this line. What this means:
The variable t5 on line 22 has not been defined yet.
NOTE: There are 2 errors given (even though only the first one is in yellow). You can go ahead and look at all those errors if you like. BUT KEEP IN MIND THEY MAY NOT BE "REAL" ERRORS. The compiler can only tell you what it thinks the error is. Once you fix the first error, the others might magically disappear when you recompile. This will be the case with the remaining error the compiler found. Read the second error, look at the Lab2a.java file, and write down, in your own words, why you think the second error will go away when you fix the error on Line 18:
IN GENERAL, WE RECOMMEND YOU START BY FIXING ONLY THE FIRST ERROR EACH TIME, then hit SAVE and COMPILE to find new errors. - To fix the error on line 22 add the following line to line 10 in Lab2a.java (under the 4 declarations of turtles t1-t4)
Turtle t5 = new Turtle(500, 500, w); - Click on Save. Click on compile. If you still get a compiler error, fix it before continuing.
- Run: Let’s see what this code does. Now that it has successfully compiled, a Lab2a.class file exists in your folder LABS/bookClasses. (You should see it listed if you hit refresh on that window). The Lab2a.class file contains the Java bytecode (something humans can’t really read) that the computer CAN read to execute your program.
- Click on Run in Dr. Java (to the right of Reset). Draw the output in your World window.
- Your output is missing one of the lines for the X through the box! Do you notice a turtle that would very easily make this line?
- Add these lines below the last line of code, but within the brackets:
t2.forward(250);
t2.turn(-315);
t2.backward(350); - Save, Compile and Run. It Draws the completed X! Good Job!
- cd ../public/Lab2
- make directories
- make install
- cd
- cd Lab2
- mv Lab2a.java.empty Lab2a.java
- mv Lab2b.java.empty Lab2b.java
- mv Picture.java.empty Picture.java
- ls
Part B
Switch who is driver and who is navigator: If you've been controlling the mouse and keyboard, it's time for your partner to do so.
Use a method to modify a Picture to make it all one color.
Copy Needed Files:
Compile and Run:
Use a method to modify a Picture to make it all one color.
Copy Needed Files:
- Click on your home directory (second icon from the top). Click on the box with the down arrow in the bottom left hand corner. Then click on the second from the bottom – it should say cs8f. That will open a new window. Scroll to the bottom to find the public directory.
- Double click on the Lab2 Folder
- Right click on the file makeRed.txt and open it with a text editor
- Highlight all the code. Right click and copy it.
- In DrJava, open the Picture.java file, by selecting File > Open, navigating to the Lab2 Folder and selecting Picture.java
- Scroll to the end of the file. Paste the method makeRed() in the end of Picture.java, but BEFORE the last "}."
- EXPLANATION: We are providing you some code that is a "Picture method"; that is, it’s a message you can send to a Picture object. Like all Picture methods, it needs to be inside the Picture class, which is defined in that Picture.java file.
Compile and Run:
- Before we modify this method, let’s
make sure what we have to start with compiles, runs and doesn't change the
picture at all. Compile Picture.java. Run it.
What happens? (HINT: Look in the Compiler Output pane.)- Write down the exact Error message from this window.
- Why do you think you got this error? Discuss with with your partner -- ask us a question if you don't know why.
- Write down the exact Error message from this window.
- OK, so we need an "application" that we can use to test out this makeRed method. You already copied some "application"code that will run makeRed, namely Lab2b.java.
- Open Lab2b.java in DrJava and read the code in it. What is it doing? (There are 5 steps.) (Ask if any part of it confuses you.)
- How will it know to look in the file Picture.java (actually Picture.class at run time) to find the code to execute makeRed? (ask if you don’t know)
- Compile Lab2b.java.
- Run Lab2b and click on Desktop and then mediasources to pick any picture you like. If you can’t decide what to pick, try Barbara.jpg. It should draw an entirely red picture (every pixel has been set to red = 255, blue = 0, green = 0).
- Modify the makeRed method so that instead of making the picture all Red, it makes it all Blue. Hint, you need to modify 2 lines in the makeRed method (one that setsBlue and one that setsRed.) Ask if you have any questions.
- Compile and Run your code.
- Be sure you have the Lab2b.java file open, then click Run.
- Did
it work? Look at your output and see!
Invite your Next Partner and Logout
Using autograder and instructions previously provided to you, invite your partner for PSA3
Click on System > Log Out cs8wzz... > Log Off
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 computer and partner.
The quizzes are open book & note, but closed computer and partner.