The file Parameters.java contains a program to test the variable-length method average. 1. Read the code. Fill in the code to compute mean1 and mean2. Notice that you are calling the same method, but with a different number of parameters. 2. Add a call to find the average of a single integer, say 8. Print the result of the call. Is the result what you expected? 3. Add a call with an empty parameter list and print the result. Is the result what you expected? 4. Add a method called minimum to return the minimum value of the parameter list. Invoke this method in the main() and test it with different input numbers. //******************************************************* // 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 = // Your code:calculate average of 11,12 and 18 mean2 = // Your code:calculate average of 11,12,18 and 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; } }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

The file Parameters.java contains a program to test the variable-length method average.
1. Read the code. Fill in the code to compute mean1 and mean2. Notice that you are calling the same method, but with a different number of parameters.
2. Add a call to find the average of a single integer, say 8. Print the result of the call. Is the result what you expected?
3. Add a call with an empty parameter list and print the result. Is the result
what you expected?
4. Add a method called minimum to return the minimum value of the
parameter list. Invoke this method in the main() and test it with different
input numbers. 

 

//*******************************************************
// 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 = // Your code:calculate average of 11,12 and 18
        mean2 = // Your code:calculate average of 11,12,18 and 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;
    }
}

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 4 images

Blurred answer
Knowledge Booster
Arrays
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education