Archive for October, 2007

298 Object-Based Programming Chapter 8 Providing set and (Make web site)

Monday, October 22nd, 2007

298 Object-Based Programming Chapter 8 Providing set and get capabilities appears to be the same as making the instance variables public. However, this is another one of C# s subtleties that makes the language so attractive from a software-engineering standpoint. If an instance variable is public, the instance variable can be read or written to by any method in the program. If an instance variable is private, a public get accessor seems to allow other methods to read the data at will. However, the getaccessor can control the formatting and display of the data. Similarly, a public set accessor can scrutinize attempts to modify the instance variable s value, thus ensuring that the new value is appropriate for that data member. For example, an attempt to setthe day of the month to 37 would be rejected, and an attempt to seta person s weight to a negative value would be rejected. So, setand getaccessors can provide access to private data, but the implementation of these accessors controls what the client code can do to the data. The declaration of instance variables as privatedoes not guarantee their integrity. Programmers must provide validity checking C# provides only the framework with which programmers can design better programs. Testing and Debugging Tip 8.1 Methods that set the values of private data should verify that the intended new values are valid; if they are not, the set accessors should place the private instance variables into an appropriate consistent state. The set accessors of a property cannot return values indicating a failed attempt to assign invalid data to objects of the class. Such return values could be useful to a client of a class when handling errors. The client could take appropriate actions if the objects occupy invalid states. Chapter 11 presents exception handling a mechanism that can be used to indicate attempts to set an object s members to invalid values. Figure 8.6 enhances our Timeclass, now called Time3, to include properties for the private instance variables hour, minute and second. The set accessors of these properties strictly control the setting of the instance variables to valid values. An attempt to set any instance variable to an incorrect value causes the instance variable to be set to zero (thus leaving the instance variable in a consistent state). Each get accessor returns the appropriate instance variable s value. This application also introduces enhanced GUI event-handling techniques, as we define a GUI (Fig. 8.7) that includes several buttons the user can click to manipulate the time stored in a Time3object. 1 // Fig. 8.6: Time3.cs 2 // Class Time2 provides overloaded constructors. 3 4 using System; 5 6 // Time3 class definition 7 public class Time3 8 { 9 private int hour; // 0-23 10 private int minute; // 0-59 11 private int second; // 0-59 12 Fig. 8.6 Fig. 8.Fig.Fi 8.6g. 8.66Properties provide controlled access to an object s data. (Part 1 of 3.) Fig. 8.
Go visit our java server pages services for a reliable, lowcost webhost to satisfy all your needs.

Chapter 8 Object-Based (Best web hosting site) Programming 297 Fig. 8.5 Fig.

Sunday, October 21st, 2007

Chapter 8 Object-Based Programming 297 Fig. 8.5 Fig. 8.Fig.Fi 8.5g. 8.55Overloaded constructor demonstration. (Part 3 of 3.) Fig. 8. Each Time2constructor can be written to include a copy of the appropriate statements from method SetTime. This might be slightly more efficient, because it eliminates the extra call to SetTime. However, consider what would happen if the programmer were to change the representation of the time from three int values (requiring 12 bytes of memory) to a single intvalue representing the total number of seconds that have elapsed in the day (requiring 4 bytes of memory). Placing identical code in the Time2constructors and method SetTimemakes such a change in the class definition more difficult, because every constructor s body would require modifications to manipulate the data as a single int rather than three ints. If the Time2 constructors call SetTime directly, any changes to the implementation of SetTimemust be made only once, in the body of Set- Time. This reduces the likelihood of introducing a programming error when altering the implementation, because we make only one change in the class, rather than changing every constructor and method SetTime. Software Engineering Observation 8.14 If a method of a class provides functionality required by a constructor (or other method) of the class, call that method from the constructor (or other method). This simplifies the maintenance of the code and reduces the likelihood of introducing errors into the code. 8.7 Properties Methods of a class can manipulate that class s privateinstance variables. A typical manipulation might be the adjustment of a customer s bank balance a private instance variable of a class BankAccount by a ComputeInterestmethod. Classes often provide publicproperties to allow clients to set (i.e., assign values to) or get (i.e., obtain the values of) privateinstance variables. For example, in Fig. 8.6, we create three properties Hour, Minute and Second which access variables hour, minute and second, respectively. Each property contains a get accessor (to retrieve the field value) and a set accessor (to modify the field value).
Check Tomcat Web Hosting services for best quality webspace to host your web application.

