Question: Part 1. Primitive Types, Sorting, Recursion for Homework.java a) Implement the static method initializeArray that receives as a parameter an array of integers. Use a

Part 1. Primitive Types, Sorting, Recursion for Homework.java

a) Implement the static method initializeArray that receives as a parameter an array of integers. Use a for loop and an if statement to put 0s in the odd positions of the array and 5s in the even positions.

b) Implement the static method printArray that receives as a parameter an array of integers. Use a for statements to print all the elements in the array.

c) Implement the static method insertionSort that receives as a parameter an array of integers and order its element in descending order. Implement Insertion Sort algorithm. It should be Insertion Sort, not Selection Sort, not Quick Sort, etc.

d) Implement the static recursive method that calculate and returns the factorial a number. The method receives the number (integer number) as parameter

------------------------------------------------------------------------------------------------------------------------

CODE:

Homework.java

public class Homework {

public static void initializeArray(int[] array) {

//Add code here

}

public static void printArray(int[] array) {

//Add code here

}

public static void insertionSort(int[] a) {

//Add code here

}

public static int factorial(int a) {

//Add code here

return 0;

}

//Main method. DO NOT CHANGE

public static void main(String[] args) {

int [] a = {2, 5, 7, 9, 12, 13, 15, 17, 19, 20};

int [] b = {18, 16, 19, -5, 3, 14, 6, 0};

int [] c = {4, 2, 5, 3, 1};

// testing initializeArray

printArray(a); // print: 2, 5, 7, 9, 12, 13, 15, 17, 19, 20

initializeArray(a);

printArray(a); // print: 5, 0, 5, 0, 5, 0, 5, 0, 5, 0

// testing insertionSort

printArray(b); // print: 18, 16, 19, -5, 3, 14, 6, 0

insertionSort (b);

printArray(b); // print: 19, 18, 16, 14, 6, 3, 0, -5

// testing factorial

System.out.println (factorial (5)); //print: 120

c[0] = factorial (c[0]);

c[1] = factorial (c[2]);

printArray(c); // print: 24, 120, 5, 3, 1

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!