programming methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5...

Post on 31-Mar-2015

217 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Programming Methodology (1)

temperature1temperature2

temperature3

temperature4

temperature5

temperature6 temperature7

System.out.println( temperature1 );

System.out.println( temperature2 );

System.out.println( temperature3 );

System.out.println( temperature4 );

System.out.println( temperature5 );

System.out.println( temperature6 );

System.out.println( temperature7 );

for ( int i = 1 ; ; i++ )

{

}

System.out.println( );

i <= 7

temperature1

temperature1temperature2

temperature3

temperature4

temperature5

temperature6 temperature7

Arrays

temperature

Arrays

Learning objectives

• create arrays;

• use loops to process arrays;

• use an enhanced for loop to process an array;

• use arrays as method inputs and outputs;

• develop routines for accessing and manipulating arrays.

What is an array?

An array is a data type that stores a collection of items!

These items are referred to as the elements of the array!

All elements must be of the same type BUT there is no restriction on which type this is!

1.75

75.1

12.5

1.75

31.5

‘T’

‘a’

‘s’

‘s’

‘W’

“Blue”

“Red”

“White”

“Black”

“Green”

double char String

Examples

9.2

“Java”

12

‘j’

true

mixed

Examples

How do you create an array?

Array creation is a two-step process:

STEP 1:Declare an array variable.

typeOfElement arrayName;[ ]

int score;[ ]

An array of exam scores:Exam scores are whole numbers

typeOfElement arrayName;[ ]

String name;[ ]

An array of student names:Names are Strings

typeOfElement arrayName;[ ]

temperature;double[ ]

An array of temperatures:temperatures are real numbers

The effect on computer memory of declaring an array …..

Java Instructions

double[ ] temperature ;

Computer Memory

temperature

?

This is called a reference variable

STEP 2:Allocating memory to store the array elements!

What information do we need to provide ?

a) The size of the array.

b) The type of each element.

Combine the two with a new operator.

Example: an array of 10 scores:

nameOfArray = new arrayType [ size ];

score = new int [ 10 ];

Example: an array of 20 student names:

nameOfArray = new arrayType [ size ];

name = new String [ 20 ];

nameOfArray = new arrayType [ size ];

Returning to the temperature array:

temperature = new double [ 7 ];

The effect on computer memory of sizing an array….

Java Instructions

double[ ] temperature ;

Computer Memory

temperature

?

item of type 'double'

item of type 'double'

item of type 'double'

item of type 'double'

item of type 'double'

item of type 'double'

item of type 'double' temperature = new double[7];

Combining steps 1 and 2

double[ ] temperature ;

= new double [7];temperature

= new double [7];

Initializing an array

double[ ] temperature = new double [7];{9, 11.5, 11, 8.5, 7, 9, 8.5} ;

Naming the array elements….

0 1 2 3 4 5 6

temperature

First item is

temperature[0]

Last item is

temperature[6]

Array variables can be used like any other variable of the given type in Java.

score[0] = 12;

Entering 12 into the first position of the score array:

name[2] = “Aaron”;

Entering “Aaron” into the third position of the name array:

temperature[6] = 21.5;

Entering 21.5 into the 7th position of the temperature array:

Entering values into an array from the keyboard..

double num;

num = sc.nextDouble();

Entering into a simple double variable

temperature [ ] = sc.nextDouble();

Entering into first array variable

0

double num;

num = sc.nextDouble();

Entering into a simple double variable

temperature [ ] = sc.nextDouble();

Entering into second array variable

1

double num;

num = sc.nextDouble();

temperature [ ] = sc.nextDouble();

Entering into last array variable

6

Entering into a simple double variable

More examples of accessing array elements…

0 1 2 3 4 5 6

temperature

Printing 6th element on the screen…

0 1 2 3 4 5 6

temperature

0 1 2 3 4 5 6

temperature

System.out.println( num );

0 1 2 3 4 5 6

temperature

System.out.println( temperature[5] );

0 1 2 3 4 5 6

temperature

Double the 5th element….

0 1 2 3 4 5 6

temperature

num = num * 2;

0 1 2 3 4 5 6

temperature

temperature[4] = temperature[4] * 2;

Check the temperature on the third day…..

0 1 2 3 4 5 6

temperature

if (num >= 18){

System.out.println("it was hot today");}

0 1 2 3 4 5 6

temperature

if ( temperature[2] >= 18){

System.out.println("it was hot today");}

0 1 2 3 4 5 6

