First, launch NetBeans and close any previous projects that may be open (at the top menu go to File ==> Close All Projects).Then create a new Java application called "AverageWithMethods" (without the quotation marks) according to the following guidelines.The program prompts the user for five to ten numbers, all on one line, and separated by spaces.  Then the user calculates the average of those numbers, and displays the numbers and their average to the user.The program uses methods to:Get the numbers entered by the userCalculate the average of the numbers entered by the userPrint the results with the whole number, a decimal, and two decimal positionsThe first method should take no arguments and return a String of numbers separated by spaces.  [CASE 2]The second method should take a String as its only argument and return a double (the average).  [CASE 4]  The third method should take a String and a double as arguments but have no return value.          [CASE 3]For example, if the user input is...  20 40 60 80 100...the program should give as output...  The average of the numbers 20 40 60 80 100 is 60.00.BE SURE TO INCLUDE THE OUTPUT FROM MULTIPLE PROGRAM RUNS AS A COMMENT BLOCK AT THE BOTTOM OF YOUR SOURCE CODE FILE.

Question
Asked Nov 13, 2019
3 views

First, launch NetBeans and close any previous projects that may be open (at the top menu go to File ==> Close All Projects).

Then create a new Java application called "AverageWithMethods" (without the quotation marks) according to the following guidelines.

The program prompts the user for five to ten numbers, all on one line, and separated by spaces.  Then the user calculates the average of those numbers, and displays the numbers and their average to the user.

The program uses methods to:

  1. Get the numbers entered by the user
  2. Calculate the average of the numbers entered by the user
  3. Print the results with the whole number, a decimal, and two decimal positions

The first method should take no arguments and return a String of numbers separated by spaces.  [CASE 2]

The second method should take a String as its only argument and return a double (the average).  [CASE 4]  

The third method should take a String and a double as arguments but have no return value.          [CASE 3]

For example, if the user input is...  20 40 60 80 100

...the program should give as output...  The average of the numbers 20 40 60 80 100 is 60.00.

BE SURE TO INCLUDE THE OUTPUT FROM MULTIPLE PROGRAM RUNS AS A COMMENT BLOCK AT THE BOTTOM OF YOUR SOURCE CODE FILE.

check_circle

Expert Answer

Step 1

Open netbeans and create a new project name "AverageWithMethods", by using the below given steps:

  1. Choose File -> New Project. Under Categories, select Java. Under Projects, select Java Application. Then, Click on Next.
  2. Under Project Name, type AverageWithMethods and check that the Project Location is set to NetBeansProjects.
  3. Enter Main as the main class.
  4. Ensure that the Create Main Class checkbox is checked.
  5. Click on Finish. The AverageWithMethods project is displayed in the Project window and java opens in the Source Editor.
Step 2

The below given java program will obey the following rubrics:

  • Importing necessary header files.
  • In main class declaring three methods,
  • The first method, readNumbers() takes no argument and return string of numbers separated by spaces. This method takes input of data from the user.
  • The second method, avg() accepts the input data as its argument and return type of this function is double. This function firstly, split the entered string data (by using split() method), calculates the sum of entered data and after then it calculates the average of the given data.
  • The third method print() will display the average of given numbers to user.
  • In main method, firstly, calling the readNumbers() method and storing its result in input variable.
  • After that, calling avg() method which stores the double data in average variable.
  • Calling print function.
Step 3

Program code:

 

//importing necessary header files

import java.util.Scanner;

//main class

public class Sample {

//defining a method with return type string

public static String readNumbers() {

//instantiating scanner class

Scanner scnr = new Scanner(System.in);

//declaring string variable for taking input

String input;

//displaying message to user to enter number

System.out.println("Enter five to ten numbers, all on one line and separated by spaces: ");

//taking input of number from user

input = scnr.nextLine();

//returning inputs

return input;

}

//defining method average

public static double avg(String input) {

//storing the numbers in string by using split function

String numbers[] = input.split(" ");

//declaring sum variable for storing sum of user entered value

double sum = 0;

//for loop

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

//storing result in the sum variable

sum += Double.parseDouble(numbers[i]);

}

//returning average of the ca...

Want to see the full answer?

See Solution

Check out a sample Q&A here.

Want to see this answer and more?

Solutions are written by subject experts who are available 24/7. Questions are typically answered within 1 hour.*

See Solution
*Response times may vary by subject and question.
Tagged in

Engineering

Computer Science

Related Computer Science Q&A

Find answers to questions asked by student like you

Show more Q&A add
question_answer

Q: Within the NTDS folder, which file stores the main Active Directory database? Group of answer choice...

A: Within the NTDS folder, the “Ntds.dit”file stores the main Active Directory database .Hence, option ...

question_answer

Q: Question 5, output in C++

A: C++ PROGRAM:#include&lt;iostream&gt;void swap(int a, int b) {int tmp = b;b = a; a = tmp;return;}void...

question_answer

Q: In Python Define a function writeScores with two parameters. The first should be the name of a file,...

A: Below is the approach and python code:Approach:Define a function “writeScores” having two parameters...

question_answer

Q: Write a Java program that prompts the user for the page size used in a virtual memory system; this w...

A: A Java program that accepts a valid system page size and virtual address and calculates the page num...

question_answer

Q: What folder contains group policy templates, logon/logoff scripts, and DFS synchronization data? Gro...

A: “System Volume (SYSVOL)” folder contains group policy templates, logon scripts, and Distributed File...

question_answer

Q: WRITE A SHORT NOTE ON THE PRIMITIVE COMPONENTS OF BITCOIN VALUE CHAIN?

A: Bitcoin Value Chain can be defined with the help of three terms BlockChain, Private Keys and third M...

question_answer

Q: Write a method named sumInts that can take a variable number of int arguments and return the sum of ...

A: Below is the approach, and a java program to display the sum of integer arguments, passed in the sum...

question_answer

Q: What 1s the output of the following code! { String myStr = new String("Cheggers2019") ; System.out.p...

A: There seems to be some error in the question, hence we are proceeding with a small assumption on our...

question_answer

Q: In MS Access, to ensure that a grade must be either A, B, C, D or F, whichvalidation rule should be ...

A: Microsoft (MS) AccessMicrosoft access is a database management system from Microsoft that combines r...