Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com
Answertopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

Thinking in Java
Prev Contents / Index Next

Exercises

Solutions to selected exercises can be found in the electronic document The Thinking in Java Annotated Solution Guide, available for a small fee from www.BruceEckel.com.

  1. Create a class with a default constructor (one that takes no arguments) that prints a message. Create an object of this class.
  2. Add an overloaded constructor to Exercise 1 that takes a String argument and prints it along with your message.
  3. Create an array of object references of the class you created in Exercise 2, but don’t actually create objects to assign into the array. When you run the program, notice whether the initialization messages from the constructor calls are printed.
  4. Complete Exercise 3 by creating objects to attach to the array of references.
  5. Create an array of String objects and assign a string to each element. Print the array by using a for loop.
  6. Create a class called Dog with an overloaded bark( ) method. This method should be overloaded based on various primitive data types, and print different types of barking, howling, etc., depending on which overloaded version is called. Write a main( ) that calls all the different versions.
  7. Modify Exercise 6 so that two of the overloaded methods have two arguments (of two different types), but in reversed order relative to each other. Verify that this works.
  8. Create a class without a constructor, and then create an object of that class in main( ) to verify that the default constructor is automatically synthesized.
  9. Create a class with two methods. Within the first method, call the second method twice: the first time without using this, and the second time using this.
  10. Create a class with two (overloaded) constructors. Using this, call the second constructor inside the first one.
  11. Create a class with a finalize( ) method that prints a message. In main( ), create an object of your class. Explain the behavior of your program.
  12. Modify Exercise 11 so that your finalize( ) will always be called.
  13. Create a class called Tank that can be filled and emptied, and has a termination condition that it must be empty when the object is cleaned up. Write a finalize( ) that verifies this termination condition. In main( ), test the possible scenarios that can occur when your Tank is used.
  14. Create a class containing an int and a char that are not initialized, and print their values to verify that Java performs default initialization.
  15. Create a class containing an uninitialized String reference. Demonstrate that this reference is initialized by Java to null. Create a class with a String field that is initialized at the point of definition, and another one that is initialized by the constructor. What is the difference between the two approaches?
  16. Create a class with a static String field that is initialized at the point of definition, and another one that is initialized by the static block. Add a static method that prints both fields and demonstrates that they are both initialized before they are used.
  17. Create a class with a String that is initialized using “instance initialization.” Describe a use for this feature (other than the one specified in this book).
  18. Write a method that creates and initializes a two-dimensional array of double. The size of the array is determined by the arguments of the method, and the initialization values are a range determined by beginning and ending values that are also arguments of the method. Create a second method that will print the array generated by the first method. In main( ) test the methods by creating and printing several different sizes of arrays. Repeat Exercise 19 for a three-dimensional array.
  19. Comment the line marked (1) in ExplicitStatic.java and verify that the static initialization clause is not called. Now uncomment one of the lines marked (2) and verify that the static initialization clause is called. Now uncomment the other line marked (2) and verify that static initialization only occurs once. href="TIJ306_001.htm">[19] In some of the Java literature from Sun, they instead refer to these with the awkward but descriptive name “no-arg constructors.” The term “default constructor” has been in use for many years, so I will use that.

    [20] Some people will obsessively put this in front of every method call and field reference, arguing that it makes it “clearer and more explicit.” Don’t do it. There’s a reason that we use high-level languages: they do things for us. If you put this in when it’s not necessary, you will confuse and annoy everyone who reads your code, since all the rest of the code they’ve read won’t use this everywhere. Following a consistent and straightforward coding style saves time and money.

    [21] The one case in which this is possible occurs if you pass a reference to an object into the static method. Then, via the reference (which is now effectively this), you can call non-static methods and access non-static fields. But typically, if you want to do something like this, you’ll just make an ordinary, non-static method.

    [22] Joshua Bloch goes further in his section titled “avoid finalizers”: “Finalizers are unpredictable, often dangerous, and generally unnecessary.” Effective Java, page 20 (Addison-Wesley 2001).

    [23] A term coined by Bill Venners (www.artima.com) during a seminar that he and I were giving together.

    [24] In contrast, C++ has the constructor initializer list that causes initialization to occur before entering the constructor body, and is enforced for objects. See Thinking in C++, 2nd edition (available on this book’s CD ROM and at www.BruceEckel.com).

    [25] See Thinking in C++, 2nd edition for a complete description of C++ aggregate initialization.

    Thinking in Java
    Prev Contents / Index Next

 
 
   Reproduced courtesy of Bruce Eckel, MindView, Inc. Design by Interspire