Suggested Projects


Skill-Building Exercises

  1. Question:   How many classes can a derived class extend? How many interfaces can a derived class implement?

  2. Question:   How many interfaces can a derived interface extend?

  3. API Familiarization:   Look up the Vector, Hashtable, and Enumeration classes in the Java API documentation and study their usage. This will help you to understand their usage in the aircraft engine simulation code presented in this chapter.

  4. Simple Inheritance:   Write a small program to test the effects of inheritance. Create a class named ClassA that implements the following methods: a(), b(), and c(). Each method should print a short text message to the screen. Create a default constructor for ClassA that prints a message to the screen announcing the creation of a ClassA object. Next, create a class named ClassB that extends ClassA. Give ClassB a default constructor that announces the creation of a ClassB object. In a test driver program create three references. Two of the references should be of type ClassA and the third should be of type ClassB. Initialize the first reference to point to a ClassA object, initialize the second reference to point to a ClassB object, and initialize the third reference to point to a ClassB object as well. Via each of the references call the methods a(), b(), and c(). Run the test driver program and note the results.

  5. Overriding Methods:   Reusing some of the code you created in the previous exercise create another class named ClassC that extends ClassA and provides overriding methods for each of ClassA’s methods a(), b(), and c(). Have each of the methods defined in ClassC print short messages to the screen. In the test driver program declare three references, the first two of type ClassA and the third of type ClassC. Initialize the first reference to point to an object of type ClassA, the second to point to an object of type ClassC, and the third to point to an object of ClassC as well. Via each of the references call the methods a(), b(), and c(). Run the test driver program and note the results.

  6. Abstract Classes:   Create an abstract class named AbstractClassA and give it a default constructor and three abstract methods named a(), b(), and c(). Create another class named ClassB that extends ClassA. Provide overriding methods for each of the abstract methods declared in ClassA. Each overriding method should print a short text message to the screen. Create a test driver program that declares two references. The first reference should be of type ClassA, the second reference should be of type ClassB. Initialize the first reference to point to an object of type ClassB, and the second reference to point to an object of ClassB as well. Via each reference call the methods a(), b(), and c(). Run the program and note the results.

  7. Interfaces:   Convert the abstract class you created in the previous exercise to an interface. What changes did you have to make to the code? Compile your interface and test driver program code, re-run the program, and note the results.

  8. Mental Exercise:   Consider the scenario then answer the questions that follow: Given an abstract base class named ClassOne with the following abstract public interface methods a(), b(), and c(). Given a class named ClassTwo that derives from ClassOne, provides implementations for each of ClassOne’s abstract methods, and defines one additional method named d(). Now, you have two references. One is of type ClassOne, the other of type ClassTwo. What methods can be called via the ClassOne reference without casting? Likewise, what methods can be called via the ClassTwo reference without casting?




Java For Artists(c) The Art, Philosophy, and Science of Object-Oriented Programming
Java For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
ISBN: 1932504052
EAN: 2147483647
Year: 2007
Pages: 452

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