home—info—exams—lectures—labs—hws
Recipe—Laws—lies—syntax—java.lang docs—java.util docs
hw05
fields
recording game results
Part (a)
Due Feb.18 (Mon) as extra credit (optional):
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 Feb.18 (Mon):
As usual,
print out the html (via Tools >Project Documentation),
and
on WebCT attach your .java file.
Test cases will still be in comments,
but are now a sequence of statements
(since the correct answer can now depend on an object's state).
For example, for the tip method from
lab05a's
PizzaServer, a test sequence would be:
/** Add a tip directly to this PizzaServer's balance.
* @param amt The amount tipped.
*
* Tests (assuming `ps` is a PizzaServer):
* ps.setBalance( 0 );
* ps.tip( 10 );
* ps.getBalance() = 10
* ps.tip( 1 );
* ps.getBalance() = 11
* ps.tip( 0 );
* ps.getBalance() = 11
*/
void tip( double amt ) {
this.setBalance( this.getBalance() + amt );
}
|
Notice how testing tip requires calling
getBalance and setBalance!
-
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
…
For hw05, your test cases will be just as described above — a
series
of calls to your method, getters, and setters.
(This Tuesday's lab will show us how to make automated test cases.)
-
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 Feb.20 (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