CODE IN JAVA 2.  Exploring Variable Length Parameter Lists   The file Parameters.java contains a program to test the variable length method average. Note that average must be a static method since it is called from the static method main.

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

CODE IN JAVA

2.  Exploring Variable Length Parameter Lists

 

The file Parameters.java contains a program to test the variable length method average. Note that average must be a static method since it is called from the static method main.

 

1. Compile and run the program. 

 

2. Add a call to find the average of a single integer, say 13. Print the result of the call.

 

3. Add a call with an empty parameter list and print the result. Is the behavior what you expected?

 

4. Add an interactive part to the program. Ask the user to enter a sequence of at most 20 nonnegative integers. Your program should have a loop (Use a While loop) that reads the integers into an array and stops when a negative is entered (the negative number should not be stored). Invoke the average method to find the average of the integers in the array (send the array as the parameter). Does this work?  If not, then why?  Edit your program to fix the problem. Do not modify the original while loop. 

 

5. Add a method minimum that takes a variable number of integer parameters and returns the minimum of the parameters. Invoke your method on each of the parameter lists used for the average function.

 

 

 

//*******************************************************

// Parameters.java

//

// Illustrates the concept of a variable parameter list.

//*******************************************************

import java.util.Scanner;

public class Parameters

{

//-----------------------------------------------

// Calls the average and minimum methods with

// different numbers of parameters.

//-----------------------------------------------

public static void main(String[] args)

{

double mean1, mean2;

mean1 = average(42, 69, 37);

mean2 = average(35, 43, 93, 23, 40, 21, 75);

System.out.println ("mean1 = " + mean1);

System.out.println ("mean2 = " + mean2);

}

//----------------------------------------------

// Returns the average of its parameters.

//----------------------------------------------

public static double average (int ... list)

{

double result = 0.0;

if (list.length != 0)

{

int sum = 0;

for (int num: list)

sum += num;

result = (double)sum / list.length;

}

return result;

}

}

 

Here is a sample of what your output may look like:

mean1 = 49.333333333333336 and min1 = 37
mean2 = 47.142857142857146 and min2 = 21
mean3 = 13.0 and min3 = 13
Return value of average for an empty parameter list is 0.0
Return value of minimum is 2000000000
Enter a sequence of at most 20 non-negative  integers with a negative to signal the end....
22
44
66
-1
The average of the array elements is 6.6
The minimum is 0
Average of numbers read in is 44.0
Minimum of numbers read in is 22

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY