professor: alvin chaoeduc.jmu.edu › ~chaoaj › cs149fall2016 › slides › lecture16.pdfexercise...

31
Professor: Alvin Chao

Upload: others

Post on 28-Jun-2020

8 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Professor: Alvin Chaoeduc.jmu.edu › ~chaoaj › cs149fall2016 › slides › lecture16.pdfExercise #2 l Complete the following method using an enhanced for loop: /** * This method

Professor:AlvinChao

Page 2: Professor: Alvin Chaoeduc.jmu.edu › ~chaoaj › cs149fall2016 › slides › lecture16.pdfExercise #2 l Complete the following method using an enhanced for loop: /** * This method

CS149 – For Each and Reference Arrays

Page 3: Professor: Alvin Chaoeduc.jmu.edu › ~chaoaj › cs149fall2016 › slides › lecture16.pdfExercise #2 l Complete the following method using an enhanced for loop: /** * This method

Looping Over the Contents of an Array

l We often use a for loop to access each element in an array:

l If only there were a better way…

for (int i = 0; i < names.length; i++) {System.out.println("Hello " + names[i]);

}

Page 4: Professor: Alvin Chaoeduc.jmu.edu › ~chaoaj › cs149fall2016 › slides › lecture16.pdfExercise #2 l Complete the following method using an enhanced for loop: /** * This method

Enhanced For Loop(also called a for each loop)

l This loop does the same thing as the loop on the previous slide:

l This code is shorter, easier to understand, less error-prone

for (String name : names) {System.out.println("Hello " + name);

}

This variable will be assigned the elements from this array

Page 5: Professor: Alvin Chaoeduc.jmu.edu › ~chaoaj › cs149fall2016 › slides › lecture16.pdfExercise #2 l Complete the following method using an enhanced for loop: /** * This method

When To Use an Enhanced For Loop

l Alwaysl Unless you can't:

− Need to modify the array− Need to know the element index for some reason− Need to process the elements out of order− ...

Page 6: Professor: Alvin Chaoeduc.jmu.edu › ~chaoaj › cs149fall2016 › slides › lecture16.pdfExercise #2 l Complete the following method using an enhanced for loop: /** * This method

Exercise #1

1)What will be printed by the following code?

2)Where is the style problem in this code?String[] summer = {"June", "July", "August"};

String letters = "";

for (String i : summer) {letters += i.charAt(0);

}

System.out.println(letters);

Page 7: Professor: Alvin Chaoeduc.jmu.edu › ~chaoaj › cs149fall2016 › slides › lecture16.pdfExercise #2 l Complete the following method using an enhanced for loop: /** * This method

Exercise #1

1)What will be printed by the following code?1)JJA

2)Where is the style problem in this code?String[] summer = {"June", "July", "August"};

String letters = "";

for (String i : summer) {letters += i.charAt(0);

}

System.out.println(letters);

This is not an index variable, it requires a meaningful name (like “month”).

Page 8: Professor: Alvin Chaoeduc.jmu.edu › ~chaoaj › cs149fall2016 › slides › lecture16.pdfExercise #2 l Complete the following method using an enhanced for loop: /** * This method

Exercise #2l Complete the following method using an enhanced for

loop (reminder: use .equals to compare strings.)/*** This method counts the number of times a target word occurs in* an array of words. Comparisons are case-sensitive.** @paramwords - The array to search* @param target - The word to search for* @returnThe word count*/public static int countWord(String[] words, String target) {

}

Page 9: Professor: Alvin Chaoeduc.jmu.edu › ~chaoaj › cs149fall2016 › slides › lecture16.pdfExercise #2 l Complete the following method using an enhanced for loop: /** * This method

Exercise #2l Complete the following method using an enhanced

for loop:/*** This method counts the number of times a target word occurs in* an array of words. Comparisons are case-sensitive.** @paramwords - The array to search* @param target - The word to search for* @returnThe word count*/public static int countWord(String[] words, String target) {

int count = 0;

for (String curWord : words) {if (curWord.equals(target)) {

count++;}

}

return count;}

