Agenda Warmup Lesson 1 6 Dowhile loops sentinels

  • Slides: 11
Download presentation
Agenda • Warmup • Lesson 1. 6 (Do-while loops, sentinels, etc) • Guided Practice

Agenda • Warmup • Lesson 1. 6 (Do-while loops, sentinels, etc) • Guided Practice (1. 6 Assignments) • (Time Permitting): Lesson 1. 7 • Closure Activity Students will be able to: • know how to write a do-while loop • Understand how a do-while loop differs from a while loop • Know what a sentinel is • Use the Math class to accomplish certain arithmetic operations • See how today's lesson fits into the unit and the course as a whole

Socrative. com - EASTQUINN 1) char x = ‘a’; char y = ‘b’; System.

Socrative. com - EASTQUINN 1) char x = ‘a’; char y = ‘b’; System. out. print(x + y); // result? 2) String z = “a”; if (2 > 3 || z. char. At(1)==‘a’) // result? 3) 45 % 63 = 4) 45 / 63 = 5) int x = 0; while (x>0); { System. out. print(x); } // result? 6) What is the name for a variable that determines how many times a loop iterates? 7) What is the name for a variable in a loop that calculates a sum or product? 8) What is the most important advantage of using Object-Oriented programming languages, such as Java? 9) True/False: the center of the Earth is liquid. 10) Is Java a low-level or high-level language? What’s the difference?

 • Quiz: Tomorrow

• Quiz: Tomorrow

Do-While Loops • A do-while loop is always guaranteed to iterate at least once.

Do-While Loops • A do-while loop is always guaranteed to iterate at least once. • This is because the condition is evaluated at the end of the loop, instead of the beginning (as is done in while and for loops). • Note: a do-while loop is the only loop that requires a semi-colon. • Demo: Do. While. Demo

Local vs global variables • If you declare a variable in a loop, then

Local vs global variables • If you declare a variable in a loop, then that variable is local to that loop – meaning that it will not be recognized outside of the loop. • In order to make the variable global, or accessible throughout a program, declare it outside of the loop. • Show in Do. While. Demo.

 • Sometimes the user determines when a loop will stop iterating. • Demo:

• Sometimes the user determines when a loop will stop iterating. • Demo: Do. While. Demo 2

Sentinels • A sentinel is a value that the user can enter in order

Sentinels • A sentinel is a value that the user can enter in order to stop the loop • Usually seen in while or do-while loops • Demo: Sentinel

Java’s Math class • Allows you to do basic math functions • Not necessary

Java’s Math class • Allows you to do basic math functions • Not necessary to import it • 3 methods (functions) you must know: Name of method What it returns (what type the answer is) Math. abs(num) an int Math. pow(base, exponent) a double Math. sqrt(num) a double Demo: Math. Demo

 • To help with today’s assignments… – Open Powerpoint 1. 4 – review

• To help with today’s assignments… – Open Powerpoint 1. 4 – review the String examples, especially the substring method. – Also open the assignment Middle

Assignment: Space. Checker Prompt the user to enter their first & last name (using

Assignment: Space. Checker Prompt the user to enter their first & last name (using 1 variable, not separately). Then…. 1) Error check: The name should contain a space, and should be at least 5 characters long. If not, make them try again until they get it right. 2) Display how many times an invalid name was entered. 3) Display whether or not the name is the same as yours. 4) Display the number of characters in the name (not including the space). 5) Display the first, third, and last characters (of the full name). 6) Display the name in all caps. 7) If the letters a and z (lowercase) are both in the name, display “both. ” 8) Display the 3 rd to the 5 th (including the 5 th) characters in the name. 9) Display the 2 nd to the last (including the last) characters in the name. 10) Display the person’s initials only; display “whoa” if they’re the same. 11) Using a do-while loop, display each character in the person’s name on separate lines. 12) Display how many times the letter e appears in the name.

Assignment: QBRating • Click here for the NFL QB Rating Formula. • Write a

Assignment: QBRating • Click here for the NFL QB Rating Formula. • Write a program that asks the user to enter an NFL QB’s stats (completions, attempts, yards, TDs, and interceptions), and calculates his rating. Round off the rating to 1 decimal place. • On the QB rating page, take note of the line “a, b, c, and d can not…” – this means that if any of them exceed 2. 375, then you must set that one equal to 2. 375, and if any fall below 0, you must set that one to 0. • Then, click here for Nick Foles’ 2018 stats, and use them to test your program, making sure that you calculate his correct QB rating (96. 0). • Once the program works properly, add a do-while loop that wraps around much of the code so that after calculating a QB Rating, you ask the user (yes/no) if they would like to look up another. The user should be able to repeat the process as many times as they want to. • If the user looks up 3 or more QB Ratings, you should start displaying “WHY? ” every time you display the rating. • If a QB rating is over 120 or under 20, display “system crash” and stop the loop from iterating.