Title: Introduction to Programming with Java, for Beginners
1Introduction to Programmingwith Java, for
Beginners
2What if our Frog (from lab) could say 10
different things?
- public class Frog
- private boolean formerPrince
- private String phrase1
- private String phrase2
- private String phrase3
- private String phrase4
- private String phrase5
- private String phrase6
- private String phrase7
- private String phrase8
- private String phrase9
- private String phrase10
- . . .
3What a Coach could have 20 Players?
- public class Coach
- private String name
- private Player Player1
- private Player Player2
- private Player Player3
- private Player Player4
- private Player Player5
- private Player Player5
- private Player Player6
- private Player Player6
- private Player Player8
- private Player Player9
- private Player Player10
- private Player Player11
- private Player Player12
- private Player Player13
- private Player Player14
- private Player Player15
- . . .
4How about a Playlist with 500 Songs?
public class PlayList private Song
song1 private Song song2 private Song
song3 private Song song4 private Song
song5 private Song song6 private Song
song7 private Song song8 private
Song song9 private Song song10
private Song song11 private Song song12
private Song song13 private Song song14
private Song song15 private Song
song16 private Song song17 private
Song song18 private Song song19
private Song song20 private Song song21
private Song song22 private Song
song23 private Song song24 private
Song song25 private Song song26 . . .
5What if we want to store lots of things that all
have the same type
but we dont want to declare a separate variable
for each one?
Thats what arrays are good for.
6What is an Array?
A collection of values, all with same type
7Array Elements and Indices
- The data array has 5 elements. The value of
- data0 is 6
- data1 is 10
- data2 is 12
- data3 is 0
- data4 is 0
- The number within square brackets is called an
index - The valid indices are 0 thru (array length - 1)
- NOTE Whenever you see square brackets in a
Java program, it means youre dealing with an
array.
8Array of Primitives
- int data
- data new int3
- data0 5
- data1 10
9An Array is an Object
10Using Array Elements in Expressions
- An element of an array of ints can be used
virtually anywhere an expression of type int is
valid. - Likewise for arrays of other types.
- int data new int 6, 10, 12, 0, 0
- int x data0
- data3 data2
- data4 data3 data2 2
- System.out.println(data0 is data0)
- // note order of operations below
- data4 Math.pow(2, data4)
11Accessing an Arrays Length
- int data
- data new int5 // data.length is 5
- data0 6
- data1 10
- data2 12
- int result 0
- for (int i 0 i lt data.length i)
- result result datai
-
12Complete the sum(..) method
public class ArrayTool / Takes an
array of ints as an argument. returns the
sum of all the integers in the array. /
public int sum
Welcome to DrJava gt ArrayTool tool new
ArrayTool() gt int data new int 6, 10, 12,
0, 0 gt tool.sum(data) 28
13Array Out of Bounds Exceptions
public class ArrayTool public int sum(int
data) int sum 0 for (int i 0 i lt
data.length i) sum sum datai //
sum datai return sum
public int sum2(int data) int sum 0
for (int i 0 i lt data.length i) //
error! sum sum datai // sum
datai return sum
gt ArrayTool tool new ArrayTool() gt int data
new int 6, 10, 12, 0, 0 gt
tool.sum(data) 28 gt tool.sum2(data) ArrayIndexOutO
fBoundsException
14Examples of Arrays of Other Primitive Types
15Complete this method
public class Toolkit / Returns true if all
integers in the data array are positive,
false otherwise. / public boolean
allPositive(int data)