296 Object-Based Programming Chapter (Free web hosting with ftp) 8 4 using System;

Saturday, October 20th, 2007

296 Object-Based Programming Chapter 8 4 using System; using System.Windows.Forms; 6 7 // TimeTest2 demonstrates constructors of class Time2 8 class TimeTest2 9 { // main entry point for application 11 static void Main( string[] args ) 12 { 13 Time2 time1, time2, time3, time4, time5, time6; 14 time1 = new Time2(); // 00:00:00 16 time2 = new Time2( 2 ); // 02:00:00 17 time3 = new Time2( 21, 34 ); // 21:34:00 18 time4 = new Time2( 12, 25, 42 ); // 12:25:42 19 time5 = new Time2( 27, 74, 99 ); // 00:00:00 time6 = new Time2( time4 ); // 12:25:42 21 22 String output = “Constructed with: ” + 23 “ntime1: all arguments defaulted” + 24 “nt” + time1.ToUniversalString() + “nt” + time1.ToStandardString(); 26 27 output += “ntime2: hour specified; minute and ” + 28 “second defaulted” + 29 “nt” + time2.ToUniversalString() + “nt” + time2.ToStandardString(); 31 32 output += “ntime3: hour and minute specified; ” + 33 “second defaulted” + 34 “nt” + time3.ToUniversalString() + “nt” + time3.ToStandardString(); 36 37 output += “ntime4: hour, minute, and second specified” + 38 “nt” + time4.ToUniversalString() + 39 “nt” + time4.ToStandardString(); 41 output += “ntime5: all invalid values specified” + 42 “nt” + time5.ToUniversalString() + 43 “nt” + time5.ToStandardString(); 44 output += “ntime6: Time2 object time4 specified” + 46 “nt” + time6.ToUniversalString() + 47 “nt” + time6.ToStandardString(); 48 49 MessageBox.Show( output, “Demonstrating Overloaded Constructors” ); 51 52 } // end method Main 53 54 } // end class TimeTest2 Fig. 8.5 Fig. 8.Fig.Fi 8.5g. 8.55Overloaded constructor demonstration. (Part 2 of 3.) Fig. 8.
Please visit Domain Name Hosting services for high quality webhost to host and run your jsp applications.

Chapter 8 Object-Based Programming 295 ment constructor that (Web hosting comparison)

Friday, October 19th, 2007

