Archive for April, 2007

Web server info - 126 Control Structures: Part 1 Chapter 4 Expanded

Monday, April 30th, 2007

126 Control Structures: Part 1 Chapter 4 Expanded code Fig. 4.17 Windows Form Designer generated code when expanded. Upon initial inspection, the expanded code (Fig. 4.17) looks incredibly complex.This code is created by the IDE and normally is not edited by the programmer. We feel it is important for novice programmers to see the amount of code that is generated by the IDE, even though much of the code is not explained until later in the book. This type of code is present in every Windows application. Allowing the IDE to create this code saves the programmer considerable development time. If the IDE did not provide the code, the programmer would have to write it, and this would require a considerable amount of time. The vast majority of the code shown has not been introduced yet, so you are not expected to understand how it works. However, certain programming constructs, such as comments and control structures, should be familiar. Our explanation of this code will enable us to discuss visual programming in greater detail. As you continue to study C#, especially in Chapters 8 13, the purpose of this code will become clearer. When we created this application in Chapter 2, we used the Properties window to set properties for the form, label and picture box. Once a property was set, the form or control was updated immediately. Forms and controls contain a set of default properties, which are displayed initially in the Properties window when a form or control is selected. These default properties provide the initial characteristics of a form or control when it is created. When a control, such as a label, is placed on the form, the IDE adds code to the class (e.g., ASimpleProgram) that creates the control and that sets some of the control s property values, such as the name of the control and its location on the form. Figure 4.18 shows a portion of the code generated by the IDE for setting the label s (i.e., welcomeLabel s) properties. These include the label s Font, Location, Name, Text and TextAlign properties. Recall from Chapter 2 that we explicitly set values for the label s Name, Text and TextAlign properties. Other properties, such as Location are set only when the label is placed on the form.
Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision web design programs services

Chapter 4 (Web design service) Control Structures: Part 1 125 Collapsed

Monday, April 30th, 2007

Chapter 4 Control Structures: Part 1 125 Collapsed code Collapsed comment Fig. 4.16 IDE showing program code for Fig. 2.15. immediately by the class name (ASimpleProgram). Recall that the form s name is set using the (Name) property. A colon (:) indicates that the class ASimpleProgram inherits existing pieces from another class. The class from which ASimpleProgram inherits here, System.Windows.Forms.Form appears to the right of the colon. In this inheritance relationship, Form is called the base class (or superclass), and ASimpleProgramis called the derived class (or subclass). With inheritance ASimpleProgram s class definition has the attributes (data) and behaviors (methods) of class Form. We discuss the significance of the keyword public in Chapter 6. [Note: Changing a control s name in the Properties window may not change all occurrences of the control s name in the code. The reader should search the code and replace names that were not changed by the IDE. For example, the original form name (and class name) was Form1. Search the code for Form1 and change any remaining instances to ASimpleProgram.] A key benefit of inheriting from class Form is that someone else has previously defined what it means to be a form. The Windows operating system expects every window (e.g., form) to have certain attributes and behaviors. However, because class Form already provides those capabilities, programmers do not need to reinvent the wheel by defining all those capabilities themselves. In fact, class Form has over 400 methods! In our programs up to this point, we have used only one method (i.e., Main), so you can imagine how much work went into creating class Form. The use of the colon to extend from class Form enables programmers to create forms quickly. In the editor window (Fig. 4.16), notice the text Windows Form Designer generatedcode, which is colored gray and has a plus box next to it. The plus box indicates that this section of code is collapsed. Although collapsed code is not visible, it is still part of the program. Code collapsing allows programmers to hide code in the editor, so that they can focus on smaller (and perhaps more important) code segments. Notice that the entire class definition also can be collapsed by clicking the minus box to the left of public. In Fig. 4.16, the description in gray indicates that the collapsed code was created by the Windows Form Designer (i.e., the part of the IDE that creates the code for the GUI). This collapsed code contains the code created by the IDE for the form and its controls, as well as code that enables the program to run. Click the plus box to view the code.
Note: If you are looking for best quality webspace to host and run your tomcat application check Vision tomcat hosting services

124 Control Structures: Part 1 Chapter 4 Operators (Mac os x web server)

Sunday, April 29th, 2007

