Lab Assignment #1
There will be a total of 9 in-lab assignments, the lowest of which will be dropped. None of the lab sections will meet during the week of the Thanksgiving Holiday.
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 (but make sure that person is registered for this class). The other person should follow these instructions after class to set up his or her account.
- Login: login to the machine in CentOS (this is linux).
- Part A: Set up your programming (development) environment with Dr. Java.
- Part B: You’ll copy a file, try to compile it, find a problem, fix it, and run it.
- Post to Piazza Discussion Forum
- Quiz: Each person will individually take a short quiz.
Introductions
Before diving into the assignment, be sure to take a couple of minutes to introduce yourself to your partner. You don't want the awkwardness of not knowing "that girl's" name after working with her for at least an hour. Plus, you never know who you might meet!
Login
By the time you're reading this, you should be logged in to one the partner's accounts specific to CSE 8A. If your the login name you used does not begin with cs8f, log out and use your account that does begin with cs8a. If you don't know your account, look it up using the ACS account lookup tool on the course website.
Part A: Running Dr. Java
Set up your development environment (Dr. Java) and its requirements. There is no need to fully understand these instructions. You just have to do them once and never again.
- On your desktop you should have (at least) 3 folders including:
- bookClasses
- mediasources
- classResources
- You should also have a blue and white icon called DrJava. Double click on DrJava and click the right most button (RUN) – NOT the default (Run in Terminal). You should see the application open up.
Note: You must leave these folders and files on your desktop all term.
If double clicking on the Dr. Java Icon does not open Dr. Java (and in fact does nothing) it means we're still working out some initial kinks with ACS. If this is the case, you'll need to open Jr. Java another way:- Open up a terminal: Click on Applications (top left corner of the screen) > Mouse over System Tools > Click on Terminal
- Navigate to the desktop by typing "cd Desktop" and pressing enter
- Type the following command and press enter: "java -jar ClassResources/drjava.jar"
- Double click on your desktop shortcut you just made called DrJava. This should launch the Dr. Java integrated development environment (IDE) application. You will create, compile and run Java programs in this application.
- We need to set up Dr. Java to be personalized for you. Go to Edit, Preferences (bottom of list). Click on the Add button.
- This should pull up your home directory. Double Click on the Desktop folder and SINGLE click on bookClasses and click on Select
- Click on the Apply button, then the OK button. (so the Preferences window goes away)
- Now, in Dr. Java, click on the “Reset Button” near the top right of the window.
- Did it work? Find out by going to the Interactions pane (bottom of Dr. Java window, click on interactions). You should see in that pane:
Welcome to DrJava. Working directory is (SOMETHING)
> - The “>” is a prompt for you to type something. Click to the right of it and you should get a blinking “|” sign. Type the following:
World w = new World(); - and then hit the Enter key (whenever we ask you to type a line in Dr. Java, we expect you to hit Enter when you are done). If this pops up a blank Window titled "World" you are ready to go on to Part B.
- Switch who's controlling the mouse and keyboard, and Repeat steps 1-10 on the other partner's account.
Part B: Copy, Compile, Debug and Run!
Switch driver and navigator: if you’ve been controlling the mouse and keyboard, it’s time for your partner to do so.
- Copy the Lab1.java file provided for you. We often provide code for you to start
with. It is stored in a directory
“above” your home directory called “public”.
- 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 Lab1 folder. Double click on that and you will find a file called Lab1. Lab1 is a .java file. It holds Java code. We want to copy this file into your bookClasses folder (which is on your desktop).
- Open the bookClasses folder. Right click on Lab1.java, select copy and then right click and hit paste in the open bookClasses folder (the one on YOUR DESKTOP – NOT the one in the public folder)
- You can now close your window to the public folder.
- Open the Lab1.java file in Dr. Java. Do this by going (in the Dr. Java window) to File, The window should automatically go to your bookClasses folder on your desktop. If not, surf there. Then click on the file “Lab1.java”
- Read through the Lab1.java file and talk with your partner. DON’T CHANGE ANYTHING. Answer this:
What do you think this code is going to do? (Write your answer down.) - Compile: The compile step translates the Lab1a.java file in pseudo-english (Java code) to a Java byte-code file (byte-code is the same “language” for all types of machines) called Lab1a.class:
- Click on the “Compile” Button in Dr. Java (left of Reset on the top - right side)
- AHA! This code will not compile. The compiler shows you an error which looks like this (the one highlighted in yellow in the Compiler Output pane at the bottom):
5 errors found:
File: /home/linux/ieng6/cs8f/cs8fzz/Desktop/bookClasses/Lab1.java [line: 17]
Error: /home/linux/ieng6/cs8f/cs8fzz/Desktop/bookClasses/Lab1.java:17: non-static method forward(int) cannot be referenced from a static context - Debug: Here’s how to interpret this error:
- The error is on line 17 on Lab1.java (That’s what the “File” line tells you)
- It is unhappy calling the forward method on something that is “static”. This is a hard error to understand when you are just getting started programming. Here’s what it means:
- You used the class name (Turtle) instead of the object name (in this case jose) in the attempt to make the turtle go forward.
IN GENERAL, WE RECOMMEND YOU START BY FIXING THE TOP ERROR EACH TIME, then hit SAVE and COMPILE to find new errors. - Edit and Compile: To fix the error on line 17.
- On the highlighted (yellow) line in the Definitions Pane (where your Lab1.java file is shown), change the code to read:
jose.forward(55); - Click on Save. Click on compile. You should now see the same error reported on line 18. (and below that highlighted error, on line 19, 20, and 21).
- Go ahead and change all those lines to refer to the jose object instead of the Turtle class. BUT DON’T HIT SAVE.
- Hit Compile without saving. Sometimes one forgets to do this. Dr. Java prompts you to save the file before you compile it. Why does this have to happen? When you make changes in the Definitions Pane in a file (like Lab1.java) you have to save it before you can compile it. Just like you would need to save a file in Word before you can send it to a friend in email. While Word automatically saves your file every so often, Dr. Java won’t do that for you. You have to hit Save yourself.
- Now go ahead and Save and Compile. If you still have errors after fixing all the lines of code, ask for help.
- On the highlighted (yellow) line in the Definitions Pane (where your Lab1.java file is shown), change the code to read:
- Run: Let’s see what this code does. Now that it has successfully compiled, a Lab1.class file exists in your folder bookClasses. (You should see it listed if you hit refresh on that window). The Lab1.class file contains the Java bytecode (something that is really not fun for humans to read) that the computer can read to execute your program (in reality, it is interpreted by the computer to a series of 1s and 0s which the computer can execute). HINT: You need to know the difference between .java files and .class files and how and when .class files are created.
- Click on run in Dr. Java (to the right of reset). Draw the output in your World window.
- Hah! That’s not a U shape (which is what the comment in the code said it would produce). HINT: Can you identify all the comments in the code? There are actually 3!
- What’s wrong with this code? The first line for the U is going up, then over to the right. This is because, by default, turtles START LIFE facing up. One way to fix it is to have the turtle start by rotating to face down.
- Add this line of code right after the “//Make a U shape” comment (but before any of the other turtle direction lines):
jose.turn(180); //Face down - Save, Compile and Run. What does the output look like now?
- Hey – we have a U! Congratulations!
Try Out the Class Piazza Site
If you haven't logged in to introduce yourself for PSA0, do so now: https://piazza.com/ucsd/fall2012/cse8a/home. Note: this link is also available on the class website.
By the end of your lab session, both partners must have an introduction to themselves posted to Piazza (posting your introduction before the lab period will count as well)
By the end of your lab session, both partners must have an introduction to themselves posted to Piazza (posting your introduction before the lab period will count as well)
More On Your Own
(Do this after the lab if you don't have time in lab.)
Can you make a U such that:
Can you make a U such that:
- The turtle finishes on the right side of the U (at the top).
- The U has the top left hand corner at (100, 50)
Log Out
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.
The quizzes are open book & note, but closed computer and partner.
Appendix
Starter Code for Part B
/* Lab 1: Created by Beth Simon
* Date: Sept 29, 2008
*/
public class Lab1
{
public static void main(String [] args)
{
World w = new World();
Turtle jose = new Turtle(100,50,w); //Creates a turtle in w at (x,y) (100,50)
//Make a U shape
Turtle.forward(55);
Turtle.turn(90);
Turtle.forward(30);
Turtle.turn(90);
Turtle.forward(55);
}
}
/* Lab 1: Created by Beth Simon
* Date: Sept 29, 2008
*/
public class Lab1
{
public static void main(String [] args)
{
World w = new World();
Turtle jose = new Turtle(100,50,w); //Creates a turtle in w at (x,y) (100,50)
//Make a U shape
Turtle.forward(55);
Turtle.turn(90);
Turtle.forward(30);
Turtle.turn(90);
Turtle.forward(55);
}
}