Chapter 8 Object-Based Programming 295 ment constructor that sets the time to midnight. Lines 22 25 define a Time2constructor that receives a single intargument representing the hourand sets the time using the specified hour value and zero for the minute and second. Lines 29 32 define a Time2 constructor that receives two intarguments representing the hourand minuteand sets the time using those values and zero for the second. Lines 35 38 define a Time2 constructor that receives three intarguments representing the hour, minuteand second and uses those values to set the time. Lines 41 44 define a Time2constructor that receives a reference to another Time2object. When this last constructor is called, the values from the Time2argument are used to initialize the hour, minuteand secondvalues of the new Time2 object. Even though class Time2declares hour, minuteand second as private(lines 9 11), the Time2constructor can access these values in its Time2argument directly using the expressions time.hour, time.minuteand time.second. Software Engineering Observation 8.13 When one object of a class has a reference to another object of the same class, the first object can access all the second object s data and methods (including those that are private). Notice that the second, third and fourth constructors (lines 22, 29 and 35) have some arguments in common and that those arguments are kept in the same order. For instance, the constructor that begins on line 29 has as its two arguments an integer representing the hour and an integer representing the minute. The constructor on line 35 has these same two arguments in the same order, followed by its last argument (an integer representing the second). Good Programming Practice 8.5 When defining overloaded constructors, keep the order of arguments as similar as possible; this makes client programming easier. Constructors do not specify return types; doing so results in syntax errors. Also, notice that each constructor receives a different number or different types of arguments. Even though only two of the constructors receive values for the hour, minute and second, each constructor calls SetTime with values for hour, minute and second and uses zeros for the missing values to satisfy SetTime s requirement of three arguments. Class TimeTest2(Fig. 8.5) starts the application that demonstrates the use of overloaded constructors (Fig. 8.4). Lines 15 20 create six Time2 objects that invoke various constructors of the class. Line 15 invokes the no-argument constructor by placing an empty set of parentheses after the class name. Lines 16 20 invoke the Time2 constructors that receive arguments. To invoke the appropriate constructor, pass the proper number, types and order of arguments (specified by the constructor s definition) to that constructor. For example, line 16 invokes the constructor that is defined in lines 22 25 of Fig. 8.4. Lines 22 47 invoke methods ToUniversalString and ToStandardString for each Time2object to demonstrate that the constructors initialize the objects correctly. 1 // Fig. 8.5: TimeTest2.cs 2 // Using overloaded constructors. 3 Fig. 8.5 Fig. 8.Fig.Fi 8.5g. 8.55Overloaded constructor demonstration. (Part 1 of 3.) Fig. 8.
From our experience, we can recommend PHP Web Hosting services, if you need affordable webhost to host and run your web application.

Free php web host - 294 Object-Based Programming Chapter 8 26 27 //

Friday, October 19th, 2007

294 Object-Based Programming Chapter 8 26 27 // Time2 constructor: hour and minute supplied, second 28 // defaulted to 0 29 public Time2( int hour, int minute ) 30 { 31 SetTime( hour, minute, 0 ); 32 } 33 34 // Time2 constructor: hour, minute and second supplied 35 public Time2( int hour, int minute, int second ) 36 { 37 SetTime( hour, minute, second ); 38 } 39 40 // Time2 constructor: initialize using another Time2 object 41 public Time2( Time2 time ) 42 { 43 SetTime( time.hour, time.minute, time.second ); 44 } 45 46 // Set new time value in 24-hour format. Perform validity 47 // checks on the data. Set invalid values to zero. 48 public void SetTime( 49 int hourValue, int minuteValue, int secondValue ) 50 { 51 hour = ( hourValue >= 0 && hourValue < 24 ) ? 52 hourValue : 0; 53 minute = ( minuteValue >= 0 && minuteValue < 60 ) ? 54 minuteValue : 0; 55 second = ( secondValue >= 0 && secondValue < 60 ) ? 56 secondValue : 0; 57 } 58 59 // convert time to universal-time (24 hour) format string 60 public string ToUniversalString() 61 { 62 return String.Format( 63 "{0:D2}:{1:D2}:{2:D2}", hour, minute, second ); 64 } 65 66 // convert time to standard-time (12 hour) format string 67 public string ToStandardString() 68 { 69 return String.Format( "{0}:{1:D2}:{2:D2} {3}", 70 ( ( hour == 12 || hour == 0 ) ? 12 : hour % 12 ), 71 minute, second, ( hour < 12 ? "AM" : "PM" ) ); 72 } 73 74 } // end class Time2 Fig. 8.4 Fig. 8.Fig.Fi 8.4g. 8.44Overloaded constructors provide flexible object-initialization options. (Part Fig. 8. 2 of 2.) Because most of the code in class Time2is identical to that in class Time1, this discussion concentrates only on the overloaded constructors. Lines 15 18 define the no-argu
Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision J2ee Web Hosting services.

My web site - Chapter 8 Object-Based Programming 293 If a class

