[ ] review exam [ ] questions [ ] lab 6 [ ] hw6 [ ] method block diagram [ ] methods, methods [ ]...

15
[ ] Review Exam [ ] Questions [ ] Lab 6 [ ] HW6 [ ] Method Block Diagram [ ] Methods, methods [ ] Looping constructs [ ] for loop [ ] ++, -- (Thursday) [ ] Examples [ ] Class exercise

Upload: vincent-stevens

Post on 15-Jan-2016

222 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: [ ] Review Exam [ ] Questions [ ] Lab 6 [ ] HW6 [ ] Method Block Diagram [ ] Methods, methods [ ] Looping constructs [ ] for loop [ ] ++, -- (Thursday)

[ ] Review Exam

[ ] Questions

[ ] Lab 6

[ ] HW6[ ] Method Block Diagram[ ] Methods, methods[ ] Looping constructs

[ ] for loop[ ] ++, -- (Thursday)

[ ] Examples[ ] Class exercise

Page 2: [ ] Review Exam [ ] Questions [ ] Lab 6 [ ] HW6 [ ] Method Block Diagram [ ] Methods, methods [ ] Looping constructs [ ] for loop [ ] ++, -- (Thursday)

Lab 6

Put loop logic in seuController main to exit only when user enters 9.

analyzeSentence - how many words in sentence?

consoleMethods class

What about $ ? It blows up! We’ll catch that in a couple weeks.

Page 3: [ ] Review Exam [ ] Questions [ ] Lab 6 [ ] HW6 [ ] Method Block Diagram [ ] Methods, methods [ ] Looping constructs [ ] for loop [ ] ++, -- (Thursday)

Method Block Diagram

seuController.java systemMethods.java

seuCalculator.java consoleMethods.java

Page 4: [ ] Review Exam [ ] Questions [ ] Lab 6 [ ] HW6 [ ] Method Block Diagram [ ] Methods, methods [ ] Looping constructs [ ] for loop [ ] ++, -- (Thursday)

‘ask permission’

Page 5: [ ] Review Exam [ ] Questions [ ] Lab 6 [ ] HW6 [ ] Method Block Diagram [ ] Methods, methods [ ] Looping constructs [ ] for loop [ ] ++, -- (Thursday)

‘ask forgiveness’

Page 6: [ ] Review Exam [ ] Questions [ ] Lab 6 [ ] HW6 [ ] Method Block Diagram [ ] Methods, methods [ ] Looping constructs [ ] for loop [ ] ++, -- (Thursday)

while( )

{

}

Pre-condition loopuser-controlledwhat goes in the blank spaces?What is the while-body?

Page 7: [ ] Review Exam [ ] Questions [ ] Lab 6 [ ] HW6 [ ] Method Block Diagram [ ] Methods, methods [ ] Looping constructs [ ] for loop [ ] ++, -- (Thursday)

// illustration of a ‘primer event’ with while

Scanner cscan = new Scanner(System.in);int num1 = 0;

System.out.println(“Please enter > 0: ”);num1 = cscan.nextInt();

while( !(num1 > 0 ))

{ System.out.println(“Invalid entry.”);

System.out.println(“Please enter > 0: ”);

num1 = cscan.nextInt();

}

System.out.println(“Good work!”);

Page 8: [ ] Review Exam [ ] Questions [ ] Lab 6 [ ] HW6 [ ] Method Block Diagram [ ] Methods, methods [ ] Looping constructs [ ] for loop [ ] ++, -- (Thursday)

// illustration of a ‘loop controller’ (usually a boolean)

Scanner cscan = new Scanner(System.in);int num1 = 0;

boolean iAmNotHappy = true.

while( iAmNotHappy)

{

System.out.println(“Please enter > 0: ”);num1 = cscan.nextInt();if (num1 > 0)

iAmNotHappy = false;

else

System.out.println(“Invalid entry.”);

}

System.out.println(“Good work!”);

Page 9: [ ] Review Exam [ ] Questions [ ] Lab 6 [ ] HW6 [ ] Method Block Diagram [ ] Methods, methods [ ] Looping constructs [ ] for loop [ ] ++, -- (Thursday)

do

{

}

while( );

Post-condition loopuser-controlledwhat goes in the blank spaces?What is the do-body?

Page 10: [ ] Review Exam [ ] Questions [ ] Lab 6 [ ] HW6 [ ] Method Block Diagram [ ] Methods, methods [ ] Looping constructs [ ] for loop [ ] ++, -- (Thursday)

do

{

System.out.println(“here we go ….”);

}

while( 1 == 1 );

Post-condition loopuser-controlled

What is the do-body?

Page 11: [ ] Review Exam [ ] Questions [ ] Lab 6 [ ] HW6 [ ] Method Block Diagram [ ] Methods, methods [ ] Looping constructs [ ] for loop [ ] ++, -- (Thursday)

int num = 0;

num++

++num

num = num + 1

All these accomplish the same thing in the end.Specifically, they all increment num by 1.BUT – context is the conundrum

int num2 = 5;

num2++;

int num3 = -3;

num3 = --num2;

int num4 = 5;

num4 = num2++ + --num3

Page 12: [ ] Review Exam [ ] Questions [ ] Lab 6 [ ] HW6 [ ] Method Block Diagram [ ] Methods, methods [ ] Looping constructs [ ] for loop [ ] ++, -- (Thursday)

int num4 = 5;

System.out.println(num4++);

System.out.println(“Really, num4 is “ + num4);

int num5 = 5;

System.out.println(“Hello “ + --num5);

System.out.println(“Really, num5 is “ + num5);

int num6 = 3;

int num7 = -6;

int num8 = 0;

num8 = --num6 + num7++;

System.out.println(“num8 is “ + num8);

Page 13: [ ] Review Exam [ ] Questions [ ] Lab 6 [ ] HW6 [ ] Method Block Diagram [ ] Methods, methods [ ] Looping constructs [ ] for loop [ ] ++, -- (Thursday)

for ( ; ; )

{

}

Pre-condition looploop-controlled

What is the ‘for-body’

Is the index automatically incremented? When?What goes in all the blank areas?

Page 14: [ ] Review Exam [ ] Questions [ ] Lab 6 [ ] HW6 [ ] Method Block Diagram [ ] Methods, methods [ ] Looping constructs [ ] for loop [ ] ++, -- (Thursday)

for ( starting index ;logical expression; index mover)

{

for bodyindex++

}

The ‘index mover’ is implicit – automatically done on way up.

Note: DO NOT MUCK WITH AN INDEX

Page 15: [ ] Review Exam [ ] Questions [ ] Lab 6 [ ] HW6 [ ] Method Block Diagram [ ] Methods, methods [ ] Looping constructs [ ] for loop [ ] ++, -- (Thursday)

for ( int index = 0; index < 5; index++)

{

System.out.println(“hello!”); }

Hand trace Output