CIS163AA - Java Prog I - Sect 5811 - Worksheet #4 - by Pat Moss
Due Monday, October 10, 2005
Name: Pat Moss
I. Definitions: Please explain the following terms:
1. object -- (1) The primary software construct in the object-
(p. 654) oriented programming paradigm.
(2) An encapsulated collection of data variables
and methods.
(3) An instance of a class.
2. instance -- An object created from a class. Multiple objects
(p. 650) can be instantiated from a single class.
3. instance data -- A variable that must be referenced through a
(p. 650) particular instance of a class, as opposed to
a class variable.
4. local data -- A variable defined within a method, which does not
(p. 652) exist except during the execution of the method.
In classic computer theory terminology, this is
a "local stack variable" that is "pushed" and
"popped" off the stack, as opposed to a heap
variable.
5. scope -- The areas within a program in which an identifier,
(p. 658) such as a variable, can be referenced.
Local data has local scope. Public data has global
scope. Also consider "visibility" and "longevity".
6. return type -- The type of value returned from a method, specified
(p. 658) before the method name in the method declaration.
Could be "void", which indicates that no value is
returned.
7. constructor -- A special method in a class that is invoked when an
(p. 644) object is instantiated from the class. Used to
initialize an object. A constructor cannot have a
return type, not even "void".
8. parameter -- (1) A value passed from a method invocation to its
(p. 655) definition.
(2) The identifier in a method definition that
accepts the value passed to it when the method
is invoked.
(p. 639) (3) actual parameter: The value passed to a method
as a parameter.
(p. 648) (4) formal parameter: An identifier that serves as
a parameter name in a method. It receives its
initial value from the actual parameter passed
to it.
9. mutator method -- Because instance data is generally declared with
(p. 165) private visibility, a class usually provides services
to access and modify data values. An "accessor method"
is used to access a private data value. And a "mutator
method" is used to modify a private data value. These
types of methods are also sometimes referred to as
"getters" and "setters", respectively.
10. listener -- An object that is set up to respond to an event when
(p. 652) it occurs. Also see "listener adaptor class" and
"listener interface".
II. Question or short essay
1. What is the difference between an object and a class?
A class is the blueprint for an object, the model that defines the
variables and methods that an object will contain when instantiated.
An object is an encapsulated collection of data variables and methods.
It is an instance of a class. (p. 643, 654)
2. What does the return statement do?
A return statement allows a method to return an "answer" back to the
calling procedure, at the next instruction following the point of
invocation. (p. 658)
3. How is a constructor different from a regular method?
A constructor is a special method in a class that is invoked when an
object is instantiated from the class. It is used to initialize an
object. A constructor cannot have a return type, not even "void".
(p. 644)
4. How is private visibility different from public visibility?
Private methods and data are only visible within a class, and within
an object instantiated from that class. Public methods and data are
visible both from within the class and from elsewhere in a program.
5. What is the difference between a formal parameter and an actual
parameter?
actual parameter: The value passed to a method as a parameter.
formal parameter: An identifier that serves as a parameter name
in a method. It receives its initial value from
the actual parameter passed to it. (p. 639, 648)
6. How is a method different from a class?
A class is the blueprint from which to instantiate an object.
A class contains methods, such as a constructor, and getters and
setters, etc.
A method is a named group of declarations and programming statements
that can be invoked (executed) when needed. A method is part of a
class. (p. 643, 653)
III. Programming Exercises
Write source code for the following.
Hand in a printed copy of your source code.
Write and implement a class called PairOfDice. Its instance data
should be two six-sided Die objects. It should have at least a
constructor and a method called "throw". Whenever the "throw"
method is called it should roll the 2 Die objects and return the
sum of the two rolls. Create a driver class with a "main" method
that throws the dice five times and prints the values returned.