Chapter 7


Exercise 1 Name four traits that arrays and objects have in common.

Solution 1 Any four of the following are acceptable answers:

  • They contain clusters of data.

  • They are created by invoking the keyword new.

  • They inhabit inaccessible memory.

  • They are manipulated indirectly, via references.

  • They cannot be passed as array method arguments, but references to them can.

  • They are not destroyed explicitly. They are garbage-collected when they have no more references.

Exercise 2 Name two differences between arrays and objects.

Solution 2 Any two of the following are acceptable answers:

  • They can contain data of different types.

  • They can contain methods as well as data.

  • They are related to classes.

Exercise 3 Objects are not passed as method arguments, but references to objects can be passed. When a reference is passed into a method, any changes made to the referenced object by the method should be visible to the method's caller. Write an application to demonstrate this.

Your application will have two classes: Cat and Ager. The Cat class should have a single variable: an int called age. The Ager class should have a method whose signature is makeOlder(Cat kitty, int nYears). This method should add nYears to the age of the Cat object referenced by kitty. Your main method should go in the Ager class. It should create one instance of each class, set the cat's age, and then use the Ager's method to change the age. Your main should then print out the cat's new age, and verify that it really changed.

Solution 3 In file Cat.java:

public class Cat {   int age; }

In file Ager.java:

public class Ager {     void makeOlder(Cat kitty, int nYears)     {         kitty.age += nYears;     }     public static void main(String[] args)     {         Ager myAger = new Ager();         Cat myCat = new Cat();         myCat.age = 5;         System.out.println("Age was " +                            myCat.age);         myAger.makeOlder(myCat, 2);         System.out.println("Age became " +                            myCat.age);     } } 

Exercise 4 What happens if you move the main method of the previous question from the Ager class to the Cat class?

Solution 4 You have to run the program by typing "java Cat" instead of "java Ager". Otherwise the output is the same. The point of this question is that in a multiple-class application, you have to decide where to put your main method.

Exercise 5 Write an application that causes a "null pointer exception" failure.

Solution 5 The following application causes a "null pointer exception" failure:

public class IFail {   int x;   public static void main(String[] args)   {     IFail ref = null;     ref.x = 10;   } }

This is just one of an infinite number of possible examples. When you write long programs, "null pointer exception" failures are unavoidable in the course of developing, debugging, and refining your code.

Exercise 6 What does the following application print out?

public class Question {   static long x;   public static void main(String[] args)   {     Question q1 = new Question();     Question q2 = new Question();     q1.x = 10;     q2.x = q1.x + 20;     System.out.println("q1.x = " + q1.x);   } }

Solution 6 The code prints out "q1.x = 30". Since x is static,"q1.x" and "q2.x" are both names for the same variable. The main method could be rewritten as

public static void main(String[] args) {   Question q1 = new Question();   Question q2 = new Question();   Question.x = 10;   Question.x = Question.x + 20;   System.out.println("q1.x = " + q1.x); }

This question points out that referring to a static variable via the class name is clearer than referring to it via a reference to an instance of the class.




Ground-Up Java
Ground-Up Java
ISBN: 0782141900
EAN: 2147483647
Year: 2005
Pages: 157
Authors: Philip Heller

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net