cop 3503 fall 2012 shayan javed lecture 3

53
1 / 53 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 3 Programming Fundamentals using Java 1

Upload: carney

Post on 23-Feb-2016

36 views

Category:

Documents


1 download

DESCRIPTION

COP 3503 FALL 2012 Shayan Javed Lecture 3. Programming Fundamentals using Java. Arrays. Objects in Java (but not in C/C++). An array is a container object that holds a fixed number of values of a single type . Specify length when creating. Arrays. Arrays. Example: - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: COP 3503  FALL 2012 Shayan Javed Lecture 3

1 / 531

COP 3503 FALL 2012SHAYAN JAVED

LECTURE 3

Programming Fundamentals using Java

Page 2: COP 3503  FALL 2012 Shayan Javed Lecture 3

2 / 53

Arrays

Objects in Java (but not in C/C++).

An array is a container object that holds a fixed number of values of a single type.

Specify length when creating.

Page 3: COP 3503  FALL 2012 Shayan Javed Lecture 3

3 / 53

Arrays

Page 4: COP 3503  FALL 2012 Shayan Javed Lecture 3

4 / 53

Arrays

Example: int array of length 5.

Declaration:int[] array1;

Initialization:array1 = new int[5];

Page 5: COP 3503  FALL 2012 Shayan Javed Lecture 3

5 / 53

Arrays

Length provided can be an int variable.

Example:

int x = 6;array1 = new int[x];

Page 6: COP 3503  FALL 2012 Shayan Javed Lecture 3

6 / 53

Arrays

Two ways to declare:

int[] array1; // convention

int array1[]; // discouraged!

Page 7: COP 3503  FALL 2012 Shayan Javed Lecture 3

7 / 53

Arrays

Declare and initialize with some values:

int[] array1 = {5, 100, 200, 45};

Length = no. of elements between the braces.

Page 8: COP 3503  FALL 2012 Shayan Javed Lecture 3

8 / 53

Arrays

Property “length”.

Very useful!

Especially for traversing

Page 9: COP 3503  FALL 2012 Shayan Javed Lecture 3

9 / 53

Arrays

Example: Printing out all the values of an array

for (int i = 0; i < array1.length; i++) {System.out.println(array1[i]);

}

Page 10: COP 3503  FALL 2012 Shayan Javed Lecture 3

10 / 53

Arrays

Another way of traversing: for-each loop:

for (int i : array1) {System.out.println(i);

}

For each element i in array1 do…

Page 11: COP 3503  FALL 2012 Shayan Javed Lecture 3

11 / 53

Arrays

What happens here?

System.out.println(array1[array1.length]);

Page 12: COP 3503  FALL 2012 Shayan Javed Lecture 3

12 / 53

Arrays

ArrayIndexOutOfBoundsException thrown

Will later look at how to handle these exceptions.

Be careful about boundaries!

Page 13: COP 3503  FALL 2012 Shayan Javed Lecture 3

13 / 53

Passing Arrays to Methods

public static void printArray(int[] array) { for (int i = 0; i < array.length; i++) {

System.out.println(array[i]); } } int[] array1 = {5, 100, 200, 45};

printArray( array1 );

Page 14: COP 3503  FALL 2012 Shayan Javed Lecture 3

14 / 53

Passing Arguments to Methods

Two ways: Pass-by-Value:

Used for primitive types (int/char/float/etc.)Changing value in the method does not change the original variable

Pass-by-reference:Used for objects (such as arrays). Changing in the method changes the original!

Page 15: COP 3503  FALL 2012 Shayan Javed Lecture 3

15 / 53

Pass-by-value

public void changeValue(float y) {y = 3.4;

}

float y = 5.4; changeValue(y); System.out.println(y);

What is the output?

Page 16: COP 3503  FALL 2012 Shayan Javed Lecture 3

16 / 53

Pass-by-reference

public void changeValue(float[] y) {y[y.length - 1] = 3.4;

}

float[] y = {5.4, 10.4, 1.2}; changeValue(y); System.out.println(y[1]);

What is the output?

Page 17: COP 3503  FALL 2012 Shayan Javed Lecture 3

17 / 53

