CS46A Lab

Running Java Programs

Copyright © Cay S. Horstmann 2009, 2012 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. Go to http://www.oracle.com/technetwork/java/javase/downloads/index.html. Curse the Oracle marketing department as you see far too many options that change frequently. You do not want any bundles. You do not want the JRE. Look for the Java  Development Kit (JDK) Standard Edition (SE) Version 7 without anything bundled.

  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. 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://www.alice.org/index.php?page=downloads/download_alice3.1. Pick the offline installer. The download takes a very long time, so you should do this at home, not in the lab. (It was a part of homework 0.)

  2. Download the Alice Netbeans Plugin from the same site.
  3. Run the Alice installer that you downloaded in step B1.
  4. Download NetBeans from http://netbeans.org . Pick the first option (Java SE). That's also a pretty long download that you should do at home. Then install it.
  5. Start NetBeans (not Alice!). Select Tools -> Plugins -> Downloaded -> Add Plugins. Navigate to the plugin file that you downloaded in step 1 (named something like Alice3ProjectWizard3.1.37.0.0.nbm). Click Ok. Click Install.
  6. Download this file and unzip it somewhere. Be sure it is actually extracted. (By default, Windows only gives you the illusion of extracting it.) In NetBeans (not Alice!), select File->Open Project and select the extracted hello directory. Select Run -> Run project from the menu. What happens?

  7. Click on the triangle next to Source packages, then the triangle next to <default package>. Then double-click on Scene.java and scroll down to the end, until you can see the myFirstMethod method. (Ignore all the complicated code above.) Make a change so that the trash can says something different. What change did you make?
  8. Add the following line under the say command:
    trashcan.move(MoveDirection.LEFT, 1);

    Run the program. What happens?