building java programs chapter 2 days of for loops past

23
BUILDING JAVA PROGRAMS CHAPTER 2 Days of For Loops Past

Upload: jemimah-carson

Post on 16-Jan-2016

224 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: BUILDING JAVA PROGRAMS CHAPTER 2 Days of For Loops Past

BUILDING JAVA PROGRAMSCHAPTER 2Days of For Loops Past

Page 2: BUILDING JAVA PROGRAMS CHAPTER 2 Days of For Loops Past

days until the AP Computer Science

test

227

5488hours

Page 3: BUILDING JAVA PROGRAMS CHAPTER 2 Days of For Loops Past

Review of Project 1.3

Page 4: BUILDING JAVA PROGRAMS CHAPTER 2 Days of For Loops Past

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)

Page 5: BUILDING JAVA PROGRAMS CHAPTER 2 Days of For Loops Past

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)

Page 6: BUILDING JAVA PROGRAMS CHAPTER 2 Days of For Loops Past

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!

Page 7: BUILDING JAVA PROGRAMS CHAPTER 2 Days of For Loops Past

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)

Page 8: BUILDING JAVA PROGRAMS CHAPTER 2 Days of For Loops Past

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)

Page 9: BUILDING JAVA PROGRAMS CHAPTER 2 Days of For Loops Past

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.”); … }}

Page 10: BUILDING JAVA PROGRAMS CHAPTER 2 Days of For Loops Past

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)

Page 11: BUILDING JAVA PROGRAMS CHAPTER 2 Days of For Loops Past

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(); … }

…}

Page 12: BUILDING JAVA PROGRAMS CHAPTER 2 Days of For Loops Past

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.

Page 13: BUILDING JAVA PROGRAMS CHAPTER 2 Days of For Loops Past

Simple for loop (Review)

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

Page 14: BUILDING JAVA PROGRAMS CHAPTER 2 Days of For Loops Past

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

Page 15: BUILDING JAVA PROGRAMS CHAPTER 2 Days of For Loops Past

Multi-line Loop Body+----+\ // \\ // \\ // \+----+

System.out.println("+----+");

for (int i = 1; i <= 3; i++) { System.out.println("\\ /"); System.out.println("/ \\");}

System.out.println("+----+");

Page 16: BUILDING JAVA PROGRAMS CHAPTER 2 Days of For Loops Past

Multi-line Loop BodySystem.out.println("+----+");

for (int i = 1; i <= 3; i++) System.out.println("\\ /"); System.out.println("/ \\");

System.out.println("+----+");

+----+\ /\ /\ // \+----+

Page 17: BUILDING JAVA PROGRAMS CHAPTER 2 Days of For Loops Past

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

Page 18: BUILDING JAVA PROGRAMS CHAPTER 2 Days of For Loops Past

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(“)”);

Page 19: BUILDING JAVA PROGRAMS CHAPTER 2 Days of For Loops Past

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)”

Page 20: BUILDING JAVA PROGRAMS CHAPTER 2 Days of For Loops Past

String ConcatenationSystem.out.println(3 + 4 + “ = “ + 2 + 5);

3 + 4 + “ = “ + 2 + 5

7 + “ = “ + 2 + 5

“7 = “ + 2 + 5

“7 = 2“ + 5

“7 = 25“

Page 21: BUILDING JAVA PROGRAMS CHAPTER 2 Days of For Loops Past

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

Page 22: BUILDING JAVA PROGRAMS CHAPTER 2 Days of For Loops Past

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); }}

Page 23: BUILDING JAVA PROGRAMS CHAPTER 2 Days of For Loops Past

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