Presentation is loading. Please wait.

Presentation is loading. Please wait.

School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 1 CMT1000: Introduction to Programming Ed Currie Lecture 9: Arrays.

Similar presentations


Presentation on theme: "School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 1 CMT1000: Introduction to Programming Ed Currie Lecture 9: Arrays."— Presentation transcript:

1 School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 1 CMT1000: Introduction to Programming Ed Currie Lecture 9: Arrays

2 School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 2 Arrays (Tables of data) declaration initialisation manipulation arrays as parameters

3 School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 3 Problem You have collected the ages of each student in CMT1000 (1200 people) and want a program to read them into memory and find the average age.

4 School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 4 Read in numbers... String s; s = JOptionPane.showInputDialog( “enter a no:”); int age0= Integer.parseInt(s); s = JOptionPane.showInputDialog( “enter a no:”); int age1= Integer.parseInt(s);. s = JOptionPane.showInputDialog( “enter a no:”); int age1199= Integer.parseInt(s);

5 School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 5 … and find average double ave = (age0 + age1 + age2.... + age1199)/ 1200.0; System.out.println(“Average is ” + ave);

6 School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 6 Problems? Having lots of separate variables is very tedious It would be better if we could make one declaration –one name –one type –give number of cells

7 School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 7 A new plan: use an array 1. Read each value into a separate entry in an array 2.Initialise a total to 0 3. for i = 0 to 1199 add the ith age to total 4.Output total/1200

8 School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 8 Reading in values... int ages[] = new int[1200]; String s; for (int i = 0; i <= 1199; i++ ) { s = JOptionPane.showInputDialog( “enter a no:”); ages[i]= Integer.parseInt(s); }

9 School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 9 …and computing average int total = 0; for (int i = 0; i <= 1199; i++ ) { total += ages[i]; } int ave = total / 1200.0; System.out.println(“Average is ” + ave);

10 School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 10 Notes We declare an array reference variable like this int numarr[]; // array of ints char charray[]; // array of chars double decarr[]; //array of doubles To create an array object and store a reference to it in a reference variable: numarr = new int[100];

11 School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 11 Notes When an array is allocated, if it contains numbers, they are initialised to 0. The array subscript type must be integer Array subscripts start at 0 int heights[] = new int[20]; How many elements? Subscript range?

12 School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 12 Length The length of an array A is the value of the expression A.length What is the value of heights.length

13 School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 13 Array reference variables Once an array is allocated, it is fixed in size. However, a reference variable may be set to point to a different array What happens? int A[] = new int[5]; int B[] = new int [10]; A = new int[3]; B = A;

14 School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 14 Initialiser lists What does this do? int ages [] = {19, 27, 18, 18, 95, 14, 20, 19, 22, 37}; Note we don’t need new here - it’s called automatically

15 School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 15 Initialise and print From Fig. 7.4: Deitel and Deitel String output = ""; // Initializer list specifies number of elements // and value for each element. int n[] = { 32, 27, 64, 18, 95, 14, 90, 70, 60, 37 }; output += "Subscript\tValue\n"; for ( int i = 0; i < n.length; i++ ) output += i + "\t" + n[ i ] + "\n";

16 School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 16 Initialise and print (cont) A neat output technique: JTextArea JTextArea outputArea = new JTextArea( 11, 10 ); outputArea.setText( output ); JOptionPane.showMessageDialog( null, outputArea, "Initializing an Array of int Values", JOptionPane.INFORMATION_MESSAGE);

17 School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 17 JTextArea JTextArea outputArea = new JTextArea( 11, 10 ); declares reference and creates JTextArea object with 11 rows and 10 columns of text JTextArea is a GUI component for displaying large amounts of text - if too much for the screen, allows scrolling. Initially contains an empty string; can add (concat) to this string using method append outputArea.append(“blah blah”); But we used the setText method outputArea.setText( output );

18 School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 18 Output We output the text using JOptionPane.showMessageDialog( null, outputArea, "Initializing an Array of int Values", JOptionPane.INFORMATION_MESSAGE); Note that showMessageDialog can take a string or a GUI component as an argument

19 School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 19 Another example From Fig. 7.5 D and D Initialize array to the even integers from 2 to 20 final int ARRAY_SIZE = 10; int n[]; // reference to int array // allocate array n = new int[ ARRAY_SIZE ]; // Set the values in the array for ( int i = 0; i < n.length; i++ ) n[ i ] = 2 + 2 * i;

20 School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 20 Student poll Fig 7.9 D and D Survey rates café food quality on scale 1 to 10 Prog must compute the frequency (quantity) of each response

21 School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 21 Initialise int responses[] = { 1, 2, 6, 4, 8, 5, 9, 7, 8, 10, 1, 6, 3, 8, 6, 10, 3, 8, 2, 7, 6, 5, 7, 6, 8, 6, 7, 5, 6, 6, 5, 6, 7, 5, 6, 4, 8, 6, 8, 10 }; int frequency[] = new int[ 11 ]; String output = "";

22 School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 22 Compute frequencies for ( int answer = 0; // initialize answer < responses.length; // condition answer++ ) // increment ++frequency[ responses[ answer ] ];

23 School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 23 Build output string output += "Rating\tFrequency\n"; for ( int rating = 1; rating < frequency.length; rating++ ) output += rating + "\t" + frequency[ rating ] + "\n";

24 School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 24 Do output JTextArea outputArea = new JTextArea( 11,10 ); outputArea.setText( output ); JOptionPane.showMessageDialog( null, outputArea, "Student Poll Program", JOptionPane.INFORMATION_MESSAGE );

25 School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 25 Reference parameters Primitive type arguments are passed to methods using call by value semantics(see last week’s lecture) Objects cannot be passed as arguments to methods; instead we pass object references, using call by value However, this allows the method to manipulate the object directly; we are effectively ‘passing’ the object using ‘call by reference’ semantics Arrays are treated as objects –efficiency versus side effect elimination

26 School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 26 What happens? public static void modifyArray( int b[] ) { for ( int j = 0; j < b.length; j++ ) b[ j ] *= 2; } public void modifyElement( int e ) { e *= 2; } int a[] = { 1, 2, 3, 4, 5 }; modifyArray( a ); modifyElement( a[ 3 ] );

27 School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 27 Reference vs value The array is passed by reference The primitive-type element is passed by value


Download ppt "School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 1 CMT1000: Introduction to Programming Ed Currie Lecture 9: Arrays."

Similar presentations


Ads by Google