122
CPSC 111 Introduction to Computation May 25, 2009

cpsc lecture8

Embed Size (px)

Citation preview

CPSC 111Introduction to Computation

May 25, 2009

Administrative StuffAssignment #3 is due by 11:59pm Wednesday.

Midterm exam #2 in class on Friday.

Administrative StuffBig reading in Big Java 3rd edition:

Chapter 1.1 through 1.8 (introduction)Chapter 2.1 through 2.10 (using objects)Chapter 3.1 through 3.8 (implementing classes)Chapter 4 (data types)Chapter 5.1 through 5.4 (decisions)Chapter 6.1 through 6.5 (iteration) Friday and todayChapter 7.1, 7.5, 7.6, 7.7 (arrays) today and WednesdayChapter 14.1, 14.3 (searching and sorting)Chapter 8.1 through 8.9 (designing classes)Chapter 9.1, 9.2, 9.3 (interfaces and polymorphism)Chapter 10 (inheritance)Chapter 2.11, 2.12 (simple graphics)Chapter 9.5, 9.6, 9.7, 9.8, 10.9, 10.10 (event handling

and graphical user interfaces)

Administrative StuffBig reading in Big Java 2nd edition:

Chapter 1.1 through 1.8 (introduction)Chapter 2.1 through 2.10 (using objects)Chapter 3.1 through 3.8 (implementing classes)Chapter 4 (data types)Chapter 6.1 through 6.4 (decisions)Chapter 7.1 through 7.5 (iteration) Friday and todayChapter 8.1, 8.5, 8.6, 8.7 (arrays) today and WednesdayChapter 19.1, 19.3 (searching and sorting)Chapter 9.1 through 9.9 (designing classes)Chapter11.1, 11.2, 11.3 (interfaces and polymorphism)Chapter 13 (inheritance)Chapter 5.1, 5.2 (simple graphics)Chapter 11.5, 12.1, 12.2, 12.3 (event handling

and graphical user interfaces)

Administrative StuffImportant reminder:

Please note that in order to pass the course you must:

• obtain an overall grade of at least 50% • obtain a grade of at least 50% on the final exam • obtain an overall grade of at least 50% on the combined lab and assignment grades

If you fail to satisfy any of the above criteria, a grade no greater than45% will be assigned in the course. The instructor reserves the rightto modify this grading scheme as necessary throughout the term.

If you skip enough labs and assignments, it won't matter how well youdo on the exams...you will fail the course.

From the previous lectureIteration - executing the same block of instructions over and over

• the 'while' loop

• the 'for' loop

• how to impress your drinking buddies with a loop

Practice problemWrite a program that uses a while loop to simulate the flipping of a coin one million times. Keep track of how manytimes the coin lands heads up, and how many times it landstails up. Print the results.

Practice problempublic class CoinFlipper{ public static void main (String[] args) { int heads = 0, tails = 0, count = 0; double rnum; while (count < 1000000) { rnum = Math.random(); count++; if (rnum < 0.5) { heads++; } else { tails++; } } System.out.println("Coin landed heads " + heads + " times"); System.out.println("Coin landed tails " + tails + " times"); }}

Practice problempublic class CoinFlipperFor{ public static void main (String[] args) { int heads = 0, tails = 0; double rnum; for (int count = 0; count < 1000000; count++) { rnum = Math.random(); if (rnum < 0.5) { heads++; } else { tails++; } } System.out.println("Coin landed heads " + heads + " times"); System.out.println("Coin landed tails " + tails + " times"); }}

More data collectionIf you complete our second survey no later thanFriday, May 29, we'll give you additional quiz marks.Again, it's a little safety net in case you have a badquiz day.

This survey is waiting for you now at

http://ls.olt.ubc.ca/index.php?sid=76476&lang=en

What we'll see today......if we don't run out of time

How to create and manage loops within loops (nestedloops)

How to organize related data as a collection of variablesof the same type (the array)

How to access the array elements with a loop

How to make a more complex collection of data (the two-dimensional array)

How to use the nested loop with the 2D array

Here's a very simple for loop:

public class SimpleLoop{ public static void main (String[] args) { for (int i = 1; i <= 3; i++) { System.out.println(i); } }}

What does it do?

It prints the following:

123

Nested loops

Here's a very simple for loop:

public class SimpleLoop{ public static void main (String[] args) { for (int i = 1; i <= 3; i++) { System.out.println(i); } }}

