Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.

Similar presentations


Presentation on theme: "Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7."— Presentation transcript:

1 Programming Methodology (1)

2 temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7

3 System.out.println( temperature1 ); System.out.println( temperature2 ); System.out.println( temperature3 ); System.out.println( temperature4 ); System.out.println( temperature5 ); System.out.println( temperature6 ); System.out.println( temperature7 );

4 for ( int i = 1 ; ; i++ ) { } System.out.println( ); i <= 7 temperature1

5 temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7

6 Arrays temperature

7 Arrays Learning objectives create arrays; use loops to process arrays; use an enhanced for loop to process an array; use arrays as method inputs and outputs; develop routines for accessing and manipulating arrays.

8 What is an array?

9 An array is a data type that stores a collection of items!

10 These items are referred to as the elements of the array!

11 All elements must be of the same type BUT there is no restriction on which type this is!

12 ‘T’ ‘a’ ‘s’ ‘W’ “Blue” “Red” “White” “Black” “Green” double charString Examples

13 9.2 “Java” 12 ‘j’ true  mixed Examples

14 How do you create an array?

15 Array creation is a two-step process:

16 STEP 1: Declare an array variable.

17 typeOfElementarrayName;[ ] intscore;[ ] An array of exam scores: Exam scores are whole numbers

18 typeOfElementarrayName;[ ] Stringname;[ ] An array of student names: Names are Strings

19 typeOfElementarrayName;[ ] temperature;double[ ] An array of temperatures: temperatures are real numbers

20 The effect on computer memory of declaring an array …..

21 Java Instructions double[ ] temperature ; Computer Memory temperature ? This is called a reference variable

22 STEP 2: Allocating memory to store the array elements!

23 What information do we need to provide ?

24 a) The size of the array.

25 b) The type of each element.

26 Combine the two with a new operator.

27 Example: an array of 10 scores: nameOfArray = newarrayType[ size ]; score=newint[ 10 ];

28 Example: an array of 20 student names: nameOfArray = newarrayType[ size ]; name=newString [ 20 ];

29 nameOfArray = newarrayType[ size ]; Returning to the temperature array: temperature =newdouble[ 7 ];

30 The effect on computer memory of sizing an array….

31 Java Instructions double[ ] temperature ; Computer Memory temperature ? item of type 'double' temperature = new double[7];

32 Combining steps 1 and 2 double[ ] temperature ; = new double [7];temperature = new double [7];

33 Initializing an array double[ ] temperature =new double [7];{9, 11.5, 11, 8.5, 7, 9, 8.5} ;

34 Naming the array elements….

35 temperature First item is temperature[0] Last item is temperature[6]

36 Array variables can be used like any other variable of the given type in Java.

37 score[0] = 12; Entering 12 into the first position of the score array:

38 name[2] = “Aaron”; Entering “Aaron” into the third position of the name array:

39 temperature[6] = 21.5; Entering 21.5 into the 7 th position of the temperature array:

40 Entering values into an array from the keyboard..

41 double num; num = sc.nextDouble(); Entering into a simple double variable temperature [ ] = sc.nextDouble(); Entering into first array variable 0

42 double num; num = sc.nextDouble(); Entering into a simple double variable temperature [ ] = sc.nextDouble(); Entering into second array variable 1

43 double num; num = sc.nextDouble(); temperature [ ] = sc.nextDouble(); Entering into last array variable 6 Entering into a simple double variable

44 More examples of accessing array elements… temperature

45 Printing 6 th element on the screen…

46 System.out.println( num );

47 System.out.println( temperature[5] );

48 temperature Double the 5 th element….

49 num = num * 2;

50 temperature[4] = temperature[4] * 2;

51 Check the temperature on the third day… temperature

52 if (num >= 18) { System.out.println("it was hot today"); }

53 if ( temperature[2] >= 18) { System.out.println("it was hot today"); } temperature

54 What is wrong here? System.out.println( temperature[ 7 ] ); The index must be valid (0 -6) otherwise you will get an error at run-time. ArrayIndexOutOfBoundsException

55 Using a variable as the array index…

