Anonymous web server - 164 Control Structures: Part 1 Chapter 4 user

164 Control Structures: Part 1 Chapter 4 user types in the input dialog. Variable gradeValue stores the integer value of grade after the program converts it from a String to an int. Notice that the preceding declarations appear in the body of method main. Remember that variables declared in a method definition s body are local variables and can be used only from the line of their declaration in the method to the closing right brace (}) of the method definition. A local variable s declaration must appear before the variable is used in that method. A local variable declared in one method of a class cannot be accessed directly by other methods of a class. Good Programming Practice 4.7 Always place a blank line before a declaration that appears between executable statements. This format makes the declarations stand out in the program and contributes to program clarity. Good Programming Practice 4.8 If you prefer to place declarations at the beginning of a method, separate the declarations from the executable statements in that method with one blank line, to highlight where the declarations end and the executable statements begin. Common Programming Error 4.6 Attempting to use a local variable s value before initializing the variable (normally with an assignment statement) results in a compile error indicating that the variable may not have been initialized. The value of a local variable cannot be used until the variable is initialized. The program will not compile properly until the variable receives an initial value. Lines 19 20, total = 0; // clear total gradeCounter = 1; // prepare to loop are assignment statements that initialize totalto 0 and gradeCounterto 1. Note that these statements initialize variables total and gradeCounter before they are used in calculations. Line 23, while ( gradeCounter <= 10 ) { // loop 10 times indicates that the while structure should continue looping (also called iterating) as long as the value of gradeCounter is less than or equal to 10. Lines 26 27, grade = JOptionPane.showInputDialog( “Enter integer grade: ” ); correspond to the pseudocode statement Input the next grade. The statement displays an input dialog with the prompt Enterintegergrade: on the screen. After the user enters the grade, the program converts it from a String to an intat line 30, gradeValue = Integer.parseInt( grade ); Remember that class Integer is from package java.lang that the compiler imports in every Java program. The pseudocode for the class-average problem does not reflect the pre Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/2/01

Leave a Reply