17
1 Lecture 16/2/11: Contents Local variable Array: the concept Working with an array An advert: Reading week 2 March. No lecture. All are welcome to room B12 Cruciform from 7.30- 9.00; Martin O’Shea will consult

1 Lecture 16/2/11: Contents Local variable Array: the concept Working with an array An advert: Reading week 2 March. No lecture. All are welcome to room

Embed Size (px)

DESCRIPTION

3 Local variables(1) public class Prog06{ private static int i=2; public static void One(){ int i=2; i-=2; System.out.println(i); } public static void Two(int i){ i=i+2; System.out.println(i); One(); } public static int Three(int b){ b=2*b+1; return b+2; }

Citation preview

Page 1: 1 Lecture 16/2/11: Contents Local variable Array: the concept Working with an array An advert: Reading week 2 March. No lecture. All are welcome to room

1

Lecture 16/2/11: Contents

• Local variable• Array: the concept • Working with an array

An advert: Reading week 2 March. No lecture. All are welcome to room B12 Cruciform from 7.30- 9.00; Martin O’Shea will consult

Page 2: 1 Lecture 16/2/11: Contents Local variable Array: the concept Working with an array An advert: Reading week 2 March. No lecture. All are welcome to room

2

Local variables: Definition

Local variable:

is created within a method or instance in a { } (curly brace) block.

Its scope is limited within the block.

Therefore, same name can be used in different blocks for different variables.

Page 3: 1 Lecture 16/2/11: Contents Local variable Array: the concept Working with an array An advert: Reading week 2 March. No lecture. All are welcome to room

3

Local variables(1)• public class Prog06{• private static int i=2;

• public static void One(){• int i=2;• i-=2;• System.out.println(i); }• public static void Two(int i){• i=i+2;• System.out.println(i);• One(); }• public static int Three(int b){• b=2*b+1;• return b+2;• }

Page 4: 1 Lecture 16/2/11: Contents Local variable Array: the concept Working with an array An advert: Reading week 2 March. No lecture. All are welcome to room

4

Local variables(2)• public static void main(String[] args){• System.out.println(i+1);• One();• i = Three(i);• System.out.println(i);• Three(i);• Two(i+1);• One();• }//end of main• }//end of class

Page 5: 1 Lecture 16/2/11: Contents Local variable Array: the concept Working with an array An advert: Reading week 2 March. No lecture. All are welcome to room

5

Actual printoutWorking from main method:Print Why 3 First line executed, static i=2 0 Second line executed, One() prints 0

Third line: Three(2) returns class’ i=7 7 Forth line executed, printing class’ i=7

Fifth line: Three(7) produces 17, going nowhere 10 Sixth line: Two(8), prints 8+2, then executing 0 One() to print 0 0 Seventh line: One() to print 0

Page 6: 1 Lecture 16/2/11: Contents Local variable Array: the concept Working with an array An advert: Reading week 2 March. No lecture. All are welcome to room

6

Array (1) Array is an indexed list of elements of the

same type; the index is supplied by default (!) A string array nam[ ]: contains both entries and index.String nam[]

={“John”,“Paul”,“George”,“Ringo”}; Index:         0          1           2         3

Length (the number of entries) is  4  

An integer array age[ ]: int age[ ]= {23, 32, 19, 30, 25, 25, 23, 30}; Index:    0  1    2  3   4   5   6  7

Length is  8  

Page 7: 1 Lecture 16/2/11: Contents Local variable Array: the concept Working with an array An advert: Reading week 2 March. No lecture. All are welcome to room

7

Array (2) Not an array: abc[ ]={8, Ringo, +} - WHY? (different types) [ ] - on the array name's right is used

to indicate arrays2. Declaring arrays Both, int ages[ ]; and int[ ] ages;  is OK

Page 8: 1 Lecture 16/2/11: Contents Local variable Array: the concept Working with an array An advert: Reading week 2 March. No lecture. All are welcome to room

8

Array (3) Initialisation of an array: either ages = new int[8]; // array with 8 zeros or ages[ ] = {23, 32, 19, 30, 25, 25, 23,

30}; //specify what is needed  Simultaneously declaring & initialising (with

zeros) int ages[] = new int[8];

Page 9: 1 Lecture 16/2/11: Contents Local variable Array: the concept Working with an array An advert: Reading week 2 March. No lecture. All are welcome to room

