building java programs chapter 2 days of for loops past
TRANSCRIPT
BUILDING JAVA PROGRAMSCHAPTER 2Days of For Loops Past
days until the AP Computer Science
test
227
5488hours
Review of Project 1.3
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)
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)
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!
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)
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)
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.”); … }}
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)
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(); … }
…}
ObjectivesAt 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.
Simple for loop (Review)
for (int count = 1; count <= 1000; count++) { System.out.println("All work and no play” + “makes Jack a dull boy.");}
Expressions for the counterint highCelsiusTemp = 100;
for(double fTemp = 32; i <= highCelsiusTemp * 1.8 + 32; i += 1.8) { System.out.println(“Temperature: “ + fTemp);}
3233.835.6…212
Multi-line Loop Body+----+\ // \\ // \\ // \+----+
System.out.println("+----+");
for (int i = 1; i <= 3; i++) { System.out.println("\\ /"); System.out.println("/ \\");}
System.out.println("+----+");
Multi-line Loop BodySystem.out.println("+----+");
for (int i = 1; i <= 3; i++) System.out.println("\\ /"); System.out.println("/ \\");
System.out.println("+----+");
+----+\ /\ /\ // \+----+
Homework• Self Check 2.26, 2.27, 2.32, 2.34, 2.35• Exercise 2.2, 2.3
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(“)”);
String ConcatenationSystem.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)”
String ConcatenationSystem.out.println(3 + 4 + “ = “ + 2 + 5);
3 + 4 + “ = “ + 2 + 5
7 + “ = “ + 2 + 5
“7 = “ + 2 + 5
“7 = 2“ + 5
“7 = 25“
Nested for Loopsfor(int row = 0; row < 5; row++) { for(int col = 0; col < 3; col++) { System.out.print(“+”); } System.out.println();}
+++++++++++++++ 5 sets, 3 reps
Nested for LoopsWhy do we need nested for loops?
for(int row = 0; row < 5; row++) { System.out.println(“***”);}
123451234123121
for(int row = 0; row < 5; row++) { for(int col = 1; col <= 5 - row; col++) { System.out.println(col); }}
Homework• Self Check 2.26, 2.27, 2.32, 2.34, 2.35• Exercise 2.2, 2.3, 2.4, 2.5