Carnegie Mellon University

ActiveTutors Project

GridCity Programming


Getting Started with GridCity

 

This document describes how use JDK 1.2.1 to run GridCity. It assumes that your computer has JDK 1.2.1 (which you can download from java.sun.com) and Windows 95, 98 or NT. We will cover the following topics:

GridCity

GridCity is a simulator (see https://dataprivacylab.org/activetutors/gridcity/simworld.html to read about simulators) developed to provide a friendly environment in which to learn the basics of Java programming. (If you are using our Tutor program, we will only be using GridCity for Unit 1. Starting with Unit 2, we will be programming without the simulator.)

The first step is to get GridCity onto your computer. You should certainly store GridCity on your computer in the place that makes the most sense to you, but for purposes of our directions, we will assume that GridCity is in the C:\Temp directory.

There are two ways to get a copy of GridCity into C:\Temp:

We will be using JDK 1.2.1 to access and use files from GridCity. Instructions on how to use JDK and an example showing how JDK and GridCity work together are in the JDK 1.2.1 section of this page.

Downloading GridCity from the Internet

GridCity and instructions for downloading it are on the internet at https://dataprivacylab.org/activetutors/gridcity/download.html.

Copying GridCity from a Diskette

If you have a copy of the GridCity program on a diskette, insert your diskette into your computer's disk drive and use Windows Explorer to navigate to the A: drive. Find the folder GridCity-1.1 and copy it into the C:\Temp directory.

    How to do this:
  • Right-click on the Start button at the bottom lefthand corner of your screen.
  • Select Explore from the menu that appears.
  • A box saying Exploring - Start Menu will appear on your screen. Scroll to the top of the box and click on 3½ Floppy (A:). Your screen will look similar to this:



    Right-click on GridCity-1.1, which is on the righthand side of your Explorer window, and drag it to the folder titled Temp in the C: directory. When your mouse is properly positioned, Temp will be highlighted. Release the right mouse button, and a menu will appear. Select Copy Here from that menu:



JDK 1.2.1

Working with Java involves two processes:
· making and editing programs
· compiling and running programs

First let's discuss compiling and running existing programs. You will then have the tools you need to make and edit programs.

We will use JDK 1.2.1 (hereafter called JDK) to compile (make our code readable to the computer) and run our Java programs.

Run the Command Prompt program in order to start JDK. To do this, left-click on the Start button at the bottom lefthand corner of your screen. A menu will appear. Choose Programs from the menu. Another menu will appear. Select Command Prompt, and the Microsoft Windows Command Prompt (DOS) screen will appear:



We will be entering our commands onto the Command Prompt screen.

To make sure JDK is working, let's run a sample program. For our sample, let's use Demo, which should now be in C:\Temp with the rest of our GridCity files. In the instructions below, the text that the Command Prompt screen outputs to you appears in bold type. The text that you should type onto the screen appears in regular type.

  1. The Command Prompt screen will say:

            Microsoft(R) Windows NT(TM)
                         (C) Copyright 1985-1996 Microsoft Corp.
                         
                         C:\users>
    
  2. Change to the GridCity directory by typing:

            C:\users>cd\Temp\GridCity-1.1
    
    and hitting the Enter key.
  3. Compile the Demo.java program by typing:
            C:\TEMP\GridCity-1.1> c:\jdk1.2.1\bin\javac Demo.java
    

    and hitting Enter.

    To compile any program, you will need to type "c:\jdk1.2.1\bin\javac" and then the name of the program. If later you write a program called "Idea.java", you can compile it by typing "c:\jdk1.2.1\bin\javac Idea.java".

  4. If there are no errors found in the program (i.e., if the program compiles successfully), and the Console Output screen will only display:
            C:\TEMP\GridCity-1.1>
    
    Otherwise, a description of the kinds of problems found will be reported. You can only move on to the next step and run the program if the program compiles successfully.
  5. To run the Demo program, type:
            C:\TEMP\GridCity-1.1 c:\jdk1.2.1\bin\java Demo
    
    and hit Enter.

    Here's a shortcut that will work on a Windows NT machine:
    You can hit the up arrow on your keyboard, the Console Output screen will enter the last thing you typed for you:
            C:\TEMP\GridCity-1.1 c:\jdk1.2.1\bin\javac Demo.java
    
    You can then just edit out the "c" at the end of "java" and the ".java" extension on "Demo" before you hit Enter.
    In fact, you can use both the up and down arrows to cycle through the various things you have entered since you opened Command Prompt.

  6. GridDisplay will appear on your screen. It will look like this:



    Click Run, which is the middle button near the top of the GridDisplay screen, and the program will run.
    (Note that the above picture is only an illustration. In order for Run to work, you need to click on it in the actual GridDisplay screen you brought up by entering commands on the Command Prompt screen.)

    When the Demo program runs, the van moves ahead two spaces, turns left three times, and then moves ahead two more spaces. After the program has run successfully, GridDisplay will look like this:



    If your GridDisplay screen looks like the one above, you have now successfully used JDK to run a GridCity program.

Editing Programs

We will edit programs in a text editor such as Notepad. (We use Notepad for our examples.) Let's edit the program Demo. (The Tutor program will discuss in detail how to write code. The following is an example to illustrate how to use Notepad to edit programs.)

  1. Open Notepad by left-clicking on the Start button at the bottom lefthand corner of your screen, and choosing Programs from the menu that appears. Another menu will appear. Select Accessories, and then Notepad.
  2. Open Demo.java in Notepad by selecting Open from the File menu. Find GridCity-1.1 in the C:/Temp directory, and double click on it. From the drop-down menu beside Files of type, choose All Files. Then find the program Demo.java and double click on it. (The suffix ".java" may not appear. The file we want to choose is the Java Source File. You can see details about which files are which by clicking one time on the Details icon, which is in the upper righthand corner of the Open dialog box.)



  3. The opened file Demo.java should look like this:



  4. Edit the program by typing edits directly into the program in Notepad. If we want Andie to go forward two additional spaces at the end of the program, we can add some code saying:

                           andie.ahead();
                           andie.ahead();
    

    The end part of our program will now look like this:



  5. Save the edited file by selecting Save from the File menu in Notepad. We are replacing the existing Demo file in the C:\Temp directory.
  6. Exit Notepad by selecting Exit from the File menu or by clicking once on the X at the extreme top righthand corner of the Notepad screen.
  7. We will need to compile our program again after we have edited it. (See directions for using JDK 1.2.1 for a refresher on how to do this.)
  8. When we run our program again, the final GridDisplay screen will look like this:



Writing Programs

We will also use a text editor (our examples use Notepad) to write our own programs from scratch. (The Tutor program will discuss in detail how to write code. The information provided here is to enable you to use a text editor to write programs.)

Type your program onto a blank screen in Notepad. When you first open Notepad, you automatically have a blank screen to work with. If you have been working and have someting on your screen already, you can get a blank screen by selecting New from the File menu.

Our programs will be starting with the words "public class," and then a name. In our example below, "Example" is the name of our class. This name can actually be anything you want it to be. The important thing to know when you are writing a new program is that, in Java, all programs will have exactly one class, and the name we give to any program we write has to be the same as the name of the class that the program contains. If "Example" is the name of our class, then "Example.java" must be the name of our program. Java is case sensitive, so "Example" and "example" are not the same name.

When you want to save your work, do the following:

It is a good idea to save your work often. If you experience electrical or hardware problems, you could lose any unsaved work.


Related links at CMU:
Fall 1999 90-775 Java Programming [java@sweeney.heinz.cmu.edu]