Download presentation
Presentation is loading. Please wait.
1
Introduction to Computer Programming Decisions If/Else Booleans
2
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); }}
3
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?
4
4 The if statement if statement: Do this only if true –General syntax: if ( ) { ;... ; } Example: double average = sum/3; if (average >= 65) { System.out.println(“ You passed."); }
5
5 Relational expressions OperatorMeaningExampleValue == equals 1 + 1 == 2true != does not equal 3.2 != 2.5true < less than 10 < 5false > greater than 10 > 5true <= less than or equal to 126 <= 100false >= greater than or equal to 5.0 >= 5.0true
6
6 if statement flow diagram
7
7 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 ( ) { ; } else { ; } Example: double average = sum/3; if (average >= 65) { System.out.println(“ You passed.");} else{ System.out.println(“You failed.”); }
8
8 if/else flow diagram
9
9 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
10
10 Nested if/else statements nested if/else statement: A chain of if/else that chooses between outcomes using many conditions. –General syntax: if ( ) { ; } else if ( ) { ; } else { ; } Example: if (number > 0) { System.out.println("Positive"); } else if (number < 0) { System.out.println("Negative"); } else { System.out.println("Zero"); }
11
11 Nested if/else flow diagram if ( ) { ; } else if ( ) { ; } else { ; }
12
12 Nested if/else/if diagram if ( ) { ; } else if ( ) { ; } else if ( ) { ; }
13
13 Sequential if diagram if ( ) { ; } if ( ) { ; } if ( ) { ; }
14
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 AverageLetter Grade 90.0+A 80-89.9B 70-79.9C 60-69.9D below 60.0F write a program that will calculate a student's grade.
15
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.
16
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.1Prompt the user for number of tests 1.2Read the number of tests
17
Refining the Grade Program 1.1Prompt the user for number of tests 1.2Read the number of tests 2.Find the average of n tests 3.Find the corresponding letter grade and print it out. 2.1Add up the test grades 2.2Divide the total by n
18
Refining the Grade Program 1.1Prompt the user for number of tests 1.2Read the number of tests 2.1Add up the test grades 2.2Divide the total by n 3.Find the corresponding letter grade and print it out. 2.1For each of the n tests: 2.1.1Prompt the user for the test grade 2.1.2Read the test grade 2.1.3Add it to the total
19
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.1Prompt the user for the test grade 2.1.2Read the test grade 2.1.3Add 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();
20
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.1Prompt the user for the test grade 2.1.2Read the test grade 2.1.3Add 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++) {
21
Refining the Grade Program System.out.println ("How many tests did you take ?"); numTests = keyb.nextInt(); for (thisTest = 0; thisTest < numTests; thisTest++) { 2.1.1Prompt the user for the test grade 2.1.2Read the test grade 2.1.3Add 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();
22
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.3Add it to the total } 2.2Divide the total by n 3.Find the corresponding letter grade and print it out. total = total + thisGrade;
23
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;
24
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’
25
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';
26
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();
27
// 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';
28
// Print the results. System.out.println ("Your test average is " + testAverage); System.out.println ("Your grade will be " + courseGrade); }
29
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.
30
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();
31
//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."); }
32
// 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';
33
// Print the results. System.out.println ("Your test average is " + testAverage); System.out.println ("Your grade will be " + courseGrade); }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.