Thursday, October 18th, 2007

Chapter 8 Object-Based Programming 293 If a class does not define any constructors, the compiler provides a default (no-argument) constructor. This compiler-provided default constructor contains no code (i.e., the constructor has an empty body) and takes no parameters. The programmer also can provide a default constructor, as we demonstrated in class Time1(Fig. 8.1). Programmer-provided default constructors can have code in their bodies. Common Programming Error 8.3 If a class has constructors, but none of the public constructors is a default constructor, and a program attempts to call a no-argument constructor to initialize an object of the class, a compilation error occurs. A constructor can be called with no arguments only if there are no constructors for the class (in which case the compiler-provided default constructor is called) or if the class defines a public no-argument constructor. 8.6 Using Overloaded Constructors Like methods, constructors of a class can be overloaded. The Time1constructor in Fig. 8.1 initialized hour, minuteand secondto 0(i.e., 12 midnight in universal time) via a call to the class SetTimemethod. However, class Time2(Fig. 8.4) overloads the constructor to provide a variety of ways to initialize Time2 objects. Each constructor calls Time2 method SetTime, which ensures that the object begins in a consistent state by setting outof- range values to zero. C# invokes the appropriate constructor by matching the number, types and order of the arguments specified in the constructor call with the number, types and order of the parameters specified in each constructor definition. 1 // Fig. 8.4: Time2.cs 2 // Class Time2 provides overloaded constructors. 3 4 using System; 5 6 // Time2 class definition 7 public class Time2 8 { 9 private int hour; // 0-23 10 private int minute; // 0-59 11 private int second; // 0-59 12 13 // Time2 constructor initializes instance variables to 14 // zero to set default time to midnight 15 public Time2() 16 { 17 SetTime( 0, 0, 0 ); 18 } 19 20 // Time2 constructor: hour supplied, minute and second 21 // defaulted to 0 22 public Time2( int hour ) 23 { 24 SetTime( hour, 0, 0 ); 25 } Fig. 8.4 Fig. 8.Fig.Fi 8.4g. 8.44Overloaded constructors provide flexible object-initialization options. (Part Fig. 8. 1 of 2.)
We would like to recommend you tested and proved virtual web hosting services, which you will surely find to be of great quality.

292 Object-Based Programming Chapter 8 Fig. 8.3 Fig.

Wednesday, October 17th, 2007

292 Object-Based Programming Chapter 8 Fig. 8.3 Fig. 8.3Fig. 8.FiFi3g. 8.3g. 8.3Accessing privateclass members from client code generates syntax errors. (Part 2 of 2.) Software Engineering Observation 8.10 Class designers need not provide set or get accessors for each private data member; these capabilities should be provided only when doing so makes sense. Software Engineering Observation 8.11 Declaring the instance variables of a class as private and the methods and properties of the class as public facilitates debugging, because problems with data manipulations are localized to the class methods that manipulate that data. 8.5 Initializing Class Objects: Constructors When a program creates an instance of a class, the program invokes the class s constructor to initialize the class s instance variables (data members). A class can contain overloaded constructors to provide multiple ways to initialize objects of that class. Instance variables can be initialized either by a constructor or when they are declared in the class body. Regardless of whether instance variables receive explicit initialization values, the instance variables always are initialized. In such cases, instance variables receive their default values (0 for primitive numeric type variables, falsefor boolvariable and nullfor references). Performance Tip 8.1 Because instance variables always are initialized to default values by the runtime, avoid initializing instance variables to their default values in the constructor. Software Engineering Observation 8.12 When appropriate, provide a constructor to ensure that every object is initialized with meaningful values. When creating an object of a class, the programmer can provide initializers in parentheses to the right of the class name. These initializers are the arguments to the constructor. In general, declarations take the form: ClassName objectReference =newClassName(arguments ); where objectReference is a reference of the appropriate data type, newindicates that an object is being created, ClassName indicates the type of the new object (and the name of the constructor being called) and arguments specifies a comma-separated list of the values used by the constructor to initialize the object. Figure 8.4 demonstrates using initializers and overloaded constructors.
From our experience, we can recommend PHP Web Hosting services, if you need affordable webhost to host and run your web application.

