RU beehive logo ITEC dept promo banner
ITEC 120
2008fall
aaray,
ejderrick,
ibarland,
jmdymacek

homeinfolabshwsexams
textbookjava.lang docsjava.util docsarchive

lab01a
Compiling; syntax errors; BlueJ's Code Pad

Today we will learn how to type in a Java program using BlueJ, and how to call the program once it's been typed in.

BlueJ is an “Integrated Development Environment” for Java -- a fancy way of saying, a program which lets you write your own Java programs. BlueJ is installed in the Davis Hall labs (1st and 2nd floors), and the basement of Stuart Hall, although not other labs. If you want to download and run BlueJ on your own computer (it's free!), it is an easy install from BlueJ.org; here are some additional tips.

Your H: drive and you

On your H: drive, make a folder to keep all your ITEC120 work in. Do all your ITEC120 work on your H: drive. Even if you live off-campus, you can access your H: drive (by first running VPN on your computer; see a PI for advice).
Not only does this keep you from accumulating multiple versions of your homework or lab work on different computers, but you can pull up your homework in a professor's office hours, no problem (and they can verify the date your file was last modified, should that ever be an issue).

BlueJ, and the Code Pad

To evaluate expressions written in Java:

  1. Start BlueJ.
    On windows, do this by selecting Start > Programs > Radford University Course Software > College of Science and Technology > Editors > BlueJ > BlueJ (whew!)
    On the lab macs, BlueJ is accessible from Dock > Developer Applications > Editors > BlueJ 2.2.0 > BlueJ.
    If, as BlueJ tries to start, it asks for which version of Java to use, select the highest one (e.g. 1.5.0). This version-number might be embedded in the middle of the filename.
  2. Choose Project > New Project..., and enter a name (perhaps “lab01a-hello-world”). BlueJ will ask you where to save it; put it on your H: drive, inside your itec120 folder.
  3. Select the menu item View... > Show Code Pad.
  4. To make sure everything is running as expected, in the lower-right pane (the code pad), type “2+3” and hit return, and confirm that it responds with the answer “5 (int)”.

Running a program

  1. Okay, let's create a new class (by selecting that button), and name it HelloTester. This creates a place where we can write our code. Double-click on that box, and you'll see that BlueJ has created a boilerplate class for you. It has a lot more than we need, so let's go back to the basics for now:

    Once you have done this, you can check for superficial errors by pressing "compile".

  2. Now we're ready to call our method main.
    1. Return to the BlueJ's main window (the one you saw first, with the codepad in the lower-right). Right-click (mac users: control-click) on the tan “PizzaServer” box. Choose the very second option, void main( String[] ). BlueJ will pop up a box; press Ok (leaving { } in the text-box1).

      You should see a window pop up (named something like BlueJ: Terminal Window - lab01a-hello-world), with your message printed inside of it!

    2. Try changing your program so that Hello, cruel world. is printed on the console.
  3. Let's go back to the code pad. Evaluate: Math.sqrt(2*8)
  4. Compare to System.out.println( 2*8 ); (remember the semicolon!) and System.out.println( Math.sqrt(2*8) ); Discuss with your lab leader.
  5. Type Math.sqrt(2*8) again, but this time with a semicolon at the end:
    Math.sqrt(2*8);
    What happens? Nothing!2
    Gotcha: When typing into BlueJ's code pad, do not put a semicolon at the end of an expression. If you ever type in something and don't get any answer at all, double-check that you don't have a semicolon.
  6. To do: Go back to the text of our program, and add/change/remove a single character of the program (introducing an error). Press compile. What error message do you get (if any)? Does BlueJ highlight (in yellow) the line you changed? If you had made this typo when entering the program, would the message have helped you understand the problem?
    (We will go around the room, and see who got the wackiest error message.)

After having done this, take a few moments to explore BlueJ -- look at the menus, right-click on various items and look at all the choices, etc..


1 the { } is an array of no Strings at all -- the program main needs to be handed an array of Strings before it can run (the String[] mentioned in the program); that's why we have to provide some array of Strings, and { } is the simplest one there is. Sound complicated? That's only because there is a lot of overhead/scaffolding for making any Java program (even this simple one). By week 11 the semester, this all will be clear.      

2Technically, the semicolon turns the expression (which has a value) into a statment — which is an expression which doesn't yield an answer!(?).      

homeinfolabshwsexams
textbookjava.lang docsjava.util docsarchive


©2008, Ian Barland, Radford University
Last modified 2008.Aug.31 (Sun)
Please mail any suggestions
(incl. typos, broken links)
to iba�rlandrad�ford.edu
Powered by PLT Scheme