Exercises


Note

The solutions to these exercises are in Appendix B.

  1. In the beginning of this chapter 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?

  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?

  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?

  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.

  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);   } }
  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);   } } 
  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 this chapter.)

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

  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);




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