Chapter 8 (Web hosting account) Object-Based Programming 291 As previously stated,

Tuesday, October 16th, 2007

Chapter 8 Object-Based Programming 291 As previously stated, publicmethods present to the class s clients a view of the services that the class provides (i.e., the publicinterface of the class). Previously, we mentioned the merits of writing methods that perform only one task. If a method must execute other tasks to calculate its final result, these tasks should be performed by a helper method. A client does not need to call these helper methods, nor does it need to be concerned with how the class uses its helper methods. For these reasons, helper methods are declared as privatemembers of a class. Common Programming Error 8.2 Attempting to access a private class member from outside that class is a compiler error. The application of Fig. 8.3 (which uses the Time1 class from Fig. 8.1) demonstrates that privateclass members are not accessible outside the class. Lines 12 14 attempt to access the privateinstance variables hour, minuteand secondof the Time1object to which timerefers. When this program is compiled, the compiler generates errors stating that the privatemembers hour, minuteand secondare not accessible. Access to privatedata should be controlled carefully by a class s methods. To allow clients to read the values of private data, the class can define a property that enables client code to access this private data safely. Properties, which we discuss in detail in Section 8.7, contain accessor methods that handle the details of modifying and returning data. A property definition can contain a get accessor, a set accessor or both. A get accessor enables a client to read a privatedata value; a setaccessor enables the client to modify that value. Such modification would seem to violate the notion of private data. However, a set accessor can provide data-validation capabilities (such as range checking) to ensure that the value is set properly. A set accessor also can translate between the format of the data used in the interface and the format used in the underlying implementation. Similarly, a get accessor need not expose the data in raw format; rather, the getaccessor can edit the data and limit the client s view of that data. 1 // Fig. 8.3: RestrictedAccess.cs 2 // Demonstrate compiler errors from attempt to access 3 // private class members. 4 5 class RestrictedAccess 6 { 7 // main entry point for application 8 static void Main( string[] args ) 9 { 10 Time1 time = new Time1(); 11 12 time.hour = 7; 13 time.minute = 15; 14 time.second = 30; 15 } 16 17 } // end class RestrictedAccess Fig. 8.3 Fig. 8.3Fig. 8.FiFi3g. 8.3g. 8.3Accessing privateclass members from client code generates syntax errors. (Part 1 of 2.)
Please visit our professional web hosting services to find out about cheap and reliable webhost service that will surely answer all your demands.

My space web page - 290 Object-Based Programming Chapter 8 conventional function calls

Monday, October 15th, 2007