Page 10: Professor: Alvin Chaoeduc.jmu.edu › ~chaoaj › cs149fall2016 › slides › lecture16.pdfExercise #2 l Complete the following method using an enhanced for loop: /** * This method

Reference Arrays

Die[] dice;

dice = newDie[4];

dice[0] = newDie(6);dice[2] = newDie(5);

for (Die curDie : dice) {if (curDie != null) {

curDie.roll();}

}

dice

Page 11: Professor: Alvin Chaoeduc.jmu.edu › ~chaoaj › cs149fall2016 › slides › lecture16.pdfExercise #2 l Complete the following method using an enhanced for loop: /** * This method

Reference Arrays

Die[] dice;

dice = newDie[4];

dice[0] = newDie(6);dice[2] = newDie(5);

for (Die curDie : dice) {if (curDie != null) {

curDie.roll();}

}

dice

null

null

null

null

Page 12: Professor: Alvin Chaoeduc.jmu.edu › ~chaoaj › cs149fall2016 › slides › lecture16.pdfExercise #2 l Complete the following method using an enhanced for loop: /** * This method

Reference Arrays

Die[] dice;

dice = newDie[4];

dice[0] = newDie(6);dice[2] = newDie(5);

for (Die curDie : dice) {if (curDie != null) {

curDie.roll();}

}

dice

null

null

null

Die face: 6

Page 13: Professor: Alvin Chaoeduc.jmu.edu › ~chaoaj › cs149fall2016 › slides › lecture16.pdfExercise #2 l Complete the following method using an enhanced for loop: /** * This method

Reference Arrays

Die[] dice;

dice = newDie[4];

dice[0] = newDie(6);dice[2] = newDie(5);

for (Die curDie : dice) {if (curDie != null) {

curDie.roll();}

}

dice

null

null

Die face: 6

Die face: 5

Page 14: Professor: Alvin Chaoeduc.jmu.edu › ~chaoaj › cs149fall2016 › slides › lecture16.pdfExercise #2 l Complete the following method using an enhanced for loop: /** * This method

Exercise #3

l Draw the memory diagram and determine output.Die single;Die[] dice;

dice = newDie[4];

single = newDie(1);

for (int i = 0; i < dice.length; i++) {dice[i] = single;

}

dice[0].setFace(3);

for (Die curDie : dice) {System.out.println(curDie.getFace());

}

Page 15: Professor: Alvin Chaoeduc.jmu.edu › ~chaoaj › cs149fall2016 › slides › lecture16.pdfExercise #2 l Complete the following method using an enhanced for loop: /** * This method

Exercise #3

l Draw the memory diagram and determine output.Die single;Die[] dice;

dice = newDie[4];

single = newDie(1);

for (int i = 0; i < dice.length; i++) {dice[i] = single;

}

dice[0].setFace(3);

for (Die curDie : dice) {System.out.println(curDie.getFace());

}

single

Page 16: Professor: Alvin Chaoeduc.jmu.edu › ~chaoaj › cs149fall2016 › slides › lecture16.pdfExercise #2 l Complete the following method using an enhanced for loop: /** * This method

Exercise #3

l Draw the memory diagram and determine output.Die single;Die[] dice;

dice = newDie[4];

single = newDie(1);

for (int i = 0; i < dice.length; i++) {dice[i] = single;

}

dice[0].setFace(3);

for (Die curDie : dice) {System.out.println(curDie.getFace());

}

single

dice

Page 17: Professor: Alvin Chaoeduc.jmu.edu › ~chaoaj › cs149fall2016 › slides › lecture16.pdfExercise #2 l Complete the following method using an enhanced for loop: /** * This method

Exercise #3

l Draw the memory diagram and determine output.Die single;Die[] dice;

dice = newDie[4];

single = newDie(1);

for (int i = 0; i < dice.length; i++) {dice[i] = single;

}

dice[0].setFace(3);

