publie class ArrayMethods public class Array Methods // precondition: anArray is
ID: 3680073 • Letter: P
Question
publie class ArrayMethods public class Array Methods // precondition: anArray is not null. target is an element in the array // postcondition: absolute value of difference between target and average of // precondition: anArray is not null // postcondition: anArray is output anArray is returned. public static void PrintArray (int [0 anArray) public static double StatCale (Int [I anArray, int target) // precondition: anArray is not null. // method body not shown. // postcondition: average of anArray is returned as a double. public static double FindAverage Cint [I anArray) Write an outline indicating the steps necessary in the method // precondition: anArray is not null. // postcondition: largest element in array is returned. public static int FindLargest (int D anArray) // precondition: anArray is not null. // postcondition: position of search Num is returned. -1 if not on list. public static int LinearSearch (int D anArray) // precondition: anArray is not null. // postcondition: array is returned with elements arranged in order from least to greatest public static int SelectionSort (int [] anArray) 2. Write the method bodyExplanation / Answer
public class ArrayMethods {
/*Steps necessary in the methods-->
*
* 1.First we check size of array. if it is greater than 0. only then we will proceed
* 2.We will use loop until length of array and print all elements of array
*
* */
public static void PrintArray(int[] anArray)
{
//Method body
if(anArray.length>0)
{
for(int i=0;i<anArray.length;i++)
{
System.out.println(anArray[i]);
}}
else
{
System.out.println("anArray is empty... Exit");
}
}
public static double FindAverage(int[]anArray)
{
/*Steps necessary in the methods-->
*
* 1.First we check size of array. if it is greater than 0. only then we will proceed
* 2.We will use loop until length of array and add all elements of array and then divide the total by length of array
*
* */
//method body
int total=0;
double avg=0.0;
if(anArray.length>0)
{
for(int i=0;i<anArray.length;i++)
{
total=total+anArray[i];
avg=total/anArray.length;
}
}
else
{
avg=-1;
System.out.println("anArray is empty... Exit");
}
return avg;
}
public static double FindLargest(int[]anArray)
{
/*Steps necessary in the methods-->
*
* 1.First we check size of array. if it is greater than 0. only then we will proceed
* 2.We will use loop until length of array and compare each element with max element so far.
* if current element is greater than max then max will be assigned this current element.
*
* */
//method body
int max=-0; //sentinel value to show anArray is empty
if(anArray.length>0)
{
max=anArray[0];
for(int i=0;i<anArray.length;i++)
{ if(anArray[i]>=max)
max=anArray[i];
}
}
else
{
System.out.println("anArray is empty... Exit");
}
return max;
}
public static int LinearSearch(int[]anArray,int searchNum)
{
/*Steps necessary in the methods-->
*
* 1.First we check size of array. if it is greater than 0. only then we will proceed
* 2.We will use loop until length of array and compare each element with searchNum.
* if current element is equal to searchNum then we will return position.
* 3. if we reach till end of loop and searchNum not found then we will return -1
*
* */
//method body
if(anArray.length>0)
{
for(int i=0;i<anArray.length;i++)
{
if(anArray[i]==searchNum)
return i;
}
}
return -1;
}
public static int[] SelectionSort(int[]anArray)
{
/*Steps necessary in the methods-->
*
* 1.First we check size of array. if it is greater than 0. only then we will proceed
* 2. We will find smallest number in anArray and place it in first position.
* 3. Then we will Next smallest in rest array leaving first position and replace it with second element.
* 4. Same process will continue until end of anArray. After that in last we will have sorted array in ascending order.
* */
//method body
if(anArray.length>0)
{
for (int i = 0; i < anArray.length - 1; i++)
{
int index = i;
for (int j = i + 1; j < anArray.length; j++)
if (anArray[j] < anArray[index])
index = j;
int smallNumber = anArray[index];
anArray[index] = anArray[i];
anArray[i] = smallNumber;
} }
else
{ int arr1[]=new int[1];
return arr1; // if anArray is null . Method will return blank array
}
return anArray;
}
public static double StatCalc(int[]anArray,int target)
{
/*Steps necessary in the methods-->
*
* 1.First we check size of array. if it is greater than 0. only then we will proceed
* 2.Then we will check if target element is present in Array or not
* 3.Then we will find average of array
* 4.In last we will calculate difference and return its absolute value.
* 5.We will return -1 if any error occurs.
* */
//method body
double diff=-1;
if(anArray.length>0)
{ int index=LinearSearch(anArray, target);
if(index>=0)
{
double average=FindAverage(anArray);
diff=target-average;
}
else
{
return -1;
}
}
else
{
return -1;
}
return Math.abs(diff);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.