Chapter 5 questions
5.1
1. An object is a runtime entity that contains data and responds to messages, while a class is a software package or template that describes the characteristics of similar objects. Classes are used with objects, but objects do not need to be used with classes.
2. When an object’s memory storage is no longer referenced by a variable, it is deleted, using the process “garbage collection”
3. The behavior, the state, and the identity of an object are three characteristics of objects that are important.
4. The server sends information to the client, in the form of messages.
5. The interface of a class is the list of methods supported by the server.
5.2
1. mutators are messages that change an object’s state. To access the objects state and check to see if the mutators worked correctly, accessors are used.
2. public and private are both visibility modifiers. When neither of them are used, the program basically labels everything as public. Private is used for most instance variables.
3. Constructors are used to initialize the instance variables of a newly instantiated object. They are activated by “new”.
4. toString obtains the string representation for the object
5. a variable can be assigned to another variable to represent the same object or value. An example would be s1 = s2, making s1 equal to s2.
6. in Java, primitive and reference type variables are handled differently in memory, an example of primitive is int, and an example of reference is String.
7. the null value, once assigned to a reference variable, causes the computer to reclaim the object’s memory during garbage collection.
8. when a program attempts to run a method with an obhject that is null, Java throws a null pointer exception.
String str = null;
System.out.println (str.length());
9. default constructors have empty parameter lists.
10. it provides a default constructor
11. constructors that expect another object of the same class can be used to chain constructors.
5.4
1. parameters listed in a method’s definition are formal parameters, while values passed to a method when invoked are called arguments or actual parameters.
2. the purpose of a parameter is the pass information to a method.
3.
public int getAverage(){
int sum;
int num1;
int num2;
sum = (int) Math.add(num1 + num2);
debug(”Sum:”, sum);
return sum;
4. local variables are temporary working storage for data in a method.
5.5
1. the lifetime of a variable is the period in which it can be used. Local variables and formal parameters exist for one execution of the method. Instance variables last for the lifetime of an object.
2. shadowing is dangerous because it increases the chances of getting a programming error.
3. a. the variables in the code are a, b, x, y, c, d.
b. a and b are both global variables. c and d are local variables.
c. a and b do not expire, while c, d, x, and y are part of the method, and are no longer accessible when the method stops executing, so are wiped and renewed when the method starts again.
Loading...