Homework Java Chapter 5 5.1 1. What is the difference between a class and an object? A class is a software that describes the characteristics of similar objects and an object is a run-time entity that contains data and responds to messages 2. What happens to an object’s memory storage when it is no longer referenced by a variable? It is purged from the memory 3. List the three important characteristics of an object. Behavior State Identity 4. Describe the client-server relationship. The client knows only the interface of the server and sends messages to it 5. What is the interface of a class? The lists of methods supported by the server 5.2 1. What are mutators and accessors? Give examples. Mutators are methods that change an object’s state- s1.setName (“Bill”); Accessors access the object’s state to see if the mutator works- str = s1.getName(); 2. List two visibility modifiers and describe when they are used. private public 3. What is a constructor method? Methods that indicate how to initialize a new object 4. Why do we include a toString method with a new user-defined class? To obtain the object’s string representation 5. How can two variables refer to the same object? Give an example. By assigning a variable to a variable-s1 = s2 6. Explain the difference between a primitive type and a reference type and give an example of each. Primitives are simple(int, double, char, etc.) while reference types are more complex(all classes) 7. What is the null value? A value given to an object that has had its memory reclaimed by the garbage collection of the computer 8. What is a null pointer exception? Give an example. When something is set to null and then tries to run but it can’t because the object that is going to be used is set to null Ex- String str = null; System.out.println(str.length()); 9. How does a default constructor differ from other constructors? It has empty parameter lists 10. How does java handle the initialization of instance variables if no constructors are provided? It provides a primitive default constructor 5.4 1. Explain the difference between formal parameters and actual parameters. Formal parameters-listed in a method’s definition Values passed to a method when it is invoked 2. How does java transmit data by means of parameters? The variable is stored in a formal parameter and then transferred to an instance variable 3. Define a method sum. This method expects two integers as parameters and returns the sum of the numbers ranging from the first integer to the second one. It assigns the method the result of adding two integers together and returns the value 4. What is the purpose of local variables? Temporary working storage data in a method 5.5 1. What are the lifetimes of an instance variable, a local variable, and a parameter? Instance variable- all the methods in the defining class Local variable- The body of the method that declares it Parameter- The body of the method that declares it 2. What is shadowing? Give an example and describe the bad things that shadowing might cause to happen in a program. Assigning a local variable the same name as a global variable Ex- public void setName(fishing) { this.fishing = fishing; …… } It increases the chances of making a coding error 3. a/ List the instance variable, parameters, and local variables in this code. Instance variable- int x & int y Parameters- private int a, b Local variables- int c, d b/ Describe the scope of each variable or parameter. Int x, y- in the body of the method public void aMutator method Int a, b- in the body of the method public class SomeClass Int c, d- for the temporary storage of what they are assigned c/ Describe the lifetime of each variable or parameter. Int x, y- for the duration of the public void aMutator method Int a, b- for the durationg of public class SomeClass Int c, d- for the temporary storage of what they are assigned