Archive for May, 2007

86 Introduction to Java Applications Chapter (Web hosting directory) 2 Common

Saturday, May 5th, 2007

86 Introduction to Java Applications Chapter 2 Common Programming Error 2.17 Placing a semicolon immediately after the right parenthesis of the condition in an ifstructure is normally a logic error. The semicolon will cause the body of the if structure to be empty, so the ifstructure itself will perform no action, regardless of whether its condition is true. Worse yet, the intended body statement of the ifstructure will now become a statement in sequence with the ifstructure and will always be executed. Notice the use of spacing in Fig. 2.20. Remember that white-space characters, such as tabs, newlines and spaces, are normally ignored by the compiler. So, statements may be split over several lines and may be spaced according to the programmer s preferences without affecting the meaning of a program. It is incorrect to split identifiers and string literals. Ideally, statements should be kept small, but it is not always possible to do so. Good Programming Practice 2.19 A lengthy statement may be spread over several lines. If a single statement must be split across lines, choose breaking points that make sense, such as after a comma in a comma- separated list, or after an operator in a lengthy expression. If a statement is split across two or more lines, indent all subsequent lines until the end of the statement. The chart in Fig. 2.21 shows the precedence of the operators introduced in this chapter. The operators are shown from top to bottom in decreasing order of precedence. Notice that all of these operators, with the exception of the assignment operator, =, associate from left to right. Addition is left associative, so an expression like x+y+zis evaluated as if it had been written as (x+y)+z. The assignment operator, =, associates from right to left, so an expression like x=y=0is evaluated as if it had been written as x=(y=0), which, as we will soon see, first assigns the value 0to variable yand then assigns the result of that assignment, 0, to x. Good Programming Practice 2.20 Refer to the operator precedence chart (see the complete chart in Appendix C) when writing expressions containing many operators. Confirm that the operations in the expression are performed in the order you expect. If you are uncertain about the order of evaluation in a complex expression, use parentheses to force the order, exactly as you would do in algebraic expressions. Be sure to observe that some operators, such as assignment, =, associate from right to left rather than from left to right. Operators Associativity Type () left to right parentheses * / % left to right multiplicative + -left to right additive < <= > >= left to right relational == != left to right equality = right to left assignment Fig. 2.2121 Precedence and associativity of the operators discussed so far. Fig. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/2/01
Note: If you are looking for best quality webspace to host and run your tomcat application check Vision shared web hosting services

Chapter 2 Introduction to Java Applications 85 Good (Web hosting plans)

Saturday, May 5th, 2007

Chapter 2 Introduction to Java Applications 85 Good Programming Practice 2.17 Indent the statement i7n the body of an ifstructure to make the body of the structure stand out and to enhance program readability. Good Programming Practice 2.18 Place only one statement per line in a program. This format enhances program readability In the preceding ifstructure, if the values of variables number1 and number2 are equal, line 35 assigns to result the value of result + number1 + “==” + number2. As discussed in Fig. 2.9, the + operator in this expression performs string concatenation. For this discussion, we assume that each of the variables number1 and number2 has the value 123. First, the expression converts number1 s value to a string and appends it to result (which currently contains the empty string) to produce the string “123″. Next, the expression appends “==”to “123″to produce the string “123==”. Finally, the expression appends number2 to “123==” to produce the string “123==123″. The Stringresult becomes longer as the program proceeds through the if structures and performs more concatenations. For example, given the value 123for both number1 and number2 in this discussion, the if conditions at lines 46 47 (<=) and 49 50 (>=) are also true. So, the program displays the result 123 == 123 123 <= 123 123 >= 123 in a message dialog. Common Programming Error 2.15 Replacing operator == in the condition of an ifstructure, such as if(x==1), with operator =, as in if(x=1), is a syntax error. Common Programming Error 2.16 Forgetting the left and right parentheses for the condition in an if structure is a syntax error. The parentheses are required. Notice that there is no semicolon (;) at the end of the first line of each if structure. Such a semicolon would result in a logic error at execution time. For example, if ( number1 == number2 ); // logic error result = result + number1 + ” == ” + number2; would actually be interpreted by Java as if ( number1 == number2 ) ; result = result + number1 + ” == ” + number2; where the semicolon on the line by itself called the empty statement is the statement to execute if the condition in the ifstructure is true. When the empty statement executes, no task is performed in the program. The program then continues with the assignment statement, which executes regardless of whether the condition is true or false. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/2/01
Note: In case you are looking for affordable webhost to host and run your servlet application check Vision ecommerce web hosting services