temperature

What is wrong here?

System.out.println( temperature[ 7 ] );

The index must be valid (0 -6)

otherwise you will get an error at run-time.

ArrayIndexOutOfBoundsException

Using a variable as the array index…

System.out.println( temperature[ ] ); i0123456

for ( ; ; )

{

}

int i = 0 i++ i < 7i <= 7

This will cause your program to crash!What will happen here?

7

ArrayIndexOutOfBoundsException

System.out.println( temperature[ ] ); i0123456

for ( ; ; )

{

}

int i = 0 i++ i < 7 i < temperature.length; i++)

for ( ; ; )

{

} temperature[ ] = sc.nextDouble(); i01

76

int i = 0 i++ i < temperature.length; i++)

System.out.println ("enter max temperature for day " + ); (i+1)12

Putting it all together…..

public static void main(String[ ] args){ Scanner sc = new Scanner(System.in); double[ ] temperature = new double[7]; for (int i = 0; i < temperature.length; i++) { // code to enter temperatures } for (int i = 0; i < temperature.length; i++) {

// code to display temperatures }}

Re-writing this program using methods enterTemps and displayTemps …..

public static void main(String[ ] args){ Scanner sc = new Scanner(System.in); double[ ] temperature = new double[7]; for (int i = 0; i < temperature.length; i++) { // code to enter temperatures } for (int i = 0; i < temperature.length; i++) {

// code to display temperatures }}

Put this loop into enterTemps method

Put this loop into displayTemps method

public static void main(String[ ] args){ Scanner sc = new Scanner(System.in); double[ ] temperature = new double[7];

}

private static void enterTemps ( ){

// loop to enter into temperature array here}private static void displayTemps ( ){

// loop to display temperature array here}

enterTemps( );displayTemps( );temperature

temperature

temperatureIn

temperatureIn

double[ ]

double[ ]

// loop to enter into temperatureIn array here

// loop to display temperatureIn array here

Passing arrays as parameters…

private static void enterTemps(double[ ] temperatureIn){ Scanner sc = new Scanner(System.in); for (int i = 0; i < temperatureIn.length; i++) { System.out.println ("enter max temperature for day " + (i+1)); temperatureIn[i] = sc.nextDouble(); }}

public static void main (String [ ] args){

double[ ] temperature = new double[7];enterTemps( temperature );displayTemps( temperature );

}

temperature

temperatureIn

The effect on computer memory of passing an array as a parameter………

Java Instructions

main (String[ ] args){ // create temperature array

}

Computer Memory

temperature

[ 6 ]

[ 5 ]

[ 4 ]

[ 3 ]

[ 2 ]

[ 1 ]

[ 0 ]enterTemps( temperature );

enterTemps (double[] temperatureIn)

{

// update temperatureIn

}

temperatureIn

Returning an array from a method……..

private static void enterTemps(double[ ] temperatureIn){ Scanner sc = new Scanner(System.in); for (int i = 0; i < temperatureIn.length; i++) { System.out.println ("enter max temperature for day " + (i+1)); temperatureIn[i] = sc.nextDouble(); }}

private static ? enterTemps( ){ Scanner sc = new Scanner(System.in); for (int i = 0; i < temperatureIn.length; i++) { System.out.println ("enter max temperature for day " + (i+1)); temperatureIn[i] = sc.nextDouble(); }}

private static double[ ] enterTemps( ){ Scanner sc = new Scanner(System.in); for (int i = 0; i < temperatureIn.length; i++) { System.out.println ("enter max temperature for day " + (i+1)); temperatureIn[i] = sc.nextDouble(); }}

private static double[ ] enterTemps( ){ Scanner sc = new Scanner(System.in); for (int i = 0; i < temperatureIn.length; i++) { System.out.println ("enter max temperature for day " + (i+1)); temperatureIn[i] = sc.nextDouble(); }}

double[ ] temperatureOut = new double[7];

private static double[ ] enterTemps( ){ Scanner sc = new Scanner(System.in); for (int i = 0; i < temperatureOut.length; i++) { System.out.println ("enter max temperature for day " + (i+1)); temperatureOut[i] = sc.nextDouble(); }}

double[ ] temperatureOut = new double[7];

private static double[ ] enterTemps( ){ Scanner sc = new Scanner(System.in); for (int i = 0; i < temperatureOut.length; i++) { System.out.println ("enter max temperature for day " + (i+1)); temperatureOut[i] = sc.nextDouble(); } return temperatureOut;}

double[ ] temperatureOut = new double[7];

Modifying the main method..

public static void main (String [ ] args){

double[ ] temperature

displayTemps( temperature );}

= new double[7];;enterTemps( temperature );enterTemps( );temperature = enterTemps( );

The enhanced 'for' loop….

for ( ){

}

int i = 0; i < temperatureIn.length ; i++

System.out.println( );

temperatureIn[ i ]

temperatureIn item : double

item

“For every item in the temperatureIn array”

Some useful array methods…

public class SomeUsefulArrayMethods{

public static void main (String[] args){ Scanner sc = new Scanner(System.in);

int[ ] someArray; System.out.println("How many elements to store?"); int size = sc.nextInt(); someArray = new int[size];

// call methods here }

// methods to process an array here}

Allows user to size the array.

Array summation..

SET total TO zero

LOOP FROM first element TO last element

BEGIN

END

SET total TO total + current element

RETURN total

private static sum ( int[ ] arrayIn ) int

{

}

int total = 0;LOOP FROM first element TO last element

BEGIN

END

SET total TO total + current element

RETURN total

private static sum ( int[ ] arrayIn ) int

{

}

int total = 0;for ( int i = 0 ; i < arrayIn.length ; i++ ) BEGIN

END

SET total TO total + current element

RETURN total

private static sum ( int[ ] arrayIn ) int

{

}

int total = 0;for ( int i = 0 ; i < arrayIn.length ; i++ ) {

}

SET total TO total + current element

RETURN total

private static sum ( int[ ] arrayIn ) int

{

}

int total = 0;for ( int i = 0 ; i < arrayIn.length ; i++ ) {

}

total = total + arrayIn [ i ] ;

RETURN total

private static sum ( int[ ] arrayIn ) int

{

}

int total = 0;for ( int i = 0 ; i < arrayIn.length ; i++ ) {

}

total = total + arrayIn [ i ] ;

return total;

private static sum ( int[ ] arrayIn ) int

{

}

int total = 0;for ( int currentElement: arrayIn ) {

}

total = total + arrayIn [ i ] ;

return total;

private static sum ( int[ ] arrayIn ) int

{

}

int total = 0;for ( int currentElement: arrayIn ) {

}

total = total + currentElement ;

return total;

private static sum ( int[ ] arrayIn ) int

{

}

Consider the following explicit creation of an array:

a) What would be the value of someArray.length ?

int[ ] someArray = {2, 5, 1, 9, 11};

Consider the following explicit creation of an array:

int[ ] someArray = {2, 5, 1, 9, 11};

5

2

1

9

someArray

11

a) What would be the value of someArray.length ?

Consider the following explicit creation of an array:

a) What would be the value of someArray.length ?

int[ ] someArray = {2, 5, 1, 9, 11};

5

2

1

9

someArray

11

5

Consider the following explicit creation of an array:

b)What is the value of someArray[2]?

int[ ] someArray = {2, 5, 1, 9, 11};

5

2

1

9

someArray

11

Consider the following explicit creation of an array:

b)What is the value of someArray[2]?

int[ ] someArray = {2, 5, 1, 9, 11};

5

2

1

9

someArray

11

[0]

[1]

[2]

[3]

[4]

Consider the following explicit creation of an array:

b)What is the value of someArray[2]?

int[ ] someArray = {2, 5, 1, 9, 11};

5

2

1

9

someArray

11

[0]

[1]

[2]

[3]

[4]

Consider the following explicit creation of an array:

c)What would happen if you tried to access someArray[6]?