124 Control Structures: Part 1 Chapter 4 Operators Associativity Type ++ — + - (type) right to left unary prefix */ % left to right multiplicative + left to right additive < <= > >= left to right relational == != left to right equality ?: right to left conditional = += -= *= /= %= right to left assignment Fig. 4.15 Precedence and associativity of the operators discussed so far in this book. (Part 2 of 2.) 4.13 Introduction to Windows Application Programming Today, users demand software with rich GUIs that allow them to click buttons, select items from menus and much more. In this chapter and the previous, we created console applications. However, most C# programs used in industry are Windows applications with GUIs. For this reason, we are introducing Windows applications early in the book, although doing so exposes some concepts that we do not explain fully until later chapters. In Chapter 2, Introduction to the Visual Studio .NET IDE, we introduced the concept of visual programming, which allows programmers to create graphical user interfaces (GUIs) without writing any programming code. In this section, we combine visual programming with the conventional programming techniques introduced in this chapter and Chapter 3, Introduction to C# Programming. Through this combination, we can enhance considerably the Windows application introduced in Chapter 2. Load the project ASimpleProject from Chapter 2 into the IDE. To identify easily the form and its controls in the program code, change the (Name) properties of the form, label and picture box to ASimpleProgram, welcomeLabel and bugPictureBox, respectively. To change a GUI component s properties, select (click) the component in the design window, then locate the property in the Properties window. Click the box to the right of the property name to input a new value, then press the Enter key. With visual programming, the IDE generates the program code that creates the GUI. This code contains instructions for the creation of the form and every control on it. Unlike a console application, a Windows application s program code is not displayed initially in the editor window. Once the program s project (e.g., ASimpleProgram) is opened in the IDE, the program code can be viewed by selecting View > Code. Figure 4.16 shows the code editor displaying the program code. Windows applications use classes. We already have seen examples of classes such as Console and MessageBox, which are defined within the .NET Framework Class Library. Classes are logical groupings of procedures and data that simplify program organization. In-depth coverage of classes is provided in Chapter 8, Object-Based Programming. Every Windows application consists of at least one class that inherits from class Form (which represents a form) in the .NET Framework Class Library s System.Windows. Forms namespace. The keyword class begins a class definition and is followed
Note: If you are looking for cheap and reliable webhost to host and run your mysql application check Vision mysql hosting services

Chapter 4 Control Structures: Part 1 123 passes (Apache web server tutorial)

Sunday, April 29th, 2007

Chapter 4 Control Structures: Part 1 123 passes = passes + 1; failures = failures + 1; student = student + 1; can be written more concisely with assignment operators as passes += 1; failures += 1; student += 1; with preincrement operators as ++passes; ++failures; ++student; or with postincrement operators as passes++; failures++; student++; It is important to note here that when incrementing or decrementing a variable in an expression or statement by itself, the preincrement and postincrement forms have the same effect, and the predecrement and postdecrement forms have the same effect. It is only when a variable appears in the context of a larger expression that preincrementing and postincrementing the variable have different effects (and similarly for predecrementing and postdecrementing). Common Programming Error 4.10 Attempting to use the increment or decrement operator on an expression other than a variable reference is a syntax error. A variable reference is a variable or expression that can appear on the left side of an assignment operation. For example, writing ++(x + 1) is a syntax error, because (x+1) is not a variable reference.2 The chart in Fig. 4.15 shows the precedence and associativity of the operators introduced to this point. The operators are shown top to bottom in decreasing order of precedence. The second column describes the associativity of the operators at each level of precedence. Notice that the conditional operator (?:), the unary operators increment (++), decrement (–), plus (+), minus (-), cast and the assignment operators (=, +=, -=, *=, / = and %=) associate from right to left. All other operators in the operator precedence chart of Fig. 4.15 associate from left to right. The third column names the groups of operators. Operators Associativity Type () left to right parentheses ++–right to left unary postfix Fig. 4.15 Precedence and associativity of the operators discussed so far in this book. (Part 1 of 2.) 2. The term variable reference is equivalent to the term lvalue ( left value ), which is popular among C and C++ programmers.
Note: In case you are looking for affordable webhost to host and run your web application check Vision http web server services

Web site construction - 122 Control Structures: Part 1 Chapter 4 1

Saturday, April 28th, 2007

122 Control Structures: Part 1 Chapter 4 1 // Fig. 4.14: Increment.cs 2 // Preincrementing and postincrementing 3 4 using System; 5 6 class Increment 7 { 8 static void Main( string[] args ) 9 { 10 int c; 11 12 c = 5; 13 Console.WriteLine( c ); // print 5 14 Console.WriteLine( c++ ); // print 5 then postincrement 15 Console.WriteLine( c ); // print 6 16 17 Console.WriteLine(); // skip a line 18 19 c = 5; 20 Console.WriteLine( c ); // print 5 21 Console.WriteLine( ++c ); // preincrement then print 6 22 Console.WriteLine( c ); // print 6 23 24 } // end of method Main 25 26 } // end of class Increment 5 5 6 5 6 6 Fig. 4.14 The difference between preincrementing and postincrementing. The program displays the value of c before and after the ++ operator is used. The decrement operator (–) works similarly. Good Programming Practice 4.8 For readability, nary operators should be placed next to their operands, with no intervening spaces. Line 17, Console.WriteLine(); // skip a line uses Console.WriteLine to output a blank line. If Console.WriteLine receives no arguments, it simply outputs a newline character. The arithmetic assignment operators and the increment and decrement operators can be used to simplify program statements. For example, the three assignment statements in Fig. 4.11 (lines 22, 25 and 27)
Note: In case you are looking for affordable webhost to host and run your servlet application check Vision mysql5 web hosting services

