Page 463 - Introduction to Programming with Java: A Problem Solving Approach
P. 463
13. [after §10.8] Trace the following code and show the exact output. 1 public class ModifyArray
2{
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
list[i] = i + 100;
3
public static void main(String[] args)
4{
5
int sum = 0;
int[] list = new int[3];
for (int i=0; i<3; i++)
6
7
8
9{
}
// end ModifyArray
}
}
}
modify(list, sum);
for (int i=0; i<3; i++)
{
}
{
}
sum += list[i];
System.out.print(list[i] + " ");
System.out.println("\nsum = " + sum);
public static void modify(int[] list, int sum)
{
int temp = list[0];
Apago PDF Enhancer
list[0] = list[list.length - 1];
list[list.length - 1] = temp;
for (int i=0; i<3; i++)
Use the following trace header:
14. [after §10.9] Specify a single statement that initializes an array of int’s named myTable to all 1’s. The array should be a two-dimensional array with 2 rows and 3 columns.
15. [after §10.9] Write a method named getMask that receives a single parameter named table which is
a two-dimensional array of int’s. The getMask method should create and return an array mask for
the passed-in table array. The programming term mask refers to an array that is built from another array and it contains all 0’s and 1’s. For each element in the mask array, if the original array’s corresponding element contains a positive number, the mask array’s element should contain a 1. And if the original array’s corresponding element contains a zero or negative number, the mask array’s element should contain a 0. Note this example:
Exercises 429
ModifyArray
<arrays>
line#
i
main
list
(list)
(sum)
modify
temp
i
length
arr1
sum
0
1
2
output