comp 110 augustus gloop, augustus gloop luv kohli september 24, 2008 mwf 2-2:50 pm sitterson 014

31
COMP 110 COMP 110 Augustus G Augustus G loop loop , Augustus , Augustus G G loop loop Luv Kohli September 24, 2008 MWF 2-2:50 pm Sitterson 014

Upload: evelyn-owen

Post on 20-Jan-2018

215 views

Category:

Documents


0 download

DESCRIPTION

Some notes about printing your code Print from jGRASP if you can Print with a fixed-width font (such as Consolas or Courier New ) Print double-sided if you can 3

TRANSCRIPT

Page 1: COMP 110 Augustus Gloop, Augustus Gloop Luv Kohli September 24, 2008 MWF 2-2:50 pm Sitterson 014

COMP 110COMP 110Augustus GAugustus Glooploop, Augustus G, Augustus Glooploop……

Luv KohliSeptember 24, 2008

MWF 2-2:50 pmSitterson 014

Page 2: COMP 110 Augustus Gloop, Augustus Gloop Luv Kohli September 24, 2008 MWF 2-2:50 pm Sitterson 014

AnnouncementsAnnouncementsProgram 2 due Friday, 2pm◦Remember to also print out your code and

hand it in

I will be out of town on Friday◦Recitation will still be held by another

COMP110 instructor at the usual time◦No office hours Friday morning

Ask me questions before 6pm Thursday

2

Page 3: COMP 110 Augustus Gloop, Augustus Gloop Luv Kohli September 24, 2008 MWF 2-2:50 pm Sitterson 014

Some notes about printing your codeSome notes about printing your code

Print from jGRASP if you can

Print with a fixed-width font (such as Consolas or Courier New)

Print double-sided if you can

3

Page 4: COMP 110 Augustus Gloop, Augustus Gloop Luv Kohli September 24, 2008 MWF 2-2:50 pm Sitterson 014

Questions?Questions?

4

Page 5: COMP 110 Augustus Gloop, Augustus Gloop Luv Kohli September 24, 2008 MWF 2-2:50 pm Sitterson 014

Today in COMP 110Today in COMP 110Nested loops

Some comments on Lab 3

5

Page 6: COMP 110 Augustus Gloop, Augustus Gloop Luv Kohli September 24, 2008 MWF 2-2:50 pm Sitterson 014

Nested loopsNested loopsJust like we have nested if/else

statements, we can have nested loops

6

Page 7: COMP 110 Augustus Gloop, Augustus Gloop Luv Kohli September 24, 2008 MWF 2-2:50 pm Sitterson 014

Nested loops example: friendly greetingsNested loops example: friendly greetings

7

Page 8: COMP 110 Augustus Gloop, Augustus Gloop Luv Kohli September 24, 2008 MWF 2-2:50 pm Sitterson 014

Nested loops example: friendly greetingsNested loops example: friendly greetings

Student 1 shakes Student 4’s handStudent 1 shakes Student 5’s handStudent 1 shakes Student 6’s handStudent 2 shakes Student 4’s handStudent 2 shakes Student 5’s handStudent 2 shakes Student 6’s handStudent 3 shakes Student 4’s handStudent 3 shakes Student 5’s handStudent 3 shakes Student 6’s hand

8

Page 9: COMP 110 Augustus Gloop, Augustus Gloop Luv Kohli September 24, 2008 MWF 2-2:50 pm Sitterson 014

Nested loops example: friendly greetingsNested loops example: friendly greetings

Student 1 shakes Student 4’s hand shakes Student 5’s hand shakes Student 6’s handStudent 2 shakes Student 4’s hand shakes Student 5’s hand shakes Student 6’s handStudent 3 shakes Student 4’s hand shakes Student 5’s hand shakes Student 6’s hand

9

Page 10: COMP 110 Augustus Gloop, Augustus Gloop Luv Kohli September 24, 2008 MWF 2-2:50 pm Sitterson 014

Nested loops example: friendly greetingsNested loops example: friendly greetings

for (every student in line A){ Student in line A shakes every Student’s hand in line B}

10

