- May 9, 2005
- 2,044
- 1
- 81
So I submitted my lab and the professor sent it back. His comments have me confused. My submission and his comments are in the 2nd post. Any help would be greatly appreciated.
Here is the original lab requirements:
Lab 21
Defining and using primitive arrays
1. This program "bombs" at runtime. It's SUPPOSED to use a for loop to display the values of its array elements in reverse order as follows:
u
o
i
e
a
See if you can fix it.
2. See if you can fill-in the blanks to make this program assign a value of 1 to the first array element, 2 to the second array element, etc.. NOTE: Your code must be able to handle an array of ANY size so be sure to test your answer with an array having a different number of elements to see if it still works.
3. See if you can supply the missing code to make this program "swap" the values of the first and last array elements. If done correctly, the program's output will be as follows:
d
b
c
a
NOTE: Be flexible in your code so that it can handle an array of ANY size, not just a four element array.
Here is the original lab requirements:
Lab 21
Defining and using primitive arrays
1. This program "bombs" at runtime. It's SUPPOSED to use a for loop to display the values of its array elements in reverse order as follows:
u
o
i
e
a
See if you can fix it.
Code:
public class Lab21A {
public static void main(String[] args) {
char[] vowels = {'a', 'e', 'i', 'o', 'u'};
for (int i = 5; i >= 1; i--)
System.out.println(vowels[i]);
}
}
2. See if you can fill-in the blanks to make this program assign a value of 1 to the first array element, 2 to the second array element, etc.. NOTE: Your code must be able to handle an array of ANY size so be sure to test your answer with an array having a different number of elements to see if it still works.
Code:
public class Lab21B {
public static void main(String[] args) {
int[] values = new int[10];
for (int i = 0; _______________; i++) {
_____________ = _________;
System.out.println("Element " + i + " = " + values[i]);
}
}
}
3. See if you can supply the missing code to make this program "swap" the values of the first and last array elements. If done correctly, the program's output will be as follows:
d
b
c
a
NOTE: Be flexible in your code so that it can handle an array of ANY size, not just a four element array.
Code:
public class Lab21C {
public static void main(String[] args) {
char[] table = {'a', 'b', 'c', 'd'};
// YOUR CODE GOES HERE
for (int i = 0; i < table.length; i++) {
System.out.println(table[i]);
}
}
}
Last edited: