Chapter 8 Object-Based Programming (Web site domain) 409 Set methods are
Chapter 8 Object-Based Programming 409 Set methods are certainly important from a software engineering standpoint, because they can perform validity checking. Set and get methods have another important software engineering advantage, discussed in the following Software Engineering Observation. Software Engineering Observation 8.16 Accessing private data through set and get methods not only protects the instance variables from receiving invalid values, but also insulates clients of the class from the representation of the instance variables. Thus, if the representation of the data changes (typically, to reduce the amount of storage required, improve performance or enhance the class in other ways), only the method implementations need to change the clients need not change as long as the interface provided by the methods remains the same. 8.8.1 Executing an Applet that Uses Programmer-Defined Packages After compiling the classes in Fig. 8.8 and Fig. 8.9, you can execute the applet from a command window with the command appletviewer TimeTest5.html As we discussed when we introduced packages earlier in this chapter, the interpreter can locate packaged classes in the current directory. The appletviewer is a Java application that executes a Java applet. Like the interpreter, the appletviewer can load standard Java classes and extension classes installed on the local computer. However, the applet- viewer does not use the class path to locate classes in programmer-defined packages. For an applet, such classes should be bundled with the applet class in an archive file called a Java Archive (JAR) file. Remember that applets normally are downloaded from the Internet into a Web browser (see Chapter 3 for more information). Bundling the classes and packages that compose an applet enables the applet and its supporting classes to be downloaded as a unit, then executed in the browser (or via the Java Plug-in for browsers that do not support Java 2). To bundle the classes in Fig. 8.8 and Fig. 8.9, open a command window and change directories to the location in which TimeTest5.class is stored. In that same directory should be the com directory that begins the package directory structure for class Time3. In that directory, issue the following command jar cf TimeTest5.jar TimeTest5.class com*.* to create the JAR file. [Note: This command uses as the directory separator from the MSDOS prompt. UNIX would use / as the directory separator.] In the preceding command, jar is the Java archive utility used to create JAR files. Next are the options for the jar utility cf. The letter c indicates that we are creating a JAR file. The letter f indicates that the next argument in the command line (TimeTest5.jar) is the name of the JAR file to create. Following the options and JAR file name are the actual files that will be included in the JAR file. We specified TimeTest5.class and com*.*, indicating that TimeTest5.class and all the files in the com directory should be included in the JAR file. The com directory begins the package that contains the .class file for the Time3. [Note: You can include selected files by specifying the path and file name for each individ Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01