RU beehive logo ITEC dept promo banner
ITEC 120
2008spring
ibarland,
jdymacek

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs

lab01a
Making Objects with BlueJ

Today we will learn how to type in a Java function (like ufoHeightAt from lecture) using BlueJ, and how to call the function 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 > OpenSource Java IDEs > 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 Open > New Project..., and enter a name (perhaps “lab01a - Krusteaze Pizza”). 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)”.
    hw01a has some further expressions for you to type into the code pad.

Running a program

  1. Okay, let's create a new class (by selecting that button), and name it PizzaServer. This creates a box representing a blueprint for future PizzaServers. 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 function. We'll (a) create a particular PizzaServer, and then (b) ask it questions:
    1. Return to the BlueJ's main window (the one you saw first, with the codepad in the lower-left). Right-click (mac users: control-click) on the tan “PizzaServer” box. Choose the very first option, new PizzaServer. It will ask you for a name; jo is fine.

      A red box, jo, should appear on the "bench".

    2. Right-click on jo, and select the option pizzaArea. It will ask you for the input, and then jo will give you the answer!

  3. In addition to calling a method by right-clicking on the object (the red box), you can do so using actual Java. From BlueJ's code pad, type the following:

    jo.pizzaArea(0)
    jo.pizzaArea(8)
    jo.pizzaArea(16)
    

    We have a mantra:

    To call a function in Java: “object-dot-functionName-openParen-argument”.

  4. Evaluate: jo.pizzaArea(2*8)
    That is, you can do some arithmetic before1 asking jo to compute a pizzaArea.
  5. Compare to 2 * jo.pizzaArea(8), where you double the answer jo gave, instead of doubling the argument you give her. Two entirely different things!
  6. Type the same expression again, but this time with a semicolon at the end:
    jo.pizzaArea(16);
    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.
  7. Is jo.pizzaArea(10+12) the same as jo.pizzaArea(10) + jo.pizzaArea(12)? Type in each, to find out.
  8. There is a built-in Java object3 named Math, which knows how to compute square-roots. Evaluate
    Math.sqrt(144)
    . This is a function call, follows our pattern for calling a function: object-dot-methodName-openParen-argument.
  9. What is jo.pizzaArea( Math.sqrt(144) )?
  10. 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..

If you have time for more…

To do, with a (new) friend: we can practice writing other functions, which take in a number, and return a number.


1 It turns out, in Java, that jo is handed the number 12; she doesn't actually do any multiplying herself. In fact, she doesn't even know that the 12 she received is the result of doing some multiplication. That's fine, since all that matters to her is what diameter she has to compute the area of; anything else is distraction.      

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

3Actually, it's a lie that Math is an object; it's actually “a class with static methods”. However, we won't cover static methods for another few weeks, and for now it behaves as if it were an object, in the way we call functions.      

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs


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