Question
Task is in java on intellij, just need help with these frist 2 checkpoints as im not good with arrays Task 1/4: Checkpoint 32 The
Task is in java on intellij, just need help with these frist 2 checkpoints as im not good with arrays
Task 1/4: Checkpoint 32
The main method in the class ArrayTask declares and initialises an array named intList (or more precisely, the variable intList contains a reference to an array object) to contain the five int values 5, 20, 32, 7 and 9.
public class ArrayTask { public static void main(String[] args) { int[] intList = {5, 20, 32, 7, 9}; for (int i = 0; i < intList.length; i++) { System.out.println("intList[" + i + "]: " + intList[i]); } } }
The for loop prints out each element of the array, producing the following output: intList[0]: 5 intList[1]: 20 intList[2]: 32 intList[3]: 7 intList[4]: 9
Copy the code above into the skeleton ArrayTask class
Modify the for loop in the main method of ArrayTask.java so that the values in the array intList are printed out in reverse order. When run, the modified program should produce the following output: intList[4]: 9 intList[3]: 7 intList[2]: 32 intList[1]: 20 intList[0]: 5
Now you will add code to the program so that it also calculates and prints the sum of all the elements in the array intList.
Add statements to the main method as described below.
Declare an int variable called sum and initialise it to 0.
Include an additional statement in the for loop which adds the ith element of the array to the variable sum.
Include a statement to print the value of sum (see example below).
Run the program. The following output should be produced: intList[4]: 9 intList[3]: 7 intList[2]: 32 intList[1]: 20 intList[0]: 5 Sum = 73
Task 2/4: Checkpoint 33
For this task you will add code to the program from Task 1 so that it makes a copy of the array and increases each element by 1.
Add statements to the main method as described below.
Each value in intList should be increased by 1 (this must be done by changing the values in the array and not by adding 1 when the value is printed). The resulting output should now be: intList[4]: 10 intList[3]: 8 intList[2]: 33 intList[1]: 21 intList[0]: 6 Sum = 78
Declare another array called copy with the same type as intList. That is, it is also an array of 5 ints.
Immediately after the declaration of copy, include another for loop which assigns to each element of copy the respective element of intList. After doing this both arrays should contain the same values. Include statements to calculate and then print the sum of the elements of copy.
Run the program. The output should be the following: intList[4]: 10 intList[3]: 8 intList[2]: 33 intList[1]: 21 intList[0]: 6 Sum of intList = 78 Sum of copy = 73
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