GradeBook: Java Program for Calculating Class Average and Letter Grades - Prof. Ruigang Ya, Study notes of Computer Science

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

Pre 2010

Uploaded on 10/01/2009

koofers-user-4wj
koofers-user-4wj 🇺🇸

9 documents

1 / 122

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 335
Java Controls
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f
pf60
pf61
pf62
pf63
pf64

Partial preview of the text

Download GradeBook: Java Program for Calculating Class Average and Letter Grades - Prof. Ruigang Ya and more Study notes Computer Science in PDF only on Docsity!

CS 335

Java Controls

Algorithms

  • Algorithms

The actions to executeThe order in which these actions execute

  • Program control

Specifies the order in which actions execute in a program

Control Structures

  • Sequential execution

Statements are normally executed one after the other in the order in which they are written

  • Transfer of control

Specifying the next statement to execute that is not necessarily the next one in orderCan be performed by the goto statement

  • Structured programming eliminated goto statements

Control Structures (Cont.)

  • Bohm and Jacopini‘s research

Demonstrated that goto statements were unnecessaryDemonstrated that all programs could be written with three control structures

  • The sequence structure,
  • The selection structure and
  • The repetition structure

Control Structures (Cont.)

  • Repetition statements

Also known as looping statementsRepeatedly performs an action while its loop- continuation condition remains truewhile statement

  • Performs the actions in its body zero or more times  do…while statement
  • Performs the actions in its body one or more times  for statement
  • Performs the actions in its body zero or more times

Control Structures (Cont.)

  • Java has three kinds of control structures

Sequence statement,Selection statements (three types) andRepetition statements (three types)All programs are composed of these control statements

  • Control-statement stacking
    • All control statements are single-entry/single-exit
  • Control-statement nesting

Good Programming Practice (GPP)

  • Indent both body statements of an

if…else statement.

  • If there are several levels of indentation,

each level should be indented the same

additional amount of space.

  • Conditional expressions are more difficult

to read than if…else statements and

should be used to replace only simple

if…else statements that choose between

two values

if…else Double-Selection

Statement (Cont.)

  • Nested if…else statements

if…else statements can be put inside other if…else statements

  • Dangling-else problem

elses are always associated with the immediately preceding if unless otherwise specified by braces { }

  • Blocks

Braces { } associate statements into blocksBlocks can replace individual statements as an if body

Common Programming Error

  • Forgetting one or both of the braces that delimit

a block can lead to syntax errors or logic errors

in a program.

  • Always using braces in an if...else (or

other) statement helps prevent their accidental

omission, especially when adding statements to

the if-part or the else-part at a later time. To

avoid omitting one or both of the braces, some

programmers type the beginning and ending

braces of blocks before typing the individual

statements within the braces.

Common Programming Error

  • Placing a semicolon after the condition in an if

or if...else statement leads to a logic error

in single-selection if statements and a syntax

error in double-selection if...else

statements (when the if-part contains an

actual body statement).

Common Programming Error

  • Infinite loop: Not providing, in the body of a

while statement, an action that eventually

causes the condition in the while to become

false normally results in a logic error called an

infinite loop , in which the loop never terminates.

Formulating Algorithms:

Counter-Controlled Repetition

  • Counter-controlled repetition

Use a counter variable to count the number of times a loop is iterated

  • Integer division

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

  • GradeBook.java (2 of 3)

Declare method displayMessage

Declare method determineClassAverage Declare and initialize Scanner variable input

Declare local int variables total , gradeCounter , grade and average