Best web hosting site - Chapter 8 Object-Based Programming 385 new object, and
Chapter 8 Object-Based Programming 385 new object, and that reference is assigned to time. Similarly, line 32 in class Time1 (Fig. 8.1) uses new to allocate the memory for a DecimalFormatobject, then calls the DecimalFormat constructor with the argument “00″ to indicate the number format control string. Software Engineering Observation 8.5 Every time new creates an object of a class, that class s constructor is called to initialize the instance variables of the new object. Note that class Time1 was not imported into the TimeTest1.java file. Actually, every class in Java is part of a package (like the classes from the Java API). If the programmer does not specify the package for a class, the class is automatically placed in the default package, which includes the compiled classes in the current directory. If a class is in the same package as the class that uses it, an import statement is not required. We import classes from the Java API because their .class files are not in the same package with each program we write. Section 8.5 illustrates how to define your own packages of classes for reuse. 1 // Fig. 8.2: TimeTest1.java 2 // Class TimeTest1 to exercise class Time1 3 4 // Java extension packages 5 import javax.swing.JOptionPane; 6 7 public class TimeTest1 { 8 9 // create Time1 object and manipulate it 10 public static void main( String args[] ) 11 { 12 Time1 time = new Time1(); // calls Time1 constructor 13 14 // append String version of time to String output 15 String output = “The initial universal time is: ” + 16 time.toUniversalString() + 17 “nThe initial standard time is: ” + time.toString() + 18 “nImplicit toString() call: ” + time; 19 20 // change time and append String version of time to output 21 time.setTime( 13, 27, 6 ); 22 output += “nnUniversal time after setTime is: ” + 23 time.toUniversalString() + 24 “nStandard time after setTime is: ” + time.toString(); 25 26 // use invalid values to change time and append String 27 // version of time to output 28 time.setTime( 99, 99, 99 ); 29 output += “nnAfter attempting invalid settings: ” + 30 “nUniversal time: ” + time.toUniversalString() + 31 “nStandard time: ” + time.toString(); 32 Fig. 8.2 Using an object of class Time1in a program (part 1 of 2). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01
Note: In case you are looking for affordable webhost to host and run your web application check Vision cheap hosting services