Java Reference
In-Depth Information
Answers to Review Questions
1.
Object.
2.
Class.
3.
Attributes are represented as fields in the class.
4.
Behaviors are represented as methods in the class.
5.
Four: name, city, numberOfWins, numberOfLosses.
6.
Four: home, visitor, homeScore, visitorScore.
7.
Zero. The Team class has only fields.
8.
Five: homeTeamScored, visitorTeamScored, gameOver, setHomeTeam, and
setVisitingTeam.
9.
Home and visitor are references, so they will both be null. homeScore and visitorScore
are of type int, so they will both be zero.
10.
Name and city are references and will both be null, whereas numberOfWins and
numberOfLosses will be zero because they are ints.
11.
There are no objects in memory because the new keyword was not used.
12.
Angels and worldSeries are references, and even though they are of different data
types, they consume the same amount of memory.
13.
The new keyword was used once, so there is one Team object in memory. Both a and
b refer to this one object.
14.
There are two Team references, a and b.
15.
The parameter visitor is the same name as the field visitor. Setting the following
assigns the visitor parameter equal to itself and does not change the field visitor:
visitor = visitor;
To distinguish the parameter from the field, the field needs to explicitly use the this
reference. To fix the problem, change the body of the method to the following:
this.visitor = visitor;
Search WWH ::




Custom Search