|
home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs
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.
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).
To evaluate expressions written in Java:
/** Functions to calculate the area of pizza purchased, given a diameter. * * @author Ian Barland * @version 2007.Jan.10 */ class PizzaServer { /** Calculate the area of a pizza, given its diameter. * @param diam The diameter of the pizza, in inches. * @return The area of the pizza, in square inches. * * If 'p' is a PizzaServer instance, then: * p.pizzaArea( 0) = 0 +/- 0.001 * p.pizzaArea( 2) = 3.14 +/- 0.001 * p.pizzaArea(12) = 113.04 +/- 0.001 * p.pizzaArea(16) = 200.96 +/- 0.001 * p.pizzaArea(20) = 314 +/- 0.001 */ double pizzaArea( double diam ) { return 3.14 * (diam/2) * (diam/2); } } |
class RocketScientist { int ufoHeightAt( int frameNum ) { return 400 - (102 + 82 + frameNum); } } |
Once you have done this, you can check for superficial errors by pressing "compile".
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".
Right-click on jo, and select the option pizzaArea. It will ask you for the input, and then jo will give you the answer!
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”.
jo.pizzaArea(16); |
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.
Math.sqrt(144) |
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..
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. ↩
home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs
©2008, Ian Barland, Radford University Last modified 2008.Jan.24 (Thu) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |