for Repetition Statement
The while statement can be used to implement any counter-controlled loop. Java also provides the for repetition statement, which specifies the counter-controlled-repetition details in a single line of code.
1 //for loop 2 //Counter-controlled repetition statement. 3 4 public class ForCounter 5 { 6 public static void main( String args[] ) 7 { 8 // for statement header includes initialization, 9 // loop-continuation condition and increment 10 for ( int counter = 1; counter <= 10; counter++ ) 11 System.out.printf( "%d ", counter ); 12 13 System.out.println(); // output a newline 14 } // end main 15 } // end class ForCounter
The application's main method operates as follows: When the for statement (lines 1011) begins executing, the control variable counter is declared and initialized to 1. Next, the program checks the loop-continuation condition, counter <= 10, which is between the two required semicolons. Because the initial value of counter is 1, the condition initially is true. Therefore, the body statement (line 11) displays control variable counter's value, namely 1. After executing the loop's body, the program increments counter in the expression counter++, which appears to the right of the second semicolon.
do...while Repetition Statement
The do...while repetition statement is similar to the while statement. In the while, the program tests the loop-continuation condition at the beginning of the loop, before executing the loop's body. If the condition is false, the body never executes. The do...while statement tests the loop-continuation condition after executing the loop's body; therefore, the body always executes at least once. When a do...while statement terminates, execution continues with the next statement in sequence.
1 // Fig. 5.7: DoWhileTest.java 2 // do...while repetition statement. 3 4 public class DoWhileTest 5 { 6 public static void main( String args[] ) 7 { 8 int counter = 1; // initialize counter 9 10 do 11 { 12 System.out.printf( "%d ", counter ); 13 ++counter; 14 } while ( counter <= 10 ); // end do...while 15 16 System.out.println(); // outputs a newline 17 } // end main 18 } // end class DoWhileTest
Line 8 declares and initializes control variable counter. Upon entering the do...while statement, line 12 outputs counter's value and line 13 increments counter. Then the program evaluates the loop-continuation test at the bottom of the loop (line 14). If the condition is true, the loop continues from the first body statement in the do...while (line 12). If the condition is false, the loop terminates and the program continues with the next statement after the loop.
switch Multiple-Selection Statement
We discussed the if single-selection statement and the if...else double-selections . Java provides the switch multiple-selection statement to perform different actions based on the possible values of an integer variable or expression. Each action. is associated with the value of a constant integral expression (i.e., a constant value of type byte, short, int or char, but not long) that the variable or expression on which the switch is based may assume.
GradeBook Class with switch Statement to Count A, B, C, D and F Grades
The version of the class we now present not only calculates the average of a set of numeric grades entered by the user, but uses a switch statement to determine whether each grade is the equivalent of an A, B, C, D or F and to increment the appropriate grade counter.
1 // Fig. 5.9: GradeBook.java 2 // GradeBook class uses switch statement to count A, B, C, D and F grades. 3 import java.util.Scanner; // program uses class Scanner 4 5 public class GradeBook 6 { 7 private String courseName; // name of course this GradeBook represents 8 private int total; // sum of grades 9 private int gradeCounter; // number of grades entered 10 private int aCount; // count of A grades 11 private int bCount; // count of B grades 12 private int cCount; // count of C grades 13 private int dCount; // count of D grades 14 private int fCount; // count of F grades 15 16 // constructor initializes courseName; 17 // int instance variables are initialized to 0 by default 18 public GradeBook( String name ) 19 { 20 courseName = name; // initializes courseName 21 } // end constructor 22 23 // method to set the course name 24 public void setCourseName( String name ) 25 { 26 courseName = name; // store the course name 27 } // end method setCourseName 28 29 // method to retrieve the course name 30 public String getCourseName() 31 { 32 return courseName; 33 } // end method getCourseName 34 35 // display a welcome message to the GradeBook user 36 public void displayMessage() 37 { 38 // getCourseName gets the name of the course 39 System.out.printf( "Welcome to the grade book for\n%s!\n\n", 40 getCourseName() ); 41 } // end method displayMessage 42 43 // input arbitrary number of grades from user 44 public void inputGrades() 45 { 46 Scanner input = new Scanner( System.in ); 47 48 int grade; // grade entered by user 49 50 System.out.printf( "%s\n%s\n %s\n %s\n", 51 "Enter the integer grades in the range 0-100.", 52 "Type the end-of-file indicator to terminate input:", 53 "On UNIX/Linux/Mac OS X type <ctrl> d then press Enter", 54 "On Windows type <ctrl> z then press Enter" ); 55 56 // loop until user enters the end-of-file indicator 57 while ( input.hasNext() ) 58 { 59 grade = input.nextInt(); // read grade 60 total += grade; // add grade to total 61 ++gradeCounter; // increment number of grades 62 63 // call method to increment appropriate counter 64 incrementLetterGradeCounter( grade ); 65 } // end while 66 } // end method inputGrades 67 68 // add 1 to appropriate counter for specified grade 69 public void incrementLetterGradeCounter( int numericGrade ) 70 { 71 // determine which grade was entered 72 switch ( grade / 10 ) 73 { 74 case 9: // grade was between 90 75 case 10: // and 100 76 ++aCount; // increment aCount 77 break; // necessary to exit switch 78 79 case 8: // grade was between 80 and 89 80 ++bCount; // increment bCount 81 break; // exit switch 82 83 case 7: // grade was between 70 and 79 84 ++cCount; // increment cCount 85 break; // exit switch 86 87 case 6: // grade was between 60 and 69 88 ++dCount; // increment dCount 89 break; // exit switch 90 91 default: // grade was less than 60 92 ++fCount; // increment fCount 93 break; // optional; will exit switch anyway 94 } // end switch 95 } // end method incrementLetterGradeCounter 96 97 // display a report based on the grades entered by user 98 public void displayGradeReport() 99 { 100 System.out.println( "\nGrade Report:" ); 101 102 // if user entered at least one grade... 103 if ( gradeCounter != 0 ) 104 { 105 // calculate average of all grades entered 106 double average = (double) total / gradeCounter; 107 108 // output summary of results 109 System.out.printf( "Total of the %d grades entered is %d\n", 110 gradeCounter, total ); 111 System.out.printf( "Class average is %.2f\n", average ); 112 System.out.printf( "%s\n%s%d\n%s%d\n%s%d\n%s%d\n%s%d\n", 113 "Number of students who received each grade:", 114 "A: ", aCount, // display number of A grades 115 "B: ", bCount, // display number of B grades 116 "C: ", cCount, // display number of C grades 117 "D: ", dCount, // display number of D grades 118 "F: ", fCount ); // display number of F grades 119 } // end if 120 else // no grades were entered, so output appropriate message 121 System.out.println( "No grades were entered" ); 122 } // end method displayGradeReport 123 } // end class GradeBookMETHODS CREATED IN THIS CLASS USED IN ANOTHER CLASS
1 // Fig. 5.10: GradeBookTest.java 2 // Create GradeBook object, input grades and display grade report. 3 4 public class GradeBookTest 5 { 6 public static void main( String args[] ) 7 { 8 // create GradeBook object myGradeBook and 9 // pass course name to constructor 10 GradeBook myGradeBook = new GradeBook( 11 "CS101 Introduction to Java Programming" ); 12 13 myGradeBook.displayMessage(); // display welcome message 14 myGradeBook.inputGrades(); // read grades from user 15 myGradeBook.displayGradeReport(); // display report based on grades 16 } // end main 17 } // end class GradeBookTest
No comments:
Post a Comment