decisions ppt chap 4.1 to 4.3

advertisement
Introduction to Computer Programming
Decisions
If/Else
Booleans
Your old Average friend
import java.util.Scanner;
public class AverageN {
//AverageN - Find the average of N values
public static void main(String[] args) {
Scanner keyb = new Scanner(System.in);
double sum, average, value1, value2, value3;
//get values
System.out.println (“What is value 1?”);
value1 = keyb.nextDouble();
System.out.println (“What is value 2?”);
value2 = keyb.nextDouble();
System.out.println (“What is value 3?”);
value3 = keyb.nextDouble();
sum = value1 + value2 + value3;
average = sum/3;
System.out.println(“The average is “ + average);
}}
Average Program - did you pass?
• You have the average program averaging 3
grades, but did you pass? If the grade is
over 65, you passed.
• How can you code this?
The if statement
• if statement: Do this only if true
– General syntax:
if (<condition>) {
<statement> ;
...
<statement> ;
}
• Example:
double average = sum/3;
if (average >= 65) {
System.out.println(“ You passed.");
}
4
Relational expressions
Operator
Meaning
Example
Value
==
equals
1 + 1 == 2
true
!=
does not equal
3.2 != 2.5
true
<
less than
10 < 5
false
>
greater than
10 > 5
true
<=
less than or equal to
126 <= 100
false
>=
greater than or equal to 5.0 >= 5.0
true
5
if statement flow diagram
6
The if/else statement
•
if/else statement: A Java statement that executes one block of statements if a
certain condition is true, and a second block of statements if it is false.
– General syntax:
if (<condition>) {
<statement(s)> ;
} else {
<statement(s)> ;
}
•
Example:
double average = sum/3;
if (average >= 65) {
System.out.println(“ You passed.");}
else{
System.out.println(“You failed.”);
}
7
if/else flow diagram
8
if/else question
• Write code to read a number from the user and print whether it is even
or odd using an if/else statement.
– Example executions:
Type a number: 42
Your number is even
Type a number: 17
Your number is odd
9
Nested if/else statements
•
nested if/else statement: A chain of if/else that chooses between outcomes using
many conditions.
– General syntax:
if (<condition>) {
<statement(s)> ;
} else if (<condition>) {
<statement(s)> ;
} else {
<statement(s)> ;
}
•
Example:
if (number > 0) {
System.out.println("Positive");
} else if (number < 0) {
System.out.println("Negative");
} else {
System.out.println("Zero");
}
10
Nested if/else flow diagram
if (<condition>) {
<statement(s)> ;
} else if (<condition>) {
<statement(s)> ;
} else {
<statement(s)> ;
}
11
Nested if/else/if diagram
if (<condition>) {
<statement(s)> ;
} else if (<condition>) {
<statement(s)> ;
} else if (<condition>) {
<statement(s)> ;
}
12
Sequential if diagram
if (<condition>) {
<statement(s)> ;
}
if (<condition>) {
<statement(s)> ;
}
if (<condition>) {
<statement(s)> ;
}
13
A program to calculate Grade Point Average
Example – Professor Smith gives n tests during the term and
uses a grading system, where each test is 1/n of the course
grade. Assuming that that the average of the test grades
translate into a letter grade as follows:
Test Average
Letter Grade
90.0+
A
80-89.9
B
70-79.9
C
60-69.9
D
below 60.0
F
write a program that will calculate a student's grade.
A Program To Calculate Test Average
Input - Number of tests and the student's test grades
Output – Test average and course grade
Other information
A 90+ average is an “A”.
A 80-90 average is a “B”.
A 70-80 average is a “C”.
A 60-70 average is a “D”
An average below 60 is an “F”.
Test average = Sum of the test grade/ Number of tests
Our first step is to write out our initial algorithm:
1. Find the number of tests
2. Find the average of n tests
3. Find the corresponding letter grade and print it out.
Designing the Grade Program
1. Find the number of tests
2. Find the average of n tests
3. Find the corresponding letter grade and print it out.
1.1
1.2
Prompt the user for number of tests
Read the number of tests
Refining the Grade Program
1.1
Prompt the user for number of tests
1.2
Read the number of tests
2. Find the average of n tests
3. Find the corresponding letter grade and print it out.
2.1
2.2
Add up the test grades
Divide the total by n
Refining the Grade Program
1.1
Prompt the user for number of tests
1.2
Read the number of tests
2.1
Add up the test grades
2.2
Divide the total by n
3. Find the corresponding letter grade and print it out.
2.1
For each of the n tests:
2.1.1
Prompt the user for the test grade
2.1.2
Read the test grade
2.1.3
Add it to the total
Refining the Grade Program
1.1Prompt the user for number of tests
1.2Read the number of tests
2.1For each of the n tests:
2.1.1
Prompt the user for the test grade
2.1.2
Read the test grade
2.1.3
Add it to the total
2.2Divide the total by n
3. Find the corresponding letter grade and print it out.
System.out.println
("How many tests did you take ?");
numTests = keyb.nextInt();
Refining the Grade Program
System.out.println
("How many tests did you take ?");
numTests = keyb.nextInt();
2.1For each of the n tests:
2.1.1
Prompt the user for the test grade
2.1.2
Read the test grade
2.1.3
Add it to the total
2.2Divide the total by n
3. Find the corresponding letter grade and print it out.
for (thisTest = 0;
thisTest < numTests;
thisTest++) {
Refining the Grade Program
System.out.println
("How many tests did you take ?");
numTests = keyb.nextInt();
for (thisTest = 0; thisTest < numTests;
thisTest++) {
2.1.1
Prompt the user for the test grade
2.1.2
Read the test grade
2.1.3
Add it to the total
}
2.2Divide the total by n
3. Find the corresponding letter grade and print it out.
System.out.println
("What grade did you get on this test
thisGrade = keyb.nextInt();
?");
Refining the Grade Program
System.out.println
("How many tests did you take ?");
numTests = keyb.nextInt();
for (thisTest = 0; thisTest < numTests;
thisTest++) {
System.out.println
("What grade did you get on this test ?");
thisGrade = keyb.nextInt();
2.1.3
Add it to the total
}
2.2Divide the total by n
3. Find the corresponding letter grade and print it out.
total = total + thisGrade;
Refining the Grade Program
System.out.println
("How many tests did you take ?");
numTests = keyb.nextInt();
for (thisTest = 0; thisTest < numTests;
thisTest++) {
System.out.println
("What grade did you get on this test ?");
thisGrade = keyb.nextInt();
total = total + thisGrade;
}
2.2Divide the total by n
3. Find the corresponding letter grade and print it out.
testAverage = total/numTests;
Refining the Grade Program
System.out.println
("How many tests did you take ?");
numTests = keyb.nextInt();
for (thisTest = 0; thisTest < numTests;
thisTest++) {
… …
}
testAverage = total/numTests;
3. Find the corresponding letter grade and print it out.
3.1 IF Average >= 90 THEN Grade = ‘A’
3.2 ELSE IF Average >= 80 THEN Grade = ‘B’
3.3 ELSE IF Average >= 70 THEN Grade = ‘C’
3.4 ELSE IF Average >= 60 THEN Grade = ‘D’
3.2 ELSE Grade = ‘F’
Refining the Grade Program
… …
testAverage = total/numTests;
3.1 IF Average >= 90 THEN Grade = ‘A’
3.2 ELSE IF Average >= 80 THEN Grade = ‘B’
3.3 ELSE IF Average >= 70 THEN Grade = ‘C’
3.4 ELSE IF Average >= 60 THEN Grade = ‘D’
3.2 ELSE Grade = ‘F’
if (testAverage >= 90)
courseGrade = 'A';
else if (testAverage >= 80)
courseGrade = 'B';
else if (testAverage >= 70)
courseGrade = 'C';
else if (testAverage >= 60)
courseGrade = 'D';
else
courseGrade = 'F';
Our program
public static void main(String[] args) {
Scanner keyb = new Scanner(System.in);
int thisTest, numTests, total, thisGrade;
float testAverage;
char courseGrade;
// Find out the number of classes
System.out.println
("How many tests did you take ?");
numTests = keyb.nextInt();
for (thisTest = 0;
thisTest < numTests;
thisTest++) {
System.out.println
("What grade did you get on this test ?");
thisGrade = keyb.nextInt();
// Make sure that the grades are valid
// percentages
total = total + thisGrade;
}
// Find the average
testAverage = total/numTests;
// Find the letter grade corresponding to the
// average
if (testAverage >= 90)
courseGrade = 'A';
else if (testAverage >= 80)
courseGrade = 'B';
else if (testAverage >= 70)
courseGrade = 'C';
else if (testAverage >= 60)
courseGrade = 'D';
else
courseGrade = 'F';
// Print the results.
System.out.println
("Your test average is " + testAverage);
System.out.println
("Your grade will be " + courseGrade);
}
}
One Last Refinement
• A test score must be between 0 and 100;
therefore any grade greater than 100 or else
than 0 is invalid.
• Let’s test each grade to make sure it’s valid.
• Also, we have to initialize the total to zero
before we do any addition.
The Final Grade Program
import java.util.Scanner;
public class CalcGrade {
// Calculates the average test grade and
// converts it to a letter grade assuming that
// A is a 90 average, B is an 80 average and so
// on.that
public static void main(String[] args) {
Scanner keyb = new Scanner(System.in);
int thisTest, numTests, total, thisGrade;
float testAverage;
char courseGrade;
// Find out the number of classes
System.out.println
("How many tests did you take ?");
numTests = keyb.nextInt();
//Add up the test grades
total = 0;
for (thisTest = 0;
thisTest < numTests;
thisTest++) {
System.out.println
("What grade did you get on this test ?");
thisGrade = keyb.nextInt();
// Make sure that the grades are valid
// percentages
if (thisGrade > 100)
System.out.println
("This is not a valid test grade.");
else if (thisGrade >= 0)
total = total + thisGrade;
else
System.out.println
("This is not a valid test grade.");
}
// Find the average
testAverage = total/numTests;
// Find the letter grade corresponding to the
// average
if (testAverage >= 90)
courseGrade = 'A';
else if (testAverage >= 80)
courseGrade = 'B';
else if (testAverage >= 70)
courseGrade = 'C';
else if (testAverage >= 60)
courseGrade = 'D';
else
courseGrade = 'F';
// Print the results.
System.out.println
("Your test average is " + testAverage);
System.out.println
("Your grade will be " + courseGrade);
}
}
Download