Computer Program Reading Comprehension Project
U of T Computer Program Reading Comprehension Project
Squirrel
Your task for this exercise will be to answer questions about a Java program excerpt that models two different types of Robotic Squirrel. The code consists of three Java classes. File SquirrelManager.java contains the class with the harvest method that creates Robotic Squirrel objects and calls methods on these objects. SquirrelSimplex.java and SquirrelSapiens.java contain the classes that model the two different types of Robotic Squirrel. To start, read each of the class comments to get a feel for what they each do.

Download files: SquirrelManager.java, SquirrelSimplex.java, SquirrelSapiens.java
Areas: Introduction to classes and objects.


SquirrelManager.java
1/**
2 * SquirrelManager: This class manages two Robotic Squirrels.  One is a
3 * cheap Robotic Squirrel and the other is an advanced Robotic Squirrel.
4 *
5 * -- A. Bolintineanu, May 2001 (modelled on a program by A. Hunter)
6 */
7
8public class SquirrelManager {
9
10public void harvest() {
Displaying 10 of 64 lines
View full source


SquirrelSimplex.java
1/**
2 * SquirrelSimplex: This class models a basic Robotic Squirrel.
3 */
4
5public class SquirrelSimplex {
6
7    // number of hazelnuts currently in reservoir
8    private int numHazelnuts;
9
10    /**
Displaying 10 of 34 lines
View full source


SquirrelSapiens.java
1/**
2 * SquirrelSapiens: This class models an advanced Robotic Squirrel.
3 */
4
5public class SquirrelSapiens {
6
7    // price of one hazelnut (in Martian cents)
8    private int hazelnutPrice = 0;
9
10    // number of hazelnuts currently in reservoir
Displaying 10 of 60 lines
View full source


Question 1
Open up the full source of SquirrelSimplex.java. At what line number is the variable numHazelnuts declared?
Solution

Question 2
Can methods outside class SquirrelSimplex access numHazelnuts?
Solution

Question 3
In SquirrelManager.java, how many objects of type SquirrelSimplex are instantiated?
Hints Solution

Question 4
How can we find out how many hazelnuts are currently in chip's reservoir?
Solution

Question 5
After chip's initial picking of hazelnuts, but before any are extracted, that is, at line 22 of SquirrelManager.java, how many hazelnuts does chip have?
Hints Solution

Question 6
How many hazelnuts does dale have after he was first told to pick hazelnut?
Hints Solution

Question 7
On lines 35 and 36 of SquirrelManager, does the output appear on the same line or on different lines?
Solution

Question 8
For the statement
System.out.println(chip.getNumHazelnuts());
found at line 36 of SquirrelManager.java, what is the output?
Solution

Question 9
Open up SquirrelSapiens.java. In the report method, hazelnutPrice, numHazelnuts, and totalValue are not in quotes. Why not?
Solution

Question 10
What is the output to the screen after the harvest method in SquirrelManager.java has executed?
Hints Solution