Lecture 7

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

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

View Full Document Right Arrow Icon
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
Background image of page 1

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

View Full DocumentRight Arrow Icon
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.
Background image of page 2
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
Background image of page 3

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

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/17/2012 for the course ECE 203 taught by Professor Robincarr during the Fall '07 term at Drexel.

Page1 / 16

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

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online