{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Lecture 7

# Lecture 7 - Lecture 7 Introduction to Arrays Review Here...

This preview shows pages 1–4. Sign up to view the full content.

Page 1 of 17 Lecture 7: Introduction to Arrays Review: Here again is our example from week 5 where a die was shaken multiple times. We shall redo this example at the end of this lecture using arrays. (See chapter 6 in Savitch). Example 1: Use of the switch statement. We will investigate how many times each face comes up on our die when it is shaken N = 6000 times. We expect each face to appear about N /6 times. Since the die is a uniform random number generator. Code: RollDie.java // Roll a six-sided die 6000 times. import java.util.Random; public class RollDie { public static void main( String args[] ) { Random randomNumbers = new Random(12345678); int frequency1 = 0; // count of 1s rolled int frequency2 = 0; // count of 2s rolled int frequency3 = 0; // count of 3s rolled int frequency4 = 0; // count of 4s rolled int frequency5 = 0; // count of 5s rolled int frequency6 = 0; // count of 6s rolled int face; // stores most recently rolled value // summarize results of 6000 rolls of a die for ( int roll = 1; roll <= 6000; roll++ ) { face = 1 + randomNumbers.nextInt( 6 ); // number from 1 to 6

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Page 2 of 17 // determine roll value 1-6 and increment appropriate counter switch ( face ) { case 1: ++frequency1; // increment the 1s counter break; case 2: ++frequency2; // increment the 2s counter break; case 3: ++frequency3; // increment the 3s counter break; case 4: ++frequency4; // increment the 4s counter break; case 5: ++frequency5; // increment the 5s counter break; case 6: ++frequency6; // increment the 6s counter break; // optional at end of switch } // end switch block } // end for loop System.out.println( "Face\tFrequency" ); // output headers System.out.printf( "1\t%d\n2\t%d\n3\t%d\n4\t%d\n5\t%d\n6\t%d\n", frequency1, frequency2, frequency3, frequency4,frequency5, frequency6 ); } // end main } // end class RollDie Here is the output using the seed 12345678 in the Random constructor. We will see that this problem can be programmed much more efficiently using arrays.
Page 3 of 17 Array Basics Declaring arrays • Arrays are an indexed set of objects of the same type. • An array can be declared and created in one of the following ways: 1. int row[] = new int [ 5 ]; 2. int [] row = new int [ 5 ]; 3. int [] row; row = new int [ 5 ]; 4. String [] day = new String [ 7 ]; Initializing arrays • An array can be initialized in one of the following ways: 1. for ( int i = 0; i < 5; i++ ) row[i] = (i+1)*(i+1); 2. String [] day = { "Mo", "Tu", "We", "Th", "Fr", "Sa", "Su" }; 3. int [] row = { 1, 4, 9, 16, 25 }; Note the difference on where square brackets must be used and where curly brackets must be used. Note the first element in an array has the index zero and not one. Example 4 declares a new array containing 7 String variables. The value of its only instance variable length is thus 7 and its base type is String. The for loop initializes an array of squares: { 1, 4, 9, 16, 25 } All these methods use the keyword new to create an array. Use square brackets to declare arrays. Examples 1, 2 and 3 all declare a new array of 5 integers named row. The array row has the single instance variable 5, which is its length .

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern