Presentation is loading. Please wait.

Presentation is loading. Please wait.

BUILDING JAVA PROGRAMS CHAPTER 2 Days of For Loops Past.

Similar presentations


Presentation on theme: "BUILDING JAVA PROGRAMS CHAPTER 2 Days of For Loops Past."— Presentation transcript:

1 BUILDING JAVA PROGRAMS CHAPTER 2 Days of For Loops Past

2 days until the AP Computer Science test hours

3 Review of Project 1.3

4 Review of Project 1.3 (Rubric) Code is clearly commented, including your name and class period (1 point) Program compiles (1 point) Program produces the correct output (1 point) Use methods for each verse and the refrain (1 point) Repetitive code is isolated into methods (1 point)

5 Review of Project 1.3 (Rubric) Code is clearly commented, including your name and class period (1 point) Program compiles (1 point) Program produces the correct output (1 point) Use methods for each verse and the refrain (1 point) Repetitive code is isolated into methods (1 point)

6 Review of Project 1.3 (Common Errors) Program compiles (1 point) The public type OldLady must be defined in it’s own file. JD_Proj_1_3.java public class OldLady { … } Filename and class name must match!

7 Review of Project 1.3 (Rubric) Code is clearly commented, including your name and class period (1 point) Program compiles (1 point) Program produces the correct output (1 point) Use methods for each verse and the refrain (1 point) Repetitive code is isolated into methods (1 point)

8 Review of Project 1.3 (Rubric) Code is clearly commented, including your name and class period (1 point) Program compiles (1 point) Program produces the correct output (1 point) Use methods for each verse and the refrain (1 point) Repetitive code is isolated into methods (1 point)

9 Review of Project 1.3 (Common Errors) Use methods for each verse and the refrain (1 point) public class OldLady { public static void main(String args[]) { System.out.println(“There was an old lady who swallowed a fly.”); flyRefrain(); … System.out.println(“There was an old lady who swallowed a spider.”); … }

10 Review of Project 1.3 (Rubric) Code is clearly commented, including your name and class period (1 point) Program compiles (1 point) Program produces the correct output (1 point) Use methods for each verse and the refrain (1 point) Repetitive code is isolated into methods (1 point)

11 Review of Project 1.3 (Common Errors) Repetitive code is isolated into methods (1 point) public class OldLady { public static void spiderRefrain(String args[]) { System.out.println(“She swallowed the spider to catch the fly.”); } public static void birdRefrain(String args[]) { System.out.println(“She swallowed the bird to catch the spider.”); } … public static void cowVerse() { … cowRefrain(); dogRefrain(); catRefrain(); birdRefrain(); … } … }

12 Objectives At the end of you class you will be able to… Write for loops with multiple statements in the body. Describe when to use for loops with complex expressions. Trace the program flow of programs with nested for loops.

13 Simple for loop (Review) for (int count = 1; count <= 1000; count++) { System.out.println("All work and no play” + “makes Jack a dull boy."); }

14 Expressions for the counter int highCelsiusTemp = 100; for(double fTemp = 32; i <= highCelsiusTemp * 1.8 + 32; i += 1.8) { System.out.println(“Temperature: “ + fTemp); } 32 33.8 35.6 … 212

15 Multi-line Loop Body +----+ \ / / \ \ / / \ \ / / \ +----+ System.out.println("+----+"); for (int i = 1; i <= 3; i++) { System.out.println("\\ /"); System.out.println("/ \\"); } System.out.println("+----+");

16 Multi-line Loop Body System.out.println("+----+"); for (int i = 1; i <= 3; i++) System.out.println("\\ /"); System.out.println("/ \\"); System.out.println("+----+"); +----+ \ / / \ +----+

17 Homework Self Check 2.26, 2.27, 2.32, 2.34, 2.35 Exercise 2.2, 2.3

18 String Concatenation (2.0,5.1,3.2) (1.8,6.9,2.5) (x,y,z) System.out.print(“(“); System.out.print(x); System.out.print(“,”); System.out.print(y); System.out.print(“,”); System.out.print(z); System.out.print(“)”);

19 String Concatenation System.out.print( “(“ + x + “,” + y + “,” + z + “)”); Addition of strings: x = 2.1, y = 3.2, z = 4.8 “(“ + x + “,” + y + “,” + z + “)”  “(2.1” + “,” + y + “,” + z + “)”  “(2.1,” + y + “,” + z + “)”  “(2.1,3.2” + “,” + z + “)”  “(2.1,3.2,” + z “)”  “(2.1,3.2,4.8” + “)”  “(2.1,3.2,4.8)”

20 String Concatenation System.out.println(3 + 4 + “ = “ + 2 + 5); 3 + 4 + “ = “ + 2 + 5 7 + “ = “ + 2 + 5 “7 = “ + 2 + 5 “7 = 2“ + 5 “7 = 25“

21 Nested for Loops for(int row = 0; row < 5; row++) { for(int col = 0; col < 3; col++) { System.out.print(“+”); } System.out.println(); } +++ 5 sets, 3 reps

22 Nested for Loops Why do we need nested for loops? for(int row = 0; row < 5; row++) { System.out.println(“***”); } 12345 1234 123 12 1 for(int row = 0; row < 5; row++) { for(int col = 1; col <= 5 - row; col++) { System.out.println(col); }

23 Homework Self Check 2.26, 2.27, 2.32, 2.34, 2.35 Exercise 2.2, 2.3, 2.4, 2.5


Download ppt "BUILDING JAVA PROGRAMS CHAPTER 2 Days of For Loops Past."

Similar presentations


Ads by Google