CIS163AA - Java Prog I - Sect 5811 - Worksheet #6 - by Pat Moss
Due Monday, November 7, 2005
Name: Pat Moss
I. Definitions: Please explain the following terms:
1. static method -- A static method (also called a class method) can
(p. 292) be invoked through the class name. We don't have
to instantiate an object of the class in order to
invoke the method. All methods of the Math class
are static methods.
Example:
double x = Math.sqrt(27);
2. static variable -- A static variable (also called a class variable)
(p. 292) is shared among all instances of a class. There
is only one copy of a static variable for all
objects of the class. Therefore, changing the
value of a static variable in one object changes
it for all the others as well.
Example:
private static int count = 0;
3. interface -- (1) A Java reserved word that is used to define
(p. 650) a set of abstract methods that will be
implemented by particular classes.
(2) The set of messages to which an object
responds, defined by the methods that can
be invoked from outside of the object.
(3) The techniques through which a human user
interacts with a program, often graphically.
Also consider Graphical User Interface (GUI).
4. algorithm -- A step-by-step process for solving a problem.
(p. 639, p. 319) A computer program is based on one or more
algorithms.
5. method decomposition -- Occasionally, a service that an object
(p. 320) provides is so complicated that it cannot
reasonably be implemented using one method.
Therefore, we sometimes need to decompose
a method into multiple methods, in order to
create a more understandable design. This
is called method decomposition.
6. method overloading -- We can use the same method name with different
(p. 328) parameter lists for multiple methods. This
technique is called method overloading. It is
useful when we need to perform similar methods
on different types of data.
In Java, a method name can be used for multiple
methods as long as the number of parameters,the
types of those parameters, and/or the order of
the types of parameters is distinct.
(p. 333) Constructors can be overloaded and often are.
By providing multiple versions of a constructor,
we provide multiple ways to set up an object.
7. method signature -- A method's name, along with the number, type,
(p. 332) and order of its parameters, is called the
method signature. The Java compiler uses the
complete method signature to bind a method
invocation to the appropriate definition.
Notice that the return type of a method is not
part of the method signature. That is, two
overloaded methods cannot differ only by their
return type.
8. flow layout -- Organizes components from left to right, starting
(p. 338, 339) new rows as necessary. Flow layout puts as many
components as possible on a row, at their
preferred size. When a component cannot fit on a
row, it is put on the next row. As many rows as
needed are added to fit all components that have
been added to the container.
II. Question or short essay
1. What are the four main activities in software development? (p. 288)
a. establishing the requirements
b. creating a design
c. impementing the design
d. testing
2. How does aggregation demonstrate a "has-a" relationship? (p. 303)
Some objects are made up of other objects. For example, a car is
made up of its engine, its chassis, its wheels, etc. Therefore we
can say that a car is an aggregation -- it is composed, at least
in part, of other objects. Aggregation is sometimes described as
a "has-a" relationship. For instance, a car has a chassis.
3. What does the "this" reference refer to? (p. 305)
This word "this" is a reserved with in Java. It allows an object
to refer to itself. Inside a nonstatic method, the this reference
can be used to refer to the currently executing object.
Example:
if (this.position == piece2.position)
return false;
4. What does it mean that in Java all parameters are passed by value?
(p. 325) When passing actual parameters to format parameters, it
is possible to:
a. pass the memory address of each parameter (pass by reference), or
b. pass an actual copy of each parameter value (pass by value).
Java passes an actual copy of each parameter value, except for
objects in which case it passes a memory address of the object.
When an object is passed by value, we can modify the parameter
inside a method without affecting the source parameter value of
the caller.
5. Describe a good strategy for testing a program. (p. 333)
We must test each alrogithm separately. And then we must test
each alrogithm in combination with other alrogithms, building
up in complexity until we are testing the entire program.
We must supply both "good data" and "bad data" values to the
program, in order to exercise the "normal" and also the "error"
trapping code.
6. How is a border layout different from a box layout? (p. 350, p. 354)
a. A box layout organizes components either vertically or horizontally,
in one row or one column.
b. A border layout lays out a container using five distinct areas
(North, South, East, West, and Center). (p. 750)
7. Describe how a grid layout works. (p. 348-350)
A grid layout presents a container's components in a rectangular grid
of rows and columns. One component is placed in each grid cell, and
all cells are the same size.
8. What is the purpose of the BorderFactory class? (p. 749-750)
A public class, derived from Object, that represents a factory for
creating GUI borders.
III. Programming Exercises
Modify the Student class on page 306 in Chapter 6 as follows. Each
student object should also contain the scored for three tests.
Provide a constructor that sets all instance values based on
parameter values. Overload the constructor such that each test
score is assumed to be initially zero. Provide a method called
setTestScore() that accepts two parameters: the test number (1
through 3) and the test score. Also provide a method called
getTestScore() that accepts the test number and returns the
appropriate score. Provide a method called average() that computes
and returns the average score for this student. Modify the
toString() method such that the test scores and average are
included in the description of the student. Modify the driver class
main method to exercise the new Student methods.