Please answer all the parts of question 1 this should be done in java. 1. Call c
ID: 3694447 • Letter: P
Question
Please answer all the parts of question 1 this should be done in java.
1. Call class Arrays functions to finish the following task
(a) sort all elements in ascending order for an array (doubleArray)
(b) fill value 8 to each element for an int array (filledIntArray)
(c)Check if the two arrays (intArray intAnotheArray) are equal with returning a boolean type
(d)Check if there is an element value equals 5 within a sorted array (intArray) and return the position
(e) Make a copy of an array (intArray) to another array (intArrayCopy)
Explanation / Answer
/*Array_Functions.java java code to implement different functions */
import java.util.*;
public class Array_Functions {
/*The main method is the program's starting point */
public static void main(String[] args){
double[] array = new double[5];
array[0] = 5;
array[1] = 4;
array[2] = 3;
array[3] = 7;
array[4] = 1;
sort(array);
System.out.println("Sorted array");
for (int j = 0; j<array.length; j++)
System.out.println(array[j]);
System.out.println(" Filling the array");
int[] array1 = new int[5];
fill(array1);
for (int j = 0; j<array1.length; j++)
System.out.println(array1[j]);
int[] array2 = new int[5];
fill(array2);
System.out.println(" Calling equal function");
if (equal(array1,array2) == true) {
System.out.println("Arrays are equal ");
}
else
System.out.println("Arrays are not equal ");
if(check(array1) == -1) System.out.println(" Arrays does not contain 5 ");
else System.out.println("Array contain 5 at index " + check(array1) +" ");
int[] intArrayCopy = new int[5];
copy(array1,intArrayCopy);
System.out.println(" Copied array");
for (int j = 0; j<intArrayCopy.length; j++)
System.out.println(intArrayCopy[j]);
}
public static void sort(double[] array){
for (int j = 0; j<array.length; j++) {
for (int k = 0; k < array.length; k++){
if (array[j] < array[k]) {
double buffer = array[j];
array[j] = array[k];
array[k] = buffer;
}
}
}
}
public static void fill(int[] filledIntArray){
for (int j = 0; j<filledIntArray.length; j++) {
filledIntArray[j] = 8;
}
}
public static boolean equal(int[] intArray, int[] intAnotheArray ){
if(intArray.length != intAnotheArray.length)
return false;
for (int j = 0; j<intArray.length; j++) {
if(intArray[j] != intAnotheArray[j])
return false;
}
return true;
}
public static int check(int[] intArray){
for (int j = 0; j<intArray.length; j++) {
if(intArray[j] == 5) return j;
}
return -1;
}
public static void copy(int[] intArray, int[] intArrayCopy)
{
for (int j = 0; j<intArray.length; j++) {
intArrayCopy[j] = intArray[j];
}
}
}