home—info—lects—labs—exams—hws
tutor/PIs—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;
within that, make a folder for today's lab.
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.
I recommend creating two subfolders inside, Labs and Homeworks.
BlueJ, and the Code Pad
To evaluate expressions written in Java:
-
Start BlueJ.
On windows, do this by selecting
Start > Programs > Radford University Course Software > College of Science and Technology > ITEC > Editors > BlueJ > BlueJ (whew!)
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 "Lab01" 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 all 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.
(In any version of BlueJ except the one installed
in this particular lab,
you can select Edit > Auto-layout, to auto-indent the file.)
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.)
Two ways to call our function:
- By clicking within BlueJ:
From the project window, right-click1 the tan “PizzaServer”
box.
on the tan class Pizzeria,
and you'll get a pop-up menu;
choose pizzaArea.
Type a diameter into the box (say, 20),
and BlueJ should respond with “pizzaArea(20) = -99.9”.
Yeah, -99.9 isn't the correct answer,
but before we can fix that, we have to figure out the correct formula
and program it!
- By typing into Code Pad:
type
“Pizzeria.pizzaArea(20)”,
and it the answer 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.
-
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 the semicolon.
What gives?
Java is making our life difficult:
You leave off a semicolon unless it's a void method,
in the Code Pad. (Sigh.)
-
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.143).
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!
-
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.)
If you have time...
-
Evaluate:
Pizzeria.pizzaArea(2*6)
That is, you can do some arithmetic before4
asking Java
to compute a pizzaArea.
-
Type the same expression again, but this time
with a semicolon at the end:
What happens?
Nothing!5
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..
1
Mac OS X users: control-click is how to effect a right-click on
with single-button mouse.
↩
2
It'd work just fine to type this in before pizzaArea
as well. ↩
3You 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.
↩
4
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.
↩
5Technically, 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—java.lang docs—java.util docs