int[ ] someArray = {2, 5, 1, 9, 11};

5

2

1

9

someArray

11

[0]

[1]

[2]

[3]

[4]

Consider the following explicit creation of an array:

c)What would happen if you tried to access someArray[6]?

int[ ] someArray = {2, 5, 1, 9, 11};

5

2

1

9

someArray

11

[0]

[1]

[2]

[3]

[4]

Consider the following explicit creation of an array:

c)What would happen if you tried to access someArray[6]?

int[ ] someArray = {2, 5, 1, 9, 11};

5

2

1

9

someArray

11

[0]

[1]

[2]

[3]

[4]ArrayIndexOutOfBoundsException

5

2

1

9

someArray

11

[0]

[1]

[2]

[3]

[4]

d)Write a standard for loop that will double the value of every item in someArray.

5

2

1

9

someArray

11

[0]

[1]

[2]

[3]

[4]

d)Write a standard for loop that will double the value of every item in someArray.

for ( ? ; ? ; ? )

{

// code to double a value here

}

5

2

1

9

someArray

11

[0]

[1]

[2]

[3]

[4]

d)Write a standard for loop that will double the value of every item in someArray.

for (int i = 0; ? ; ? )

{

// code to double a value here

}

5

2

1

9

someArray

11

[0]

[1]

[2]

[3]

[4]

d)Write a standard for loop that will double the value of every item in someArray.

for (int i = 0; ? ; i++)

{

// code to double a value here

}

5

2

1

9

someArray

11

[0]

[1]

[2]

[3]

[4]

d)Write a standard for loop that will double the value of every item in someArray.

for (int i = 0; i <= 4 ; i++)

{

// code to double a value here

}

5

2

1

9

someArray

11

[0]

[1]

[2]

[3]

[4]

d)Write a standard for loop that will double the value of every item in someArray.

for (int i = 0; i < 5 ; i++)

{

// code to double a value here

}

5

2

1

9

someArray

11

[0]

[1]

[2]

[3]

[4]

d)Write a standard for loop that will double the value of every item in someArray.

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

{

// code to double a value here

}

5

2

1

9

someArray

11

[0]

[1]

[2]

[3]

[4]

d)Write a standard for loop that will double the value of every item in someArray.

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

{

someArray[i] = someArray[i] * 2;

}

10

4

2

18

someArray

22

[0]

[1]

[2]

[3]

[4]

d)Write a standard for loop that will double the value of every item in someArray.

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

{

someArray[i] = someArray[i] * 2;

}

10

4

2

18

someArray

22

[0]

[1]

[2]

[3]

[4]

e)Should you use an enhanced for loop here?

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

{

someArray[i] = someArray[i] * 2;

}

10

4

2

18

someArray

22

[0]

[1]

[2]

[3]

[4]

e)Should you use an enhanced for loop here?

for ( int item: someArray )

{

someArray[i] = someArray[i] * 2;

}

10

4

2

18

someArray

22

[0]

[1]

[2]

[3]

[4]

e)Should you use an enhanced for loop here?

for ( int item: someArray )

{

item = item * 2;

}

10

4

2

18

someArray

22

[0]

[1]

[2]

[3]

[4]

e)Should you use an enhanced for loop here?

for ( int item: someArray )

{

item = item * 2;

}

Enhanced for loops should not modify the original array!

f) Place the original for loop in a method, increaseMarks.

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

{

someArray[i] = someArray[i] * 2;

}

public static void main (String [ ] args)

{

int [ ] someArray = {2, 5, 1, 9, 11};

}

private static ? increaseMarks( ? )

{

}

f) Place the original for loop in a method, increaseMarks.

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

{

someArray[i] = someArray[i] * 2;

}

public static void main (String [ ] args)

{

int [ ] someArray = {2, 5, 1, 9, 11};

// call method

}

private static ? increaseMarks( int[ ] someArray )

{

}

f) Place the original for loop in a method, increaseMarks.

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

{

someArray[i] = someArray[i] * 2;

}

public static void main (String [ ] args)

{

int [ ] someArray = {2, 5, 1, 9, 11};

// call method

}

private static void increaseMarks( int[ ] someArray )

{

}

f) Place the original for loop in a method, increaseMarks.

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

{

someArray[i] = someArray[i] * 2;

}

public static void main (String [ ] args)

{

int [ ] someArray = {2, 5, 1, 9, 11};

// call method

}

private static void increaseMarks( int[ ] someArray )

{

}

f) Place the original for loop in a method, increaseMarks.

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

{

someArray[i] = someArray[i] * 2;

}

public static void main (String [ ] args)

{

int [ ] someArray = {2, 5, 1, 9, 11};

increaseMarks( );

}

private static void increaseMarks( int[ ] someArray )

{

}

f) Place the original for loop in a method, increaseMarks.

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

{

someArray[i] = someArray[i] * 2;

}

public static void main (String [ ] args)

{

int [ ] someArray = {2, 5, 1, 9, 11};

increaseMarks( someArray);

}

Practical Task…

Room 1 Room 2 Room 3 Room 4 Room 5

30 25 50 30 40

a) Declare and initialise an array, rooms, to hold the following capacities:

c) Add an instruction in main to call the displayRooms method.

b) Write a method, displayRooms, that accepts the array of room capacities and displays the capacity of each room.

top related