comp 110 more arrays, 2d arrays, program 4

23
COMP 110 COMP 110 More arrays, 2D arrays, More arrays, 2D arrays, Program 4 Program 4 Luv Kohli November 10, 2008 MWF 2-2:50 pm Sitterson 014

Upload: kylie-manning

Post on 31-Dec-2015

36 views

Category:

Documents


1 download

DESCRIPTION

COMP 110 More arrays, 2D arrays, Program 4. Luv Kohli November 10, 2008 MWF 2-2:50 pm Sitterson 014. Announcements. Lab 7 due, Friday 2pm Final program, Program 4, assigned today, due Monday, December 1, 2pm Electronic Arts (EA) information session Tuesday, November 11 (tomorrow), 6pm - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: COMP 110 More arrays, 2D arrays, Program 4

COMP 110COMP 110More arrays, 2D arrays, Program 4More arrays, 2D arrays, Program 4

Luv KohliNovember 10, 2008

MWF 2-2:50 pmSitterson 014

Page 2: COMP 110 More arrays, 2D arrays, Program 4

AnnouncementsAnnouncementsLab 7 due, Friday 2pm

Final program, Program 4, assigned today, due Monday, December 1, 2pm

Electronic Arts (EA) information session◦ Tuesday, November 11 (tomorrow), 6pm◦ Sitterson Hall 011◦ Free food!◦ Free games!

2

Page 3: COMP 110 More arrays, 2D arrays, Program 4

Questions?Questions?

3

Page 4: COMP 110 More arrays, 2D arrays, Program 4

Today in COMP 110Today in COMP 110More about arrays

2D arrays

Program 4

4

Page 5: COMP 110 More arrays, 2D arrays, Program 4

Arrays as instance variablesArrays as instance variablespublic class Weather{ private double[] temperature; private double[] pressure;

public void initializeTemperature(int len) { temperature = new double[len]; }}

Example in jGRASP

5

Page 6: COMP 110 More arrays, 2D arrays, Program 4

Arrays of objectsArrays of objectsWhen you create an array of objects like

this:Student[] students = new Student[35];

Each of the elements of students is not yet an object

You have to instantiate each individual onestudents[0] = new Student();

students[1] = new Student();

…or do this in a loop6

Page 7: COMP 110 More arrays, 2D arrays, Program 4

Smiley[] smilies = new Smiley[3];

for (int i = 0; i < smilies.length; i++)

{

smilies[i] = new Smiley();

}

1045 2584 2836? ? ?

Arrays of objectsArrays of objects

7

true

GREEN

3

false

BLUE

1

false

CYAN

4

Page 8: COMP 110 More arrays, 2D arrays, Program 4

Arrays of objectsArrays of objectsExample in jGRASP

8

Page 9: COMP 110 More arrays, 2D arrays, Program 4

Arrays as parametersArrays as parameterspublic void changeArray(int[] arr){ int len = arr.length; arr[len – 1] = 25;}

9

23 47 52 14 725

Page 10: COMP 110 More arrays, 2D arrays, Program 4

Arrays as return typesArrays as return typespublic double[] buildArray(int len){ double[] retArray = new double[len]; for (int i = 0; i < retArray.length; i++) { retArray[i] = i * 1.5; }

return retArray;}

10

Page 11: COMP 110 More arrays, 2D arrays, Program 4

Indexed variables as method argumentsIndexed variables as method arguments

No different from using a regular variable

public void printNum(int num){ System.out.println(num);}

public void doStuff(){ int[] scores = { 15, 37, 95 };

for (int index = 0; index < scores.length; index++) { printNum(scores[index]); }}

11

Page 12: COMP 110 More arrays, 2D arrays, Program 4

2D arrays2D arraysArrays having more than one index are

often useful◦Tables◦Grids◦Bingo games

12

0: Open 1: High 2: Low 3: Close

0: Apple Inc. 99.24 99.85 95.72 98.24

1: Walt Disney Co. 21.55 24.20 21.41 23.36

2: Google Inc. 333.12 341.15 325.33 331.14

3: Microsoft Corp. 21.32 21.54 21.00 21.50

Page 13: COMP 110 More arrays, 2D arrays, Program 4

Declaring and creating 2D arraysDeclaring and creating 2D arraysint[][] table = new int[4][3];

or

int[][] table;

table = new int[4][3];

13

Page 14: COMP 110 More arrays, 2D arrays, Program 4

Declaring and creating 2D arraysDeclaring and creating 2D arraysint[][] table = new int[4][3];

gives you the ability to use

table[0][0]table[0][1]table[0][2]table[1][0]table[1][1]table[1][2]table[2][0]table[2][1]table[2][2]table[3][0]table[3][1]table[3][2]

14

Page 15: COMP 110 More arrays, 2D arrays, Program 4

How do you use a 2D array?How do you use a 2D array?We used a loop to iterate over a 1D array

int[] scores = { 13, 57, 93, 60, 102 };

for (int i = 0; i < scores.length; i++)

{

System.out.println(scores[i]);

}

15

Page 16: COMP 110 More arrays, 2D arrays, Program 4

How do you use a 2D array?How do you use a 2D array?How about a 2D array?

int[][] table = new int[4][3];

Use a nested loop

for (int row = 0; row < 4; row++){ for (int column = 0; column < 3; column++) { table[row][column] = 37; }}

16

Page 17: COMP 110 More arrays, 2D arrays, Program 4

Multidimensional arraysMultidimensional arraysYou can have more than two dimensions

int[][][] table = new int[4][3][5];

Use more nested loops to access all elements

17

Page 18: COMP 110 More arrays, 2D arrays, Program 4

Multidimensional arrays as parametersMultidimensional arrays as parameterspublic void print2DArray(int[][] arr)

{

for (int row = 0; row < arr.length; row++)

{

for (int column = 0; column < arr[row].length; column++)

{

System.out.print(arr[row][column] + " ");

}

System.out.println();

}

}

18

Page 19: COMP 110 More arrays, 2D arrays, Program 4

Multidimensional arrays as return typesMultidimensional arrays as return types

public int[][] giveMeAnArray()

{

int[][] table = new int[4][3];

// put values in the table

return table;

}

19

Page 20: COMP 110 More arrays, 2D arrays, Program 4

length for a 2D arraylength for a 2D arrayint[][] table = new int[4][3];

table.length is the number of rows, or the integer in the first pair of brackets (4)

table[i].length is the number of columns, or the integer in the second pair of brackets (3)

20

Page 21: COMP 110 More arrays, 2D arrays, Program 4

Why? Arrays of arraysWhy? Arrays of arraysint[] scores = new int[5];

scores is a one-dimensional array◦ base type is int

int[][] table = new int[4][3];

table is also in fact a one-dimensional array◦ base type is int[]

We still refer to table as a two-dimensional array

21

Page 22: COMP 110 More arrays, 2D arrays, Program 4

Program 4: BattleshipProgram 4: Battleship

22

This program is hard. But you can do it. START EARLY.

If you start early, this will be you

If you don’t start early, this will be you

Page 23: COMP 110 More arrays, 2D arrays, Program 4

WednesdayWednesdayMore array details

Possibly an in-class exercise related to Program 4

23