But what if for every number below, I also wanted toprint that value times 2, and that value times 3, justa little multiplication table, like this?

1 2 32 4 63 6 9

Nested loops

Here's a very simple for loop:

public class SimpleLoop{ public static void main (String[] args) { for (int i = 1; i <= 3; i++) { System.out.println(i); } }}

It's as if for every number that was printed by the loopabove

1 2 32 4 63 6 9

Nested loops

Here's a very simple for loop:

public class SimpleLoop{ public static void main (String[] args) { for (int i = 1; i <= 3; i++) { System.out.println(i); } }}

It's as if for every number that was printed by the loopabove we need another loop to print the numbers in therow.

1 2 32 4 63 6 9

Nested loops

Here's a very simple for loop:

public class SimpleLoop{ public static void main (String[] args) { for (int i = 1; i <= 3; i++) { System.out.println(i); } }}

It's as if for every number that was printed by the loopabove we need another loop to print the numbers in therow. How do we do that?

1 2 32 4 63 6 9

Nested loops

We put a loop inside a loop, like this:

public class NestedLoop{ public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

Nested loops

How does it work?

public class NestedLoop{ public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

Nested loops

How does it work?

public class NestedLoop{ public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

Nested loops

How does it work?

public class NestedLoop{ public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

Nested loops

i 1

How does it work?

public class NestedLoop{ public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

Nested loops

i 1

How does it work?

public class NestedLoop{ public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

Nested loops

i 1

How does it work?

public class NestedLoop{ public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

Nested loops

i

j

1

1

How does it work?

public class NestedLoop{ public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

Nested loops

i

j

1

1

How does it work?

public class NestedLoop{ public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

Nested loops

i

j

1

1

How does it work?

public class NestedLoop{ public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

Nested loops

i

j

1

1

1

How does it work?

public class NestedLoop{ public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

Nested loops

i

j

1

2

1

How does it work?

public class NestedLoop{ public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

Nested loops

i

j

1

2

1

How does it work?

public class NestedLoop{ public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

Nested loops

i

j

1

2

1 2

How does it work?

public class NestedLoop{ public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

Nested loops

i

j

1

3

1 2

How does it work?

public class NestedLoop{ public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

Nested loops

i

j

1

3

1 2

How does it work?

public class NestedLoop{ public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

Nested loops

i

j

1

3

1 2 3

How does it work?

public class NestedLoop{ public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

Nested loops

i

j

1

4

1 2 3

How does it work?

public class NestedLoop{ public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

Nested loops

i

j

1

4

1 2 3

How does it work?

public class NestedLoop{ public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

Nested loops

i 1

1 2 3

How does it work?

public class NestedLoop{ public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

Nested loops

i 2

1 2 3

How does it work?

public class NestedLoop{ public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

Nested loops

i 2

1 2 3

How does it work?

public class NestedLoop{ public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

Nested loops

i

j

2

1

1 2 3

How does it work?

public class NestedLoop{ public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

Nested loops

i

j

2

1

1 2 3

How does it work?

public class NestedLoop{ public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

Nested loops

i

j

2

1

1 2 32

How does it work?

public class NestedLoop{ public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

Nested loops

i

j

2

2

1 2 32

How does it work?

public class NestedLoop{ public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

Nested loops

i

j

2

2

1 2 32

How does it work?

public class NestedLoop{ public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

Nested loops

i

j

2

2

1 2 32 4

How does it work?

public class NestedLoop{ public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

Nested loops

i

j

2

3

1 2 32 4

How does it work?

public class NestedLoop{ public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

Nested loops

i

j

2

3

1 2 32 4

How does it work?

public class NestedLoop{ public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

Nested loops

i

j

2

3

1 2 32 4 6

How does it work?

public class NestedLoop{ public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

Nested loops

i

j

2

4

1 2 32 4 6

How does it work?

public class NestedLoop{ public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

Nested loops

i

j

2

4

1 2 32 4 6

How does it work?

public class NestedLoop{ public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

Nested loops

i 2

1 2 32 4 6

How does it work?

public class NestedLoop{ public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

Nested loops

i 3

1 2 32 4 6

How does it work?

public class NestedLoop{ public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

Nested loops

i 3

1 2 32 4 6

How does it work?

public class NestedLoop{ public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

Nested loops

i

j

3

1

1 2 32 4 6

How does it work?

public class NestedLoop{ public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

Nested loops

i

j

3

1

1 2 32 4 6

How does it work?

public class NestedLoop{ public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

Nested loops

i

j

3

1

1 2 32 4 63

How does it work?

public class NestedLoop{ public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

Nested loops

i

j

3

2

1 2 32 4 63

How does it work?

public class NestedLoop{ public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

Nested loops

i

j

3

2

1 2 32 4 63

How does it work?

public class NestedLoop{ public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

Nested loops

i

j

3

2

1 2 32 4 63 6

How does it work?

public class NestedLoop{ public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

Nested loops

i

j

3

3

1 2 32 4 63 6

How does it work?

public class NestedLoop{ public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

Nested loops

i

j

3

3

1 2 32 4 63 6

How does it work?

public class NestedLoop{ public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

Nested loops

i

j

3

3

1 2 32 4 63 6 9

How does it work?

public class NestedLoop{ public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

Nested loops

i

j

3

4

1 2 32 4 63 6 9

How does it work?

public class NestedLoop{ public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

Nested loops

i

j

3

4

1 2 32 4 63 6 9

How does it work?

public class NestedLoop{ public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

Nested loops

i 3

1 2 32 4 63 6 9

How does it work?

public class NestedLoop{ public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

Nested loops

i 4

1 2 32 4 63 6 9

How does it work?

public class NestedLoop{ public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

Nested loops

i 4

1 2 32 4 63 6 9

How does it work?

public class NestedLoop{ public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } }}

That's the end of the program.

Nested loops

1 2 32 4 63 6 9

Practice ProblemsCreate a static method called printbox which takes twointeger parameters, w and h, and prints a box that isw asterisks (*) wide and h asterisks high. Forexample, printbox(3,5) will print this:

***************

Practice Problemspublic class BoxTest{ public static void main (String[] args) { printbox(3,5); System.out.println(); printbox(5,3); }

public static void printbox(int wide, int high) { for (int i = 1; i <= high; i++) { for (int j = 1; j <= wide; j++) { System.out.print('*'); } System.out.println(); } }}

Practice ProblemsCreate a static method called printtriangle which takesone integer parameter, h, and prints a triangle that'sh asterisks high, as well as h asterisks wide at itsbase. For example, printtriangle(5) would print this:

***************

Practice Problemspublic class TriangleTest{ public static void main (String[] args) { printtriangle(3); System.out.println(); printtriangle(5); }

public static void printtriangle(int high) { int j; for (int i = 1; i <= high; i++) { j = 1; while (j <= i) { System.out.print('*'); j++; } System.out.println(); } }}

My Empire Strikes Back

Bubba’sroute

myroute

How do I keep track of my sales?Cans of pop soldthis month

185 92 370 485 209 128 84 151 32 563

What’s the gross income?What’s the net profit?Is Bubba stealing loonies?

How do I keep track of my sales?Cans of pop soldthis month

185 92 370 485 209 128 84 151 32 563

In other words, how can I organize the data above in my computer so that I can access it easily and do thecomputations I need to do?

Here’s the answer…Cans of pop soldthis month

185 92 370 485 209 128 84 151 32 563

We'll use an array. An array is a common programming language construct for grouping related data items together in some meaningfulorganization such that each individualdata item can be easily retrieved orupdated.

Here’s the answer…

cansSold 185 92 370 485 209 128 84 151 32 563

We'll use an array. An array is a common programming language construct for grouping related data items together in some meaningfulorganization such that each individualdata item can be easily retrieved orupdated.

You can think of an array as acollection of variables, all of the sametype, that share a common name.Each of these variables holds a singlevalue.

How to use the array

cansSold 185 92 370 485 209 128 84 151 32 563

Since this collection of variables hasa single name, how do we accessthe individual values?

How to use the array

cansSold0 1851 922 3703 4854 2095 1286 847 1518 329 563

Since this collection of variables hasa single name, how do we accessthe individual values?

Each value is stored at a uniquenumbered position within the array.The number is commonly called theindex of that array element. Sometimes that number is called asubscript.

This array is named cansSold. It holds 10 values.

How to use the array

cansSold0 1851 922 3703 4854 2095 1286 847 1518 329 563

To access an individual value in anarray, we use the array name followedby a pair of square brackets. Insidethe brackets we place the index of thearray element we want to access. Forexample

System.out.println(cansSold[4]);

would print the value 209 on themonitor. This means of referring toan array element can be used anywhere a variable name can beused.

Array declarations and types

cansSold0 1851 922 3703 4854 2095 1286 847 1518 329 563

Just like an ordinary variable, we haveto declare an array before we use it,and we have to give it a type. SincecansSold contains integers, we makeit an integer array:

int[] cansSold = new int[10]

It looks like a variable declarationexcept:

Array declarations and types

cansSold0 1851 922 3703 4854 2095 1286 847 1518 329 563

Just like an ordinary variable, we haveto declare an array before we use it,and we have to give it a type. SincecansSold contains integers, we makeit an integer array:

int[] cansSold = new int[10]

It looks like a variable declarationexcept:• the empty brackets on the left tell Java that cansSold is an array...

Array declarations and types

cansSold0 1851 922 3703 4854 2095 1286 847 1518 329 563

Just like an ordinary variable, we haveto declare an array before we use it,and we have to give it a type. SincecansSold contains integers, we makeit an integer array:

int[] cansSold = new int[10]

It looks like a variable declarationexcept:• the empty brackets on the left tell Java that cansSold is an array...• the number in the brackets on the right tell Java that the array should have room for 10 elements when it's created

Array declarations and types

cansSold0 1851 922 3703 4854 2095 1286 847 1518 329 563

Just like an ordinary variable, we haveto declare an array before we use it,and we have to give it a type. SincecansSold contains integers, we makeit an integer array:

int[10] cansSold = new int[10]

It looks like a variable declarationexcept:• the empty brackets on the left tell Java that cansSold is an array...• the number in the brackets on the right tell Java that the array should have room for 10 elements when it's created• DO NOT put the size of the array in the brackets on the left

Array declarations and types

cansSold0 1851 922 3703 4854 2095 1286 847 1518 329 563

Just like an ordinary variable, we haveto declare an array before we use it,and we have to give it a type. SincecansSold contains integers, we makeit an integer array:

int[10] cansSold = new int[10]

It looks like a variable declarationexcept:• the empty brackets on the left tell Java that cansSold is an array...• the number in the brackets on the right tell Java that the array should have room for 10 elements when it's created• DO NOT put the size of the array in the brackets on the left

Array declarations and types

cansSold0 1851 922 3703 4854 2095 1286 847 1518 329 563

Here's a simple example of how to create and initialize thecansSold array:

public class ArrayTest1{ public static void main(String[] args) { final int ARRAYSIZE = 10; int[] cansSold = new int[ARRAYSIZE]; cansSold[0] = 185; cansSold[1] = 92; cansSold[2] = 370; cansSold[3] = 485; cansSold[4] = 209; cansSold[5] = 128; cansSold[6] = 84; cansSold[7] = 151; cansSold[8] = 32; cansSold[9] = 563; // do useful stuff here System.out.println("Element 4 is " + cansSold[4]); }}

Variation on creating an array

cansSold0 1851 922 3703 4854 2095 1286 847 1518 329 563

Here's another way to do the same thing. This is called an initializer list.

public class ArrayTest2{ public static void main(String[] args) { int[] cansSold = {185, 92, 370, 485, 209, 128, 84, 151, 32, 563}; // do useful stuff here System.out.println("Element 4 is " + cansSold[4]); }}

Note that the right hand side of this declaration doesnot include type or size. Java figures out the size byitself. And the types of the values on the right have tomatch the type declared on the left.

The initializer list may only be used when the arrayis first declared.

So let's do something already...

cansSold0 1851 922 3703 4854 2095 1286 847 1518 329 563

How would we write a program to create this array, findthe total of cans sold, and print the result?

public class ArrayTest3{ public static void main(String[] args) { int totalCans = 0; int[] cansSold = {185, 92, 370, 485, 209, 128, 84, 151, 32, 563}; for (int i = 0; i < cansSold.length; i++) { totalCans = totalCans + cansSold[i]; } System.out.println("We've sold " + totalCans + " cans of pop"); }}

So let's do something already...How does it work?

public class ArrayTest3{ public static void main(String[] args) { int totalCans = 0; int[] cansSold = {185, 92, 370, 485, 209, 128, 84, 151, 32, 563}; for (int i = 0; i < cansSold.length; i++) { totalCans = totalCans + cansSold[i]; } System.out.println("We've sold " + totalCans + " cans of pop"); }}

So let's do something already...How does it work?

public class ArrayTest3{ public static void main(String[] args) { int totalCans = 0; int[] cansSold = {185, 92, 370, 485, 209, 128, 84, 151, 32, 563}; for (int i = 0; i < cansSold.length; i++) { totalCans = totalCans + cansSold[i]; } System.out.println("We've sold " + totalCans + " cans of pop"); }}

totalCans 0

So let's do something already...

0 1851 922 3703 4854 2095 1286 847 1518 329 563

How does it work?

public class ArrayTest3{ public static void main(String[] args) { int totalCans = 0; int[] cansSold = {185, 92, 370, 485, 209, 128, 84, 151, 32, 563}; for (int i = 0; i < cansSold.length; i++) { totalCans = totalCans + cansSold[i]; } System.out.println("We've sold " + totalCans + " cans of pop"); }}

totalCans

cansSold

0

cansSold.length 10

So let's do something already...

0 1851 922 3703 4854 2095 1286 847 1518 329 563

How does it work?

public class ArrayTest3{ public static void main(String[] args) { int totalCans = 0; int[] cansSold = {185, 92, 370, 485, 209, 128, 84, 151, 32, 563}; for (int i = 0; i < cansSold.length; i++) { totalCans = totalCans + cansSold[i]; } System.out.println("We've sold " + totalCans + " cans of pop"); }}

i

totalCans

cansSold

0

cansSold.length 10

0

So let's do something already...

0 1851 922 3703 4854 2095 1286 847 1518 329 563

How does it work?

public class ArrayTest3{ public static void main(String[] args) { int totalCans = 0; int[] cansSold = {185, 92, 370, 485, 209, 128, 84, 151, 32, 563}; for (int i = 0; i < cansSold.length; i++) { totalCans = totalCans + cansSold[i]; } System.out.println("We've sold " + totalCans + " cans of pop"); }}

i

totalCans

cansSold

0

cansSold.length 10

0

Is i < 10? Yes.

So let's do something already...

0 1851 922 3703 4854 2095 1286 847 1518 329 563

How does it work?

public class ArrayTest3{ public static void main(String[] args) { int totalCans = 0; int[] cansSold = {185, 92, 370, 485, 209, 128, 84, 151, 32, 563}; for (int i = 0; i < cansSold.length; i++) { totalCans = totalCans + cansSold[i]; } System.out.println("We've sold " + totalCans + " cans of pop"); }}

i

totalCans

cansSold

185

cansSold.length 10

0

So let's do something already...

0 1851 922 3703 4854 2095 1286 847 1518 329 563

How does it work?

public class ArrayTest3{ public static void main(String[] args) { int totalCans = 0; int[] cansSold = {185, 92, 370, 485, 209, 128, 84, 151, 32, 563}; for (int i = 0; i < cansSold.length; i++) { totalCans = totalCans + cansSold[i]; } System.out.println("We've sold " + totalCans + " cans of pop"); }}

i

totalCans

cansSold

185

cansSold.length 10

1

So let's do something already...

0 1851 922 3703 4854 2095 1286 847 1518 329 563

How does it work?

public class ArrayTest3{ public static void main(String[] args) { int totalCans = 0; int[] cansSold = {185, 92, 370, 485, 209, 128, 84, 151, 32, 563}; for (int i = 0; i < cansSold.length; i++) { totalCans = totalCans + cansSold[i]; } System.out.println("We've sold " + totalCans + " cans of pop"); }}

i

totalCans

cansSold

185

cansSold.length 10

1

Is i < 10? Yes.

So let's do something already...

0 1851 922 3703 4854 2095 1286 847 1518 329 563

How does it work?

public class ArrayTest3{ public static void main(String[] args) { int totalCans = 0; int[] cansSold = {185, 92, 370, 485, 209, 128, 84, 151, 32, 563}; for (int i = 0; i < cansSold.length; i++) { totalCans = totalCans + cansSold[i]; } System.out.println("We've sold " + totalCans + " cans of pop"); }}

i

totalCans

cansSold

277

cansSold.length 10

1

So let's do something already...

0 1851 922 3703 4854 2095 1286 847 1518 329 563

How does it work?

public class ArrayTest3{ public static void main(String[] args) { int totalCans = 0; int[] cansSold = {185, 92, 370, 485, 209, 128, 84, 151, 32, 563}; for (int i = 0; i < cansSold.length; i++) { totalCans = totalCans + cansSold[i]; } System.out.println("We've sold " + totalCans + " cans of pop"); }}

i

totalCans

cansSold

277

cansSold.length 10

2

So let's do something already...

0 1851 922 3703 4854 2095 1286 847 1518 329 563

How does it work?

public class ArrayTest3{ public static void main(String[] args) { int totalCans = 0; int[] cansSold = {185, 92, 370, 485, 209, 128, 84, 151, 32, 563}; for (int i = 0; i < cansSold.length; i++) { totalCans = totalCans + cansSold[i]; } System.out.println("We've sold " + totalCans + " cans of pop"); }}

i

totalCans

cansSold

277

cansSold.length 10

2

Is i < 10? Yes.

So let's do something already...

0 1851 922 3703 4854 2095 1286 847 1518 329 563

How does it work?

public class ArrayTest3{ public static void main(String[] args) { int totalCans = 0; int[] cansSold = {185, 92, 370, 485, 209, 128, 84, 151, 32, 563}; for (int i = 0; i < cansSold.length; i++) { totalCans = totalCans + cansSold[i]; } System.out.println("We've sold " + totalCans + " cans of pop"); }}

i

totalCans

cansSold

647

cansSold.length 10

2

So let's do something already...

0 1851 922 3703 4854 2095 1286 847 1518 329 563

How does it work?

public class ArrayTest3{ public static void main(String[] args) { int totalCans = 0; int[] cansSold = {185, 92, 370, 485, 209, 128, 84, 151, 32, 563}; for (int i = 0; i < cansSold.length; i++) { totalCans = totalCans + cansSold[i]; } System.out.println("We've sold " + totalCans + " cans of pop"); }}

i

totalCans

cansSold

647

cansSold.length 10

3

So let's do something already...

0 1851 922 3703 4854 2095 1286 847 1518 329 563

How does it work?

public class ArrayTest3{ public static void main(String[] args) { int totalCans = 0; int[] cansSold = {185, 92, 370, 485, 209, 128, 84, 151, 32, 563}; for (int i = 0; i < cansSold.length; i++) { totalCans = totalCans + cansSold[i]; } System.out.println("We've sold " + totalCans + " cans of pop"); }}

i

totalCans

cansSold

647

cansSold.length 10

3

Is i < 10? Yes.

So let's do something already...

0 1851 922 3703 4854 2095 1286 847 1518 329 563

How does it work?

public class ArrayTest3{ public static void main(String[] args) { int totalCans = 0; int[] cansSold = {185, 92, 370, 485, 209, 128, 84, 151, 32, 563}; for (int i = 0; i < cansSold.length; i++) { totalCans = totalCans + cansSold[i]; } System.out.println("We've sold " + totalCans + " cans of pop"); }}

i

totalCans

cansSold

1132

cansSold.length 10

3

So let's do something already...

0 1851 922 3703 4854 2095 1286 847 1518 329 563

How does it work?

public class ArrayTest3{ public static void main(String[] args) { int totalCans = 0; int[] cansSold = {185, 92, 370, 485, 209, 128, 84, 151, 32, 563}; for (int i = 0; i < cansSold.length; i++) { totalCans = totalCans + cansSold[i]; } System.out.println("We've sold " + totalCans + " cans of pop"); }}

i

totalCans

cansSold

1341

cansSold.length 10

4

And so on...

So let's do something already...

0 1851 922 3703 4854 2095 1286 847 1518 329 563

How does it work?

public class ArrayTest3{ public static void main(String[] args) { int totalCans = 0; int[] cansSold = {185, 92, 370, 485, 209, 128, 84, 151, 32, 563}; for (int i = 0; i < cansSold.length; i++) { totalCans = totalCans + cansSold[i]; } System.out.println("We've sold " + totalCans + " cans of pop"); }}

i

totalCans

cansSold

1469

cansSold.length 10

5

And so on...

So let's do something already...

0 1851 922 3703 4854 2095 1286 847 1518 329 563

How does it work?

public class ArrayTest3{ public static void main(String[] args) { int totalCans = 0; int[] cansSold = {185, 92, 370, 485, 209, 128, 84, 151, 32, 563}; for (int i = 0; i < cansSold.length; i++) { totalCans = totalCans + cansSold[i]; } System.out.println("We've sold " + totalCans + " cans of pop"); }}

i

totalCans

cansSold

1553

cansSold.length 10

6

And so on...

So let's do something already...

0 1851 922 3703 4854 2095 1286 847 1518 329 563

How does it work?

public class ArrayTest3{ public static void main(String[] args) { int totalCans = 0; int[] cansSold = {185, 92, 370, 485, 209, 128, 84, 151, 32, 563}; for (int i = 0; i < cansSold.length; i++) { totalCans = totalCans + cansSold[i]; } System.out.println("We've sold " + totalCans + " cans of pop"); }}

i

totalCans

cansSold

1704

cansSold.length 10

7

And so on...

So let's do something already...

0 1851 922 3703 4854 2095 1286 847 1518 329 563

How does it work?

public class ArrayTest3{ public static void main(String[] args) { int totalCans = 0; int[] cansSold = {185, 92, 370, 485, 209, 128, 84, 151, 32, 563}; for (int i = 0; i < cansSold.length; i++) { totalCans = totalCans + cansSold[i]; } System.out.println("We've sold " + totalCans + " cans of pop"); }}

i

totalCans

cansSold

1736

cansSold.length 10

8

And so on...

So let's do something already...

0 1851 922 3703 4854 2095 1286 847 1518 329 563

How does it work?

public class ArrayTest3{ public static void main(String[] args) { int totalCans = 0; int[] cansSold = {185, 92, 370, 485, 209, 128, 84, 151, 32, 563}; for (int i = 0; i < cansSold.length; i++) { totalCans = totalCans + cansSold[i]; } System.out.println("We've sold " + totalCans + " cans of pop"); }}

i

totalCans

cansSold

2299

cansSold.length 10

9

So let's do something already...

0 1851 922 3703 4854 2095 1286 847 1518 329 563

How does it work?

public class ArrayTest3{ public static void main(String[] args) { int totalCans = 0; int[] cansSold = {185, 92, 370, 485, 209, 128, 84, 151, 32, 563}; for (int i = 0; i < cansSold.length; i++) { totalCans = totalCans + cansSold[i]; } System.out.println("We've sold " + totalCans + " cans of pop"); }}

i

totalCans

cansSold

2299

cansSold.length 10

10

So let's do something already...

0 1851 922 3703 4854 2095 1286 847 1518 329 563

How does it work?

public class ArrayTest3{ public static void main(String[] args) { int totalCans = 0; int[] cansSold = {185, 92, 370, 485, 209, 128, 84, 151, 32, 563}; for (int i = 0; i < cansSold.length; i++) { totalCans = totalCans + cansSold[i]; } System.out.println("We've sold " + totalCans + " cans of pop"); }}

i

totalCans

cansSold

2299

cansSold.length 10

10

Is i < 10? No.

So let's do something already...

0 1851 922 3703 4854 2095 1286 847 1518 329 563

How does it work?

public class ArrayTest3{ public static void main(String[] args) { int totalCans = 0; int[] cansSold = {185, 92, 370, 485, 209, 128, 84, 151, 32, 563}; for (int i = 0; i < cansSold.length; i++) { totalCans = totalCans + cansSold[i]; } System.out.println("We've sold " + totalCans + " cans of pop"); }}

i

totalCans

cansSold

2299

cansSold.length 10

10

"We've sold 2299 cans of pop" is printedon the monitor

So let's do something already...

0 1851 922 3703 4854 2095 1286 847 1518 329 563

What would happen if we made this little change?

public class ArrayTest3{ public static void main(String[] args) { int totalCans = 0; int[] cansSold = {185, 92, 370, 485, 209, 128, 84, 151, 32, 563}; for (int i = 0; i <= cansSold.length; i++) { totalCans = totalCans + cansSold[i]; } System.out.println("We've sold " + totalCans + " cans of pop"); }}

i

totalCans

cansSold

2299

cansSold.length 10

10

java.lang.ArrayIndexOutOfBoundsException: 10

Some programming languages don't catch this.

Something to remember

cansSold0 1851 922 3703 4854 2095 1286 847 1518 329 563

The array cansSold was created with10 elements. The indices (plural ofindex) are 0 through 9.

In general, an array of size n will haveindices ranging from 0 through n-1.

When you number things, you're usedto beginning with 1. Computer folksbegin with 0. This leads to "off by one"errors, even among computer veterans.

10cansSold.length

185 92 370 485 209 128 84 151 32 563

1

2

34

5

67

89 10

cansSold0 1851 922 3703 4854 2095 1286 847 1518 329 563

You'll always be mappingyour addressing system onto the array's numberingsystem, so pay close attention to what you'redoing.

Something to remember

What if...

cansSold0 1851 922 3703 4854 2095 1286 847 1518 329 563

...we want to change this program so that it prints the number of cans at each array index starting with the lastindex and working up to the first index?

public class ArrayTest3{ public static void main(String[] args) { int totalCans = 0; int[] cansSold = {185, 92, 370, 485, 209, 128, 84, 151, 32, 563}; for (int i = 0; i <= cansSold.length; i++) { totalCans = totalCans + cansSold[i]; } System.out.println("We've sold " + totalCans + " cans of pop"); }}

What if...

cansSold0 1851 922 3703 4854 2095 1286 847 1518 329 563

...we want to change this program so that it prints the number of cans at each array index starting with the lastindex and working up to the first index?

public class ArrayTest3a{ public static void main(String[] args) { int[] cansSold = {185, 92, 370, 485, 209, 128, 84, 151, 32, 563}; for (int i = cansSold.length - 1; i >= 0; i--) { System.out.println(cansSold[i]); } }}

What if...

cansSold0 1851 922 3703 4854 2095 1286 847 1518 329 563

...we want to change this program so that it computes andprints the average of the number of cans sold per Cokemachine?

public class ArrayTest3{ public static void main(String[] args) { int totalCans = 0; int[] cansSold = {185, 92, 370, 485, 209, 128, 84, 151, 32, 563}; for (int i = 0; i <= cansSold.length; i++) { totalCans = totalCans + cansSold[i]; } System.out.println("We've sold " + totalCans + " cans of pop"); }}

What if...

cansSold0 1851 922 3703 4854 2095 1286 847 1518 329 563

...we want to change this program so that it computes andprints the average of the number of cans sold per Cokemachine?

public class ArrayTest3b{ public static void main(String[] args) { int totalCans = 0; int[] cansSold = {185, 92, 370, 485, 209, 128, 84, 151, 32, 563}; for (int i = 0; i < cansSold.length; i++) { totalCans = totalCans + cansSold[i]; } System.out.println("Average sales: " + ((double)totalCans/cansSold.length)); }}

Note that we're assuming the length of the array is alwaysthe same as the number of Coke machines to be used incalculating the average. This assumption may be wrong.

What if...

cansSold0 1851 -12 3703 4854 2095 1286 847 -18 329 563

...dysfunctional machines are indicated by a negativesales value in the array, so we only want to use non-negative values when computing the average?

public class ArrayTest3b{ public static void main(String[] args) { int totalCans = 0; int[] cansSold = {185, -1, 370, 485, 209, 128, 84, -1, 32, 563}; for (int i = 0; i < cansSold.length; i++) { totalCans = totalCans + cansSold[i]; } System.out.println("Average sales: " + ((double)totalCans/cansSold.length)); }}

What if...

cansSold0 1851 -12 3703 4854 2095 1286 847 -18 329 563

...dysfunctional machines are indicated by a negativesales value in the array, so we only want to use non-negative values when computing the average?

public class ArrayTest3c{ public static void main(String[] args) { int totalCans = 0, totalMachines = 0; int[] cansSold = {185, -1, 370, 485, 209, 128, 84, -1, 32, 563}; for (int i = 0; i < cansSold.length; i++) { if (cansSold[i] >= 0) { totalCans = totalCans + cansSold[i]; totalMachines++; } // no else needed } System.out.println("Average sales: " + ((double)totalCans/totalMachines)); }}

Can we have an array of arrays?Of course we can!

0123

0 01 02 0

0 01 12 2

0 01 22 4

0 01 32 6

But in any given array, all the data has to be of the same type.And all the arrays in an array of arrays have to be of the same type.So if that's what you need, it's easier to use a two-dimensional array...

The two-dimensional array

0123

0 1 2

0 0 00 1 20 2 40 3 6

"rows"

"columns"

In Java, the two-dimensional array is actuallyimplemented internally as an array of arrays,but externally the syntax of the two-dimensionalarray may seem easier to use.

The typical control structure for computing witha two-dimensional array is a nested loop --a loop within another loop. (Note however that earlier we used a nested loop with a one-dimensional array...it's often the case that anested loop indicates a two-dimensional array orvice versa, but it's not a given.)

How would you write a program to load thisarray with the values shown, and thenprint the contents of the array?

The two-dimensional array

0123

0 1 2

0 0 00 1 20 2 40 3 6

public class ArrayTest5{ public static void main(String[] args) { int[][] multTable = new int[4][3]; for (int row = 0; row < multTable.length; row++) { for (int col = 0; col < multTable[row].length; col++) { multTable[row][col] = row * col; } }

for (int row = 0; row < multTable.length; row++) { for (int col = 0; col < multTable[row].length; col++) { System.out.print(multTable[row][col] + " "); } System.out.println(); } }}