Presentation is loading. Please wait.

Presentation is loading. Please wait.

Agenda Warmup Lesson 1.6 (Do-while loops, sentinels, etc)

Similar presentations


Presentation on theme: "Agenda Warmup Lesson 1.6 (Do-while loops, sentinels, etc)"— Presentation transcript:

1 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

2 Warmup char x = ‘a’; char y = ‘b’; System.out.print(x + y); // result?
String z = “a”; if (2 > 3 || z.charAt(1)==‘a’) // result? 45 % 63 = 45 / 63 = int x = 0; while (x>0); { System.out.print(x); } // result?

3 Quiz: Tuesday

4 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: DoWhileDemo

5 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. Demo

6 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

7 Java’s Math class Allows you to do basic math functions
Not necessary to import it 3 methods you must know: Math.abs(num) // find the absolute value of num Math.pow(base, exp) // raises base to the exp power Math.sqrt(num) // finds the square root of num Demo: MathDemo

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

9 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 it equal to 2.375, and if any fall below 0, you must set it to 0. Then, click here for Tom Brady’s 2016 stats, and use them to test your program, making sure that you calculate his correct QB rating (112.2). 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. (see next slide)

10 Assignment: SpaceChecker
Prompt the user to enter their first & last name (using 1 variable, not separately). Then…. Error check: If the user forgot to enter a space, make them try again until they get it right. Display the number of characters in the name (not including the space). Display the first, third, and last characters (of the full name). Display the name in all caps. If the letters a and z (lowercase) are both in the name, display “both.” Display the characters from index #4 to (and including) index #6 Display the characters from index #2 to the end of the name Display the person’s initials only. Using a do-while loop, display each character in the person’s name on separate lines.


Download ppt "Agenda Warmup Lesson 1.6 (Do-while loops, sentinels, etc)"

Similar presentations


Ads by Google