Set 04 Answers

1.    List as many reasons as you can for why it is so important to invest lots of time in the “design” of a large-scale software project before the project is coded.  (Several reasons were listed in class.)

Here are some of the things a good design can improve:

·        Efficiency (speed)

·        Efficiency (memory)

·        Ease of coding

·        Ease of debugging

·        Ease of expansion

 

2.   What is meant by “the state” of an object?

The data that the object maintains.

 

3.    What is meant by “the behavior” of an object?

These are “actions” that the object can perform.

 

4.     How is the state of an object represented in the corresponding class definition?

Using instance variables.

 

5.   How is the behavior of an object represented in the corresponding class definition?

Using instance methods.

 

6.  What is “the heap”?

The heap is the region in memory where Objects are stored.  When objects are no longer being used, that memory is “recycled” automatically so that it can be used by other Objects later.

 

7.  What is a “reference variable”?

A reference variable stores the memory address of an object.  We say that the variable “refers” to the object.

 

8.  Draw the memory map (including the “variable stack” and the “heap”) for the following code fragment:

double x = 7.9;

double y = x;

String q = new String(“Cat”);

String r = q;

String s = new String(r);

 


9.   What is actually stored in the variable “q” in the previous question?

q holds the memory location of where the String “cat” is in RAM

 

10.   Given the code fragment above, is it correct to say that “q is a String object”?  Is it correct to say that “q refers to a String object”?  Is it correct to say that “x is a double”?  Is it correct to say that “x refers to a double”?

You should NOT say “q is a String”.  You SHOULD say “q refers to a String”.  It is correct to say “x is a double”, but you should NOT say “x refers to a double”, since x is not a reference variable.

 

11.  How many String objects are actually created (instantiated) in the code fragment above?

two

 

12.  After the code fragment above has been executed, decide which of the following expressions are true and which are false:

x == y       TRUE

q == r       TRUE

r == s       FALSE

q == s          FALSE

q.equals(r)     TRUE

r.equals(s)     TRUE

q.equals(s)     TRUE


13.  What is aliasing?

Aliasing is having more than one reference variable that refer to the same object (for example, r and q in the previous question.)

 

14.  What is meant by the term “garbage” in Java?  Write a code fragment that creates garbage.

Any object that is on the heap, but no longer referred to by any variable is called “garbage”.  Here is a short example:

String p = "junk";

p = "other";

 

(Now the String that says “junk” is garbage.)

 

15.  Consider the assignment statement:

x = y;

If the variables are of type “int”, does the integer get copied?  If the variables are of type “String”, does the String get copied?

For primitive types (like int), the data is copied.  For reference variables (like a String reference), the memory locations are copied, NOT the objects themselves.

 

16. Write a class called “Cat”.  Invent a few “instance variables” that make sense for a Cat.  Include a constructor that allows the user to specify values for all of the instance variables at the moment the Cat object is instantiated (created).  Include a toString method that produces a String representation of the state of the Cat.  Include an equals method that makes sense to you.  Write some other methods (behaviors) that Cats should be able to do.  Make sure that you have practiced some method(s) that require parameters to be passed in.  Make sure that you have practiced some methods that return a value.

 

ANSWERS WILL VARY

 

17.    Write a driver for the Cat class that you created above.  The driver should create a few Cats and then test out all of the methods that you have written.  Try to test everything thoroughly!

 

ANSWERS WILL VARY