In: Computer Science
public class Main
{
public static void main(String [] args)
{
int [] array1 = {5, 8, 34, 7, 2, 46, 53, 12, 24, 65};
int numElements = 10;
System.out.println("Part 1");
// Part 1
// Enter the statement to print the numbers in index 5 and index
8
// put a space in between the two numbers and a new line at the
end
// Enter the statement to print the numbers 8 and 53 from the array
above
// put a space in betwaeen the two numbers and a new line at the
end.
// Enter the statement to change the number 2 in the array to be
12
// then write the statement to print out that number in the
array
// and a new line at the end
System.out.println("Part 2");
// Part 2
// Write the loop to print out all of the numbers in the
array
// Use the variables declared above (you might have to
declare
// another variable) Put a space between each number
System.out.println("\nPart 3");
// Part 3
// Write the loop to print out all of the EVEN numbers in the
array
// Use the variables declared above (you might have to
declare
// another variable) Put a space between each number.
System.out.println("\nPart 4");
// Part 4
// Write a method called computeTotal that has the array
// and the number of elements passed into it. It will return
// the total of all of the numbers in the array (it will be
// below the main method)
int total;
// Call the function you just wrote and store the answer
// in the variable total declared above
// This will print the total out
System.out.println(total);
}
}
Source code of the completed program is given below.Detailed comments are included for better understanding the code.Screen shot of the code and output are also attached.If find any difficulty, feel free to ask in comment section. Please do upvote the answer.Thank you.
Source code
public class Main { public static void main(String[] args) { int [] array1 = {5, 8, 34, 7, 2, 46, 53, 12, 24, 65}; int numElements = 10; System.out.println("Part 1"); //statement to print the numbers in index 5 and index 8 System.out.println(array1[5]+" "+array1[8]); System.out.println(); //statement to print the numbers 8 and 53 System.out.println(array1[1]+" "+array1[6]); System.out.println(); //statement to change the number 2 in the array to 12 array1[4]=12; //printout the number System.out.println(array1[4]); System.out.println(); System.out.println("Part 2"); //loop to print all the elements of the array for(int i=0;i<numElements;i++) { System.out.print(array1[i]+" "); } System.out.println("\nPart 3"); //loop to print out all the even numbers in the array for(int i=0;i<numElements;i++) { //if the remainder obtain when array element divided by 2 is 0 //then the number is even.% gives remainder after division if(array1[i]%2==0) System.out.print(array1[i]+" "); } System.out.println("\nPart 4"); //declaring integer variable total int total; //calling the computeTotal() method total=computeTotal(array1,numElements); //displaying total System.out.println(total); } static int computeTotal(int arr[],int n) { //declare an integer variable sum and initialize it with 0 int sum=0; //loop iterate through all elements for(int i=0;i<n;i++) //add each element to sum sum=sum+arr[i]; //return the result sum return sum; } }
Screen shot of the code
Screen shot of the output