![]() |
![]() |
|
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.
On your H: drive (sometimes called the U: 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.
To evaluate expressions written in Java:
BlueJinto the entry-blank, then clicking on the BlueJ icon.
lab01a). BlueJ will ask you where to save it; put it on your H: drive, inside a "Labs" folder inside your Itec120 folder.
and hit return, and confirm that it responds with the answer2+3
.5 (int)
A first function
In between the curly-braces for
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.
, and the program's answerPizzeria.pizzaArea(20)
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.)
Open up the test editor for
We call test driver
,
because all it does is test the other functions we'll write.
Compile the program,
then call our new function
),
and it doesn't return an answer (that's what the keyword
means);
the function just prints some information on the terminal window
(which, we'll see later, is different and less satisfying than
NOTE: When calling
s in???
As the comment indicates,
replace comment at the end of
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
If you have time...
Pizzeria.pizzaArea(16); |
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..
is a constant which is defined in the (built-in) classPI
This page licensed CC-BY 4.0 Ian Barland Page last generated | Please mail any suggestions (incl. typos, broken links) to ibarland ![]() |