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

Open your home directory (double click on the icon 2nd from the top on your desktop). Create a new directory (folders are the same as directories) in your home directory called LABS. (For the rest of the quarter, when you are in CSE8AL we’ll be working in this LABS directory). Open that LABS directory (it should show an empty window).

Right click on the bookClasses directory on your Desktop. Select Copy, then right click somewhere in the openLABS directory and select Paste. You have just made a copy of the bookClasses files that we will use in the rest of the CSE8AL labs.

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 public. You will find a Lab2 folder. Double click on that and you will find a file called Lab2a.java. Lab2a is a .java file. It holds Java code. We want to copy this file into your LABS/bookClasses directory (in your home directory). Open the LABS/bookClasses folder. Back in the window of the public directory, right click on Lab2a.java, select copy. Open the LABS/bookClasses directory, then right click and hit paste in the open LABS/bookClasses folder (NOT the bookClasses in the public folder). --You can now close your window to the public folder.

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. The window should automatically go to your home/LABS/bookClasses folder. If not, surf there. Click 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):

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!

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:

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.

We need to copy more things for
Part B. This time we’ll copy a method (by pasting text) into the
file Picture.java, which is already in your home/LABS/bookClasses directory; and
copy the file Lab2b.java into your home/LABS/bookClasses directory.

In the same public/Lab2 folder,
right click on the file makeRed.txt
and open with text editor. Highlight all the code, Right click and select
Copy.

In DrJava do File, Open, surf to
your home/LABS/bookClasses folder and open 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.

Copy the "application"
file in for part B. In the public/Lab2 directory, Right click on Lab2b.java, select Copy, then in your
/home/LABS/bookClasses directory Right click and select Paste.

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 -- as us a question if you don't know why

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 -- Do you understand?

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!

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 computer and partner.

Powered by Create your own unique website with customizable templates.