Lecture 16211 Contents Local variable Array the concept

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 B 12 Cruciform from 7. 30 - 9. 00; Martin O’Shea will consult 1

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. 2

Local variables(1) • public class Prog 06{ • 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; } 3
![Local variables(2) • public static void main(String[] args){ • System. out. println(i+1); • One(); Local variables(2) • public static void main(String[] args){ • System. out. println(i+1); • One();](http://slidetodoc.com/presentation_image_h2/6c4e4391eba31c9d1efa5c72bf07fea1/image-4.jpg)
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 4

Actual printout Working 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 0 0 Sixth line: Two(8), prints 8+2, then executing One() to print 0 Seventh line: One() to print 0 5

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, 23, 30}; Index: 0 1 2 3 4 5 6 7 Length is 8 6
![Array (2) Not an array: abc[ ]={8, Ringo, +} - WHY? (different types) [ Array (2) Not an array: abc[ ]={8, Ringo, +} - WHY? (different types) [](http://slidetodoc.com/presentation_image_h2/6c4e4391eba31c9d1efa5c72bf07fea1/image-7.jpg)
Array (2) Not an array: abc[ ]={8, Ringo, +} - WHY? (different types) [ ] - on the array name's right is used to indicate arrays 2. Declaring arrays Both, int ages[ ]; and int[ ] ages; is OK 7
![Array (3) Initialisation of an array: either ages = new int[8]; // array with Array (3) Initialisation of an array: either ages = new int[8]; // array with](http://slidetodoc.com/presentation_image_h2/6c4e4391eba31c9d1efa5c72bf07fea1/image-8.jpg)
Array (3) Initialisation of an array: either ages = new int[8]; // array with 8 zeros or ages[ ] = {23, 32, 19, 30, 25, 23, 30}; //specify what is needed Simultaneously declaring & initialising (with zeros) int ages[] = new int[8]; 8
![Array (4) ages[ ] = {23, 32, 19, 30, 25, 23, 30}; Accessing array Array (4) ages[ ] = {23, 32, 19, 30, 25, 23, 30}; Accessing array](http://slidetodoc.com/presentation_image_h2/6c4e4391eba31c9d1efa5c72bf07fea1/image-9.jpg)
Array (4) ages[ ] = {23, 32, 19, 30, 25, 23, 30}; Accessing array elements ages [1] is 32 int i=4; int j = ages [i]; // assigning j with 25 9
![Work with arrays(1) Data of 5 students: double height[ ]={1. 56, 1. 72, 1. Work with arrays(1) Data of 5 students: double height[ ]={1. 56, 1. 72, 1.](http://slidetodoc.com/presentation_image_h2/6c4e4391eba31c9d1efa5c72bf07fea1/image-10.jpg)
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/height 2 (in the US, those with bmi between 20 and 25 are considered of normal weight) 10

Work with arrays(2) Loop for is natural with arrays: the index used as the counter bmi[ ]=new double[5]; for (int I = 0; I < 5; I + +) bmi[I]=weight[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]); 11
![Work with arrays(3) The same result with a method for the bmi: double[ ] Work with arrays(3) The same result with a method for the bmi: double[ ]](http://slidetodoc.com/presentation_image_h2/6c4e4391eba31c9d1efa5c72bf07fea1/image-12.jpg)
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]); 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); 12
![Finding a maximum in an array double x[ ]; //assume taken from somewhere int Finding a maximum in an array double x[ ]; //assume taken from somewhere int](http://slidetodoc.com/presentation_image_h2/6c4e4391eba31c9d1efa5c72bf07fea1/image-13.jpg)
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. 13

Finding maximum : a method int place= -1; //for keeping the index of max entry double find. Max(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” 14
![Finding the average in an array double x[ ]={1. 1, 1. 2, 1. 6, Finding the average in an array double x[ ]={1. 1, 1. 2, 1. 6,](http://slidetodoc.com/presentation_image_h2/6c4e4391eba31c9d1efa5c72bf07fea1/image-15.jpg)
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. 15
![Finding the average: method public static void main(String[ ] args){ double x[ ]={1. 1, Finding the average: method public static void main(String[ ] args){ double x[ ]={1. 1,](http://slidetodoc.com/presentation_image_h2/6c4e4391eba31c9d1efa5c72bf07fea1/image-16.jpg)
Finding the average: method public static void main(String[ ] args){ double x[ ]={1. 1, 1. 2, 1. 6, 2. 0, 1. 1}; double average=Met. Av(x); } //average=1. 4 public static double Met. Av(double[ ] a){ double av=0; for (int i = 0; i < a. length; i++) av=av+a[i]; av=av/a. length; return av; } 16
![Constrained average: method public static void main(String[ ] args){ double x[ ]={1. 1, 1. Constrained average: method public static void main(String[ ] args){ double x[ ]={1. 1, 1.](http://slidetodoc.com/presentation_image_h2/6c4e4391eba31c9d1efa5c72bf07fea1/image-17.jpg)
Constrained average: method public static void main(String[ ] args){ double x[ ]={1. 1, 1. 2, 0, 1. 6, 2. 0, 0}; double average=Con. Av(x); } public static double Con. Av(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; } 17
- Slides: 17