Chapter 4 Control Structures: Part 1 121 Assignment (Hp web site)

Friday, April 27th, 2007

Chapter 4 Control Structures: Part 1 121 Assignment operator Sample expression Explanation Assigns -= d -= 4 d = d -4 1to d *= e *= 5 e = e * 5 20to e /= f /= 3 f = f / 3 2to f %= g %= 9 g = g % 9 3to g Fig. 4.12 Arithmetic assignment operators. (Part 2 of 2.) 4.12 Increment and Decrement Operators C# provides the unary increment operator, ++, and the unary decrement operator, –, which are summarized in Fig. 4.13. A program can increment the value of a variable called c by 1 using the increment operator, ++, rather than the expression c=c+1or c+=1. If an increment or decrement operator is placed before a variable, it is referred to as the pre- increment or predecrement operator, respectively. If an increment or decrement operator is placed after a variable, it is referred to as the postincrement or postdecrement operator, respectively. Preincrementing (or predecrementing) a variable causes the variable to be incremented (or decremented) by 1, and then the new value of the variable is used in the expression in which it appears. Postincrementing (or postdecrementing) the variable causes the current value of the variable to be used in the expression in which it appears, and then the variable value is incremented (or decremented) by 1. The application in Fig. 4.14 demonstrates the difference between the preincrementing version and the postincrementing version of the ++ increment operator. Postincrementing the variable c causes it to be incremented after it is used in the Console.WriteLine method call (line 14). Preincrementing the variable c causes it to be incremented before it is used in the Console.WriteLine method call (line 21). Operator Called Sample expression Explanation ++ preincrement ++a Increment aby 1, then use the new value of ain the expression in which a resides. ++ postincrement a++ Use the current value of a in the expression in which a resides, then increment a by 1. -predecrement –b Decrement bby 1, then use the new value of b in the expression in which b resides. -postdecrement b-Use the current value of b in the expression in which b resides, then decrement b by 1. Fig. 4.13 The increment and decrement operators.
Note: In case you are looking for affordable webhost to host and run your servlet application check Vision ecommerce web hosting services

120 Control Structures: Part 1 Chapter 4 such (Affordable web design)

Friday, April 27th, 2007

120 Control Structures: Part 1 Chapter 4 such initialization normally occurs in assignment statements. Notice the use of the nested if/else structure (lines 21 25) in the while structure s body. Also, notice the new statement at line 31 that uses Console.WriteLine to output a blank line. Software Engineering Observation 4.7 The most difficult part of solving a problem on a computer is developing the algorithm for the solution. Once a correct algorithm has been specified, the process of producing a working C# program from the algorithm is normally straightforward. Software Engineering Observation 4.8 Many experienced programmers write programs without ever using program development tools like pseudocode. These programmers feel that their ultimate goal is to solve the problem on a computer, and that writing pseudocode merely delays the production of final output. Although this may work for simple and familiar problems, it can lead to serious problems on large, complex projects. 4.11 Assignment Operators C# provides several assignment operators for abbreviating assignment expressions. For example, the statement c = c + 3; can be abbreviated with the addition assignment operator += as c += 3; The += operator adds the value of the expression on the right of the operator to the value of the variable on the left of the operator and stores the result in the variable on the left of the operator. Any statement of the form variable = variable operator expression; where operator is one of the binary operators +, -, *, / or % (or others we will discuss later in the book), can be written in the form variable operator= expression; Figure 4.12 includes the arithmetic assignment operators, sample expressions using these operators and explanations. Common Programming Error 4.9 Placing a space character between symbols that compose an arithmetic assignment operator is a syntax error. Assignment operator Sample expression Explanation Assigns Assume: int c =3, d = 5, e = 4, f = 6, g = 12; += c += 7 c = c + 7 10to c Fig. 4.12 Arithmetic assignment operators. (Part 1 of 2.)
Note: In case you are looking for affordable and reliable webhost to host and run your business application check Vision ftp web hosting services

Chapter 4 Control Structures: Part 1 119 26 (Web hosting uk)

Thursday, April 26th, 2007