for (Die curDie : dice) {System.out.println(curDie.getFace());

}

single

dice

null

null

null

null

Page 18: Professor: Alvin Chaoeduc.jmu.edu › ~chaoaj › cs149fall2016 › slides › lecture16.pdfExercise #2 l Complete the following method using an enhanced for loop: /** * This method

Exercise #3l Draw the memory diagram and determine output.

Die single;Die[] dice;

dice = newDie[4];

single = newDie(1);

for (int i = 0; i < dice.length; i++) {dice[i] = single;

}

dice[0].setFace(3);

for (Die curDie : dice) {System.out.println(curDie.getFace());

}

single

dice

null

null

null

null

Die face: 1

Page 19: Professor: Alvin Chaoeduc.jmu.edu › ~chaoaj › cs149fall2016 › slides › lecture16.pdfExercise #2 l Complete the following method using an enhanced for loop: /** * This method

Exercise #3

l Draw the memory diagram and determine output.Die single;Die[] dice;

dice = newDie[4];

single = newDie(1);

for (int i = 0; i < dice.length; i++) {dice[i] = single;

}

dice[0].setFace(3);

for (Die curDie : dice) {System.out.println(curDie.getFace());

}

single

dice

null

null

null

null

Die face: 1

0

i

Page 20: Professor: Alvin Chaoeduc.jmu.edu › ~chaoaj › cs149fall2016 › slides › lecture16.pdfExercise #2 l Complete the following method using an enhanced for loop: /** * This method

Exercise #3

l Draw the memory diagram and determine output.Die single;Die[] dice;

dice = newDie[4];

single = newDie(1);

for (int i = 0; i < dice.length; i++) {dice[i] = single;

}

dice[0].setFace(3);

for (Die curDie : dice) {System.out.println(curDie.getFace());

}

single

dice

null

null

null

Die face: 1

0

i

Page 21: Professor: Alvin Chaoeduc.jmu.edu › ~chaoaj › cs149fall2016 › slides › lecture16.pdfExercise #2 l Complete the following method using an enhanced for loop: /** * This method

Exercise #3

l Draw the memory diagram and determine output.Die single;Die[] dice;

dice = newDie[4];

single = newDie(1);

for (int i = 0; i < dice.length; i++) {dice[i] = single;

}

dice[0].setFace(3);

for (Die curDie : dice) {System.out.println(curDie.getFace());

}

single

dice

null

null

null

Die face: 1

1

i

Page 22: Professor: Alvin Chaoeduc.jmu.edu › ~chaoaj › cs149fall2016 › slides › lecture16.pdfExercise #2 l Complete the following method using an enhanced for loop: /** * This method

Exercise #3

l Draw the memory diagram and determine output.Die single;Die[] dice;

dice = newDie[4];

single = newDie(1);

for (int i = 0; i < dice.length; i++) {dice[i] = single;

}

dice[0].setFace(3);

for (Die curDie : dice) {System.out.println(curDie.getFace());

}

single

dice

null

null

Die face: 1

1

i

Page 23: Professor: Alvin Chaoeduc.jmu.edu › ~chaoaj › cs149fall2016 › slides › lecture16.pdfExercise #2 l Complete the following method using an enhanced for loop: /** * This method

Exercise #3

l Draw the memory diagram and determine output.Die single;Die[] dice;

dice = newDie[4];

single = newDie(1);

for (int i = 0; i < dice.length; i++) {dice[i] = single;

}

dice[0].setFace(3);

for (Die curDie : dice) {System.out.println(curDie.getFace());

}

single

dice

null

null

Die face: 1

2

i

Page 24: Professor: Alvin Chaoeduc.jmu.edu › ~chaoaj › cs149fall2016 › slides › lecture16.pdfExercise #2 l Complete the following method using an enhanced for loop: /** * This method

Exercise #3

l Draw the memory diagram and determine output.Die single;Die[] dice;

dice = newDie[4];

single = newDie(1);

