Question
p S1-44. Write an illustrative main() using an array. Patch together some of their code in the supplement and make it work. Please use JavaDoc.
p S1-44. Write an illustrative main() using an array. Patch together some of their code in the supplement and make it work. Please use JavaDoc.
Arrays
In Java, an array is a special kind of object that stores a finite collection of items having the same data type. For example, you can create an array of seven numbers of type double as follows:
double[] temperature = new double[7];
The left side of the assignment operator declares temperature as an array whose contents are of type double. The right side uses the new operator to request seven memory locations for the array. This is like declaring the following strangely named variables to have type double:
temperature[0], temperature[1], temperature[2], temperature[3], temperature[4], temperature[5], temperature[6]
Note that the numbering starts with 0, not 1. Each of these seven variables can be used just like any other variable of type double. For example, we can write
temperature[3] = 32; temperature[6] = temperature[3] + 5; System.out.println(temperature[6]);
But these seven variables are more than just seven plain old variables of type double. The number in square bracketscalled an index, or subscriptcan be any arithmetic expression whose
value is an integer. In this example, the index value must be between 0 and 6, because we declared the array temperature to have seven memory locations. A variable such as temperature[3] is called either an indexed variable, a subscripted variable, or simply an element of the array. The value in an indexed variable is called an entry, and its data type is the arrays entry type.
For example, the following statements read seven temperatures into an array and compare them with their average:
public static final int DAYS_PER_WEEK = 7; Scanner keyboard = new Scanner(System.in); .. .
double[] temperature = new double[DAYS_PER_WEEK];
System.out.println("Enter " + DAYS_PER_WEEK + " temperatures:"); double sum = 0; for (int index = 0; index < DAYS_PER_WEEK; index++) {
temperature[index] = keyboard.nextDouble();
sum = sum + temperature[index]; } // end for
double average = sum / DAYS_PER_WEEK;
System.out.println("The average temperature is " + average); System.out.println("The temperatures are"); for (int index = 0; index < DAYS_PER_WEEK; index++) {
if (temperature[index] < average) System.out.println(temperature[index] + " below average.");
else if (temperature[index] > average) System.out.println(temperature[index] + " above average.");
else // temperature[index] == average System.out.println(temperature[index] + " average.");
} // end for
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started