56 System.out.println( temperature[ ] ); i for ( ;; ) { } int i = 0 i++ i < 7i <= 7 This will cause your program to crash! What will happen here? 7 ArrayIndexOutOfBoundsException

57 System.out.println( temperature[ ] );i for ( ;; ) { } int i = 0 i++ i < 7 i < temperature.length; i++)

58 for ( ;; ) { } temperature[ ] = sc.nextDouble();i int i = 0 i++ i < temperature.length; i++) System.out.println ("enter max temperature for day " + ); (i+1)12

59 Putting it all together…..

60 public static void main(String[ ] args) { Scanner sc = new Scanner(System.in); double[ ] temperature = new double[7]; for (int i = 0; i < temperature.length; i++) { // code to enter temperatures } for (int i = 0; i < temperature.length; i++) { // code to display temperatures } }

61 Re-writing this program using methods enterTemps and displayTemps …..

62 public static void main(String[ ] args) { Scanner sc = new Scanner(System.in); double[ ] temperature = new double[7]; for (int i = 0; i < temperature.length; i++) { // code to enter temperatures } for (int i = 0; i < temperature.length; i++) { // code to display temperatures } Put this loop into enterTemps method Put this loop into displayTemps method

63 public static void main(String[ ] args) { Scanner sc = new Scanner(System.in); double[ ] temperature = new double[7]; } private static void enterTemps ( ) { // loop to enter into temperature array here } private static void displayTemps ( ) { // loop to display temperature array here } enterTemps( ); displayTemps( ); temperature temperatureIn double[ ] // loop to enter into temperatureIn array here // loop to display temperatureIn array here

64 Passing arrays as parameters…

65 private static void enterTemps(double[ ] temperatureIn) { Scanner sc = new Scanner(System.in); for (int i = 0; i < temperatureIn.length; i++) { System.out.println ("enter max temperature for day " + (i+1)); temperatureIn[i] = sc.nextDouble(); } public static void main (String [ ] args) { double[ ] temperature = new double[7]; enterTemps( temperature ); displayTemps( temperature ); } temperature temperatureIn

66 The effect on computer memory of passing an array as a parameter………

67 Java Instructions main (String[ ] args) { // create temperature array } Computer Memory temperature [ 6 ] [ 5 ] [ 4 ] [ 3 ] [ 2 ] [ 1 ] [ 0 ] enterTemps( temperature ); enterTemps (double[] temperatureIn) { // update temperatureIn } temperatureIn

68 Returning an array from a method……..

69 private static void enterTemps(double[ ] temperatureIn) { Scanner sc = new Scanner(System.in); for (int i = 0; i < temperatureIn.length; i++) { System.out.println ("enter max temperature for day " + (i+1)); temperatureIn[i] = sc.nextDouble(); }

70 private static ? enterTemps( ) { Scanner sc = new Scanner(System.in); for (int i = 0; i < temperatureIn.length; i++) { System.out.println ("enter max temperature for day " + (i+1)); temperatureIn[i] = sc.nextDouble(); }

71 private static double[ ] enterTemps( ) { Scanner sc = new Scanner(System.in); for (int i = 0; i < temperatureIn.length; i++) { System.out.println ("enter max temperature for day " + (i+1)); temperatureIn[i] = sc.nextDouble(); }

72 private static double[ ] enterTemps( ) { Scanner sc = new Scanner(System.in); for (int i = 0; i < temperatureIn.length; i++) { System.out.println ("enter max temperature for day " + (i+1)); temperatureIn[i] = sc.nextDouble(); } double[ ] temperatureOut = new double[7];

73 private static double[ ] enterTemps( ) { Scanner sc = new Scanner(System.in); for (int i = 0; i < temperatureOut.length; i++) { System.out.println ("enter max temperature for day " + (i+1)); temperatureOut[i] = sc.nextDouble(); } double[ ] temperatureOut = new double[7];

74 private static double[ ] enterTemps( ) { Scanner sc = new Scanner(System.in); for (int i = 0; i < temperatureOut.length; i++) { System.out.println ("enter max temperature for day " + (i+1)); temperatureOut[i] = sc.nextDouble(); } return temperatureOut; } double[ ] temperatureOut = new double[7];

75 Modifying the main method..

76 public static void main (String [ ] args) { double[ ] temperature displayTemps( temperature ); } = new double[7];; enterTemps( temperature );enterTemps( );temperature =enterTemps( );

77 The enhanced 'for' loop….

78 for ( ) { } int i = 0; i < temperatureIn.length ; i++ System.out.println( );temperatureIn[ i ] temperatureInitem :double item “For every item in the temperatureIn array”

79 Some useful array methods…

80 public class SomeUsefulArrayMethods { public static void main (String[] args) { Scanner sc = new Scanner(System.in); int[ ] someArray; System.out.println("How many elements to store?"); int size = sc.nextInt(); someArray = new int[size]; // call methods here } // methods to process an array here } Allows user to size the array.

81 Array summation..

82 SET total TO zero LOOP FROM first element TO last element BEGIN END SET total TO total + current element RETURN total private staticsum ( int[ ] arrayIn ) int { }

83 int total = 0; LOOP FROM first element TO last element BEGIN END SET total TO total + current element RETURN total private staticsum ( int[ ] arrayIn ) int { }

84 int total = 0; for ( int i = 0 ; i < arrayIn.length ; i++ ) BEGIN END SET total TO total + current element RETURN total private staticsum ( int[ ] arrayIn ) int { }

85 int total = 0; for ( int i = 0 ; i < arrayIn.length ; i++ ) { } SET total TO total + current element RETURN total private staticsum ( int[ ] arrayIn ) int { }

86 int total = 0; for ( int i = 0 ; i < arrayIn.length ; i++ ) { } total = total + arrayIn [ i ] ; RETURN total private staticsum ( int[ ] arrayIn ) int { }

87 int total = 0; for ( int i = 0 ; i < arrayIn.length ; i++ ) { } total = total + arrayIn [ i ] ; return total; private staticsum ( int[ ] arrayIn ) int { }

88 int total = 0; for ( int currentElement: arrayIn ) { } total = total + arrayIn [ i ] ; return total; private staticsum ( int[ ] arrayIn ) int { }

89 int total = 0; for ( int currentElement: arrayIn ) { } total = total + currentElement ; return total; private staticsum ( int[ ] arrayIn ) int { }

90

91 Consider the following explicit creation of an array: a)What would be the value of someArray.length ? int[ ] someArray = {2, 5, 1, 9, 11};

92 Consider the following explicit creation of an array: int[ ] someArray = {2, 5, 1, 9, 11}; someArray 11 a)What would be the value of someArray.length ?

93 Consider the following explicit creation of an array: a)What would be the value of someArray.length ? int[ ] someArray = {2, 5, 1, 9, 11}; someArray 11 5

94 Consider the following explicit creation of an array: b)What is the value of someArray[2]? int[ ] someArray = {2, 5, 1, 9, 11}; someArray 11

95 Consider the following explicit creation of an array: b)What is the value of someArray[2]? int[ ] someArray = {2, 5, 1, 9, 11}; someArray 11 [0] [1] [2] [3] [4]

96 Consider the following explicit creation of an array: b)What is the value of someArray[2]? int[ ] someArray = {2, 5, 1, 9, 11}; someArray 11 [0] [1] [2] [3] [4]

97 Consider the following explicit creation of an array: c)What would happen if you tried to access someArray[6]? int[ ] someArray = {2, 5, 1, 9, 11}; someArray 11 [0] [1] [2] [3] [4]

98 Consider the following explicit creation of an array: c)What would happen if you tried to access someArray[6]? int[ ] someArray = {2, 5, 1, 9, 11}; someArray 11 [0] [1] [2] [3] [4]

99 Consider the following explicit creation of an array: c)What would happen if you tried to access someArray[6]? int[ ] someArray = {2, 5, 1, 9, 11}; someArray 11 [0] [1] [2] [3] [4] ArrayIndexOutOfBoundsException

100 someArray 11 [0] [1] [2] [3] [4] d)Write a standard for loop that will double the value of every item in someArray.

101 someArray 11 [0] [1] [2] [3] [4] d)Write a standard for loop that will double the value of every item in someArray. for ( ?;? ;?) { // code to double a value here }

102 someArray 11 [0] [1] [2] [3] [4] d)Write a standard for loop that will double the value of every item in someArray. for (int i = 0;? ;?) { // code to double a value here }

103 someArray 11 [0] [1] [2] [3] [4] d)Write a standard for loop that will double the value of every item in someArray. for (int i = 0;? ;i++) { // code to double a value here }

104 someArray 11 [0] [1] [2] [3] [4] d)Write a standard for loop that will double the value of every item in someArray. for (int i = 0;i <= 4 ;i++) { // code to double a value here }

105 someArray 11 [0] [1] [2] [3] [4] d)Write a standard for loop that will double the value of every item in someArray. for (int i = 0;i < 5 ;i++) { // code to double a value here }

106 someArray 11 [0] [1] [2] [3] [4] d)Write a standard for loop that will double the value of every item in someArray. for (int i = 0; i < someArray.length ; i++) { // code to double a value here }

107 someArray 11 [0] [1] [2] [3] [4] d)Write a standard for loop that will double the value of every item in someArray. for (int i = 0; i < someArray.length ; i++) { someArray[i] = someArray[i] * 2; }

108 someArray 22 [0] [1] [2] [3] [4] d)Write a standard for loop that will double the value of every item in someArray. for (int i = 0; i < someArray.length ; i++) { someArray[i] = someArray[i] * 2; }

109 someArray 22 [0] [1] [2] [3] [4] e)Should you use an enhanced for loop here? for (int i = 0; i < someArray.length ; i++) { someArray[i] = someArray[i] * 2; }

110 someArray 22 [0] [1] [2] [3] [4] e)Should you use an enhanced for loop here? for ( int item: someArray ) { someArray[i] = someArray[i] * 2; }

