170 likes | 289 Views
Introduction to Programming. Java Lab 9: Arrays. JavaLab9 lecture slides.ppt Ping Brennan ( Ping.Brennan@gmail.com ). 7 March 2014. Declaration of an Array. Declare an array called data which contains 10 elements, each element is of type double . double[] data = new double[10];
E N D
Introduction to Programming Java Lab 9: Arrays JavaLab9 lecture slides.ppt Ping Brennan (Ping.Brennan@gmail.com) 7 March 2014
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 moreDataand 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
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
Java Project Project Name: JavaLab9 ReverseArray ArrayMethods
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.
Class ReverseArray (2) • Algorithm for the method reverseData (in pseudo code) • Create an array variable, reversedData, using • newint[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).
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)); // output: [1, 2, 3]
Class ReverseArray (4) 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: Use a forloop to reverse the data; return the reversedDataarray. Output [ 7, 2, 1 ]
Anatomy of Class ReverseArray public class ReverseArray { public static voidmain(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 as above to print the reversed data from the array dataR*/ } // end of method main /* To Do: insert the code for the method reverseArray here (shown on slide 10) */ } // end of class ReverseArray
Anatomy of method reverseArray public staticint[]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. returnreversedData*/ } // end of method reverseArray
Class ArrayMethods • Objective • Understand how to use more than one method in a program. • Methods public static void printArray(int[] data); public static intproductElements(int[] data); public static intnumberNegativeElements(int[] data); Note: the first method is the usual one, main. See JFE, R6.7
Method printArray • Objective • 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.
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.
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 iis the array index and data is name of the array variable. 3. Return count.
Testing Class ArrayMethods Input Parameter variable, data { 1, 2, 3, -4, -7} Example (in method 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 1 2 3 -4 -7 Product: 168 Number of negative elements: 2
Anatomy of Class ArrayMethods /* The program applies various methods to an array of integers to produce certain results. Author: (type in your name) Date: 7 March 2014 */ 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
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)); } // end of method main /*To Do: Now write the code for the three methods, printArray, productElements and numberNegativeElements, after the method main in the class ArrayMethods. */