for (int i = 0; i < dice.length; i++) {dice[i] = single;

}

dice[0].setFace(3);

for (Die curDie : dice) {System.out.println(curDie.getFace());

}

single

dice

null

Die face: 1

2

i

Page 25: Professor: Alvin Chaoeduc.jmu.edu › ~chaoaj › cs149fall2016 › slides › lecture16.pdfExercise #2 l Complete the following method using an enhanced for loop: /** * This method

Exercise #3

l Draw the memory diagram and determine output.Die single;Die[] dice;

dice = newDie[4];

single = newDie(1);

for (int i = 0; i < dice.length; i++) {dice[i] = single;

}

dice[0].setFace(3);

for (Die curDie : dice) {System.out.println(curDie.getFace());

}

single

dice

null

Die face: 1

3

i

Page 26: Professor: Alvin Chaoeduc.jmu.edu › ~chaoaj › cs149fall2016 › slides › lecture16.pdfExercise #2 l Complete the following method using an enhanced for loop: /** * This method

Exercise #3

l Draw the memory diagram and determine output.Die single;Die[] dice;

dice = newDie[4];

single = newDie(1);

for (int i = 0; i < dice.length; i++) {dice[i] = single;

}

dice[0].setFace(3);

for (Die curDie : dice) {System.out.println(curDie.getFace());

}

single

dice

Die face: 1

3

i

Page 27: Professor: Alvin Chaoeduc.jmu.edu › ~chaoaj › cs149fall2016 › slides › lecture16.pdfExercise #2 l Complete the following method using an enhanced for loop: /** * This method

Exercise #3

l Draw the memory diagram and determine output.Die single;Die[] dice;

dice = newDie[4];

single = newDie(1);

for (int i = 0; i < dice.length; i++) {dice[i] = single;

}

dice[0].setFace(3);

for (Die curDie : dice) {System.out.println(curDie.getFace());

}

single

dice

Die face: 3

Page 28: Professor: Alvin Chaoeduc.jmu.edu › ~chaoaj › cs149fall2016 › slides › lecture16.pdfExercise #2 l Complete the following method using an enhanced for loop: /** * This method

Exercise #3l Draw the memory diagram and determine output.

Die single;Die[] dice;

dice = newDie[4];

single = newDie(1);

for (int i = 0; i < dice.length; i++) {dice[i] = single;

}

dice[0].setFace(3);

for (Die curDie : dice) {System.out.println(curDie.getFace());

}

single

dice

Die face: 3

Output:

3333

Page 29: Professor: Alvin Chaoeduc.jmu.edu › ~chaoaj › cs149fall2016 › slides › lecture16.pdfExercise #2 l Complete the following method using an enhanced for loop: /** * This method

Exercise #4l Complete the following method

/*** This method creates a Die array, and populates it with * Die objects. Each Die object will be initialized with * a random face value (using the zero argument constructor).* * @param numDice - The number of Die objects in the new array* @returnThe array of Die objects*/public static Die[] createDice(int numDice) {

}

Page 30: Professor: Alvin Chaoeduc.jmu.edu › ~chaoaj › cs149fall2016 › slides › lecture16.pdfExercise #2 l Complete the following method using an enhanced for loop: /** * This method

Exercise #4l Complete the following method

/*** This method creates a Die array, and populates it with * Die objects. Each Die object will be initialized with * a random face value (using the zero argument constructor).* * @param numDice - The number of Die objects in the new array* @returnThe array of Die objects*/public static Die[] createDice(int numDice) {

Die[] dice = newDie[numDice];

for (int i = 0; i < dice.length; i++) {dice[i] = newDie();

}

return dice;}

Page 31: Professor: Alvin Chaoeduc.jmu.edu › ~chaoaj › cs149fall2016 › slides › lecture16.pdfExercise #2 l Complete the following method using an enhanced for loop: /** * This method

• AcknowledgementsPartsofthisactivityarebasedonmaterialsdevelopedbyChrisMayfieldandNathanSprague.

</end>