Web design portfolio - 286 Object-Based Programming Chapter 8 Lines 15 18 define

286 Object-Based Programming Chapter 8 Lines 15 18 define the constructor of class Time1. A class s constructor initializes objects of that class. When a program creates an object of class Time1 with operator new, the constructor automatically is called to initialize the object. Class Time1 s constructor calls method SetTime (lines 22 32) to initialize instance variables hour, minute and secondto 0(representing midnight). Constructors can take arguments, but cannot return values. As we will see, a class can have overloaded constructors. An important difference between constructors and other methods is that constructors cannot specify a return type. Generally, constructors are declared public. Note that the constructor name must be the same as the class name. Common Programming Error 8.1 Attempting to return a value from a constructor is a syntax error. Method SetTime (lines 22 32) is a public method that receives three int parameters and uses them to set the time. A conditional expression tests each argument to determine whether the value is in a specified range. For example, the hour value must be greater than or equal to 0 and less than 24, because universal-time format represents hours as integers from 0 to 23. Similarly, both minute and second values must be greater than or equal to 0 and less than 60. Any values outside these ranges are invalid values and default to zero. Setting invalid values to zero ensures that a Time1 object always contains valid data (because, in this example, zero is a valid value for hour, minute and second). When users supply invalid data to SetTime, the program might want to indicate that the time was invalid. In Chapter 11, we discuss exception handling, which can be used to indicate invalid initialization values. Software Engineering Observation 8.5 Always define a class so that each of its instance variables always contains valid values. Method ToUniversalString (lines 35 39) takes no arguments and returns a string in universal-time format, consisting of six digits two for the hour, two for the minute and two for the second. For example, if the time were 1:30:07 PM, method ToUniversalString would return 13:30:07. Lines 37 38 use String method Format to configure the universal time string. Line 37 passes to Format the format string “{0:D2}:{1:D2}:{2:D2}”, which contains several format specifications indicating that arguments 0, 1 and 2 (the first three arguments after the format string argument) should each have the format D2 (a two-digit base 10 decimal number format) for display purposes. The D2 format specification causes single-digit values to appear as two digits with a leading 0 (e.g., 8 would be represented as 08). The two colons that separate the curly braces } and { are the colons that separate the hour from the minute and the minute from the second in the resulting string. Method ToStandardString (lines 42 47) takes no arguments and returns a string in standard-time format, consisting of the hour, minute and second values separated by colons and followed by an AM or a PM indicator (e.g., 1:27:06PM). Like method ToUniversalString, method ToStandardString uses String method Format to format the minute and second as two-digit values with leading zeros if necessary. Line 45 determines the value for hour in the string if the hour is 0 or 12 (AM or PM), the hour appears as 12; otherwise, the hour appears as a value from 1 11.
You want to have a cheap webhost for your apache application, then check apache web hosting services.

Leave a Reply