Using Java, let’s work on some basic skills by creating a program that contains
ID: 3769897 • Letter: U
Question
Using Java, let’s work on some basic skills by creating a program that contains a few experiments with arrays. If you are asked to print out a value, be sure to print a label that indicates what the value represents, unless otherwise indicated. For example, if you are asked to print a variable z, the output might look like this:
Perform each of the following tasks:
a) Create an array x of doubles with an initializer list that contains the following values: 8, 4, 5, 21, 7, 9, 18, 2, and 100.
b) Print the number of items in the array by printing the expression x.length.
c) Print the first array item, x[0].
d) Print the last array item. Be careful to choose the right index.
e) Print the expression x[x.length – 1]. Why is this value the same as in part d?
f) Use a standard for loop to print all the values in the array without labels.
g) Use a standard for loop to print all the values in the array with labels to indicate what each element is.
h) Use a standard for loop to print all the values in the array in reverse order with labels to indicate what each element is.
i) Use an enhanced for loop to print all the values in the array without labels.
Explanation / Answer
Complete Program:
// File: ArrayDemo.java
public class ArrayDemo
{
public static void main(String[] args)
{
// a)
double[] x = {8, 4, 5, 21, 7, 9, 18, 2, 100};
// b)
System.out.println("Number of items in the array: " + x.length);
// c)
System.out.println("First array item: " + x[0]);
// d)
System.out.println("Last array item: " + x[x.length - 1]);
// e)
System.out.println("x[x.length - 1]: " + x[x.length - 1]);
/* If an array contains n items, then the first item stored at the index 0 and the last item stored and the index n-1. Because, an index of the array of length n starts with 0 and ends with n-1. */
// f)
System.out.println(" Values in the array without labels:");
for(int i = 0; i < x.length; i++)
System.out.println(x[i]);
// g)
System.out.println(" Values in the array with labels:");
for(int i = 0; i < x.length; i++)
System.out.println(i + ": "+ x[i]);
// h)
System.out.println(" Values in the array in reverse order with labels:");
for(int i = x.length - 1; i >= 0; i--)
System.out.println(i + ": "+ x[i]);
// i)
System.out.println(" Values in the array without labels using enhanced for-loop:");
for(double item : x)
System.out.println(item);
}
}
Sample Output:
Number of items in the array: 9
First array item: 8.0
Last array item: 100.0
x[x.length - 1]: 100.0
Values in the array without labels:
8.0
4.0
5.0
21.0
7.0
9.0
18.0
2.0
100.0
Values in the array with labels:
0: 8.0
1: 4.0
2: 5.0
3: 21.0
4: 7.0
5: 9.0
6: 18.0
7: 2.0
8: 100.0
Values in the array in reverse order with labels:
8: 100.0
7: 2.0
6: 18.0
5: 9.0
4: 7.0
3: 21.0
2: 5.0
1: 4.0
0: 8.0
Values in the array without labels using enhanced for-loop:
8.0
4.0
5.0
21.0
7.0
9.0
18.0
2.0
100.0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.