9

Array (4)ages[ ] = {23, 32, 19, 30, 25, 25, 23,

30};

Accessing array elements ages [1]   is   32

   

int i=4;int j = ages [i];  // assigning j with   25

Page 10: 1 Lecture 16/2/11: Contents Local variable Array: the concept Working with an array An advert: Reading week 2 March. No lecture. All are welcome to room

10

Work with arrays(1)Data of 5 students:

double height[ ]={1.56, 1.72, 1.80, 1.85, 1.90}; //in m

double weight[ ]={65.3,80.0,78.1,76.5,112.8}; // in kg

Problem: compute the body mass index for all the students, bmi=weight/height2

(in the US, those with bmi between 20 and 25 are considered of normal weight)

Page 11: 1 Lecture 16/2/11: Contents Local variable Array: the concept Working with an array An advert: Reading week 2 March. No lecture. All are welcome to room

11

Work with arrays(2)Loop for is natural with arrays: the index used as the

counterbmi[ ]=new double[5];for (int I = 0; I < 5; I + +)

bmi[I]=weight[I] / (height[I]height[I]); If length of student arrays is not known or is

variable, put array’s length whatever it is: bmi[ ]=new double[height.length];for (int I = 0; I < height.length; I + +)

bmi[I]=weight[I] / (height[I]height[I]); 

Page 12: 1 Lecture 16/2/11: Contents Local variable Array: the concept Working with an array An advert: Reading week 2 March. No lecture. All are welcome to room

12

Work with arrays(3)The same result with a method for the bmi: double[ ] bmiindex(double h[ ], double w[ ]){ double in[ ]; for (int ii = 0; ii < h.length; ii = ii+1) in[ii]=h[ii]/(w[ii]w[ii]); return in; }Method bmiindex is just a frame box; to make it

work, one needs to put within a class this:double[ ] bmi=bmiindex(weight, height);

Page 13: 1 Lecture 16/2/11: Contents Local variable Array: the concept Working with an array An advert: Reading week 2 March. No lecture. All are welcome to room

13

Finding a maximum in an array

  double x[ ]; //assume taken from somewhere int place=-1; //index of the max entry double maxim=-1000;

for (int i = 0; i < x.length; i = i+1){ if (x[i] > maxim) {maxim=x[i]; place=i;}

} Question: Make it into a method.

Page 14: 1 Lecture 16/2/11: Contents Local variable Array: the concept Working with an array An advert: Reading week 2 March. No lecture. All are welcome to room

14

Finding maximum : a method

 int place= -1; //for keeping the index of max entrydouble findMax(double x[ ]){ //method’s wrap-up double maxim= -1000; for (int i = 0; i < x.length; i = i+1){ if (x[i] > maxim) {maxim=x[i]; place=i;} } } // Note: a trick with “place”

Page 15: 1 Lecture 16/2/11: Contents Local variable Array: the concept Working with an array An advert: Reading week 2 March. No lecture. All are welcome to room

15

Finding the average in an array

  double x[ ]={1.1, 1.2, 1.6, 2.0,1.1}; double average=0; //to accumulate the sum for (int i = 0; i < x.length; i++) average=average+x[i];//after this, average=7.0;

average=average/x.length; //average=7.0/5=1.4;

Question: Make it into a method.

Page 16: 1 Lecture 16/2/11: Contents Local variable Array: the concept Working with an array An advert: Reading week 2 March. No lecture. All are welcome to room

16

Finding the average: method

  public static void main(String[ ] args){ double x[ ]={1.1, 1.2, 1.6, 2.0,1.1}; double average=MetAv(x);} //average=1.4 public static double MetAv(double[ ] a){

double av=0; for (int i = 0; i < a.length; i++) av=av+a[i];

av=av/a.length;

return av; }

Page 17: 1 Lecture 16/2/11: Contents Local variable Array: the concept Working with an array An advert: Reading week 2 March. No lecture. All are welcome to room

17

Constrained average: method

  public static void main(String[ ] args){ double x[ ]={1.1, 1.2, 0, 1.6, 2.0, 0}; double average=ConAv(x);} public static double ConAv(double[ ] a){

double av=0;int counter=0;

for (int i = 0; i < a.length; i++){ if(a[i]!=0){ counter++;

av=av+a[i];} }

return av/counter; }