Pass-by-reference

y = {5.4, 10.4, 3.4};

After calling the method

Page 18: COP 3503  FALL 2012 Shayan Javed Lecture 3

18 / 53

public void changeValue(float y) {y = 3.4;

}

float[] y = {5.4, 10.4, 1.2}; changeValue(y[0]); System.out.println(y[0]);

What is the output?

Page 19: COP 3503  FALL 2012 Shayan Javed Lecture 3

19 / 53

Passing Arguments to Methods

So be careful when passing objects/arrays to methods.

Page 20: COP 3503  FALL 2012 Shayan Javed Lecture 3

20 / 53

Copying arrays public static void arraycopy(Object src,

int srcPos, Object dest,

int destPos, int length)

Provided in the System class

System.arrayCopy(…)

Page 21: COP 3503  FALL 2012 Shayan Javed Lecture 3

21 / 53

Copying arrays

Example:

char[] copyFrom = { 'd', 'e', 'c', 'a', 'f', 'f', 'e', 'i', 'n', 'a', 't', 'e', 'd'}; char[] copyTo = new char[7]; System.arraycopy(copyFrom, 2, copyTo, 0, 7);

System.out.println(copyTo);

What is the output?

Page 22: COP 3503  FALL 2012 Shayan Javed Lecture 3

22 / 53

Copying arrays

Output: caffein

char[] copyFrom = { 'd', 'e', 'c', 'a', 'f', 'f', 'e', 'i', 'n', 'a', 't', 'e', 'd'};

Page 23: COP 3503  FALL 2012 Shayan Javed Lecture 3

23 / 53

Multi-dimensional Arrays

Looked at 1-dimensional (1-D) arrays so far.

Multi-dimensional arrays: Array of arrays!

Used for multi-dimensional data

Page 24: COP 3503  FALL 2012 Shayan Javed Lecture 3

24 / 53

Multi-dimensional Arrays

Examples: Storing all student info in a 2-D array Matrices Storing 3D data such as points/vertices (x, y, z)

Page 25: COP 3503  FALL 2012 Shayan Javed Lecture 3

25 / 53

Multi-dimensional Arrays

Declaration (2-D arrays):

elementType[][] variableName;

Example:

float[][] matrix;

Page 26: COP 3503  FALL 2012 Shayan Javed Lecture 3

26 / 53

Multi-dimensional Arrays

Initialization:matrix = new float[2][3];

2 rows, 3 columns

Page 27: COP 3503  FALL 2012 Shayan Javed Lecture 3

27 / 53

Multi-dimensional Arrays

Declare and initialize with some values:

float[][] matrix = { {2.3, 50.1, 5.5}, {9.8, 4.0, 11.4} };

Creates 2 arrays with length 3;

Page 28: COP 3503  FALL 2012 Shayan Javed Lecture 3

28 / 53

Multi-dimensional Arrays

Each row can have a different length

float[][] matrix = { {2.3, 50.1, 5.5}, {9.8, 4.0} };

First row = length 3 Second row = length 2

Page 29: COP 3503  FALL 2012 Shayan Javed Lecture 3

29 / 53

Multi-dimensional Arrays

float[][] matrix = new float[2][];

Specifies size of the array - 2

But not the size of the array in each row – do it individually

matrix[0] = new float[3];matrix[1] = new float[1];

Page 30: COP 3503  FALL 2012 Shayan Javed Lecture 3

30 / 53

Multi-dimensional Arrays

Traversal:

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

System.out.println(matrix[row][column]);}

}

Page 31: COP 3503  FALL 2012 Shayan Javed Lecture 3

31 / 53

The String class

Array of characters in many languages (C/C++ for ex.)

But: Strings are objects in Java

Strings are widely used – so should know how to use them.

Page 32: COP 3503  FALL 2012 Shayan Javed Lecture 3

32 / 53

The String class

Creating Strings:

String string1 = new String(“this is a string”);

String string2 = “this is a string”; // slight difference in the 2nd one

String from a character array:

char[] charArray = {‘s’, ‘t’, ‘r’, ‘i’, ‘n’, ‘g’};String string3 = new String(charArray);

