Question

Programming 5_1: Create a Java class named <YourName>5_1 In this class create a main method...

Programming 5_1:

Create a Java class named <YourName>5_1

In this class create a main method, but for now leave it empty. Then write a Java method getAverage which takes an array of integers as it’s argument. The method calculate the average of all the elements in the array, and returns that average. The method should work for an array of integers of any length greater than or equal to one. The method signature is shown below:

public static double getAverage( int[] a)
Sample data and results are as below:

If array is:  

The method would return:

{ 5, 10, 15, 20,25,30,35 }

20

{ 1, 3, 5,7,9,11 }  

6


You may leave your main method blank, or you can include sample arrays to test your method. Compile your code. Your code with be tested with inputs that produce known outputs.

Programming 5_2:

Create a Java class named <YourName>5_2

In this class create a main method, but for now leave it empty. Include the method public

static int sumOfEvens( int[] myArray ) that accepts an integer array named myArray. This method should return the sum of the even integers in myArray.

Sample data and results are as below:   

myArray   

Sum of the even integers

{20, 40, 15, 12, 7, 1, 5,6}

78

where the method would return 78, since the sum of the even numbers 20,40,12,and 6 is 78.

You may leave your main method blank, or you can include sample arrays to test your method.

Your implementation will be tested with array inputs that produce known outputs.

Programming 5_3:

Create a Java class named <YourName>5_3

In this class create a main method, but for now leave it empty. Then write a Java method findPosition, which accepts two parameters: an int named target and an array of integers named numbers. The method returns the first occurrence of the subscript position/index within numbers in which target is found. Returns the value -1 if target is not stored in the array.  

The method signature is as below:  

public static int findPosition ( int target, int[] numbers)  

Sample data and return values are as below:  

Target

Array numbers

The method would return:

5

{ 1, 3, 5,7,9,11,22,8,5,7 }

2

45

{ 1, 2, 11,3, 5,7,9,11 }

-1

21

{5,40,19,21}   

3

You may leave your main method blank, or you can include sample arrays to test your method. Compile your code. Your code with be tested with inputs that produce known outputs.

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

1 class JohnNash5_1 f 3 public static double getAverage( int[] a)t 4 // declaring variables double result- 0; 6 7 8 9 10 // l

class JohnNash5_1 {

public static double getAverage( int[] a){

// declaring variables

double result = 0;

// looping through each number

for(int number: a)

// adding number to result

result += number;

// returning the average

return result / a.length;

}

public static void main(String[] args) {

// sample runs

System.out.println(getAverage(new int[]{ 5, 10, 15, 20,25,30,35 }));

System.out.println(getAverage(new int[]{ 1, 3, 5,7,9,11 } ));

}

}

// OUTPUT

// 20.0

// 6.0

Add a comment
Know the answer?
Add Answer to:
Programming 5_1: Create a Java class named <YourName>5_1 In this class create a main method...
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
  • Create a class named HW that has the main method. Leave the main method empty for...

    Create a class named HW that has the main method. Leave the main method empty for now. • Create a method named allPairs that takes an integer array parameter named a that contains only positive integers and does not return anything. The method should print out all pairs of numbers that have a sum that is prime. If there are no pairs that have a sum that is prime, print out "No pairs". Note that in the first example, (2,...

  • Java: Create a main method for your class, and then add another method to your class...

    Java: Create a main method for your class, and then add another method to your class (above the main method) named fallingDistance. This method accepts an integer into its parameter t, which is the amount of time, in seconds, that an object has been falling. This method returns the distance, in meters, that the object has fallen during the time interval. When an object s falling because of gravity, we use the following formula to determine the distance the object...

  • Programming in java In this part of this Lab exercise, you will need to create a...

    Programming in java In this part of this Lab exercise, you will need to create a new class named ArrayShiftMult. This class carries out simple manipulation of an array. Then you should add a main method to this class to check that your code does what it is supposed to do. 1. Create a BlueJ project named LAB06 as follows: a. Open the P drive and create a folder named Blue), if it does not exist already. Under this folder,...

  • JAVA HELP (ARRAYS) Assignment Create a class named Module4 containing the following data members and methods...

    JAVA HELP (ARRAYS) Assignment Create a class named Module4 containing the following data members and methods (note that all data members and methods are for objects unless specified as being for the entire class) 1) Data members: none 2) Methods a. A method named displayContents that accepts an array of type int and prints their values to the b. A method named cemoveNegatives that accepts an array of type int, copies all its non-negative the new array should be the...

  • Java 1. Create an application named NumbersDemo whose main() method holds two integer variables. Assign values...

    Java 1. Create an application named NumbersDemo whose main() method holds two integer variables. Assign values to the variables. In turn, pass each value to methods named displayTwiceTheNumber(), displayNumberPlusFive(), and displayNumberSquared(). Create each method to perform the task its name implies. 2. Modify the NumbersDemo class to accept the values of the two integers from a user at the keyboard. This is the base code given: public class NumbersDemo { public static void main (String args[]) { // Write your...

  • 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...

  • Java arrays Create method named repeatedN that takes two integer parameters named range and n and...

    Java arrays Create method named repeatedN that takes two integer parameters named range and n and returns an integer array Method should return an integer array that has numbers 0 - range repeated in order n times If range is less than or equal to 0; return an array that has 0 repeated n times Create a second method named printArray that takes an integer array as a parameter. The method should print out every element on the same line...

  • In this lab, you will create one class named ArrayFun.java which will contain a main method...

    In this lab, you will create one class named ArrayFun.java which will contain a main method and 4 static methods that will be called from the main method. The process for your main method will be as follows: Declare and create a Scanner object to read from the keyboard Declare and create an array that will hold up to 100 integers Declare a variable to keep track of the number of integers currently in the array Call the fillArray method...

  • You must create a Java class named TenArrayMethods in a file named TenArrayMethods.java. This class must...

    You must create a Java class named TenArrayMethods in a file named TenArrayMethods.java. This class must include all the following described methods. Each of these methods should be public and static.Write a method named getFirst, that takes an Array of int as an argument and returns the value of the first element of the array. NO array lists. Write a method named getLast, that takes an Array of int as an argument and returns the value of the last element...

  • This is JAVA language Create a .java class called MoreArrayFun.java with only a main method. This...

    This is JAVA language Create a .java class called MoreArrayFun.java with only a main method. This program will use the ArrayManager class. The final version of the program is described below, but initially use it to test your ArrayManager class as you write it. Complete the ArrayManager class as specified below: The class will have exactly two instance variables:  An array of integers  A count of the number of elements currently in the array The class will have...

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