can you solve this question in Java?

: In your main method, follow the steps below, to write the code required in this
part: (5 points)
1. Declare a reference variable to an array of integer elements. The name of the variable
is list1.
Remember that the syntax for declaring a reference variable is:
elementType [] refVariable;
2. Print the reference variable to the console, as in the statement below, and observe and
understand what happens. Then, remove the print statement.
System.out.println(list1);
3. Create an array of 50 integer values, and assign its reference to the variable list1.
Remember that the syntax for creating an array is:
new elementType[arraySize];
In order to assign the returned reference to a reference variable, we put the new
statement to the right of an assignment statement to a reference variable.
refVariable = new elementType[arraySize]
4. Combine array declaration and creation such that they are on the same line as in the
following syntax:
elementType [] refVariable= new elementType[arraySize];
5. Try to print the value of the reference variable to the console again, and observe the
output.
6. Print the values of the array elements all on the same line space separated using a “for”
loop. For this part, use a for loop with the following header:
for(int i=0; i<list1.length; i++)
//print statement
Make sure to print a new line after printing the array elements.
7. Assign values to the array elements using a “for” loop with a header similar to that in
the previous step, such that each element has the value of the double of its index (i.e.
for element with index i the value is 2*i).
8. Print elements values to the console again, on the same line space separated, this time
using the “foreach” statement as follows:
foreach(int e:list1)
//print statement
Make sure to print a new line after printing the array elements.
9. Compute the average of the array list1 elements and print it to the console in the
following format:
Average is: ….
10.Count the number of elements in list1 that are greater than the average and print the
count to the console as follows:
Count is: ……
11.Modify the array list1 such that it includes only elements of it that are greater than the
computed average.
Print the elements of list1 after being modified.
12. Declare and create a new array, named list2, of the following integer values using an
array initializer:
16 8 12 6 18 4
Remember that the syntax for creating an array using array initializer:
elementType [] refVariable = {element1, element2, element3, …..}
Remember you cannot separate the declaration and the creation of the array on two
separate lines when using array initializers.
13.Add the elements 1,3,5,7,9 to the array list2. Print the array elements after the new
elements are added.
14.Write a code that finds the element with the largest value in the array list2 and print it
to the console along with its index, in the following format:
Element with index .. and value .. is the largest

أضف اجابة

أضف اجابة

‫تصفح