84 Introduction to Java Applications Chapter 2 or

Saturday, May 5th, 2007

84 Introduction to Java Applications Chapter 2 or as in String firstNumber, secondNumber, result; This is set of names known as a comma-separated list. Once again, notice the comment at the end of each declaration in lines 13 17, indicating the purpose of each variable in the program. Lines 20 21, firstNumber = JOptionPane.showInputDialog( “Enter first integer:” ); use JOptionPane.showInputDialog to allow the user to input the first integer value as a string and store it in firstNumber. Lines 24 25, secondNumber = JOptionPane.showInputDialog( “Enter second integer:” ); use JOptionPane.showInputDialog to allow the user to input the second integer value as a string and store it in secondNumber. Lines 28 29, number1 = Integer.parseInt( firstNumber ); number2 = Integer.parseInt( secondNumber ); convert each string input by the user in the input dialogs to type int and assign the values to int variables number1 and number2. Line 32, result = “”; assigns to resultthe empty string a string containing no characters. Every variable declared in a method (such as main) must be initialized (given a value) before it can be used in an expression. Because we do not yet know what the final result string will be, we assign to result the empty string as a temporary initial value. Common Programming Error 2.14 Not initializing a variable defined in a method before that variable is used in the method s body is a syntax error. Lines 34 35, if ( number1 == number2 ) result = result + number1 + ” == ” + number2; define an ifstructure that compares the values of the variables number1 and number2 to determine if they are equal. The ifstructure always begins with keyword if, followed by a condition in parentheses. The if structure expects one statement in its body. The indentation shown here is not required, but it improves the readability of the program by emphasizing that the statement in line 35 is part of the if structure that begins on line 34. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/2/01
Note: In case you are looking for affordable webhost to host and run your servlet application check Vision mysql5 web hosting services

Remote web server - Chapter 2 Introduction to Java Applications 83 Fig.

Saturday, May 5th, 2007

