Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

The Temple Of Django Database Performance

Authors: Andrew Brookins

1st Edition

1734303700, 978-1734303704

More Books

Students also viewed these Databases questions

Question

Compare the different types of employee separation actions.

Answered: 1 week ago

Question

Assess alternative dispute resolution methods.

Answered: 1 week ago

Question

Distinguish between intrinsic and extrinsic rewards.

Answered: 1 week ago