Chapter 4 Control Structures: Part 1 165 ceding (Com web hosting)

Chapter 4 Control Structures: Part 1 165 ceding statement. The pseudocode statement Input the next grade requires the programmer to implement the process of obtaining the value from the user and converting it to a type that can be used in calculating the average. As you learn to program, you will find that you require fewer pseudocode statements to help you implement a program. Next, the program updates the total with the new gradeValue entered by the user. Line 33, total = total + gradeValue; adds gradeValue to the previous value of total and assigns the result to total. Line 36, gradeCounter = gradeCounter + 1; adds 1to gradeCounter to indicate that the program hasprocessed a grade and is ready to input the next grade from the user. Incrementing gradeCounter is necessary for the condition in the while structure to become false eventually and terminate the loop. Line 41, average = total / 10; // perform integer division assigns the results of the average calculation to variable average. Lines 44 46, JOptionPane.showMessageDialog( null, “Class average is ” + average, “Class Average”, JOptionPane.INFORMATION_MESSAGE ); display an information message dialog containing the string “Classaverageis ” followed by the value of variable average. The string ClassAverage (the third argument) is the title of the message dialog. Line 48, System.exit( 0 ); // terminate the program terminates the application. After compiling the class definition with javac, execute the application from the command window with the command java Average1 This command executes the Java interpreter and tells it that the main method for this application is defined in class Average1. Note that the averaging calculation in the program produced an integer result. Actually, the sum of the grade-point values in this example is 794, which, when divided by 10, should yield 79.4 (i.e., a number with a decimal point). We will see how to deal with such numbers (called floating-point numbers) in the next section. 4.9 Formulating Algorithms with Top-Down, StepwiseRefinement: Case Study 2 (Sentinel-Controlled Repetition) Let us generalize the class-average problem. Consider the following problem: Develop a class-averaging program that processes an arbitrary number of grades each time the program executes. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/2/01

Leave a Reply