1 / 33

Introduction to Computer Programming

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);

Télécharger la présentation

Introduction to Computer Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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. 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."); }

  5. Relational expressions

  6. if statement flow diagram

  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 (<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.”); }

  8. if/else flow diagram

  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. 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"); }

  11. Nested if/else flow diagram if (<condition>) { <statement(s)> ; } else if (<condition>) { <statement(s)> ; } else { <statement(s)> ; }

  12. Nested if/else/if diagram if (<condition>) { <statement(s)> ; } else if (<condition>) { <statement(s)> ; } else if (<condition>) { <statement(s)> ; }

  13. Sequential if diagram if (<condition>) { <statement(s)> ; } if (<condition>) { <statement(s)> ; } if (<condition>) { <statement(s)> ; }

  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.9 B 70-79.9 C 60-69.9 D below 60.0 F 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 • Find the average of n tests • 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.1 Prompt the user for number of tests 1.2 Read the number of tests

  17. 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 Add up the test grades 2.2 Divide the total by n

  18. 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

  19. Refining the Grade Program 1.1 Prompt the user for number of tests 1.2 Read the number of tests 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 2.2 Divide 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.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 2.2 Divide 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.1 Prompt the user for the test grade 2.1.2 Read the test grade 2.1.3 Add it to the total } 2.2 Divide 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.3 Add it to the total } 2.2 Divide 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.2 Divide 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); } }

More Related