8 March 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank...
-
Upload
samuel-mcfarland -
Category
Documents
-
view
215 -
download
0
Transcript of 8 March 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank...
![Page 1: 8 March 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems sjmaybank@dcs.bbk.ac.uk.](https://reader035.fdocuments.us/reader035/viewer/2022062511/5515d2a255034638038b45da/html5/thumbnails/1.jpg)
8 March 2013 Birkbeck College, U. London 1
Introduction to Programming
Lecturer: Steve Maybank
Department of Computer Science and Information Systems
[email protected] 2013
Week 9: Arrays
![Page 2: 8 March 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems sjmaybank@dcs.bbk.ac.uk.](https://reader035.fdocuments.us/reader035/viewer/2022062511/5515d2a255034638038b45da/html5/thumbnails/2.jpg)
Java Lab 8, Ex 1: RepeatString
Implement a class which contains the method main and the method public static String repeat(String str, int n)which returns the string str repeated n times.
Call the method repeat from main.
8 March 2013 Birkbeck College, U. London 2
![Page 3: 8 March 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems sjmaybank@dcs.bbk.ac.uk.](https://reader035.fdocuments.us/reader035/viewer/2022062511/5515d2a255034638038b45da/html5/thumbnails/3.jpg)
Code for Exercise 2 (1)/** * Program to test the method repeat which prints out a * given string, repeated a given number of times. * @author S.J. Maybank * @version 22 February 2011 */public class RepeatString{ // main goes here // repeat goes here}
8 March 2013 Birkbeck College, U. London 3
![Page 4: 8 March 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems sjmaybank@dcs.bbk.ac.uk.](https://reader035.fdocuments.us/reader035/viewer/2022062511/5515d2a255034638038b45da/html5/thumbnails/4.jpg)
Code for Exercise 2 (2)
public static void main(String[] args)
{ String str1 = repeat("Hello World", 3); String str2 = repeat("Hello World", 0); String str3 = repeat("H", 10); System.out.println("Examples of the output from the method
repeat"); System.out.println(str1); System.out.println(str2); System.out.println(str3); }
8 March 2013 Birkbeck College, U. London 4
![Page 5: 8 March 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems sjmaybank@dcs.bbk.ac.uk.](https://reader035.fdocuments.us/reader035/viewer/2022062511/5515d2a255034638038b45da/html5/thumbnails/5.jpg)
Code for Exercise 2 (3)public static String repeat(String str, int n){ String result = ""; for(int i = 1; i <= n; i++) { result = result+str; } return result;}
8 March 2013 Birkbeck College, U. London 5
![Page 6: 8 March 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems sjmaybank@dcs.bbk.ac.uk.](https://reader035.fdocuments.us/reader035/viewer/2022062511/5515d2a255034638038b45da/html5/thumbnails/6.jpg)
Java Lab 8, Ex 2: ReadDouble
Implement the method
public static double readDouble(String prompt)
which displays the string prompt, followed by a space, then reads in a floating point number and returns it.
8 March 2013 Birkbeck College, U. London 6
![Page 7: 8 March 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems sjmaybank@dcs.bbk.ac.uk.](https://reader035.fdocuments.us/reader035/viewer/2022062511/5515d2a255034638038b45da/html5/thumbnails/7.jpg)
Code for Exercise 3 (1)import java.util.Scanner;/** * Display a prompt string and then read in a number of type
double. * @author S.J. Maybank * @version 22 February 2011 */public class ReadDouble{ // main goes here // readDouble goes here}
8 March 2013 Birkbeck College, U. London 7
![Page 8: 8 March 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems sjmaybank@dcs.bbk.ac.uk.](https://reader035.fdocuments.us/reader035/viewer/2022062511/5515d2a255034638038b45da/html5/thumbnails/8.jpg)
Code for Exercise 3 (2)
public static void main(String[] args){ double r = readDouble(ꞌꞌInput a number of type
double:"); System.out.println("The input number is "+r);}
8 March 2013 Birkbeck College, U. London 8
![Page 9: 8 March 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems sjmaybank@dcs.bbk.ac.uk.](https://reader035.fdocuments.us/reader035/viewer/2022062511/5515d2a255034638038b45da/html5/thumbnails/9.jpg)
Code for Exercise 3 (3)
public static double readDouble(String prompt)
{Scanner in = new Scanner(System.in);System.out.print(prompt+" ");return in.nextDouble();
}
8 March 2013 Birkbeck College, U. London 9
![Page 10: 8 March 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems sjmaybank@dcs.bbk.ac.uk.](https://reader035.fdocuments.us/reader035/viewer/2022062511/5515d2a255034638038b45da/html5/thumbnails/10.jpg)
Overview Nature of an array Declaration of an array Array indexing Partially filled arrays Array algorithms See Java for Everyone, Ch. 6
8 March 2013 Birkbeck College, U. London 10
![Page 11: 8 March 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems sjmaybank@dcs.bbk.ac.uk.](https://reader035.fdocuments.us/reader035/viewer/2022062511/5515d2a255034638038b45da/html5/thumbnails/11.jpg)
Arrays
A mechanism for collecting together multiple values.
A way of allocating names to multiple variables.
8 March 2013 Birkbeck College, U. London 11
![Page 12: 8 March 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems sjmaybank@dcs.bbk.ac.uk.](https://reader035.fdocuments.us/reader035/viewer/2022062511/5515d2a255034638038b45da/html5/thumbnails/12.jpg)
Array Declaration and Initialisation
double[] data = {32, 54, 67, 5};
8 March 2013 Birkbeck College, U. London 12
type of arrayvariable
name of arrayvariable
list of initial values
Names of variables: data[0], data[1], data[2], data[3]The numbers 0, 1, 2, 3 are indices
![Page 13: 8 March 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems sjmaybank@dcs.bbk.ac.uk.](https://reader035.fdocuments.us/reader035/viewer/2022062511/5515d2a255034638038b45da/html5/thumbnails/13.jpg)
Example
data[3] = 35;System.out.println(data[3]); // prints 35/*Element: data[3]Value of the index: 3Value of the element data[3]: 35Each element can be used like any variable
oftype double. */
8 March 2013 Birkbeck College, U. London 13
![Page 14: 8 March 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems sjmaybank@dcs.bbk.ac.uk.](https://reader035.fdocuments.us/reader035/viewer/2022062511/5515d2a255034638038b45da/html5/thumbnails/14.jpg)
Array Indexing Array indexing begins with 0 The length of the array data is
data.length Array indexing ends with data.length-1
for(int i = 0; i < data.length; i++){
System.out.println(data[i]);}
0 March 2013 Birkbeck College, U. London 14
![Page 15: 8 March 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems sjmaybank@dcs.bbk.ac.uk.](https://reader035.fdocuments.us/reader035/viewer/2022062511/5515d2a255034638038b45da/html5/thumbnails/15.jpg)
Bounds Error
data[data.length] = 4; //errordata[-1] = 3; // error
/* A bounds error is automatically detected at run time. It is not detectedat compile time.
*/8 March 2013 Birkbeck College, U. London 15
![Page 16: 8 March 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems sjmaybank@dcs.bbk.ac.uk.](https://reader035.fdocuments.us/reader035/viewer/2022062511/5515d2a255034638038b45da/html5/thumbnails/16.jpg)
Array References
int[] scores = {10, 9, 7, 4, 5};int[] values = scores;scores[3] = 10;System.out.println(values[3]); // prints 10!
/* The array variable is a pointer to the place in memory where the array is stored. */
8 March 2013 Birkbeck College, U. London 16
![Page 17: 8 March 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems sjmaybank@dcs.bbk.ac.uk.](https://reader035.fdocuments.us/reader035/viewer/2022062511/5515d2a255034638038b45da/html5/thumbnails/17.jpg)
The Array Variable as a Pointer
8 March 2013 Birkbeck College, U. London 17
array stored in memoryscores values
10
9 7 4 5
The value of the variable score is a pointer to the array.The value of the variable values is also a pointer to the array
![Page 18: 8 March 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems sjmaybank@dcs.bbk.ac.uk.](https://reader035.fdocuments.us/reader035/viewer/2022062511/5515d2a255034638038b45da/html5/thumbnails/18.jpg)
Array Declaration and Initialisation (2)
double[] data = new double[4];
8 March 2013 Birkbeck College, U. London 18
type ofarrayvariable
name ofarrayvariable
elementtype
length
The elements of the array data are automaticallyassigned the value 0, but do not rely on this!
![Page 19: 8 March 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems sjmaybank@dcs.bbk.ac.uk.](https://reader035.fdocuments.us/reader035/viewer/2022062511/5515d2a255034638038b45da/html5/thumbnails/19.jpg)
Declaring Arrays
8 March 2013 Birkbeck College, U. London 19
int[] numbers = new int[10]; An array of 10 integers. All elements are initialised with 0
final int LENGTH = 10;int[] numbers = new int[LENGTH];
Use a named constant
int[] squares = {0, 1, 4, 9, 16};
String[] friends = {"Em", "Bob", "Sue"};
An array of three strings
double[] data = new int[10]; Error: a variable of type double[] cannot be assigned a value of type int[]
![Page 20: 8 March 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems sjmaybank@dcs.bbk.ac.uk.](https://reader035.fdocuments.us/reader035/viewer/2022062511/5515d2a255034638038b45da/html5/thumbnails/20.jpg)
Partially Filled Arrays
In a partially filled array, only part of the array is used.
A companion variable keeps count of the number of array elements that have been used.
8 March 2013 Birkbeck College, U. London 20
![Page 21: 8 March 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems sjmaybank@dcs.bbk.ac.uk.](https://reader035.fdocuments.us/reader035/viewer/2022062511/5515d2a255034638038b45da/html5/thumbnails/21.jpg)
Example of a Partially Filled Array
int currentSize = 0; // companion variableScanner in = new Scanner(System.in);while(in.hasNextDouble()) // check for further input{
if(currentSize < data.length){
data[currentSize] = in.nextDouble();currentSize++;
}}
8 March 2013 Birkbeck College, U. London 21
![Page 22: 8 March 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems sjmaybank@dcs.bbk.ac.uk.](https://reader035.fdocuments.us/reader035/viewer/2022062511/5515d2a255034638038b45da/html5/thumbnails/22.jpg)
Printing a Partially Filled Array
for(int i = 0; i < currentSize; i++){
System.out.println(data[i]);}
8 March 2013 Birkbeck College, U. London 22
![Page 23: 8 March 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems sjmaybank@dcs.bbk.ac.uk.](https://reader035.fdocuments.us/reader035/viewer/2022062511/5515d2a255034638038b45da/html5/thumbnails/23.jpg)
Array Algorithm 1/* find the sum and the average value of the
elements in the array data. */double total = 0;for(i = 0; i < data.length; i++){
total = total + data[i];}double average = 0;if(data.length>0){average = total/data.length;}
8 March 2013 Birkbeck College, U. London 23
![Page 24: 8 March 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems sjmaybank@dcs.bbk.ac.uk.](https://reader035.fdocuments.us/reader035/viewer/2022062511/5515d2a255034638038b45da/html5/thumbnails/24.jpg)
Array Algorithm 2
/* Find the maximum and the minimum of the elements in the non-empty array data. */
double largest = data[0], smallest = data[0];for(int i = 0; i < data.length; i++){
if(data[i] > largest){largest = data[i];}if(data[i] < smallest){smallest = data[i];}
}
8 March 2013 Birkbeck College, U. London 24
![Page 25: 8 March 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems sjmaybank@dcs.bbk.ac.uk.](https://reader035.fdocuments.us/reader035/viewer/2022062511/5515d2a255034638038b45da/html5/thumbnails/25.jpg)
Array Algorithm 3/* Linear search. */int searchedValue = 100, pos = 0;boolean found = false;while (pos < values.length && !found){
if(values[pos] == searchedValue){
found = true;}else{
pos++;}
}8 March 2013 Birkbeck College, U. London 25
![Page 26: 8 March 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems sjmaybank@dcs.bbk.ac.uk.](https://reader035.fdocuments.us/reader035/viewer/2022062511/5515d2a255034638038b45da/html5/thumbnails/26.jpg)
Array Algorithm 3 (Coda)
if(found){
System.out.println(ꞌꞌFound at positionꞌꞌ+pos);
}else{
System.out.println(ꞌꞌNot foundꞌꞌ);}
8 March 2013 Birkbeck College, U. London 26