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
}
}
Hey,
Note: Brother in case of any queries, just comment in box I would be very happy to assist all your queries
public class Homework {
public static void initializeArray(int[] array) {
for(int i=0;i<array.length;i++)
{
if(i%2==0)
{
array[i]=5;
}
else
{
array[i]=0;
}
}
}
public static void printArray(int[] array) {
for(int i=0;i<array.length;i++)
{
System.out.print(array[i]+" ");
}
System.out.println();
}
public static void insertionSort(int[] a) {
int n = a.length;
for (int i=1; i<n; ++i)
{
int key = a[i];
int j = i-1;
while (j>=0 && a[j] > key)
{
a[j+1] = a[j];
j = j-1;
}
a[j+1] = key;
}
}
public static int factorial(int a) {
if(a==0)
return 1;
else
return a*factorial(a-1);
}
//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
}
}
Kindly revert for any queries
Thanks.
Part 1. Primitive Types, Sorting, Recursion for Homework.java a) Implement the static method initializeArray that receives...
Please complete the following programming with clear explanations. Thanks! Homework 1 – Programming with Java: What This Assignment Is About? Classes (methods and attributes) • Objects Arrays of Primitive Values Arrays of Objects Recursion for and if Statements Selection Sort Use the following Guidelines: Give identifiers semantic meaning and make them easy to read (examples numStudents, grossPay, etc.) Use upper case for constants. • Use title case (first letter is upper case) for classes. Use lower case with uppercase...
create a file homework_part_1.c a) Implement the function initialize_array that receives two parameters: an array of integers and the array size. Use a for loop and an if statement to put 0s in the odd positions of the array and 5s in the even positions. You must use pointers to work with the array. Hint: review pointers as parameters. b) Implement the function print_array that receives as parameters an array of integers and the array size. Use a for statements...
the code needs to be modifed. require a output for the code Java Program to Implement Insertion Sort import java.util.Scanner; /Class InsertionSort * public class Insertion Sort { /Insertion Sort function */ public static void sort( int arr) int N- arr.length; int i, j, temp; for (i-1; i< N; i++) j-i temp arrli; while (j> 0 && temp < arrli-1) arrli]-arrli-1]; j-j-1; } arrlj] temp; /Main method * public static void main(String [] args) { Scanner scan new Scanner( System.in...
You will implement the following method public static int[] linearSearchExtended(int[][] numbers, int key) { // Implement this method return new int[] {}; } There is a utility method provided for you with the following signature. You may use this method to convert a list of integers into an array. public static int[] convertIntegers(List<Integer> integers) Provided code import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Scanner; public class MatrixSearch { // This method converts a list of integers to an array...
Given a bubble sort and the following array: [10,7,19,5,16] What are the values after the first iteration? Given an insertion sort and the following array: [50,5,35,70,6] What does the array look like after the first insertion: Fill in the missing code for InsertionSort method // Insertion Sort method //sorts an array of Auto objects public static void insertionSort(Auto [] arr) { Auto temp; int j; for (int i=1; i<arr.length; i++) { j=i; temp=arr[i]; while (j!=0 &&(temp.getModel().compareTo(arr[[j-1].getModel())<0)...
1. Implement generic insertion sort public static <E extends Comparable<E>> void insertionSort(E[] list){ //implement body } 2. Implement generic bubble sort public static <E extends Comparable<E>> void bubbleSort(E[] list){ //implement body } 3. Implement generic merge sort public static <E extends Comparable<E>> void mergeSort(E[] list){ //implement body } 4. Implement generic heap sort public static <E extends Comparable<E>> void heapSort(E[] list){ //implement body }
1.Write code for a Java method, printArray, that takes an int array as parameter and prints it out, one element per line. public static void printArray (int[] values){ Methods that do NOT return a result have “void” as return type. Notice how an array parameter type is passed, int [] }// end printArray 2.Write code for a Java method that takes in as input parameter an integer number, say num, and returns a double array of size num with all...
PrintArray vi Create a class called PrintArray. This is the class that contains the main method. Your program must print each of the elements of the array of ints called aa on a separate line (see examples). The method getArray (included in the starter code) reads integers from input and returns them in an array of ints. Use the following starter code: //for this program Arrays.toString(array) is forbidden import java.util.Scanner; public class PrintArray { static Scanner in = new Scanner(System.in);...
The following code is a Java code for insertion sort. I would like this code to be modified by not allowing more than 10 numbers of integer elements (if the user enters 11 or a higher number, an error should appear) and also finding the range of the elements after sorting. /* * Java Program to Implement Insertion Sort */ import java.util.Scanner; /* Class InsertionSort */ public class InsertionSortTwo { /* Insertion Sort function */ public static void sort( int...
Your running times will probably be different than these. Please do a better job with the snipping tool than I did. Java program provided: // Student Name Today's Date import java.util.Arrays; import java.util.Random; public class SortTimer { // Please expand method main() to meet the lab requirements. // You have the following sorting methods available: // insertionSort(int[] a); // selectionSort(int[] a); // mergeSort(int[] a); // quickSort(int[] a); // The array will be in sorted order after the routines are called! ...