Chapter 2 Introduction to Java Applications 83 Fig. 2.20Using equality and relational operators (part 3 of 3). Fig. 2.20 The definition of application class Comparisonbegins at line 8, public class Comparison { As discussed previously, method main (lines 11 59) begins the execution of every Java application. Lines 13 17, String firstNumber; // first string entered by user String secondNumber; // second string entered by user String result; // a string containing the output int number1; // first number to compare int number2; // second number to compare declare the variables used in method main. Note that there are three variables of type Stringand two variables of type int. Remember that variables of the same type may be declared in one declaration or in multiple declarations. If more than one name is declared in a declaration, the names are separated by commas (,), as in String firstNumber, secondNumber, result; Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/2/01
Note: If you are looking for cheap webhost to host and run your apache application check Vision apache web hosting services

82 Introduction to Java (Virtual web hosting) Applications Chapter 2 27

Friday, May 4th, 2007

82 Introduction to Java Applications Chapter 2 27 // convert numbers from type String to type int 28 number1 = Integer.parseInt( firstNumber ); 29 number2 = Integer.parseInt( secondNumber ); 30 31 // initialize result to empty String 32 result = “”; 33 34 if ( number1 == number2 ) 35 result = number1 + ” == ” + number2; 36 37 if ( number1 != number2 ) 38 result = number1 + ” != ” + number2; 39 40 if ( number1 < number2 ) 41 result = result + "n" + number1 + " < " + number2; 42 43 if ( number1 > number2 ) 44 result = result + “n” + number1 + ” > ” + number2; 45 46 if ( number1 <= number2 ) 47 result = result + "n" + number1 + " <= " + number2; 48 49 if ( number1 >= number2 ) 50 result = result + “n” + number1 + ” >= ” + number2; 51 52 // Display results 53 JOptionPane.showMessageDialog( 54 null, result, “Comparison Results”, 55 JOptionPane.INFORMATION_MESSAGE ); 56 57 System.exit( 0 ); // terminate application 58 59 } // end method main 60 61 } // end class Comparison Fig. 2.20Using equality and relational operators (part 2 of 3). Fig. 2.20 Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/2/01
Note: In case you are looking for affordable webhost to host and run your web application check Vision cheap hosting services

Chapter 2 (Free web hosting services) Introduction to Java Applications 81 Common

Friday, May 4th, 2007

Chapter 2 Introduction to Java Applications 81 Common Programming Error 2.11 It is a syntax error if the operators ==, !=, >=and<= contain spaces between their symbols, as in = =, ! =, > =and < =, respectively. Common Programming Error 2.12 Reversing the operators !=, >=and <=, as in =!, =>and =<, is a syntax error. Common Programming Error 2.13 Confusing the equality operator, ==, with the assignment operator, =, can be a logic error or a syntax error. The equality operator should be read as is equal to, and the assignment operator should be read as gets or gets the value of. Some people prefer to read the equality operator as double equals or equals equals. The next example uses six ifstructures to compare two numbers input into text fields by the user. If the condition in any of these ifstatements is true, the assignment statement associated with that if structure executes. The user inputs two values through input dialogs. Next, the program converts the input values to integers and stores them in variables number1 and number2. Then, the program compares the numbers and displays the results of the comparisons in an information dialog. The program and sample outputs are shown in Fig. 2.20. 1 // Fig. 2.20: Comparison.java 2 // Compare integers using if structures, relational operators 3 // and equality operators. 4 5 // Java extension packages 6 import javax.swing.JOptionPane; 7 8 public class Comparison { 9 10 // main method begins execution of Java application 11 public static void main( String args[] ) 12 { 13 String firstNumber; // first string entered by user 14 String secondNumber; // second string entered by user 15 String result; // a string containing the output 16 int number1; // first number to compare 17 int number2; // second number to compare 18 19 // read first number from user as a string 20 firstNumber = 21 JOptionPane.showInputDialog( "Enter first integer:" ); 22 23 // read second number from user as a string 24 secondNumber = 25 JOptionPane.showInputDialog( "Enter second integer:" ); 26 Fig. 2.20Using equality and relational operators (part 1 of 3). Fig. 2.20 Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/2/01
Note: If you are looking for cheap webhost to host and run your apache application check Vision apache web hosting services

Web hosting ratings - 80 Introduction to Java Applications Chapter 2 Step

Friday, May 4th, 2007

80 Introduction to Java Applications Chapter 2 Step 1. y = 2 * 5 * 5 + 3 * 5 + 7; 2 * 5 is 10 (Leftmost multiplication) Step 2. y = 10 * 5 + 3 * 5 + 7; 10 * 5 is 50 (Leftmost multiplication) Step 3. y = 50 + 3 * 5 + 7; 3 * 5 is 15 (Multiplication before addition) Step 4. y = 50 + 15 + 7; 50 + 15 is 65 (Leftmost addition) Step 5. y = 65 + 7; 65 + 7 is 72 (Last addition) Step 6. y = 72; (Last operation place 72into y) Fig. 2.1818 Order in which a second-degree polynomial is evaluated. Fig. Standard algebraic equality or relational operator Java equality or relational operator Example of Java condition Meaning of Java condition Equality operators = == x == y xis equal to y . != x != y xis not equal to y Relational operators > > x > xis greater than y < < x < xis less than y = >= x >= y xis greater than or equal to y = <= x <= y xis less than or equal to y Fig. 2.1919 Equality and relational operators. Fig. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/2/01
Note: If you are looking for best quality webspace to host and run your tomcat application check Vision virtual web hosting services

Most popular web site - Chapter 2 Introduction to Java Applications 79 The

Thursday, May 3rd, 2007

Chapter 2 Introduction to Java Applications 79 The circled numbers under the statement indicate the order in which Java applies the operators. The multiplication, modulus and division operations are evaluated first in left-toright order (i.e., they associate from left to right), because they have higher precedence than that of addition and subtraction. The addition and subtraction operations are evaluated next. These operations are also applied from left to right. Not all expressions with several pairs of parentheses contain nested parentheses. For example, the expression a * ( b + c ) + c * ( d + e ) does not contain nested parentheses. Rather, these parentheses are on the same level. To develop a better understanding of the rules of operator precedence, consider the evaluation of a second-degree polynomial (y = ax2 + bx + c): y = a * x * x + b * x + c; 16 2 4 3 5 The circled numbers under the preceding statement indicate the order in which Java applies the operators. There is no arithmetic operator for exponentiation in Java; x2 is represented as x*x. Suppose that a, b, c and x are initialized as follows: a=2, b=3, c=7 and x=5. Figure 2.18 illustrates the order in which the operators are applied in the preceding second- degree polynomial. As in algebra, it is acceptable to place unnecessary parentheses in an expression to make the expression clearer. Such unnecessary parentheses are also called redundant parentheses. For example, the preceding assignment statement might be parenthesized as follows: y = ( a * x * x ) + ( b * x ) + c; Good Programming Practice 2.16 Using parentheses for complex arithmetic expressions, even when the parentheses are not necessary, can make the arithmetic expressions easier to read. 2.8 Decision Making: Equality and Relational Operators This section introduces a simple version of Java s if structure that allows a program to make a decision based on the truth or falsity of some condition. If the condition is met (i.e., the condition is true), the statement in the body of the if structure is executed. If the condition is not met (i.e., the condition is false), the body statement does not execute. We will see an example shortly. Conditions in if structures can be formed by using the equality operators and relational operators summarized in Fig. 2.19. The relational operators all have the same level of precedence and associate from left to right. The equality operators both have the same level of precedence, which is lower than the precedence of the relational operators. The equality operators also associate from left to right. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/2/01
Note: If you are looking for reliable webhost to maintain and run your java application check Vision java hosting services

78 Introduction to Java Applications Chapter 2 Operator(s) (Top ten web hosting)

Thursday, May 3rd, 2007

78 Introduction to Java Applications Chapter 2 Operator(s) Operation(s) Order of evaluation (precedence) () Parentheses Evaluated first. If the parentheses are nested, the expression in the innermost pair is evaluated first. If there are several pairs of parentheses on the same level (i.e., not nested), they are evaluated left to right. *, /and % Multiplication Evaluated second. If there are several of this type of Division operator, they are evaluated from left to right. Modulus +or Addition Evaluated last. If there are several of this type of oper- Subtraction ator, they are evaluated from left to right. Fig. 2.17Precedence of arithmetic operators. g. 2.17 Now, let us consider several expressions in light of the rules of operator precedence. Each example lists an algebraic expression and its Java equivalent. The following is an example of an arithmetic mean (average) of five terms: abc++ de ++ Algebra: m = ————————————– 5 Java: m = ( a + b + c + d + e ) / 5; The parentheses are required, because division has higher precedence than that of addition. The entire quantity (a+b+c+d+e)is to be divided by 5. If the parentheses are erroneously omitted, we obtain a+b+c+d+e/5, which evaluates as abc ++ –e ++ d 5 The following is an example of the equation of a straight line: Algebra: y = mx + b Java: y = m * x + b; No parentheses are required. The multiplication operator is applied first, because multiplication has a higher precedence than that of addition. The assignment occurs last, because it has a lower precedence than that of multiplication and division. The following example contains modulus (%), multiplication, division, addition and subtraction operations: Algebra: z = + pr %qw /xy Java: z = p * r % q + w / x -y; 1 2 4 3 56 Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/2/01
Note: In case you are looking for affordable and reliable webhost to host and run your business application check Vision php5 hosting services

My web site - Chapter 2 Introduction to Java Applications 77 Java

Thursday, May 3rd, 2007

Chapter 2 Introduction to Java Applications 77 Java operation Arithmetic operator Algebraic expression Java expression Addition + f + 7 f + 7 Subtraction p c p -c Multiplication * bm b * m Division / x x / y x / yor –or x y y Modulus % r mod s r % s Fig. 2.16 Arithmetic operators. Arithmetic expressions in Java must be written in straight-line form to facilitate entering programs into the computer. Thus, expressions such as adivided by b must be written as a/b, so that all constants, variables and operators appear in a straight line. The following algebraic notation is generally not acceptable to compilers: a b Parentheses are used in Java expressions in the same manner as in algebraic expressions. For example, to multiply atimes the quantity b+c, we write a * ( b + c ) Java applies the operators in arithmetic expressions in a precise sequence determined by the following rules of operator precedence, which are generally the same as those followed in algebra: 1. Operators in expressions contained within pairs of parentheses are evaluated first. Thus, parentheses may be used to force the order of evaluation to occur in any sequence desired by the programmer. Parentheses are at the highest level of precedence. In cases of nested or embedded parentheses, the operators in the innermost pair of parentheses are applied first. 2. Multiplication, division and modulus operations are applied next. If an expression contains several multiplication, division or modulus operations, the operators are applied from left to right. Multiplication, division and modulus operators have the same level of precedence. 3. Addition and subtraction operations are applied last. If an expression contains several addition and subtraction operations, the operators are applied from left to right. Addition and subtraction operators have the same level of precedence. The rules of operator precedence enable Java to apply operators in the correct order. When we say that operators are applied from left to right, we are referring to the associativity of the operators. We will see that some operators associate from right to left. Figure 2.17 summarizes these rules of operator precedence. This table will be expanded as additional Java operators are introduced. A complete precedence chart is included in Appendix C. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/2/01
Note: If you are looking for best quality webspace to host and run your tomcat application check Vision personal web hosting services