home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs
hw05
fields
recording game results
Part (a)
Due Sep.21 (Fri):
only hardcopy is needed.
-
true or false?:
Suppose PizzaServers have a field for their salary.
If there are seven PizzaServer objects (instances),
then there are seven different
boxes labeled “salary”
(one inside each object).
-
For each of the following,
indicate whether it's most appropriate to use
a field (“f”),
a named constant (“nc”),
or
a local variable (“lv”).
-
A PizzaServer's social security number.
-
the amount of topping on the pizza in a
call to crustArea.
-
The total number of hours ever worked
by a PizzaServer.
-
The diameter of an Extra-extra-large pizza, at Krusteaze Pizza Korp.
-
A field is declared
inside of ,
but outside of any
.
- How many parameters does a setter method have?
- How many parameters does a getter method have?
-
The Java keyword “void”
can be used in the place of an actual type when you…
- declare a parameter
- specify a method's return type
- declare a local variable
- initialize a local variable
(Circle zero or more, appropriately.)
-
We've been drawing objects on the board as follows
(reminiscent of BlueJ's "inspection" window for objects);
here's a a before-and-after “snapshot”:
A brand-new PizzaServer: That same server, after calling:
work(10);
setSalary(8.0);
work(1);
PizzaServer PizzaServer
+-----------------------+ +-----------------------+
| _________ | | _________ |
| salary |___5.75__| | | salary |___8.0___| |
| _________ | | _________ |
| balance |____0.0__| | | balance |__65.5___| |
| _______ | | _______ |
| isManager |_false_| | | isManager |_false_| |
| | | |
+-----------------------+ +-----------------------+
After sneaking a peek at the first five bullets
for part hw05b below...
- Draw a picture for a brand new TeamStats object (“before”).
- Draw a second, “after” picture of
what that same TeamStats object looks like
after the two method-calls
recordGame(true);
recordGame(false);.
Note that the name of the class is above your box,
and the variable referring to the object
(such as “jo”)
is not included in the picture at all.
Part (b)
Due Sep.24 (Mon):
You'll turn in two printouts:
The html as usual (via Tools >Project Documentation),
and double-click on your test class and print out that
(auto-generated) code.
-
Make a new class
TeamStats.
Include your name in an @author javadoc comment
preceding the entire class.
-
Every TeamStats has three fields:
the name of a sports team,
the number of games played this season,
the number of games won this season.
(Initially, the team will
have played no games
and
will be named
“The Nameless Nine”.)
To yourself, confirm that you can create a TeamStats object
and inspect it.
-
For each of the three fields,
write the getters and setters
(documentation, signatures, then the body).
Test to yourself, that these methods work.
-
Write a method
void recordGame(boolean won),
intended to be called once after each game the team plays,
which updates the count of games played and (if appropriate)
the count of games they've won.
Write the signature and documentation for this function;
the body can be entirely empty curly-braces (no return
statement is needed1, since the function is void).
Do not write the body of the method yet.
-
As shown in Friday's lab,
use BlueJ to
add a test class (right-click on the class)
and then
create a test case named “testRecordGame”
(by right-clicking on the test-class).
- Create a TeamStats object;
- Call the getter for each of the games-played and games-won
(asserting that the answers should each be zero);
- Call your object's recordGame(true) method.
(This gives no result.)
Then call
the getter methods for the games played and the games won,
and assert that
the correct answer will be be 1.
(Of course, you haven't written the code yet,
so the fields will still be unchanged at the moment.)
- Call recordGame(
truefalse),
and (as in the previous step) call the getters,
asserting what the correct answer should be.
Be sure to End-recording of the test case.
-
Write a method
toInfoString(),
which returns a string of the form
"The Nameless Nine are 2 for 4.",
indicating that out of four games, they have won two.
-
Make a second test-case “testToInfoString”,
and
call
toInfoString two or three times,
interspersed with calls to setters and/or recordGame.
Be sure give the expected answers in exactly the form
you want, character-for-character.
(As always, remember quotation marks (") when writing
String literals.)
Part (c)
Due Sep.26 (Wed):
implementation (Java code).
Turn in hardcopy as usual.
-
Write the body for recordGame.
Click Run Tests to verify that it passes your test cases.
-
Write the body for toInfoString.
Click Run Tests to verify that it passes your test cases.
Your code should use getters and setters,
but not access any fields directly2.
Extra Credit
Due with the other work
— Sep.24 (documentation & tests) and Sep.26 (implementation).
- Write a version of recordGame
which accepts two inputs:
the team's score and the opposing team's score.
Note that this suddenly makes it possible to record ties,
which means you might need to add another field to your class.
You can update toString to include this info,
if you like.
-
Write a method winningPercentage which
returns a number between 0.0 and 100.0 (inclusive).3
4
1Note that in
a void method it's actually allowable
to write “return;” (note the lack
of expression before the semicolon).
↩
2except for
the getters and setters themselves, of course. ↩
3
If no games have been played, the winning percentage does not exist.
In that case it's reasonable to return “not a number”
— Double.NaN — which is already the
result of 0.0/0.0 (but not the result of 0/0).
↩
4Recall that you can convert
an int m to a double
either by
casting — “(double)m” —
or by
calling functions:
“(new Integer(m)).doubleValue()”.
(Casting is easier to type, but its syntax is an off-the-wall way
to just call a function.)
↩
home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs