CS46A Lab

Running Java Programs

Copyright © Cay S. Horstmann 2009 Creative Commons License
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License.

Instructions

Part A

  1. Install the Java Development Kit
  2. Download and install BlueJ from http://bluej.org/download/download.html.
  3. Start BlueJ.
  4. Make a new project: Project -> New Project, then type hello into the dialog.
  5. Click on New Class. Give a class name of HelloPrinter.

  6. Double-click on the HelloPrinter rectangle. Erase all code in the editor window and paste in this code:
    public class HelloPrinter
    {
       public static void main(String[] args)
       {
          // Display a greeting in the console window
    
          System.out.println("Hello, World!");
       }
    }

  7. Click on Compile. You should get a status message “Class compiled - no syntax errors”
  8. Click on Close. Right-click on the HelloPrinter class. Select void main(String[] args) from the menu. Click Ok on the next dialog. You should get this display:

    Congratulations: You have just executed your first Java program.

  9. Now change the program so that it prints Hello, your name!. What change did you make?

    When the lab instructions ask you a question about what you did, the scribe needs to enter the answer into the lab report. For example, “We double-clicked on the HelloPrinter class and changed the code so that it looks like this: (paste in code). Then we repeated steps 7 and 8.”

  10. It is unfortunately easy to make simple spelling mistakes that confuse the compiler. Replace System.out.println with System.ouch.println. Click Compile. What happens?

    Remember to put the answer into your lab report.

  11. Fix this mistake and now make another mistake of your choice that triggers a compiler error. What mistake did you make, and what error message did you get?

    Remember to put the answer ...

  12. Replace the program with the following:
    public class HelloPrinter
    {
       public static void main(String[] args)
       {
          // Display a greeting in the console window
          
          System.out.println("+---------------+");
          System.out.println("| Hello, World! |");
          System.out.println("+---------------+);
       }
    }

    Click Compile. What error do you get? How do you fix it? What does the program print?

    Remember to ...

  13. Replace the program with the following:
    public class HelloPrinter
    {
       public static void main(String[] args)
       {
          // Display a greeting in the console window
          
          System.out.println(+---------------+);
          System.out.println(| Hello, World! |);
          System.out.println(+---------------+);
       }
    }

    Click Compile. What error do you get? How do you fix it?

  14. Now change the program so that it displays a face, similar to, but hopefully prettier than
      /////
     | o o |
    (|  ^  |)
     | [_] |
      -----

    What is the code of your program?

Part B.

  1. Ok, enough about System.out.println. Now we want to experience a different kind of Java programs. You will install the Alice tools from Carnegie Mellon. This semester, the tools are still a bit experimental, so the setup isn't as smooth as we'd like it to be. Ask if you need help!

    First, download Alice from http://kenai.com/projects/alice/downloads. Pick the latest version (currently 3.0.0.64), not one of the ones labeled OLD. Pick the multi-purpose zip. Unzip somewhere. The download takes a very long time, so you should do this at home, not in the lab.

    Also download the Alice Netbeans Plugin (currently version 3.0.0.64.1).

  2. Download NetBeans 6.8 from http://www.netbeans.org/downloads/index.html. Pick the first option (Java SE). That's also a pretty long download that you should do at home. Then install it.
  3. Start NetBeans (not Alice!). Select Tools -> Plugins -> Downloaded -> Add Plugins. Navigate to the plugin file that you downloaded in step 1 (Alice3ProjectWizard3.0.0.64.1.nbm). Click Ok. Click Install.
  4. Download this file. In NetBeans (not Alice!), select File -> New Project -> Java Project from Existing Alice Project -> Next -> Browse, then browse for the file you just downloaded. Click Finish. Select Run -> Run Main Project from the menu. What happens?

    If you get a message about the location of the Alice galleries, click on "Select Gallery location" and locate the gallery folder inside the unzipped Alice download.

  5. Click on the triangle next to Source packages, then the triangle next to <default package>. Then double-click on MyScene.java and scroll down to the end, until you can see the run method. (Ignore all the complicated code above—in future versions of Alice, this is going to be in a separate place where students won't see it.) Make a change so that the beach chair says something different. What change did you make?
  6. Add the following line under the say command:
    chair.move(MoveDirection.LEFT, 1);

    Run the program. What happens?

Part C

  1. You and your buddy will demo to the lab assistant (a) the face (b) the Alice chair. Each of you demoes one. Start the app (BlueJ or NetBeans) and do what it takes to run the sample program.
  2. If you have additional time, help your neighbor. Since you can get Alice+NetBeans to work, help those who can't.
  3. If you have additional time, start on the homework assignment. Copy/paste any relevant starter code (from similar lab work), and get your code to compile without errors. Do not work on the actual solution--that is individual work, not groupwork.