CIS 17: Java Programming
Project C
Objective:
To investigate the cleanup of objects and the creation of libraries.
Using finalize() to verify a termination condition
- Create a public class called Tank that can be instantiated either full or empty and has a termination condition that it must be empty when the object is cleaned up.
- Include a static int objectCount field so that each Tank can be identified by a unique int id field, resulting from incrementing the objectCount field upon instantiation.
- Write a finalize() method that verifies this termination condition by reporting the status (empty or full) and identifying the Tank.
- In main(), test the possible scenarios that can occur when your Tanks are created and ultimately terminated (garbage collected.) Instantiate your Tanks without creating references to them; in that way you can force calls to finalize() by invoking System.gc(). If they have references in scope, they won't be garbage collected.
Creating a Java library or package
- Create a directory, tools, and edit your CLASSPATH environment variable to include the parent of that new directory. Don't forget to include the current directory (.) also.
- Place the source for the following class in an appropriately-named file in the new directory.
package tools;
public class Prin {
public static void t(String s) {
System.out.print(s); }
public static void tln(String s) {
System.out.println(s); }
}
- In any other directory, create a program whose public class is named PrinTest that imports the tools library and tests its methods.
Submit your work via the CATE form for Project C.
Legend: method/function keyword literal
2007/02/27