Click to set custom HTML
Lab Assignment #1
There will be a total of 9 in-lab assignments, the lowest of which will be dropped.
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 each 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.
- Login to the 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 an account specific to CSE 8A. If the login name you used does not begin with cs8w, log out and use your account that does begin with cs8w. If you don't know your account, look it up using the ACS account lookup tool on the course website or ask a tutor for help.
Part A: Setup
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 an icon called DrJava. Double click on DrJava.
- This should launch the Dr. Java integrated development environment (IDE) application. You will create, compile and run Java programs in this application.
- If you completed PSA1, you should have already completed the following steps.
- Go to Edit, Preferences (bottom of list).
- Click on the Add button next to 'Extra Classpath.' This should pull up your home directory.
- Double click on the 'Public' folder, and then Double-click the 'cs8w-public-folder''
- SINGLE click on 'bookClasses' and click Select
- Click 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.
4. Open the preferences dialog by selecting "Edit" then "Preferences" and verify the path in the extra classes reads: "../Public/cs8w-public-folder/bookClasses"
5. Now, in Dr. Java, click on the “Reset Button” near the top right of the window.
6. 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:
7. Welcome to DrJava. Working directory is (SOMETHING)>. The “>” is a prompt for you to type something.
8. Click to the right of it and you should get a blinking “|” sign. Type the following: World w = new World();
9. 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.
10. Switch who's controlling the mouse and keyboard, and Repeat the previous steps in the other partners account.
5. Now, in Dr. Java, click on the “Reset Button” near the top right of the window.
6. 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:
7. Welcome to DrJava. Working directory is (SOMETHING)>. The “>” is a prompt for you to type something.
8. Click to the right of it and you should get a blinking “|” sign. Type the following: World w = new World();
9. 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.
10. Switch who's controlling the mouse and keyboard, and Repeat the previous steps in the other partners 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.
- The file Lab1.java contains the starter code that we provide for you. To access this file complete the following steps.
- Right-click in an open space on the Desktop and select 'Open in Terminal' from the drop down menu.
- Copy and Paste the following 7 lines of code into the terminal
- cd ../public/Lab1
make directories
make install
cd
cd Lab1
mv Lab1.java.empty Lab1.java
ls - Verify that the 'Lab1.java' file is shown as the result of the last command.
- Open the Lab1.java file in Dr. Java. Do this by going (in the Dr. Java window), by pressing the "Open" button at the top right of the Dr. Java Window. The window should automatically go to your home folder. If not, navigate there.
- Double Click on the Lab1 Folder. 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 on a piece of paper.) - Compile: The compile step translates the Lab1.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 Lab1.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):
7 errors found:
File: .../Public/cs8w-public-folder/bookClasses/Lab1.java [line: 17]
Error: .../Public/cs8w-public-folder/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), and it is highlighted in yellow.
- 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. - The error is on line 17 on Lab1.java (That’s what the “File” line tells you), and it is highlighted in yellow.
- 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.
- For the remaining two errors, change the navigator and driver once again. The last two errors are very common, but are of a different type. Can you identify them? HINT: We frequently make typing errors when writing emails, messages and other forms of communication and while another person may still be able to understand the message, a compiler cannot.
- 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!
Invite your Partner for PSA2
This part of the Lab is also mandatory.
- Find a partner that you would like to work with on PSA2. This cannot be the same person that you worked on PSA1 with. Steps 2 -6 must be completed for only one of the two partners
- Open a web browser and enter in the following url: www.auto-grader.com
- Login to autograder, select CSE8A, and select PSA2 as your assignment
- Click the button in the top right with a pair of people working on an assignment in order to invite your partner
- Find your partner's name in the list of available students
- Invite your partner to work with you
- They must then accept the invite in their own autograder account
Try Out the Class Piazza Site
If you haven't logged in to PIAZZA, do so now: https://piazza.com/class#winter2013/cse8a. Note: this link is also available on the class website.
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)
4. Verify that you completed the previous steps correctly.
5. Go to Edit, Preferences and make sure that there is an entry in the extra class path that reads: "../public/cs8w-public-folder/bookClasses"
6. 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)>
7. 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()
8. 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.
9. Switch who's controlling the mouse and keyboard, and Repeat steps 1-8 on the other partner's account.
5. Go to Edit, Preferences and make sure that there is an entry in the extra class path that reads: "../public/cs8w-public-folder/bookClasses"
6. 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)>
7. 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()
8. 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.
9. Switch who's controlling the mouse and keyboard, and Repeat steps 1-8 on the other partner's account.
Log Out
Click on System > Log Out cs8wzz... > Log Off
Complete Your Lab Quiz
See the instructor or one of the tutors 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(45);
Turtle.forward(10);
Turtle.turn(90)
}
}
/* 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(45);
Turtle.forward(10);
Turtle.turn(90)
}
}