professor: alvin chaoeduc.jmu.edu › ~chaoaj › cs149fall2016 › slides › lecture16.pdfexercise...
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](https://reader034.vdocuments.net/reader034/viewer/2022042402/5f10dcef7e708231d44b2d65/html5/thumbnails/1.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022042402/5f10dcef7e708231d44b2d65/html5/thumbnails/2.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022042402/5f10dcef7e708231d44b2d65/html5/thumbnails/3.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022042402/5f10dcef7e708231d44b2d65/html5/thumbnails/4.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022042402/5f10dcef7e708231d44b2d65/html5/thumbnails/5.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022042402/5f10dcef7e708231d44b2d65/html5/thumbnails/6.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022042402/5f10dcef7e708231d44b2d65/html5/thumbnails/7.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022042402/5f10dcef7e708231d44b2d65/html5/thumbnails/8.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022042402/5f10dcef7e708231d44b2d65/html5/thumbnails/9.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022042402/5f10dcef7e708231d44b2d65/html5/thumbnails/10.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022042402/5f10dcef7e708231d44b2d65/html5/thumbnails/11.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022042402/5f10dcef7e708231d44b2d65/html5/thumbnails/12.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022042402/5f10dcef7e708231d44b2d65/html5/thumbnails/13.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022042402/5f10dcef7e708231d44b2d65/html5/thumbnails/14.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022042402/5f10dcef7e708231d44b2d65/html5/thumbnails/15.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022042402/5f10dcef7e708231d44b2d65/html5/thumbnails/16.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022042402/5f10dcef7e708231d44b2d65/html5/thumbnails/17.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022042402/5f10dcef7e708231d44b2d65/html5/thumbnails/18.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022042402/5f10dcef7e708231d44b2d65/html5/thumbnails/19.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022042402/5f10dcef7e708231d44b2d65/html5/thumbnails/20.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022042402/5f10dcef7e708231d44b2d65/html5/thumbnails/21.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022042402/5f10dcef7e708231d44b2d65/html5/thumbnails/22.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022042402/5f10dcef7e708231d44b2d65/html5/thumbnails/23.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022042402/5f10dcef7e708231d44b2d65/html5/thumbnails/24.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022042402/5f10dcef7e708231d44b2d65/html5/thumbnails/25.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022042402/5f10dcef7e708231d44b2d65/html5/thumbnails/26.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022042402/5f10dcef7e708231d44b2d65/html5/thumbnails/27.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022042402/5f10dcef7e708231d44b2d65/html5/thumbnails/28.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022042402/5f10dcef7e708231d44b2d65/html5/thumbnails/29.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022042402/5f10dcef7e708231d44b2d65/html5/thumbnails/30.jpg)
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](https://reader034.vdocuments.net/reader034/viewer/2022042402/5f10dcef7e708231d44b2d65/html5/thumbnails/31.jpg)
• AcknowledgementsPartsofthisactivityarebasedonmaterialsdevelopedbyChrisMayfieldandNathanSprague.
</end>