Chapter 12


Exercise 1 In the beginning of Chapter 12, you learned that a good rule of thumb is to use core code when you can and develop original code when you must. Because Java is an object-oriented language, you have a third option, which combines reusing existing code with creating your own. You learned about this option in an earlier chapter. What is it?

Solution 1 The third option is to subclass an existing class. The subclass you create combines preexisting features inherited from the superclass with new features that you implement in the subclass.

Exercise 2 If you write code that calls a deprecated method of one of the core Java classes, what valuable feature of Java can you no longer rely on?

Solution 2 Backward compatibility.

Exercise 3 Suppose you are reading someone else's code and you come across the following lines:

Stack myStack = new Stack(); // java.util package myStack.setSize(100); 

You decide to look up setSize() in the APIs. The comment kindly tells you that class Stack is in package java.util, so you click on java.util in the packages frame, and then you click on Stack in the classes frame. You find yourself looking at the class description. You scroll down to the method summaries, and you don't see setSize anywhere.

How should you proceed?

Solution 3 There are two ways that class Stack can get a setSize() method: it can implement it, or it can inherit it. Clearly Stack doesn't implement setSize(), so it must inherit it.

Scroll down past the end of the method summary section, to the inherited method section. You will see a list of methods inherited from java.util.Vector, which is Stack's immediate superclass. There you will see a "setSize" link. Click on it to see the method description on the java.util.Vector page.

Alternately, you can scroll up to the top of the Stack description page to the inheritance hierarchy. There you will find a link to the Vector superclass. Scroll down to the method summaries, where you will find setSize().

Exercise 4 In the section on the String class, you learned about the startsWith(String s) method, which returns true if the executing string object begins with the argument string s. It stands to reason that there should be a similar method that tells you whether the executing string object ends with a specified string. Look at the API page for java.lang.String and see if such a method exists.

Solution 4 The method does exist. It is called endsWith().

Exercise 5 What happens when you try to compile and execute the following application?

public class Ch12Q5 {   public String toString()   {     return "I am an instance of Ch12Q5.";   }   public static void main(String[] args)   {     Ch12Q5 thing = new Ch12Q5();     System.out.println(thing);   } }

Solution 5 The program compiles and executes without error. The call to System.out.println() calls toString() on thing, so the output is

I am an instance of Ch12Q6. 

Exercise 6 What happens when you try to compile and execute the following application?

class Ch12Q6 {   String toString()   {     return "I am an instance of Ch12Q6.";   }   public static void main(String[] args)   {     Ch12Q6 thing = new Ch12Q6();     System.out.println(thing);   } }

Solution 6 The difference between this application and the one in Exercise 5 is that the "public" modifiers have been removed from the declarations of the class and the toString() method. Now the code won't compile, because toString() is public in class Object, which is the superclass of Ch12Q6. If you override a method, as you have done here with toString(), it is illegal to give the subclass version weaker access than the superclass version.

Exercise 7 Look up the explanation of the equals() method on the API page of class java.lang.Object. The explanation is a bit wordy, but see if you can figure out what it does. (Focus on the last sentence, just before the "Parameters" section.) What is the technical term for what the method does? (Hint: It was introduced in Chapter 12.)

Solution 7 The method checks for reference equality. This isn't so useful, because equals() is supposed to check for object equality. No wonder subclasses of Object override equals().

Exercise 8 You're not allowed to construct an instance of the java.lang.Math class. What happens if you try?

Solution 8 If you write a program that contains the line

Math m = new Math();

you will get an error message that says something like

…constructor Math() has private access in class java.lang.Math.

The constructor for the java.lang.Math is private. This means that the constructor can be invoked only from within the class itself. This is how the class ensures that you and I can never write code that constructs a Math instance.

Exercise 9 The following code models the behavior of a familiar piece of equipment that is used in many games throughout the world. What is the piece of equipment?

long rand =  1 + Math.round(Math.random() * 5);

Solution 9 The code generates a random int that is >=1 and <=6, so it simulates shaking dice.




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