Self-Test Questions


Suggested Projects

  1. Improve The PeopleManager.addPerson() Method:   Improve the functionality of the addPerson() method of the PeopleManager class presented in this chapter. In its current state the addPerson() method only creates a new Person object and assigns the reference to the array element if the array element is null. Otherwise it does nothing and gives no indication that the creation and insertion of a new Person object failed. Make the following modifications to the addPerson() method:

    1. Search the people_array for a null element and insert the new person at that element.

    2. If the array is full create a new array that’s 1.5 times the size of the current people_array and then insert the new Person reference at that element.

    3. Have the addPerson() method return a boolean value indicating success or failure of the new Person object creation and insertion operation.

  1. Write A Submarine Commander Program:   Using the results of the problem abstraction performed in skill-building exercise 5 write a program that lets you create a fleet of nuclear submarines. You should be able to add submarines to the fleet, remove them from the fleet, and list all the submarines in your fleet. You will want to power-up their nuclear reactors and shut down their nuclear reactors. You will also want to fire their weapons. To keep this programming exercise manageable just write simple messages to the console in response to commands sent to each submarine object.

  2. Write A Gas Pump Operation Program:   Using the results of the analysis you performed in skill-building exercise 10 write a program that lets you control the operation of a gas pump. You should be able to turn the gas pump on and off. You should only be able to pump gas when the pump is on. When you are done pumping gas indicate how much gas was pumped in gallons or liters and give the total price of the gas pumped. Provide a way to set the price of gas.

  3. Write A Calculator Program:   Write a program that implements the functionality of a simple calculator. The focus of this project should be the creation of a class that performs the calculator’s operations. Give the Calculator class the ability to add, subtract, multiply, and divide integers and floating point numbers. Some of the Calculator class methods may need to be overloaded to handle different types of arguments.

  4. Write A Library Manager Program:   Write a program that lets you catalog the books in your personal library. The Book class should have the following attributes: title, author, and ISBN. You can add any other attributes you deem necessary. Create a class named LibraryManager that can create and add books to your library, delete books from your library, and list the book in your library. Use an array to hold the books in your library. Research sorting routines and implement a sortBooks() method.

  5. Write A Linked-List Program:   A special property of Java classes is that the name of the class you are defining can be used to declare fields within that class. Consider the following code example:

Here, the class name Node appears in the body of the Node class definition to declare two Node references named previous and next. This technique is used to create data structures designed for use within a linked list. Use the code shown in example 9.20 to help you write a program that manages a linked list. Here are a few hints to get you started:

Example 9.20: Node.java (Partial Listing)

image from book
 1     public class Node { 2       private Node previous = null; 3       private Node next = null; 4       private Object payload = null; 5 6       // methods omitted for now 7     }
image from book

Referring to figure 9-14 above — a linked list is comprised of a number of nodes and a head and a tail. The head points to the first node in the list. The tail always points to the last node in the list. Each node has a next and previous attribute along with a payload. Figure 9-14 shows a linked list having three nodes. The first node element’s next attribute points to the second node element, and the second node element’s next attribute points to the third node element. The third node element is the last node in the list and its next attribute points to the head, which always points to the first node in the list. Each node’s previous attribute works in the opposite fashion.

image from book
Figure 9-14: Linked List with Three Nodes

Because each node has a next and a previous attribute they can be used to create circular linked list as is shown in figure 9-14.

For this project write a linked list manager program that lets you add, delete, and list the contents of each node in the list. You will have to add methods to the Node class code given in example 9.20. At a minimum you should add getter and setter methods for each field.

This is a challenging project and will require you to put some thought into the design of both the Node and the LinkedListManager class. The most complicated part of the design will be figuring out how to insert and delete nodes into and from the list. When you successfully complete this project you will have a good, practical understanding of references and how they are related to pointers in other programming languages.

  1. Convert The Library Manager To Use A Linked List:   Rewrite the library manager program presented in suggested project 6 to use a linked list of books instead of an array.




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