290 Object-Based Programming Chapter 8 conventional function calls in procedural programming langauages. It also reduces the likelihood of passing the wrong arguments, the wrong types of arguments or the wrong number of arguments. Software Engineering Observation 8.9 The use of an object-oriented programming approach often simplifies method calls by reducing the number of parameters that must be passed. This benefit of object-oriented programming derives from the fact that encapsulation of instance variables and methods within an object gives the object s methods the right to access the object s instance variables. Classes simplify programming, because the client need be concerned only with the publicoperations encapsulated in the object. Usually, such operations are designed to be client-oriented, rather than implementation-oriented. Clients are neither aware of, nor involved in, a class s implementation. Interfaces change less frequently than do implementations. When an implementation changes, implementation-dependent code must change accordingly. By hiding the implementation, we eliminate the possibility that other program parts will become dependent on the class-implementation details. Often, programmers do not have to create classes from scratch. Rather, they can derive classes from other classes that provide behaviors required by the new classes. Classes also can include references to objects of other classes as members. Such software reuse can greatly enhance programmer productivity. Chapter 9 discusses inheritance the process by which new classes are derived from existing classes. Section 8.8 discusses composition (or aggregation), in which classes include as members references to objects of other classes. 8.3 Class Scope In Section 6.13, we discussed method scope; now, we discuss class scope. A class s instance variables and methods belong to that class s scope. Within a class s scope, class members are immediately accessible to all of that class s methods and can be referenced by name. Outside a class s scope, class members cannot be referenced directly by name. Those class members that are visible (such as public members) can be accessed only through a handle (i.e., members can be referenced via the format referenceName.memberName). If a variable is defined in a method, only that method can access the variable (i.e., the variable is a local variable of that method). Such variables are said to have block scope. If a method defines a variable that has the same name as a variable with class scope (i.e., an instance variable), the method-scope variable hides the class-scope variable in that method s scope. A hidden instance variable can be accessed in a method by preceding its name with the keyword this and the dot operator, as in this.hour. We discuss keyword this in Section 8.9. 8.4 Controlling Access to Members The member access modifiers public and private control access to a class s data and methods. (In Chapter 9, we introduce the additional access modifiers protected and internal.)
We recommend cheap and reliable webhost to host and run your web applications: Coldfusion Web Hosting services.

Chapter 8 Object-Based Programming (Web hosting e commerce) 289 Line 23 sets

Sunday, October 14th, 2007

Chapter 8 Object-Based Programming 289 Line 23 sets the time for the Time1 object to which time refers by passing valid hour, minute and second arguments to Time1 method SetTime. Lines 26 29 append to outputthe new time in both universal and standard formats to confirm that the time was set correctly. To illustrate that method SetTime validates the values passed to it, line 32 passes invalid time arguments to method SetTime. Lines 34 36 append to output the new time in both formats. All three values passed to SetTime are invalid, so instance variables hour, minute and second are set to 0. Line 38 displays a MessageBox with the results of our program. Notice in the last two lines of the output window that the time was indeed set to midnight when invalid arguments were passed to SetTime. Time1 is our first example of a class that does not contain method Main. Thus, class Time1 cannot be used to begin program execution. Class TimeTest1 defines a Main method, so class TimeTest1 can be used to begin program execution. A class containing method Main also is known as the entry point into the program. Note that the program declares instance variables hour, minute and second as private. Such instance variables are not accessible outside the class in which they are defined. A class s clients should not be concerned with the data representation of that class. Clients of a class should be interested only in the services provided by that class. For example, the class could represent the time internally as the number of seconds that have elapsed since the previous midnight. Suppose the data representation changes. Clients still are able to use the same public methods and obtain the same results without being aware of the change in internal representation. In this sense, the implementation of a class is said to be hidden from its clients. Software Engineering Observation 8.7 Information hiding promotes program modifiability and simplifies the client s perception of a class. Software Engineering Observation 8.8 Clients of a class can (and should) use the class without knowing the internal details of how the class is implemented. If the class implementation changes (to improve performance, for example), but the class interface remains constant, the client s source code need not change. This makes it much easier to modify systems. In this program, the Time1 constructor initializes the instance variables to 0 (the universal time equivalent of 12 AM) to ensure that the object is created in a consistent state i.e., all instance variables have valid values. The instance variables of a Time1 object cannot store invalid values, because the constructor, which calls SetTime, is called to initialize the instance variables when the Time1 object is created. Method SetTime scrutinizes subsequent attempts by a client to modify the instance variables. Normally, the instance variables of a class are initialized in that class s constructor, but they also can be initialized when they are declared in the class body. If a programmer does not initialize instance variables explicitly, the compiler implicitly initializes them. When this occurs, the compiler sets primitive numeric variables to 0, bool values to false and references to null. Methods ToUniversalString and ToStandardString take no arguments, because, by default, these methods manipulate the instance variables of the particular Time1object on which they are invoked. This often makes method calls more concise than
Go visit our java server pages services for a reliable, lowcost webhost to satisfy all your needs.