Put answers on blog or Google document before next class (Monday). Questions on page 74.
Exercise R2.1.Explain the difference between an object and an object reference.
An object is a value of a class type that consists of a state and related behavior while an object reference is a variable that contains the memory location of the object and allows access to that object.
Exercise R2.2.Explain the difference between an object and an object variable.
An object is a value of a class type while an object variable is a symbol that identifies a storage location that can hold different values.
Exercise R2.3.Explain the difference between an object and a class.
An object is a value of a class type while a class is a programmer-defined data type.
Exercise R2.4.Give the Java code for constructing an object of class Rectangle, and for declaring an object variable of class Rectangle.
Rectangle rectAngL = new Rectangle(0, 0, 15, 15)
Rectangle rectAngL refers to a new object variable that is being declared while new Rectangle(0, 0, 25, 25) refers to the object that is being constructed that is to be assigned to the variable.
Exercise R2.5.Explain the difference between the = symbol in Java and in mathematics.
The = symbol in Java is used to assign values and is always going to take the value to the right and assign it to the variable on the left, whereas in mathematics it is simply used to state that the expression on the left and the expression on the right are of equal value.
Exercise R2.6.Uninitialized variables can be a serious problem. Should you always initialize every int or double variable with zero? Explain the advantages and disadvantages of such a strategy.
Unless you are guaranteed to use user-inputted data for the variable, you should always initialize the variable to zero. The advantage would be that you are assured of where the value is explicitly stated and you have a point in the code in which you can trace back in the event of an error.
Exercise R2.7.Give Java code to construct the following objects:
(a)A rectangle with center (100, 100) and all side lengths equal to 50
Rectangle rectAngL = new Rectangle(100, 100, 50, 50);
(b)A string “Hello, Dave!”
Create objects, not object variables
System.out.println("Hello,Dave!");
Sunday, May 18, 2008
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment