Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Programming

Similar presentations


Presentation on theme: "Introduction to Programming"— Presentation transcript:

1 Introduction to Programming
Java Lab 9: Arrays JavaLab9 lecture slides.ppt Ping Brennan 8 March 2013

2 Declaration of an Array
Declare an array called data which contains 10 elements, each element is of type double. double[] data = new double[10]; Alternatively declare an array called moreData and initialise using a single statement. double[] moreData = { 6, 7, 2, 1, 0, -3 }; Name of array variable Element type Type of array variable Length List of initial values indices moredata [0] [1] [2] [3] [4] [5] 6 7 2 1 -3

3 Initialisation of an Array
The elements of an array are accessed using an index. The first element in the array has an index of 0. The elements of the array data can be initialised as shown: data[0] = 5; data[1] = 2; The fourth entry of the array data is accessed below: System.out.println("The 4th element of the array data has the value " + data[3]); Use brackets to access an element The index must be >= 0 and < the length of the array

4 Java Project Project Name: JavaLab9 ReverseArray ArrayMethods

5 Class ReverseArray Prints out an array of integers in reverse order.
Objectives Understand how to pass and return an array as a parameter variable for a method. Use an array to solve problems in a program. Method public static int[] reverseArray(int[] data) Method reverseArray returns the reverse of the array data.

6 Class ReverseArray (2) Algorithm for the method reverseData (in pseudo code) 1. Create an array variable, reversedData , using new int[data.length]. 2. Use a for loop to copy the values from data into reversedData in reverse order. 3. Return reversedData. Hint: use data.length in the test condition to terminate the for loop such that the loop iterates from 0 up to (data.length-1).

7 Class ReverseArray (3) Testing the method
Make the following statement as the first line of your program import java.util.Arrays; Use the method Arrays.toString to print out an array of integers. For example, int[] data = { 1, 2, 3 } ; System.out.println(Arrays.toString(data));

8 Class ReverseArray (4) Computation Output Input
Parameter variable, data  { 1, 2, 7 } Example (in method main): int[] data = { 1, 2, 7 }; int[] dataR = reverseArray(data); System.out.println("Reversed data: " + Arrays.toString(dataR)); Computation Inside the method reverseArray: for loop to reverse the data; return the reversedData array. Output [ 7, 2, 1 ]

9 Anatomy of Class ReverseArray
public class ReverseArray { public static void main(String[] args) int[] data = { 1, 2, 7 } ; int[] dataR = reverseArray(data); System.out.println("Original array: " + Arrays.toString(data)); /* To do: Write a similar statement to print the reversed data from the array dataR */ } /* To do: insert code for the method reverseArray here (shown on next slide) */ } // end of class ReverseArray

10 Anatomy of method reverseArray
public static int[] reverseArray(int[] data) { /* 1. Create an array variable, reversedData, using new int[data.length]. */ /* 2. Use a for loop to copy the values from data into reversedData in reverse order. Hint: (i) use data.length in the test condition to terminate the for loop such that the loop iterates from 0 up to (data.length-1) using the index i. (ii) use reversedData[(data.length-1) – i] = data[i]; to copy the data inside the for loop. */ /* 3. return reversedData */ } // end of method reverseArray

11 Class ArrayMethods Objective Methods
Understand how to use more than one method in a program. Methods public static void printArray(int[] data); public static int productElements(int[] data); public static int numberNegativeElements(int[] data); Note: the first method is the usual one, main. See JFE, R6.7

12 Method printArray Objective Algorithm (in pseudo code)
Print all the elements in an array of type int[] in a single row, separated by spaces. Algorithm (in pseudo code) for loop to print each array element, separated by a space except for the last element. Hint: use an if statement to check for (i < data.length-1) in order to print out a space after each array element. Note that i is the loop index and data.length is the length of the array data.

13 Method productElements
Objective Return the product of all the elements in an array of type int[]. Algorithm (in pseudo code) 1. Define an integer variable, product, and set it to 1. 2. Use a for loop to update the product by multiplying it with the current array element. 3. Return product.

14 Method numberNegativeElements
Objective Return the number of elements in an array of type int[] that are strictly less than 0. Algorithm (in pseudo code) 1. Define an integer variable, count, and set it to 0. 2. Use a for loop to increment count if a current array element is strictly less than 0. Hint: use an if statement to perform the test condition, if (data[i] < 0) , where i is the array index and data is name of the array variable. 3. Return count.

15 Testing Class ArrayMethods
Input Parameter variable, data  { 1, 2, 3, -4, -7} Example (in main): int[] data = { 1, 2, 3, -4, -7 }; printArray(data); System.out.println("Product: " + productElements(data)); System.out.println("Number of negative elements: " + numberNegativeElements(data)); Computation printArray: for loop and if statements to print all elements productElements: for loop to calculate the product of all elements; return the product numberNegativeElements: for loop to count the number of elements strictly less than 0; return the number Output Product: 168 Number of negative elements: 2

16 Anatomy of Class ArrayMethods
/* The program applies various methods to an array of integers to produce certain results. Author: (your name) Date: 8 March 2013 */ public class ArrayMethods { /* 1. insert the code for the method main */ /* 2. insert the code for the method printArray */ /* 3. insert the code for the method productElements */ /* 4. insert the code for the method numberNegativeElements */ } // end of class ArrayMethods

17 Anatomy of method main public static void main(String[] args) {
int[] data = { 1, 2, 3, -4, -7 }; printArray(data); System.out.println("Product elements: " + productElements(data)); System.out.print("Number of elements strictly less than 0: "); System.out.println(numberNegativeElements(data)); } /* To do: write the code for the three methods, printArray, productElements and numberNegativeElements, after the method main. */


Download ppt "Introduction to Programming"

Similar presentations


Ads by Google