Page 33: COP 3503  FALL 2012 Shayan Javed Lecture 3

33 / 53

The String class

Strings are immutable – Not changeable!

Page 34: COP 3503  FALL 2012 Shayan Javed Lecture 3

34 / 53

The String class

Strings are immutable – Not changeable!

So what happens here?:

String s = “Java”;s = “Another String”;

Page 35: COP 3503  FALL 2012 Shayan Javed Lecture 3

35 / 53

The String class

Initially:

Now:

s Java

s Java

Another String

Page 36: COP 3503  FALL 2012 Shayan Javed Lecture 3

36 / 53

The String class

A new object “Another String” is created

Reference s now points to it

No reference to “Java”, so garbage collection cleans it up

Page 37: COP 3503  FALL 2012 Shayan Javed Lecture 3

37 / 53

String comparisons

Interned instances of the same string

Example:String s1 = “Java”;String s2 = new String(“Java”);

String s3 = “Java”;

Page 38: COP 3503  FALL 2012 Shayan Javed Lecture 3

38 / 53

String comparisons

s1 Java

Javas2

In Memory:

Page 39: COP 3503  FALL 2012 Shayan Javed Lecture 3

39 / 53

String comparisons

s1 Javas3

Javas2

In Memory:

Page 40: COP 3503  FALL 2012 Shayan Javed Lecture 3

40 / 53

String comparisons

s1 == s2 is ? (different reference)

Page 41: COP 3503  FALL 2012 Shayan Javed Lecture 3

41 / 53

String comparisons

s1 == s2 is false (different reference)

Page 42: COP 3503  FALL 2012 Shayan Javed Lecture 3

42 / 53

String comparisons

s1 == s2 is false (different reference)

s1 == s3 is ? (same reference)

Page 43: COP 3503  FALL 2012 Shayan Javed Lecture 3

43 / 53

String comparisons

s1 == s2 is false (different reference)

s1 == s3 is true (same reference)

Page 44: COP 3503  FALL 2012 Shayan Javed Lecture 3

44 / 53

String comparisons

Don’t use “==“ for comparing objects! Compares references

Page 45: COP 3503  FALL 2012 Shayan Javed Lecture 3

45 / 53

String comparisons

Don’t use “==“ for comparing objects! Compares references

string1.equals(string2) for content comparison s1.equals(s2) is true

Page 46: COP 3503  FALL 2012 Shayan Javed Lecture 3

46 / 53

String comparisons

Don’t use “==“ for comparing objects! Compares references

string1.equals(string2) for content comparison s1.equals(s2) is true

Define equals() method individually for every class – will look at it later

Page 47: COP 3503  FALL 2012 Shayan Javed Lecture 3

47 / 53

The String class

Some useful String methods:

.length() – returns the length

.charAt(int index) – returns char at that index

.split(String regex) – Splits string according to a regular expression

Will use String throughout

Page 48: COP 3503  FALL 2012 Shayan Javed Lecture 3

48 / 53

Input

public static void main(String[] args) {...

}

Page 49: COP 3503  FALL 2012 Shayan Javed Lecture 3

49 / 53

Input

public static void main(String[] args) {...

}

String[] args = array of arguments to the program

Page 50: COP 3503  FALL 2012 Shayan Javed Lecture 3

50 / 53

Input

public static void main(String[] args) {...

}

String[] args = array of arguments to the program

Executing a program:java programName arg1 arg2 arg3 ...

Page 51: COP 3503  FALL 2012 Shayan Javed Lecture 3

51 / 53

Convert from String

Input are Strings

Need to convert to int, double, etc...

Use Wrapper classes to convert

Primitives have wrapper classes: Integer for int, Double for double, etc.

Page 52: COP 3503  FALL 2012 Shayan Javed Lecture 3

52 / 53

Convert from String

java programName 2 6.5 “testing java”

int firstArgument = Integer.parseInt(args[0]);

double secArgument = Double.parseDouble(args[1]);

String thirdArgument = args[2]; // “testing java”

Page 53: COP 3503  FALL 2012 Shayan Javed Lecture 3

53 / 53

Next lecture...

Java subclasses and inheritance