CS111

Lab 1

Wed Sep. 5, 2012

Welcome to Java and BuggleWorld

Total checkmarks: what does this mean?

What we'll cover today:



Downloading and Uploading

Use Fetch to connect to your own account in puma. Navigate around to start becoming familiar with the structure of it. Disconnect.
Connect to puma again, using the cs111d (download) account. You'll need the password given in lab. Locate and look at your own drop directory.

Locate the lab1_programs sub-directory, within the download directory. Download lab1_programs into your local computer's desktop. Click your mouse in the name until the cursor changes so that you can edit the folder name. Change the name of the folder so that it contains your name, like this:
OLD NEW

Directions for using Fetch are available here. For documentation using WinSCP on a PC, click here.


Editing and compiling Java programs in DrJava

  1. Meet DrJava (its icon looks like this: )
    DrJava is the program we will be using in this course to edit, compile and run our Java programs. Java programs are written in files that end in .java. DrJava can take our Java programs and compile them. Compiling a Java program converts Java code into something called bytecodes which can then be understood by the computer. Bytecodes are stored in files that end in .class. Don't worry, you'll see examples of all this in a few minutes.
  2. To find DrJava, go to the "Go" menu at the top of the screen and select "Applications". A window should open with a folder called DrJava. Open the DrJava folder, and you can now start DrJava by double-clicking on the icon.

    To start DrJava, double-click on the DrJava icon.

  3. Once you start DrJava, a window should open with 3 panes.

    You can resize the window by pulling on down its lower right corner. The pane at the bottom of the window is called the interactions pane (it should have a message saying "Welcome to DrJava.") The pane on the left of the window is called the Documents pane. When you are editing Java files, the name of the file (or document) will appear in this pane. Finally, the pane on the right is called the Definitions pane or Editing pane. This is where contents of files will appear (i.e., the Java code in a file).

  4. Now let's open our first Java file. Click on the button labeled "Open" in the toolbar at the top of the window.

    Important Note: Always start DrJava and open your java file from within it, to get into Java programming. If you try to open a java file by double-clicking on it, you will end up in a TextEdit program, rather than in DrJava.

    Now find the folder lab1_programs on your desktop, and open the file BuggleWorld.java from within DrJava.

    The Editing pane should now contain Java code (like below):

  5. Before we can run this Java program to see what it does, we first need to compile it. To compile the program with DrJava, click Compile.
    Alternatively, hold down the shift on your keyboard and press the key F5. When the program is finished compiling, you should see a message in the bottom pane saying "Compilation completed."

    You may see, as output of the compiler, a sequence of "Deprecated Warnings" and "Unchecked Warnings". You shouldn't worry about them, but if you prefer to hide them altogether, you can do so. Go to:
    DrJava -> Preferences --> Compiler Options
    and uncheck the appropriate checkboxes.

    If you look in the folder lab1_programs that you downloaded earlier, you should now see some new files in the folder: class files. These are the results of the compilation you just performed.

  6. Now it is time to run the executable program. The program will be interpreted and executed (i.e. run) by the Java Virtual Machine.

    Just click the Run button in DrJava, to run the program as an application.

You should see a new window pop up, titled "BuggleWorld." BuggleWorld is a special microworld we have created for CS111 students to have fun exploring programming ideas! Try clicking on a couple of buttons to see how BuggleWorld works.

Here's a quick summary of the most important buttons:
runs a program in BuggleWorld
puts BuggleWorld back to its initial state
Reset must be hit before you can Run a program again.
Each press of Step executes (roughly) one line of the program. Step can not be used if the program has been Run unless BuggleWorld is Reset.
Also note that you can just play with the BuggleWorld options. You simply need to create a new Buggle() before any BuggleWorld methods can be invoked, i.e. before any of the bottons can be pressed to do some action.


Exercise 1. Jumping into CS

Now you're all ready to write your first BuggleWorld program! The goal is to write "CS" as shown below.


We will create our own java file from scratch!
Let's call our file Cs.java.
We'll use Hi.java as a guide to creating our new file.


Exercise 2. Draw the BuggleWorld flag

Now, write a new program called Flag.java to draw the BuggleWorld flag as shown below. Some useful things to note:

At the end of lab

Save your work by uploading to your account in puma, using Fetch. You may want to use this work later as a reference. From there you can download your files to your home computer, or an other device, like a memory stick. Your programs will stay on puma until the end of the semester.

Then, please make sure to delete your files from the local computer, empty the trash (click on the blue background of your Mac, then Finder -> Empty Trash) and log out!