Page 11: COMP 110 Augustus Gloop, Augustus Gloop Luv Kohli September 24, 2008 MWF 2-2:50 pm Sitterson 014

Nested loops example: friendly greetingsNested loops example: friendly greetings

for (every student in line A){ for (every student in line B) { (Student in line A) shakes (Student in line

B)’s hand }}

11

Inner loopOuter loop

Page 12: COMP 110 Augustus Gloop, Augustus Gloop Luv Kohli September 24, 2008 MWF 2-2:50 pm Sitterson 014

Nested loops example: friendly greetingsNested loops example: friendly greetings

for (int stdLineA = 1; stdLineA <= 3; stdLineA++){ for (int stdLineB = 4; stdLineB <= 6; stdLineB+

+) { System.out.println(“Student ” + stdLineA + “ shakes Student ” + stdLineB + “’s hand.”); }}

12

stdLineA123

stdLineB456

Page 13: COMP 110 Augustus Gloop, Augustus Gloop Luv Kohli September 24, 2008 MWF 2-2:50 pm Sitterson 014

Nested loops example: friendly greetingsNested loops example: friendly greetings

for (int stdLineA = 1; stdLineA <= 3; stdLineA++){ for (int stdLineB = 4; stdLineB <= 6; stdLineB+

+) { System.out.println(“Student ” + stdLineA + “ shakes Student ” + stdLineB + “’s hand.”); }}

13

Inner loop

Outer loop

Page 14: COMP 110 Augustus Gloop, Augustus Gloop Luv Kohli September 24, 2008 MWF 2-2:50 pm Sitterson 014

Nested loopsNested loopsYou can nest different kinds of loops

inside other loops, or put if/else statements inside loops, or put loops inside if/else statements, or…

ExamAverager example in jGRASP

14

Page 15: COMP 110 Augustus Gloop, Augustus Gloop Luv Kohli September 24, 2008 MWF 2-2:50 pm Sitterson 014

Loop bugsLoop bugsInfinite loops – already talked about

these

Off-by-one errors

15

Page 16: COMP 110 Augustus Gloop, Augustus Gloop Luv Kohli September 24, 2008 MWF 2-2:50 pm Sitterson 014

Off-by-one errorsOff-by-one errorsLoop repeats one too many or one too

few times

for (count = 1; count < 10; count++) // loop 9 times: 1, 2, 3, ... 9

for (count = 1; count <= 10; count++) // loop 10 times: 1, 2, 3, ... 10

for (count = 0; count < 10; count++) // loop 10 times: 0, 1, 2, ... 9

16

Page 17: COMP 110 Augustus Gloop, Augustus Gloop Luv Kohli September 24, 2008 MWF 2-2:50 pm Sitterson 014

Finding errorsFinding errorsTrace your variables◦ Put output statements in your code to see what

values are stored in your variables System.out.println(variable); Check whether the values are correct and what you expect

◦ Remove these extra output statements after the program runs correctly

◦ Read example in the book, p. 188 (4th edition), p. 218 (5th edition)

Use a debugger

17

Page 18: COMP 110 Augustus Gloop, Augustus Gloop Luv Kohli September 24, 2008 MWF 2-2:50 pm Sitterson 014

What does this code do?What does this code do? int a = 0; int b = 0; for (int i = 1; i <= 6; i++) { if (i % 2 == 0) { b = b + i;} else {a = a + i;}}

18

Page 19: COMP 110 Augustus Gloop, Augustus Gloop Luv Kohli September 24, 2008 MWF 2-2:50 pm Sitterson 014

What does this code do?What does this code do? int oddSum = 0; int evenSum = 0; for (int i = 1; i <= 6; i++) { if (i % 2 == 0) { evenSum = evenSum + i;} else {oddSum = oddSum + i;}}

19

Page 20: COMP 110 Augustus Gloop, Augustus Gloop Luv Kohli September 24, 2008 MWF 2-2:50 pm Sitterson 014

What does this code do?What does this code do?int oddSum = 0;int evenSum = 0;for (int i = 1; i <= 6; i++){ if (i % 2 == 0) { evenSum = evenSum + i; } else { oddSum = oddSum + i; }}

