13 - Tiny Desk for a Tiny Dorm

Report 4 Downloads 300 Views
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