Update your 110 Class Materials Project: Right Click, Team > Pull, OK
COMP110
Tiny Desk for a Tiny Dorm Fall 2015 Sections 2 & 3 Sitterson 014
October 1st, 2015 đ
Kris Jordan
[email protected] Sitterson 238
Announcements â˘
Assignment 2 Due Tomorrow at 11:55pm
â˘
Midterm next Thursday October 8th â˘
Pen & Paper only, no computers
â˘
Study guide: comp110.com/midterm-study-guide
Where do we know about arrays?
Key Concepts 1) An array is a uniform structure housing many elements. 0 1 2 3 4 5 6 7
2) Elements are addressed via name and index.
myArray[0]
3) Index is an expression!!!
int  length  =  8; myArray[length-Ââ1]
Java Array Operations Operation
Form
Example
# of Elements
arrayName.length
scores.length
Read Element
arrayName[index]
scores[0]
Declare
type[] Â arrayName;
int[] Â scores;
Construct
arrayName  =  new  type[size];
scores  =  new  int[3];
Assign Element
arrayName[index] Â = Â expression;
scores[0] Â = Â 12;
Initializeâ¨
type[]  arrayName  =  {elements};
int[]  scores  =  {12,0,1};
(Just a shortcut.)
Arrays by Pen
Arrays Practice 1. Pull the latest class materials if you havenât already: Right click project, Choose Team, Choose Pull, OK 2. Open com.comp110.lecture13 / ArrayPractice.java 3. Without running! What do you think this code does?⨠Submit what you think on PollEverywhere
pollev.com/comp110
Arrays Practice, Pt 2 4. What pattern do you see in Part B? 5. Write a for loop to do the arithmetic in Part B 1. Name your counter variable i 2. Can you do this without hard coding the number 8?
Done? Check in on PollEverywhere pollev.com/comp110
Arrays Practice, Pt 3 4. What patterns do you see in Part A? 5. Write a for loop that does the assignments in Part A 1. Name your counter variable i 2. Can you do this without hard coding the number 8?
Done? Check in on PollEverywhere pollev.com/comp110
Arrays Practice, Pt 4 6. How would we calculate the maximum number of values we could store with 2 bytes instead of 1 byte?
Done? Check in on PollEverywhere pollev.com/comp110
#neverforget â˘
We always start with 0!
â˘
0 is the minimum value we can represent with N bits
â˘
2N - 1 is the maximum value we can represent with N bits
â˘
2N is the # of values that can be represented with N bits â˘
Because 0 is a value #neverforget
Ramsesâ Raffle
Open com.comp110.lecture13 / RamsesRaffle.java
Ramses' Raffle 1. Letâs allow any number of players! 2. Replace the hard-coded number of players in the winning number calculation with the length of the players array. 3. Fix the line with // TODO: Part A 1. Assign playerswinningNumber to winner 2. Does it work? Done? Check in on PollEverywhere pollev.com/comp110
Ramses' Raffle, Pt 2 4. Replace the hard-coded players names with a ⨠new int array with 3 elements. 5. Add 3 new lines of code after Step 4 and assign your friendsâ names to each element in players array 6. Still working?
Done? Check in on PollEverywhere
Ramses' Raffle, Pt 3 4. Replace the line with âv2.0â printed with a declaration of an integer variable named numberOfPlayers 5. Initialize it with input from keyboard.nextInt() 6. Modify your players array construction and winningNumber calculation to use numberOfPlayers instead of 3 7. Write a for loop asking the user to âEnter player #Nâ,⨠and assign each name to an element in players array 8. Play! Done? Check in on PollEverywhere
Practice Problems