20

Page 21: COMP 110 Augustus Gloop, Augustus Gloop Luv Kohli September 24, 2008 MWF 2-2:50 pm Sitterson 014

What is wrong with this code?What is wrong with this code? int oddSum = 0; int evenSum = 0; for (int i = 1; i <= 6; i++) { if (i % 2 == 0) evenSum = evenSum + i;} else {oddSum = oddSum + i;}}

21

Page 22: COMP 110 Augustus Gloop, Augustus Gloop Luv Kohli September 24, 2008 MWF 2-2:50 pm Sitterson 014

What is wrong with this code?What is wrong with this code?int oddSum = 0;int evenSum = 0;for (int i = 1; i <= 6; i++){ if (i % 2 == 0) evenSum = evenSum + i; } else { oddSum = oddSum + i; }}

22

Page 23: COMP 110 Augustus Gloop, Augustus Gloop Luv Kohli September 24, 2008 MWF 2-2:50 pm Sitterson 014

IndentationIndentationIndentation◦Makes code easier to read◦Helps with finding syntax and logic errors◦ Indent code that goes between { and }

Be consistent!

23

Page 24: COMP 110 Augustus Gloop, Augustus Gloop Luv Kohli September 24, 2008 MWF 2-2:50 pm Sitterson 014

ScopeScopeVariables declared in outer scopes are

visible to code inside inner scopes

public static void main(String[] args) { int total = 15; int n = 5; if (n <= 10) { total = total + n; } System.out.println(total); }

outer inner

24

Page 25: COMP 110 Augustus Gloop, Augustus Gloop Luv Kohli September 24, 2008 MWF 2-2:50 pm Sitterson 014

ScopeScopeVariables declared in inner scopes are

NOT visible to outer code

public static void main(String[] args) { int n = 5; if (n <= 10) { int total = 15 + n; } System.out.println(total); // ERROR!!! }

outerinner

25

Page 26: COMP 110 Augustus Gloop, Augustus Gloop Luv Kohli September 24, 2008 MWF 2-2:50 pm Sitterson 014

elseelseif (inputString.equals(“BLUE”)) eyeColor = Color.BLUE;else if (inputString.equals(“GREEN”)) eyeColor = Color.GREEN;else if (inputString.equals(“RED”)) eyeColor = Color.RED;else if (!inputString.equals(“BLUE”) && !inputString.equals(“GREEN”) && !inputString.equals(“RED”)) eyeColor = Color.WHITE;

26

Page 27: COMP 110 Augustus Gloop, Augustus Gloop Luv Kohli September 24, 2008 MWF 2-2:50 pm Sitterson 014

elseelseif (inputString.equals(“BLUE”)) eyeColor = Color.BLUE;else if (inputString.equals(“GREEN”)) eyeColor = Color.GREEN;else if (inputString.equals(“RED”)) eyeColor = Color.RED;else eyeColor = Color.WHITE;

27

Page 28: COMP 110 Augustus Gloop, Augustus Gloop Luv Kohli September 24, 2008 MWF 2-2:50 pm Sitterson 014

elseelse not needed when empty not needed when emptyif (inputString.equals(“MOUTH”)){ mouthStartAngle = 0;}else{}

28

Page 29: COMP 110 Augustus Gloop, Augustus Gloop Luv Kohli September 24, 2008 MWF 2-2:50 pm Sitterson 014

Multi-line commentsMulti-line comments/* This is a multi-line comment. What do you think of it? */

// You can also have multi-line comments// this way.

29

Page 30: COMP 110 Augustus Gloop, Augustus Gloop Luv Kohli September 24, 2008 MWF 2-2:50 pm Sitterson 014

Too many commentsToo many commentsDon’t need to comment every line of code,

especially when it is obvious what your code is doing

// set count to 12count = 12;

Only comment to enhance understanding and to explain why your code does what it does

30

Page 31: COMP 110 Augustus Gloop, Augustus Gloop Luv Kohli September 24, 2008 MWF 2-2:50 pm Sitterson 014

FridayFridayProgram 2 due, 2pm

Lab 4 help

31 31