For the next example, we once again formulate an algorithm by using pseudocode and topdown,
stepwise refinement, and write a corresponding Java program. We’ve seen that control
statements can be stacked on top of one another (in sequence). In this case study, we
examine the only other structured way control statements can be connected—namely, by
nesting one control statement within another.
Consider the following problem statement:
A college offers a course that prepares students for the state licensing exam for real
estate brokers. Last year, ten of the students who completed this course took the exam.
The college wants to know how well its students did on the exam. You’ve been asked to
write a program to summarize the results. You’ve been given a list of these 10 students.
Next to each name is written a 1 if the student passed the exam or a 2 if the student
failed.
Your program should analyze the results of the exam as follows:
1. Input each test result (i.e., a 1 or a 2). Display the message “Enter result” on the screen
each time the program requests another test result.
2. Count the number of test results of each type.
3. Display a summary of the test results, indicating the number of students who passed and
the number who failed.
4. If more than eight students passed the exam, print the message “Bonus to instructor!”
After reading the problem statement carefully, we make the following observations:
1. The program must process test results for 10 students. A counter-controlled loop
can be used, because the number of test results is known in advance.
2. Each test result has a numeric value—either a 1 or a 2. Each time it reads a test
result, the program must determine whether it’s a 1 or a 2. We test for a 1 in our
algorithm. If the number is not a 1, we assume that it’s a 2. (Exercise 4.24 considers
the consequences of this assumption.)
3. Two counters are used to keep track of the exam results—one to count the number
of students who passed the exam and one to count the number who failed.
4. After the program has processed all the results, it must decide whether more than
eight students passed the exam.
Let’s proceed with top-down, stepwise refinement. We begin with a pseudocode representation
of the top:
1 Initialize passes to zero
2 Initialize failures to zero
3 Initialize student counter to one
4 While student counter is less than or equal to 10
5 Prompt the user to enter the next exam result
6 Input the next exam result
7 If the student passed
8 Add one to passes
9 Else
10 Add one to failures
11 Add one to student counter
12 Print the number of passes
13 Print the number of failures
14 If more than eight students passed
15 Print “Bonus to instructor!”
the program in java is :-
import java.util.Scanner;
public class Apples {
public static void main(String[] args){
int counter = 0;
float sum = 0;
float studentGrade =0;
float average = 0;
int pass = 0;
int fails = 0;
while (counter <= 10){
System.out.println("enter student grade ");
Scanner input = new Scanner(System.in); // creating object from scanner class
studentGrade = input.nextInt();
sum += studentGrade;
counter ++;
if (studentGrade > 60)
pass ++;
else
fails ++;
}
average = sum / counter;
System.out.printf("the average is %f " , average);
if (pass > 8)
System.out.println(" \n bonus to the instructor");
else
System.out.println("lame");
}
}
thanks <3
The class was good , it is getting better to understand . Thank you Mr.Gabr :) I always look forward to your next class !
ReplyDelete