




























































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The java code for a gradebook class that calculates the class average and keeps track of the number of grades in each letter category (a, b, c, d, f) based on user input. The program uses a while loop and a switch statement to process user input and update the counter variables.
Typology: Study notes
1 / 122
This page cannot be seen from the preview
Don't miss anything!





























































































Algorithms
The actions to execute The order in which these actions execute
Specifies the order in which actions execute in a program
Control Structures
Statements are normally executed one after the other in the order in which they are written
Specifying the next statement to execute that is not necessarily the next one in order Can be performed by the goto statement
Control Structures (Cont.)
Demonstrated that goto statements were unnecessary Demonstrated that all programs could be written with three control structures
Control Structures (Cont.)
Also known as looping statements Repeatedly performs an action while its loop- continuation condition remains true while statement
Control Structures (Cont.)
Sequence statement, Selection statements (three types) and Repetition statements (three types) All programs are composed of these control statements
Good Programming Practice (GPP)
if…else Double-Selection
Statement (Cont.)
if…else statements can be put inside other if…else statements
elses are always associated with the immediately preceding if unless otherwise specified by braces { }
Braces { } associate statements into blocks Blocks can replace individual statements as an if body
Common Programming Error
Common Programming Error
Common Programming Error
Formulating Algorithms:
Counter-Controlled Repetition
Use a counter variable to count the number of times a loop is iterated
The fractional part of an integer division calculation is truncated (thrown away)
Outline
1 // Fig. 4.6: GradeBook.java 2 // GradeBook class that solves class-average problem using 3 // counter-controlled repetition. 4 import java.util.Scanner; // program uses class Scanner 5 6 public class GradeBook 7 { 8 private String courseName; // name of course this GradeBook represents 9 10 // constructor initializes courseName 11 public GradeBook( String name ) 12 { 13 courseName = name; // initializes courseName 14 } // end constructor 15 16 // method to set the course name 17 public void setCourseName( String name ) 18 { 19 courseName = name; // store the course name 20 } // end method setCourseName 21 22 // method to retrieve the course name 23 public String getCourseName() 24 { 25 return courseName; 26 } // end method getCourseName 27
Assign a value to instance variable courseName
Declare method setCourseName
Declare method getCourseName
20 Outline
28 // display a welcome message to the GradeBook user 29 public void displayMessage() 30 { 31 // getCourseName gets the name of the course 32 System.out.printf( "Welcome to the grade book for\n%s!\n\n", 33 getCourseName() ); 34 } // end method displayMessage 35 36 // determine class average based on 10 grades entered by user 37 public void determineClassAverage() 38 { 39 // create Scanner to obtain input from command window 40 Scanner input = new Scanner( System.in ); 41 42 int total; // sum of grades entered by user 43 int gradeCounter; // number of the grade to be entered next 44 int grade; // grade value entered by user 45 int average; // average of grades 46 47 // initialization phase 48 total = 0 ; // initialize total 49 gradeCounter = 1 ; // initialize loop counter
Declare method displayMessage
Declare method determineClassAverage Declare and initialize Scanner variable input
Declare local int variables total , gradeCounter , grade and average