111 someArray 22 [0] [1] [2] [3] [4] e)Should you use an enhanced for loop here? for ( int item: someArray ) { item = item * 2; }

112 someArray 22 [0] [1] [2] [3] [4] e)Should you use an enhanced for loop here? for ( int item: someArray ) { item = item * 2; } Enhanced for loops should not modify the original array!

113 f)Place the original for loop in a method, increaseMarks. for (int i = 0; i < someArray.length ; i++) { someArray[i] = someArray[i] * 2; } public static void main (String [ ] args) { int [ ] someArray = {2, 5, 1, 9, 11}; }

114 private static ? increaseMarks( ? ) { } f)Place the original for loop in a method, increaseMarks. for (int i = 0; i < someArray.length ; i++) { someArray[i] = someArray[i] * 2; } public static void main (String [ ] args) { int [ ] someArray = {2, 5, 1, 9, 11}; // call method }

115 private static ? increaseMarks( int[ ] someArray ) { } f)Place the original for loop in a method, increaseMarks. for (int i = 0; i < someArray.length ; i++) { someArray[i] = someArray[i] * 2; } public static void main (String [ ] args) { int [ ] someArray = {2, 5, 1, 9, 11}; // call method }

116 private static void increaseMarks( int[ ] someArray ) { } f)Place the original for loop in a method, increaseMarks. for (int i = 0; i < someArray.length ; i++) { someArray[i] = someArray[i] * 2; } public static void main (String [ ] args) { int [ ] someArray = {2, 5, 1, 9, 11}; // call method }

117 private static void increaseMarks( int[ ] someArray ) { } f)Place the original for loop in a method, increaseMarks. for (int i = 0; i < someArray.length ; i++) { someArray[i] = someArray[i] * 2; } public static void main (String [ ] args) { int [ ] someArray = {2, 5, 1, 9, 11}; increaseMarks( ); }

118 private static void increaseMarks( int[ ] someArray ) { } f)Place the original for loop in a method, increaseMarks. for (int i = 0; i < someArray.length ; i++) { someArray[i] = someArray[i] * 2; } public static void main (String [ ] args) { int [ ] someArray = {2, 5, 1, 9, 11}; increaseMarks( someArray); }

119 Practical Task…

120 Room 1Room 2Room 3Room 4Room a)Declare and initialise an array, rooms, to hold the following capacities: c)Add an instruction in main to call the displayRooms method. b)Write a method, displayRooms, that accepts the array of room capacities and displays the capacity of each room.


Download ppt "Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7."

Similar presentations


Ads by Google