Question

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

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


0 0
Add a comment Improve this question Transcribed image text
Answer #1


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]);

}

}

Add a comment
Know the answer?
Add Answer to:
Part 1- Method practice Create a main method. In there, make an array of 100 random...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In this same program I need to create a new method called “int findItem(String[] shoppingList, String...

    In this same program I need to create a new method called “int findItem(String[] shoppingList, String item)” that takes an array of strings that is a shopping list and a string for an item name and searches through the “shoppingList” array for find if the “item” exists. If it does exist print a confirmation and return the item index. If the item does not exist in the array print a failure message and return -1. import java.util.Scanner; public class ShoppingList...

  • In Java: Let's create a method that has parameters. Write a method, called returnHours that returns...

    In Java: Let's create a method that has parameters. Write a method, called returnHours that returns a string. Make the string from a string representing your name and a number representing the hours you spend sleeping, both are values that you pass in from the main. Create the variables to pass into the method call in main. Call the method and print the return value in main. Run and test it.

  • In Java: Executable Class create an array of Employee objects. You can copy the array you...

    In Java: Executable Class create an array of Employee objects. You can copy the array you made for Chapter 20. create an ArrayList of Employee objects from that array. use an enhanced for loop to print all employees as shown in the sample output. create a TreeMap that uses Strings for keys and Employees as values. this TreeMap should map Employee ID numbers to their associated Employees. process the ArrayList to add elements to this map. print all employees in...

  • Create a program named IntegerFacts whose Main() method declares an array of 10 integers. Call a...

    Create a program named IntegerFacts whose Main() method declares an array of 10 integers. Call a method named FillArray to interactively fill the array with any number of values up to 10 or until a sentinel value (999) is entered. If an entry is not an integer, reprompt the user. Call a second method named Statistics that accepts out parameters for the highest value in the array, lowest value in the array, sum of the values in the array, and...

  • Write a java program: Create a method fillRandom() that accepts an array of int as input...

    Write a java program: Create a method fillRandom() that accepts an array of int as input and populates it with random numbers in the range -999 to 1000 Explicitly store zero in index [0] and 900 in index [1]. (0 and 900 will be used as search keys) Create a method DisplayLastInts() that accepts an array of int as input and displays the last hundred elements to the screen in rows of 10 elements. Format the output so the 10...

  • C++ Program Int Main First Please Write one program that does the following: 1.       1.   Ask the...

    C++ Program Int Main First Please Write one program that does the following: 1.       1.   Ask the user for ten (10) grades, and store the data in an array.  Compute the average of all the grades.  Print the original ten grades and the average. a.       Declare an integer array with the name of “grades” of size 10 in the main function. b.      Create a function called “getGrades” that prompts the User for the grades and puts them in an integer array.                                                                i.      The function will receive...

  • Create a class called Student. This class will hold the first name, last name, and test...

    Create a class called Student. This class will hold the first name, last name, and test grades for a student. Use separate files to create the class (.h and .cpp) Private Variables Two strings for the first and last name A float pointer to hold the starting address for an array of grades An integer for the number of grades Constructor This is to be a default constructor It takes as input the first and last name, and the number...

  • You will create a program with two methods, the main() method of course and another method...

    You will create a program with two methods, the main() method of course and another method called printArray(). The main method will define an array of 5 elements. A loop will be used to prompt the user to enter 5 numeric values which will go into the 5 array elements. The main() method will then call the printArray() method passing the array by reference, and the array length by value. The printArray() method will then print the contents of the...

  • Array lists are objects that, like arrays, provide you the ability to store items sequentially and...

    Array lists are objects that, like arrays, provide you the ability to store items sequentially and recall items by index. Working with array lists involves invoking ArrayList methods, so we will need to develop some basic skills. Let's start with the code below: The main method imports java.utii.ArrayList and creates an ArrayList that can hold strings by using the new command along with the ArrayList default constructor. It also prints out the ArrayList and, when it does, we see that...

  • Write a method called printReverse() that takes a string and uses recursion to print the contents...

    Write a method called printReverse() that takes a string and uses recursion to print the contents of the string in reverse order. The string itself should not be reversed; it must be left in its original form. The method has the following header:   void printReverse(String s, int i) where s is a reference to the string, and i is an integer parameter that you may use as you see fit. You do not need to code up this method as...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT