Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Part 1- Method practice Create a main method. In there, make an array of 100 ran

ID: 3878702 • Letter: P

Question

Part 1- Method practice

Create a main method. In there, make an array of 100 random integers(numbers between 0 and 1000).

Create a method to find average of the array. Pass the array and return the number(double) that is the average

In the main method, call the method created in Step 2. Print out the average

Create a method to find the min value of the array. Pass the array and return the int that is the smallest.(pass the value not the index of the value)

In the main method call the method made in Step 4. Print out the minimum

In the main method print out the first and last value of the array created in step 1

Create a method to return a new array that is the “reverse” of the array created in step 1. Return the new array

In the main call the method done in step 7. In the main print out the first and last value of the returned array.

In the main method, make an array of Strings called shoppingList. It should contain {“soda”,”corn”,”beans”,”chips”,”apples”}

Create another method called reverse that is an overloaded version of method created      in 7. This one takes an array of Strings but the code to reverse should be the same. Remember an overloaded method has the same name, it just changes the parameters( in this case the return type is changed too). So the method header would

              public static String [] reverse(String[] a)

      Output should look like

The average of array is 513.44

The minimum of array is 28

The first value is 457 , the last value is 826

After reverse, first value is 826 , the new last value is 457

In shoppingList, the first value is soda, the last value is apples

After reverse, the first value is apples, the last value is soda


Explanation / Answer


import java.util.*;
import java.lang.*;
import java.io.*;

class Code{

//method for finding the average

static double average(int[] a)

{

int sum=0;

int l = a.length; //length of array

for(int i=0; i<l; i++)

{

sum+=a[i];

}

double avg=sum/l;

return avg;

}

//method for min element

static int minimum(int[] a){

int min = Integer.MAX_VALUE;
for(int i=0; i<a.length; i++)
{
min=Math.min(a[i], min);
}
return min;
}

//method to reverse an integer array

static int[] reverse(int[] a)

{

int[] b = new int[a.length];

for(int i=0; i<a.length; i++)

b[i]=a[a.length-1-i];

return b;

}

//Overloaded method for reverse

static String[] reverse(String[] a)

{

String[] b = new String[a.length];

for(int i=0; i<a.length; i++)

b[i]=a[a.length-1-i];

return b;

}

public static void main(String args[]){

int[] a = new int[100];

for(int i=0; i<100; i++)

a[i]=i+1;

System.out.println("avg of the array "+average(a)); // static method calling without any object creation

System.out.println("min ele of the array "+minimum(a));

System.out.println("First ele of the array "+a[0]+" Last ele of the array "+a[99]);

int[] rev = reverse(a);

System.out.println("after reverse first ele is "+rev[0]+" Last ele is "+ rev[99]);

String[] shopList = {"soda","corn","beans","chips","apples"};

System.out.println("In shopping list first ele is "+shopList[0]+" and last element is "+shopList[4]);

String[] revShopList=reverse(shopList);

System.out.println("after reverse first ele is "+revShopList[0]+" and last ele is "+revShopList[4]);

}

}