and retume an wray of 3 elements that containe the following Pirst element the m
ID: 3729635 • Letter: A
Question
and retume an wray of 3 elements that containe the following Pirst element the maximum element trom bot - Second element the minimum number from both arrays (sample below 2 in the 2 - Thind ement the average trom the two smalest and 2 largest numbers (here 765 754 calouiated as (2+10 999 10254) amays (sample below voa in the 2 aays) b) tt markaj Test your methods to have the following outputs: (Respects the output Numbers1 (125, 32, 33, 25, 100. 58. 909, 54, 2,9 8) Numbers2-(10, 52 18, 98, 11, 76, 87, 32, 258, 1025, 13) Verity Results- (2,099, 509 0)Explanation / Answer
Method arrays
a) Create a method take 2 arrays are arguments and return an array with 3 values
//method to create an array with the result of min,max and average
public static int[] verify(int n1[], int n2[]) {
//Loop for array1 sorting
for (int i = 0; i < n1.length; ++i)
{
for (int j = i + 1; j < n1.length; ++j)
{
if (n1[i] > n1[j])
{
int temp = n1[i];
n1[i] = n1[j];
n1[j] = temp;
}
}
}
//loop for sorting second array
for (int i = 0; i < n2.length; ++i)
{
for (int j = i + 1; j < n2.length; ++j)
{
if (n2[i] > n2[j])
{
int temp = n2[i];
n2[i] = n2[j];
n2[j] = temp;
}
}
}
//result array
int verifyRes[]={n1[0],n1[(n1.length)-1],((n1[0]+n2[0]+n1[(n1.length)-1]+n2[(n2.length)-1])/4)};
//return method
return (verifyRes);
}
------------------------------------------------------------------------------------------------------------------------------
b)Test your method
ScreenShot:-
Code:-
//Header files
import java.util.*;
import java.lang.*;
import java.io.*;
//main class
class arrayReturn
{
//main method
public static void main (String[] args) throws java.lang.Exception
{
//array declaration and initialization
int[] verifyResult;
int[] number1={125,32,33,25,100,56,999,54,9,2,8};
int[] number2={10,52,18,98,11,76,87,32,258,1025,13};
//display first array
System.out.print("number1={");
for(int i=0;i<number1.length;i++){
System.out.print(number1[i]+",");
}
System.out.println("}");
//display second array
System.out.print("number2={");
for(int i=0;i<number1.length;i++){
System.out.print(number2[i]+",");
}
System.out.println("}");
//call method
verifyResult=verify(number1,number2);
//Print result array
System.out.print("verifyResult={");
for(int i=0;i<3;i++){
System.out.print(verifyResult[i]+",");
}
System.out.print("}");
}
//method to create an array with the result of min,max and average
public static int[] verify(int n1[], int n2[]) {
//Loop for array1 sorting
for (int i = 0; i < n1.length; ++i)
{
for (int j = i + 1; j < n1.length; ++j)
{
if (n1[i] > n1[j])
{
int temp = n1[i];
n1[i] = n1[j];
n1[j] = temp;
}
}
}
//loop for sorting second array
for (int i = 0; i < n2.length; ++i)
{
for (int j = i + 1; j < n2.length; ++j)
{
if (n2[i] > n2[j])
{
int temp = n2[i];
n2[i] = n2[j];
n2[j] = temp;
}
}
}
//result array
int verifyRes[]={n1[0],n1[(n1.length)-1],((n1[0]+n2[0]+n1[(n1.length)-1]+n2[(n2.length)-1])/4)};
//return method
return (verifyRes);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.