ITEC120
lab01:
Making Objects with BlueJ
Today we will learn how to enter the pieArea function
we've been using in class, 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.
To evaluate expressions written in Java:
-
Start BlueJ
.
On windows, do this by selecting
Start > Programs > Radford University Course Software > College of Information Sciences > open-source Java IDEs > 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.5.0).
This number might be rather embedded in the name.
-
Choose Open > New Project..., and enter a name (perhaps
“Krusteaze Pizza”).
BlueJ will ask you where to save it; somewhere on your H drive is fine.
(Presumably you'll want to create a folder "ITEC120" in your H drive,
and within that drive a folder "Lab01", or something like that.)
-
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)".
-
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:
-
Delete all the provided text
(Later, if you like, you can create a new class and pore over what BlueJ
provides.)
-
At the top of the now-empty file, type
class PizzaServer {
}
- In between the squirly-braces, paste in
the pieArea function,
from class notes.
Once you have done this, you can check for superficial errors
by pressing "compile".
-
Now we're ready to call our function.
We'll create a particular PizzaServer, and then ask it questions:
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 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 pieArea.
It will ask you for the input, and then Jo will give you the answer!
-
To do:
Modify the pieArea to use the constant Math.PI,
rather than 3.14.
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,
write a second function for PizzaServers, crustArea,
as from lecture.
Include comments.
Did you remember to think up a couple of easy test cases by hand, first?
For Aug.25 (Fri)
Today we'll write a new function, from scratch.
Choose a partner -- today's work will be in groups of two.
Before leaving lab, you and your partner
must get your assignment checked off, as described below.
The problem:
VA sales tax is 5%. Let's write a program which,
given an amount, can calculate the amount of tax on an item.
-
On paper, write several examples/test cases (at least three).
If you like, you can type these in the computer.
-
Start BlueJ, and choose
Project > New Project....
(You can close any other projects.)
-
Create a New Class..., with the name (say)
TaxMan18.
This creates a box representing a blueprint for future TaxMans.
Double-click on that box, and (as before) replace the entire
contents of that file with
(Remember that inbetween the curly-braces is where we'll
put all the behaviors (methods, a.k.a. functions) a TaxMan
will know.)
-
Write a method salesTaxOn( .. ).
What is the type of the parameter (int, double, String, ...?)
Decide on a good name for the input (the formal parameter name).
What is the type returned by the salesTaxOn() function?
Write a description of what the function consume and what it returns,
in a comment.
Finally, write a return statement which computes and returns the answer.
-
Now, test your program, using the same steps you did for pieArea
above:
Compile, Create a new TaxMan instance (named ... ebeneezerScrooge? georgeHarrison? IRS? theCommonWealth?),
Ask this TaxMan instance to
compute salesTaxOn your three test cases.
or the total cost of an item (its list price plus the tax).
NOTE:
You might find that you are getting an answer of 0, instead
of what you expect! Depending on how you write your function, you might
have stumbled onto a pitfall of Java arithmetic.
Don't worry too much about fixing it for now, although you are welcome
to (a) call over the professor or a peer instructor, or (b) experiment
with wasy of re-writing your formula to see if it will get the correct
answer, or (c) compare with the team next to you, and if your answers
differ, try to figure out how you wrote the function differently,
and what Java might be doing?!
(This list of instructions is a version of
the design recipe.)
Now, write a second function (inside class TaxMan),
named priceAfterTax(...).
Follow the same steps as you did above.
-
Make several test inputs, and figure out by hand/calculator
what the answer should be by hand.
(This forces you to think and formalize exactly what the function
will do, since I did nothing but give you a suggestive name!)
-
Write the function:
-
Give the function name preceded by its return type,
and (in parentheses) the formal parameter (with its type),
followed by (for-now) empty curly-braces.
-
Write comments, saying what your function does.
Your comments should mention each of your parameters by name.
-
Write the return followed by a blank area,
followed by a semicolon, and
- Replace the blank area with an expression
which is the general case of the answer!
If you're stuck, you can always inventory:
-
What parameters you have at your disposal,
-
What the type of those parameters are, and
-
What other existing functions you might already have,
which can process data of that type.
-
Press "compile", to check for any superficial errors.
-
Close the text window, and return to the window showing the rectangle
representing the template class TaxMan.
-
Right-click on the TaxMan rectangle to make
a new TaxMan() instance.
(which will appear as a red object on the bench).
-
Right click on your new instance, and call your function.
Check for subtle errors
by verifying that your test cases really do return the predicted amounts.
With your partner, discuss:
Does your program follow the laws of programming?
If you have magic numbers or repeated code, go back and fix that!
To Check Off, before leaving lab:
Have the instructor (or peer instructor) come around and verify
that you and your partner have class TaxMan which includes:
-
Reasonably-indented code;
-
a comment in front of each method;
-
at least five test cases, between the two methods;
-
No repeated code;
-
No magic numbers.
-
Show the result of calling one of your test cases on each method
(leave up the test-case-result window).
If the test cases don't return the expected results, do you have
any theories as to why?
As always,
be sure to log yourself off the computer before leaving the lab.
After lab finishes, compare your solution
to this solution.
18You are free to choose a name which
doesn't imply it's a job only fit for men. back