Date post: | 28-Mar-2015 |
Category: |
Documents |
Upload: | samuel-mcfarland |
View: | 214 times |
Download: | 0 times |
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
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
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
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
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
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
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
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
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
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
Arrays
A mechanism for collecting together multiple values.
A way of allocating names to multiple variables.
8 March 2013 Birkbeck College, U. London 11
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
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
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
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
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
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
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!
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[]
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
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
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
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
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
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
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