Declare, Create and Initialize Arrays | CS 201, Lab Reports of Computer Science

Material Type: Lab; Class: Intro Obj Orient Programming; Subject: Computer Science; University: University of Alabama - Birmingham; Term: Unknown 2008;

Typology: Lab Reports

Pre 2010

Uploaded on 04/12/2010

koofers-user-e7y
koofers-user-e7y 🇺🇸

5

(1)

10 documents

1 / 19

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Arrays Part I
In this laboratory session you will:
1. Learn how to declare, create and initialize arrays
2. Learn how to manipulate arrays with loops
3. Investigate how arrays are passed to methods
4. Investigate arrays of primitive values and arrays of objects
5. Learn how String[] args is used in Java applications
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13

Partial preview of the text

Download Declare, Create and Initialize Arrays | CS 201 and more Lab Reports Computer Science in PDF only on Docsity!

Arrays Part I

In this laboratory session you will:

  1. Learn how to declare, create and initialize arrays
  2. Learn how to manipulate arrays with loops
  3. Investigate how arrays are passed to methods
  4. Investigate arrays of primitive values and arrays of objects
  5. Learn how String[] args is used in Java applications

One-Dimensional Arrays

In Java, an array is an object that can contain multiple values of the same data type. Each of these values is an element of the array. An array that contains int values has data type int[]. An array that contains char values has data type char[]. An array of char named grades is declared with the statement char[] grades; The statement above merely declares grades to be a variable of type char[]. It does not create an array nor does it assign a value to grades. To create an array and assign that array to the variable grades, one uses a statement such as grades = new char[5]; which is an assignment statement in which the new operator is used to create the array that is then assigned to the variable grades. Note that the size of the array is specified within brackets. In particular, the above statement creates an array capable of holding 5 integers. The capacity of an array is called the length of the array. That is, the array above is said to have length 5. The declaration, creation, and assignment of an array can be combined into a single statement such as int[] temperature = new int[24]; which declares an array of int named temperature and assigns to it a new array of int that can hold twenty-four int values. Later in the program, individual elements of an array are referenced by the array name followed by the index of the particular element within brackets. As with strings, index values start at 0; that is, the first element has index 0, the second has index 1, and so on. Thus, the array grades as defined above is an array of length 5 whose elements are referenced by grades[0], grades[1], grades[2], grades[3], and grades[4]. In particular, the statements grades[0] = 'B'; grades[1] = 'C'; assign the characters B and C to the first two elements of the array grades. Moreover, temperature[23] = temperature[9] + temperature[12]; assigns the sum of the tenth and thirteenth elements (at indices 9 and 12 respectively) of temperature to the 24th element (at index 23). The elements of an array can be initialized at the time the array is declared by following the declaration with an = symbol and a list of the values to be assigned within braces. Thus, char[] grades = {'B', 'C', 'A', 'C', 'D'} not only establishes an array named grades but also assigns the values B, C, A, C, and D to the array's elements. When declaring, creating, and initializing an array in this manner, the length of the array is determined by the number of values listed between the braces. While an array is an object, there are no existing methods that may be invoked on an array. However, every array has one public instance variable named length of type int that is assigned the length of the array when the array is created. Thus, grades.length is 5 and temperature.length is 24.

Step 2. Modify the program from Step 1 so that it uses for loops instead of while loops. Summarize your work below.


















Step 3. Add the following code to the end of the main method. Compile and execute the modified program. Record the results. int sum = 0; for(index = 0; index < numbers.length; index++) { sum = sum + numbers[index]; } System.out.println("The sum of the numbers is " + sum);








Step 4. Add code to the program in Step 3 that calculates and prints the average of the numbers as a double value. Record the modifications.


















Step 5. Compile and execute the modified program responding with the values 1, 2, 3, 4, whose average is 2.5. Record the results. If the average is not 2.5, make appropriate modifications to Step 4.











Step 8. Compile and execute the modified program. Test different combinations of numbers such as 1 2 3 4 4 3 2 1 and 2 4 1 3 to make sure that the new code works correctly. Record the results.












Experiment 10.

Step 1. Compile and execute the program J10E02.java. Record the results. class J10E { public static void main(String[] args) { double[] fatGrams = new double[6]; fatGrams[0] = 12.6; fatGrams[1] = 32.0; fatGrams[2] = 2.0; fatGrams[3] = 11.2; fatGrams[4] = 0.5; fatGrams[5] = 3.99; for(int j = 0; j < fatGrams.length; j++) { System.out.println(fatGrams[j]); } } }

____________________________________________________________________________________

____________________________________________________________________________________

____________________________________________________________________________________

____________________________________________________________________________________

____________________________________________________________________________________

____________________________________________________________________________________

____________________________________________________________________________________

____________________________________________________________________________________

____________________________________________________________________________________

____________________________________________________________________________________

Step 2. Alter the program in Step 1 so that the elements in the array are displayed in reverse order. Summarize your work below.




















Experiment 10.

This experiment investigates what happens when an array and an element in an array are passed to a method. The driver class J10E03 creates two arrays and an object of type E03. The E03 class describes only a single method that is passed two int values and one array of int. Before and after invoking the fun method on the E03 object, data values are printed. Step 1. Execute the program J10E03.java. Record the results. class J10E { public static void main(String[] args) { int[] arrayA = {2,3,4}; int[] arrayB = {20, 30, 40}; int j = 5; E03 tester = new E03(); System.out.println( "Before tester.fun(numbers):" + "\n j = " + j + "\n arrayA[0] = " + arrayA[0] + "\n arrayB[0] = " + arrayB[0]); tester.fun(j, arrayA[0], arrayB); System.out.println( "After tester.fun(numbers):" + "\n j = " + j + "\n arrayA[0] = " + arrayA[0] + "\n arrayB[0] = " + arrayB[0]); } } class E { public void fun(int x, int y, int[] a) { x = 100; y = 200; a[0] = 300; } }







____________________________________________________________________________________

____________________________________________________________________________________

____________________________________________________________________________________

____________________________________________________________________________________

Step 2. Note that in the main method the variables passed to fun are j, the first element in arrayA, and arrayB. Each of these is assigned to a formal parameter in the method fun. Which of the variables in main are affected by the assignments made in the body of the method fun? Which variables are unaffected by the assignments made in fun? Explain.
























Step 3. Compile and execute the program. Record the results.










Step 4. Modify the loop introduced in Step 2 to change the strings referenced in the array to upper case versions of the existing strings. Use the toUpperCase method. Record the modification.













Step 5. Compile and execute the modified program. Record any errors and correct them.







Step 6. Add a third loop that concatenates the string "s" onto the end of each element of the array before the values in the array are printed. Explain your modifications.






















Step 7. Compile and execute the program. Record the results.








Step 3. Execute the program again using your first and last names as command line arguments. Record the results.









Step 4. Modify the program in Step 1 to print args[0]. Execute the program using your first name as a command line argument. Record your modifications. Record the results.










Step 5. Modify the program in Step 4 to print args[1] also. Execute the program again using your first and last names as command line arguments. Record the results.









Step 6. Execute the program in Step 5 using only your first name as a command line argument. Record the results.













Step 3. Compile and execute the modified program. Record the results.








Step 4. Rewrite the program from Step 2, replacing the three for loops with one nested loop that prints the contents of grid in the same format. Record the modifications.















Step 5. Compile and execute the modified program. Record the results.