Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5...
-
Upload
beau-tanney -
Category
Documents
-
view
217 -
download
1
Transcript of Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5...
![Page 1: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/1.jpg)
Programming Methodology (1)
![Page 2: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/2.jpg)
temperature1temperature2
temperature3
temperature4
temperature5
temperature6 temperature7
![Page 3: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/3.jpg)
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 );
![Page 4: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/4.jpg)
for ( int i = 1 ; ; i++ )
{
}
System.out.println( );
i <= 7
temperature1
![Page 5: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/5.jpg)
temperature1temperature2
temperature3
temperature4
temperature5
temperature6 temperature7
![Page 6: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/6.jpg)
Arrays
temperature
![Page 7: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/7.jpg)
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.
![Page 8: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/8.jpg)
What is an array?
![Page 9: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/9.jpg)
An array is a data type that stores a collection of items!
![Page 10: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/10.jpg)
These items are referred to as the elements of the array!
![Page 11: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/11.jpg)
All elements must be of the same type BUT there is no restriction on which type this is!
![Page 12: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/12.jpg)
1.75
75.1
12.5
1.75
31.5
‘T’
‘a’
‘s’
‘s’
‘W’
“Blue”
“Red”
“White”
“Black”
“Green”
double char String
Examples
![Page 13: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/13.jpg)
9.2
“Java”
12
‘j’
true
mixed
Examples
![Page 14: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/14.jpg)
How do you create an array?
![Page 15: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/15.jpg)
Array creation is a two-step process:
![Page 16: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/16.jpg)
STEP 1:Declare an array variable.
![Page 17: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/17.jpg)
typeOfElement arrayName;[ ]
int score;[ ]
An array of exam scores:Exam scores are whole numbers
![Page 18: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/18.jpg)
typeOfElement arrayName;[ ]
String name;[ ]
An array of student names:Names are Strings
![Page 19: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/19.jpg)
typeOfElement arrayName;[ ]
temperature;double[ ]
An array of temperatures:temperatures are real numbers
![Page 20: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/20.jpg)
The effect on computer memory of declaring an array …..
![Page 21: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/21.jpg)
Java Instructions
double[ ] temperature ;
Computer Memory
temperature
?
This is called a reference variable
![Page 22: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/22.jpg)
STEP 2:Allocating memory to store the array elements!
![Page 23: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/23.jpg)
What information do we need to provide ?
![Page 24: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/24.jpg)
a) The size of the array.
![Page 25: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/25.jpg)
b) The type of each element.
![Page 26: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/26.jpg)
Combine the two with a new operator.
![Page 27: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/27.jpg)
Example: an array of 10 scores:
nameOfArray = new arrayType [ size ];
score = new int [ 10 ];
![Page 28: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/28.jpg)
Example: an array of 20 student names:
nameOfArray = new arrayType [ size ];
name = new String [ 20 ];
![Page 29: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/29.jpg)
nameOfArray = new arrayType [ size ];
Returning to the temperature array:
temperature = new double [ 7 ];
![Page 30: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/30.jpg)
The effect on computer memory of sizing an array….
![Page 31: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/31.jpg)
Java Instructions
double[ ] temperature ;
Computer Memory
temperature
?
item of type 'double'
item of type 'double'
item of type 'double'
item of type 'double'
item of type 'double'
item of type 'double'
item of type 'double' temperature = new double[7];
![Page 32: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/32.jpg)
Combining steps 1 and 2
double[ ] temperature ;
= new double [7];temperature
= new double [7];
![Page 33: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/33.jpg)
Initializing an array
double[ ] temperature = new double [7];{9, 11.5, 11, 8.5, 7, 9, 8.5} ;
![Page 34: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/34.jpg)
Naming the array elements….
![Page 35: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/35.jpg)
0 1 2 3 4 5 6
temperature
First item is
temperature[0]
Last item is
temperature[6]
![Page 36: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/36.jpg)
Array variables can be used like any other variable of the given type in Java.
![Page 37: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/37.jpg)
score[0] = 12;
Entering 12 into the first position of the score array:
![Page 38: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/38.jpg)
name[2] = “Aaron”;
Entering “Aaron” into the third position of the name array:
![Page 39: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/39.jpg)
temperature[6] = 21.5;
Entering 21.5 into the 7th position of the temperature array:
![Page 40: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/40.jpg)
Entering values into an array from the keyboard..
![Page 41: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/41.jpg)
double num;
num = sc.nextDouble();
Entering into a simple double variable
temperature [ ] = sc.nextDouble();
Entering into first array variable
0
![Page 42: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/42.jpg)
double num;
num = sc.nextDouble();
Entering into a simple double variable
temperature [ ] = sc.nextDouble();
Entering into second array variable
1
![Page 43: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/43.jpg)
double num;
num = sc.nextDouble();
temperature [ ] = sc.nextDouble();
Entering into last array variable
6
Entering into a simple double variable
![Page 44: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/44.jpg)
More examples of accessing array elements…
0 1 2 3 4 5 6
temperature
![Page 45: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/45.jpg)
Printing 6th element on the screen…
0 1 2 3 4 5 6
temperature
![Page 46: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/46.jpg)
0 1 2 3 4 5 6
temperature
System.out.println( num );
![Page 47: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/47.jpg)
0 1 2 3 4 5 6
temperature
System.out.println( temperature[5] );
![Page 48: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/48.jpg)
0 1 2 3 4 5 6
temperature
Double the 5th element….
![Page 49: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/49.jpg)
0 1 2 3 4 5 6
temperature
num = num * 2;
![Page 50: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/50.jpg)
0 1 2 3 4 5 6
temperature
temperature[4] = temperature[4] * 2;
![Page 51: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/51.jpg)
Check the temperature on the third day…..
0 1 2 3 4 5 6
temperature
![Page 52: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/52.jpg)
if (num >= 18){
System.out.println("it was hot today");}
0 1 2 3 4 5 6
temperature
![Page 53: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/53.jpg)
if ( temperature[2] >= 18){
System.out.println("it was hot today");}
0 1 2 3 4 5 6
temperature
![Page 54: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/54.jpg)
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
![Page 55: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/55.jpg)
Using a variable as the array index…
![Page 56: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/56.jpg)
System.out.println( temperature[ ] ); i0123456
for ( ; ; )
{
}
int i = 0 i++ i < 7i <= 7
This will cause your program to crash!What will happen here?
7
ArrayIndexOutOfBoundsException
![Page 57: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/57.jpg)
System.out.println( temperature[ ] ); i0123456
for ( ; ; )
{
}
int i = 0 i++ i < 7 i < temperature.length; i++)
![Page 58: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/58.jpg)
for ( ; ; )
{
} temperature[ ] = sc.nextDouble(); i01
76
int i = 0 i++ i < temperature.length; i++)
System.out.println ("enter max temperature for day " + ); (i+1)12
![Page 59: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/59.jpg)
Putting it all together…..
![Page 60: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/60.jpg)
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 }}
![Page 61: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/61.jpg)
Re-writing this program using methods enterTemps and displayTemps …..
![Page 62: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/62.jpg)
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
![Page 63: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/63.jpg)
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
temperature
temperatureIn
temperatureIn
double[ ]
double[ ]
// loop to enter into temperatureIn array here
// loop to display temperatureIn array here
![Page 64: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/64.jpg)
Passing arrays as parameters…
![Page 65: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/65.jpg)
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
![Page 66: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/66.jpg)
The effect on computer memory of passing an array as a parameter………
![Page 67: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/67.jpg)
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
![Page 68: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/68.jpg)
Returning an array from a method……..
![Page 69: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/69.jpg)
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(); }}
![Page 70: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/70.jpg)
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(); }}
![Page 71: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/71.jpg)
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(); }}
![Page 72: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/72.jpg)
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];
![Page 73: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/73.jpg)
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];
![Page 74: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/74.jpg)
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];
![Page 75: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/75.jpg)
Modifying the main method..
![Page 76: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/76.jpg)
public static void main (String [ ] args){
double[ ] temperature
displayTemps( temperature );}
= new double[7];;enterTemps( temperature );enterTemps( );temperature = enterTemps( );
![Page 77: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/77.jpg)
The enhanced 'for' loop….
![Page 78: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/78.jpg)
for ( ){
}
int i = 0; i < temperatureIn.length ; i++
System.out.println( );
temperatureIn[ i ]
temperatureIn item : double
item
“For every item in the temperatureIn array”
![Page 79: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/79.jpg)
Some useful array methods…
![Page 80: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/80.jpg)
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.
![Page 81: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/81.jpg)
Array summation..
![Page 82: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/82.jpg)
SET total TO zero
LOOP FROM first element TO last element
BEGIN
END
SET total TO total + current element
RETURN total
private static sum ( int[ ] arrayIn ) int
{
}
![Page 83: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/83.jpg)
int total = 0;LOOP FROM first element TO last element
BEGIN
END
SET total TO total + current element
RETURN total
private static sum ( int[ ] arrayIn ) int
{
}
![Page 84: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/84.jpg)
int total = 0;for ( int i = 0 ; i < arrayIn.length ; i++ ) BEGIN
END
SET total TO total + current element
RETURN total
private static sum ( int[ ] arrayIn ) int
{
}
![Page 85: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/85.jpg)
int total = 0;for ( int i = 0 ; i < arrayIn.length ; i++ ) {
}
SET total TO total + current element
RETURN total
private static sum ( int[ ] arrayIn ) int
{
}
![Page 86: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/86.jpg)
int total = 0;for ( int i = 0 ; i < arrayIn.length ; i++ ) {
}
total = total + arrayIn [ i ] ;
RETURN total
private static sum ( int[ ] arrayIn ) int
{
}
![Page 87: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/87.jpg)
int total = 0;for ( int i = 0 ; i < arrayIn.length ; i++ ) {
}
total = total + arrayIn [ i ] ;
return total;
private static sum ( int[ ] arrayIn ) int
{
}
![Page 88: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/88.jpg)
int total = 0;for ( int currentElement: arrayIn ) {
}
total = total + arrayIn [ i ] ;
return total;
private static sum ( int[ ] arrayIn ) int
{
}
![Page 89: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/89.jpg)
int total = 0;for ( int currentElement: arrayIn ) {
}
total = total + currentElement ;
return total;
private static sum ( int[ ] arrayIn ) int
{
}
![Page 90: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/90.jpg)
![Page 91: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/91.jpg)
Consider the following explicit creation of an array:
a) What would be the value of someArray.length ?
int[ ] someArray = {2, 5, 1, 9, 11};
![Page 92: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/92.jpg)
Consider the following explicit creation of an array:
int[ ] someArray = {2, 5, 1, 9, 11};
5
2
1
9
someArray
11
a) What would be the value of someArray.length ?
![Page 93: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/93.jpg)
Consider the following explicit creation of an array:
a) What would be the value of someArray.length ?
int[ ] someArray = {2, 5, 1, 9, 11};
5
2
1
9
someArray
11
5
![Page 94: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/94.jpg)
Consider the following explicit creation of an array:
b)What is the value of someArray[2]?
int[ ] someArray = {2, 5, 1, 9, 11};
5
2
1
9
someArray
11
![Page 95: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/95.jpg)
Consider the following explicit creation of an array:
b)What is the value of someArray[2]?
int[ ] someArray = {2, 5, 1, 9, 11};
5
2
1
9
someArray
11
[0]
[1]
[2]
[3]
[4]
![Page 96: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/96.jpg)
Consider the following explicit creation of an array:
b)What is the value of someArray[2]?
int[ ] someArray = {2, 5, 1, 9, 11};
5
2
1
9
someArray
11
[0]
[1]
[2]
[3]
[4]
![Page 97: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/97.jpg)
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};
5
2
1
9
someArray
11
[0]
[1]
[2]
[3]
[4]
![Page 98: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/98.jpg)
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};
5
2
1
9
someArray
11
[0]
[1]
[2]
[3]
[4]
![Page 99: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/99.jpg)
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};
5
2
1
9
someArray
11
[0]
[1]
[2]
[3]
[4]ArrayIndexOutOfBoundsException
![Page 100: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/100.jpg)
5
2
1
9
someArray
11
[0]
[1]
[2]
[3]
[4]
d)Write a standard for loop that will double the value of every item in someArray.
![Page 101: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/101.jpg)
5
2
1
9
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
}
![Page 102: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/102.jpg)
5
2
1
9
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
}
![Page 103: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/103.jpg)
5
2
1
9
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
}
![Page 104: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/104.jpg)
5
2
1
9
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
}
![Page 105: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/105.jpg)
5
2
1
9
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
}
![Page 106: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/106.jpg)
5
2
1
9
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
}
![Page 107: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/107.jpg)
5
2
1
9
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;
}
![Page 108: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/108.jpg)
10
4
2
18
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;
}
![Page 109: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/109.jpg)
10
4
2
18
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;
}
![Page 110: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/110.jpg)
10
4
2
18
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;
}
![Page 111: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/111.jpg)
10
4
2
18
someArray
22
[0]
[1]
[2]
[3]
[4]
e)Should you use an enhanced for loop here?
for ( int item: someArray )
{
item = item * 2;
}
![Page 112: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/112.jpg)
10
4
2
18
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!
![Page 113: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/113.jpg)
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};
}
![Page 114: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/114.jpg)
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
}
![Page 115: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/115.jpg)
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
}
![Page 116: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/116.jpg)
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
}
![Page 117: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/117.jpg)
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( );
}
![Page 118: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/118.jpg)
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);
}
![Page 119: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/119.jpg)
Practical Task…
![Page 120: Programming Methodology (1). temperature1 temperature2 temperature3 temperature4 temperature5 temperature6 temperature7.](https://reader035.fdocuments.us/reader035/viewer/2022062417/551ae3be550346b2288b64b1/html5/thumbnails/120.jpg)
Room 1 Room 2 Room 3 Room 4 Room 5
30 25 50 30 40
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.