16. Write Java statements for the following:
i.
Create an array to hold 15 double values.
ii. Assign the value 10.5 to the last element in the array.
iii. Display the sum of the first and the last element.
Answers
Answer:
An array is a group of values having same datatype. Each value in array is identified by an index. To create an array in Java, you must do the following:
1. Declare a variable to hold the array.
2. Create a new array object and assign it to the array variable.
3. Store information in that array.
Declaring Array Variables
The following statements are examples of array variable declarations:
int[] list;
String[] fruits;
Rectangle[] boxes;
Creating Array Object
After you declare the array variable, the next step is to create an array object and assign it to that variable, as in the following statement :
list = new int[10];
fruits = new String[5];
boxes = new Rectangle[4];
Declaring array variable and creating array object can be done in single statement, as given :
int marks = new int[5];
array
You can create and initialize an array at the same time by enclosing the elements of the array inside braces, separated by commas:
double[] prices = {10.5, 20.1, 14, 39};
The above statement creates a three-element array of double values named prices.
Accessing elements
You can accesse array element with the array name followed by a subscript enclosed within square brackets, as in the following:
marks[4] = 30; //stores 30 in fifth element of array.
Example 1
import java.util.Scanner; //Needed for Scanner class
/**
* This program shows values being read into an array's
* elements and then displayed.
*/
public class ArrayDemo1
{
public static void main(String[] args)
{
final int SIZE = 5; // size of array
// Create an array to hold employee salary.
double[] salaries = new double[SIZE];
// Create a Scanner object for keyboard input.
Scanner console = new Scanner(System.in);
System.out.println("Enter the salaries of " + SIZE
+ " employees.");
// Get employees' salary.
for (int i = 0; i < SIZE; i++)
{
salaries[i] = console.nextDouble();
}
// Display the values stored in the array.
System.out.println("The salaries are:");
for (int i = 0; i < SIZE; i++)
{
System.out.println(salaries[i]);
}
}
}
Output :
Enter the salaries of 5 employees.
234
789
123
456
453
Mark as brainliest and follow me plz
And thank me as well
Explanation:
1.double[] arr = new double[15];
2.arr[arr.length-1]=10.5;
3.System.out.println(arr[0]+arr[arr.length-1]);