Chapter 4 Control Structures: Part 1 119 26 27 student = student + 1; 28 } 29 30 // termination phase 31 Console.WriteLine(); 32 Console.WriteLine( “Passed: ” + passes ); 33 Console.WriteLine( “Failed: ” + failures ); 34 35 if ( passes > 8 ) 36 Console.WriteLine( “Raise Tuitionn” ); 37 38 } // end of method Main 39 40 } // end of class Analysis Enter result (1=pass, 2=fail): 1 Enter result (1=pass, 2=fail): 2 Enter result (1=pass, 2=fail): 1 Enter result (1=pass, 2=fail): 1 Enter result (1=pass, 2=fail): 1 Enter result (1=pass, 2=fail): 1 Enter result (1=pass, 2=fail): 1 Enter result (1=pass, 2=fail): 1 Enter result (1=pass, 2=fail): 1 Enter result (1=pass, 2=fail): 1 Passed: 9 Failed: 1 Raise Tuition Enter result (1=pass, 2=fail): 1 Enter result (1=pass, 2=fail): 2 Enter result (1=pass, 2=fail): 2 Enter result (1=pass, 2=fail): 2 Enter result (1=pass, 2=fail): 2 Enter result (1=pass, 2=fail): 2 Enter result (1=pass, 2=fail): 1 Enter result (1=pass, 2=fail): 1 Enter result (1=pass, 2=fail): 1 Enter result (1=pass, 2=fail): 1 Passed: 5 Failed: 5 Fig. 4.11 C# program for examination-results problem. (Part 2 of 2.) Lines 10 13 declare the variables used in Main to process the examination results. We have taken advantage of a C# feature that incorporates variable initialization into declarations (passes is assigned 0, failures is assigned 0 and student is assigned 1). Programs that contain repetition may require initialization at the beginning of each repetition;
Note: In case you are looking for affordable webhost to host and run your servlet application check Vision make web site services

118 Control Structures: Part 1 (Kids web site) Chapter 4 Initialize

Thursday, April 26th, 2007

118 Control Structures: Part 1 Chapter 4 Initialize passes to zero Initialize failures to zero Initialize student to one While student counter is less than or equal to ten Input the next exam result If the student passed Add one to passes Else Add one to failures Add one to student counter Print the number of passes Print the number of failures If more than eight students passed Print Raise tuition Fig. 4.10 Pseudocode for examination-results problem. 1 // Fig. 4.11: Analysis.cs 2 // Analysis of Examination Results. 3 4 using System; 5 6 class Analysis 7 { 8 static void Main( string[] args ) 9 { 10 int passes = 0, // number of passes 11 failures = 0, // number of failures 12 student = 1, // student counter 13 result; // one exam result 14 15 // process 10 students; counter-controlled loop 16 while ( student <= 10 ) 17 { 18 Console.Write( "Enter result (1=pass, 2=fail): " ); 19 result = Int32.Parse( Console.ReadLine() ); 20 21 if( result == 1 ) 22 passes = passes + 1; 23 24 else 25 failures = failures + 1; Fig. 4.11 C# program for examination-results problem. (Part 1 of 2.)
Note: If you are looking for best quality webspace to host and run your tomcat application check Vision virtual web hosting services

Chapter 4 Control Structures: Part 1 117 Initialize (Free web space)

Thursday, April 26th, 2007

Chapter 4 Control Structures: Part 1 117 Initialize variables Input the ten exam grades and count passes and failures Print a summary of the exam results and decide if tuition should be raised Even though we have a complete representation of the entire program, further refinement is necessary. We must commit to specific variables. Counters are needed to record the passes and failures. A counter controls the looping process and a variable stores the user input. The pseudocode statement Initialize variables may be refined as follows: Initialize passes to zero Initialize failures to zero Initialize student to one Only the counters for the number of passes, number of failures and number of students are initialized. The pseudocode statement Input the ten quiz grades and count passes and failures requires a loop that successively inputs the result of each exam. Here, it is known in advance that there are precisely ten exam results, so counter-controlled repetition is appropriate. Inside the loop (i.e., nested within the loop) a double-selection structure determines whether each exam result is a pass or a failure, and the structure increments the appropriate counter accordingly. The refinement of the preceding pseudocode statement is While student counter is less than or equal to ten Input the next exam result If the student passed Add one to passes Else Add one to failures Add one to student counter Notice the use of blank lines to offset the If/Else control structure to improve program readability. The pseudocode statement Print a summary of the exam results and decide if tuition should be raised may be refined as follows: Print the number of passes Print the number of failures If more than eight students passed Print Raise tuition The complete second refinement appears in Fig. 4.10. Notice that blank lines also set off the While structure for program readability. The pseudocode now is refined sufficiently for conversion to C#. The C# program and sample executions are shown in Fig. 4.11.
Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision web and email hosting services