home—info—lects—labs—exams—hws
tutor/PIs—breeze (snow day)
Object120 + its docs—java.lang docs—java.util docs
lab01a
lab intro
Compiling a program with BlueJ
Today we will learn how to enter and run
a simple program to compute the area of a pizza,
using BlueJ.
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.
Your H: drive and you
On your H: drive, make a folder to keep all your ITEC120 work in;
It is strongly recommended that you 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 do:
create a folder ITEC120 on your H: drive,
where you'll store future coursework.
BlueJ, and the Code Pad
To evaluate expressions written in Java:
-
Start BlueJ.
-
On windows: do this by selecting Start,
and typing “BlueJ” into the entry-blank,
then clicking on the BlueJ icon.
- On Mac: BlueJ can be found inside the Applications folder
on the hard drive
(which is shown on the desktop, alongside your H: drive).
If, as BlueJ tries to start,
it asks for which version of Java to use, select the highest one
(e.g. 1.6.0).
This number might be rather embedded in the name.
-
Choose Project > New Project...,
navigate to My Computer > H:
and enter a name (perhaps
“lab01a”).
BlueJ will ask you where to save it; put it on your H: drive,
inside a "Labs" folder inside your Itec120 folder.
-
Select the menu item View... > Show Code Pad
-
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
-
Okay, let's create a new class (by selecting that button),
and name it Pizzeria.
This creates a box for the pizza calculation functions we're about to write.
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:
Delete all1 the provided text.
- Class declaration:
At the top of the now-empty file, type the following:
Replace the ??? with the names of you and your partner.
-
Press compile right now.
There's not much in the program, so hopefully there aren't any
errors preventing the "compiled successfully" message in the
lower-right of the editor window.
-
A first function
In between the curly-braces for class Pizzeria,
type in the following:
Make sure your code is indented as indicated.
You can select Edit > Auto-layout, to auto-indent the file.
(If the auto-indented version looks wrong, it almost certainly
means you are missing some curly-braces or semicolons or such;
don't ignore this cue, but fix it and re-auto-layout!)
Again, press "compile",
and fix any errors that might be keeping it from compiling.
-
Call our function:
Now, let's call our function pizzaArea.
(It will just answer -99.9 for now; we'll get to that later.)
In the Code Pad (lower-right panel), type:
“Pizzeria.pizzaArea(20)”,
and the program's answer -99.9 will be displayed.
Note that here we are typing the function's full name:
className.functionName( args…).
We have to use the full name because the Code Pad is
not inside the class Pizzeria.
-
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.)
If your program isn't legal Java, we say you have a “syntax error”.
(This is different from having
a logic error: a program which runs, but does something
different from what you want.)
-
A test driver function
Open up the test editor for Pizzeria,
and type in the following code
(a) between the curly-brackets of class Pizzeria,
but
(b) entirely after2
the code for the function pizzaArea:
We call testPizzeria a “test driver”,
because all it does is test the other functions we'll write.
Compile the program,
then call our new function Pizzeria.testPizzeria();.
This function doesn't require any input (there was nothing between the parentheses in “testPizzeria()”),
and it doesn't return an answer (that's what the keyword “void” means);
the function just prints some information on the terminal window
(which, we'll see later, is different and less satisfying than returning an answer).
NOTE: When calling Pizzeria.testPizzeria();
you must include a semicolon at the end, whereas when calling
pizzaArea you must not include a semicolon5
What gives?
Java is making our life difficult:
things that don't return an answer (statements) end in a semicolon;
things that return do an answer (expressions) don't involve a semicolon.
-
Fill in the “???”s in testPizzeria
with what the asnwer should be:
the actual, honest-to-gosh area for a pizza with the indicated diameter.
Compile the program and run Pizzeria.testPizzeria()
(recall step 5 above).
-
As the comment indicates,
replace comment at the end of testPizzeria
one final test case.
(You're welcome to use a calculator,
but I suggest trying to choose a diameter so easy that you can do the math in your head.)
Note that your third test case should include -- like the two already provided
-- the actual answer as well as the expected answer.
-
Finally: implementing the function
We can now upgrade our stub function to return the correct answer.
Having worked through some examples (like 20 and 0 and your other test),
we start to see the pattern:
take the diameter divided by two, multiply that by itself,
and multiply by π.
(We can use 3.146).
Can you figure out what to put inside the curly-braces
of pizzaArea,
to do this?
Refer back to lect01a—a first program's ufoHeight,
and match the patterns/keywords!
- Checking the answer
If you got everything correct, you should be able to call testPizzeria,
look at the printed messages, and verify that the actual output matches the expected!
If you have time...
-
Evaluate:
Pizzeria.pizzaArea(2*6)
That is, you can do some arithmetic before7
asking Java
to compute a pizzaArea.
-
Type the same expression again, but this time
with a semicolon at the end:
What happens?
Nothing!8
Moral:
When typing into BlueJ's code pad,
do not put a semicolon at the end of an expression.
-
Is Pizzeria.pizzaArea(10+12)
the same as
Pizzeria.pizzaArea(10) + Pizzeria.pizzaArea(12)?
Type in each, to find out.
-
There is a built-in Java class
named Math, which knows how to compute square-roots.
Evaluate .
This is a function call, follows our pattern for calling a function:
className-dot-functionName-openParen-argument.
-
What is Pizzeria.pizzaArea( 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..
1Ctrl-A is a handy way to select-all;
command-A on mac.
↩
2
It'd work just fine to type this in before pizzaArea
as well. ↩
5
Btw, it's poor practice to have methods that both
return a value and have a side-effect.
↩
4
Well, to be more correct:
non-void function calls are expressions w/o a semicolon
and statements with a semicolon.
So 3+4 can't be typed in codepad with a semicolon,
but Math.sqrt(25) can be typed with or without a semicolon,
and will either be an expression or a statement.
The rationale is that operators like 3+4 never have
side-effects, so you'd never want them as a statement.
However, someFunc(23) might return a value and
have a side-effect5,
so it's conceivable that you might want to use it as a
statement — that is, to call it but ignore its return-value.
↩
3
If you do accidentally include a semicolon at the end of
an expression5
in CodePad, it's not an error -- Java regards it as
a statement, and so it doesn't evaluate to an answer.
↩
6You are welcome to use Math.PI.
As you may deduce from its full name, “PI”
is a constant which is defined in the (built-in) class Math.
↩
7
It turns out, in Java, that pizzaArea is handed the number 12;
pizzaArea doesn't actually multiplying 2 and 6 itself. In fact,
it doesn't even know that the 12 she received is the result of
doing some multiplication. That's fine, since all that matters
to pizzaArea is what diameter she has to compute the area of;
anything else is distraction.
↩
8Technically, the semicolon
turns the expression (which has a value)
into a statment — which is an expression
which doesn't yield an answer!(?).
↩
home—info—lects—labs—exams—hws
tutor/PIs—breeze (snow day)
Object120 + its docs—java.lang docs—java.util docs