Download presentation
Presentation is loading. Please wait.
Published byIveta Pokorná Modified over 5 years ago
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
Socrative.com - EASTQUINN
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 = 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?
3
Quiz: Tomorrow
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. Show in DoWhileDemo.
6
Sometimes the user determines when a loop will stop iterating.
Demo: DoWhileDemo2
7
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
8
What it returns (what type the answer is)
Java’s Math class Allows you to do basic math functions Not necessary to import it 3 methods (functions) you must know: Demo: MathDemo 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)
9
To help with today’s assignments…
Open Powerpoint 1.4 – review the String examples, especially the substring method. Also open the assignment Middle
10
Assignment: SpaceChecker
Prompt the user to enter their first & last name (using 1 variable, not separately). Then…. 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. Display how many times an invalid name was entered. Display whether or not the name is the same as yours. 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 3rd to the 5th (including the 5th) characters in the name. Display the 2nd to the last (including the last) characters in the name. Display the person’s initials only; display “whoa” if they’re the same. Using a do-while loop, display each character in the person’s name on separate lines. Display how many times the letter e appears in the name. (see next slide)
11
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.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.