chapter 6 homework

advertisement
Chrisstian souza
NOVEMBER 2018
CHAPTER SIX HOMEWORK
6.1
What is the difference between a class and an object?
 Object is a instance of a class, Object is a physical entity. Example. Class Fruits Object
Apple.
What happens to an object’s memory storage when it is no longer referenced by a variable?
It is returned by garbage collector.
List the three important characteristics of an object.
The three characteristics of an object are state, behavior, and identity.
Describe the client-server relationship.
Servers provide the menu to clients. Clients obtain the services and instantiate a server by
sending it messages.
What is the interface of a class.
A class's interface is the set of methods it provides to clients.
6.2
What are the mutators and accessors? Give examples.
 A mutator is a method that modifies a variable in a class or object. An accessor is a
method that returns a value without modifying a variable in a class or object. Ex.
getAddress() / setPhone(anInteger, anInteger)
List two visibility modifiers and describe when they are used.
 Visiblity modifier is a public indentifier which is used for methods and makes this
possible for all clients of a class. Visiblity modifiers private is used for instance variables
and make these visible only during the writing of a class.
What is a constructor?
 What is used when you initialize new objects.
Why do we include a tostring method with a new user-defined class?
 The toString method returns a string representation of an object. Used for debugging a
class during the developing phase.
How can two variables refer to the same object? Give an example.
 A memory address that shadows each other can refer to the same object. Ex.
Student s2 = s1; // s1 and s2 refer to the same object
6.4
Explain the difference between formal parameters and actual parameters.
 Formal parameters is its name of what appears in the method header and the body. Actual
is a expression passed by method when called.
How does Java transmit data by means of parameters?
 The expressions which are the actual parameters are first evaluated. These values are then
assigned to temporary storage locations, which are referenced by the corresponding
formal parameters of the method
Define a method sum. This method expects two integers and returns the sum of the number from
first to the second one.
 Int sum (int first, int second)
Int result = 0
for (int i = first; i <= second; i++)
result +=i;
return result;
What is the purpose of a local variables?

Local is ran ran under argument passed to a function with a limited life time. Less laggy,
more reliable and easier to fix errors.
6.5
What are the lifetimes of an instance variable, a local variable, and a parameter?
 Instance variable last from when object is instantiated until the object ends. Local last
during the execution of method.. Parameter during execution of method.
What is shadowing? Give an example and describe the problems that shadowing might cause in a
program.
 It is assigning a local variable that overrides a global variable. Shadowing can be
dangerous because it greatly increases the likelihood of coding errors. Like using a
variable already pre-defined.
Consider the following code segment: List the instance variables, parameters, and local variables
in this code.
 Instance variables—int a, b
 Parameters—int x, y
 Local Variables—int c, d
Describe the scope of each variable or parameter.
 Instance variables inside “AnyClass”
 Parameters The body of a method “aBoy”
 Local variables - The body of a method “aBoy”
c. Describe the lifetime of each variable or parameter.
 Instance Variables - Instantiate “AnyClass” until it is over.
 Parameters - Only during execution of the method
 Local Variables - Exist only during execution of its method
Review Questions
1. Explain the difference between a class and an instance of a class.

A class could print while the instance of a class is the actual project.
2. Explain the difference between the visibility modifiers public and private.

Private can only be used from within the class. Public can be used from outside the class.
3. What are accessor and mutator methods?
 Get, set methods for encapsulation.
4. Develop a design for a new class called BaseballPlayer. Instructions in the book.
public class BaseballPlayer{
private String name;
private String team;
private int homeRuns;
private double battingAverage;
public BaseBallPlayer(){
}
public void setName(String str){
name = str;
}
public void setTeam(String str){
team = str;
}
public void setHomruns(int i){
homeruns = i;
}
public void setBattingAverage(double i){
battingAverage = i;
}
public string getName(){
return name;
}
public string getTeam(){
return team;
}
public int getHomeruns(){
return homeruns;
}
public double getBattingAverage(){
return battingAverage;
5. Explain how a parameter transmits data to a method.

Uses set to set a variable used in a method to the value of the passing arguement in a
parameter.
6. What are local variables and how should they be used in a program?

Variables in function ends when the function is over. Only used in their function.
Download