Question

In: Computer Science

First, launch NetBeans and close any previous projects that may be open (at the top menu...

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 user Calculate the average of the numbers entered by the user 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. The second method should take a String as its only argument and return a double (the average). The third method should take a String and a double as arguments but have no return value. 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. Thoughts Refer to Horstmann chapter 2 for details on formatting output values.

Instructor gave this part of code:

public static double getAverage(String numbers){
int numSpaces = 0;  
double total = 0, counter = 1;
for (int i = 0; i < numbers.length(); i++) {
if(numbers.charAt(i)==32)
numSpaces++;
}
while(numSpaces > 0){
int space = numbers.indexOf(" ");
String numString = numbers.substring(0,space);
double num = Double.parseDouble(numString);
total += num;
numbers = numbers.substring(space+1);
counter ++;
numSpaces--;
}
total += Double.parseDouble(numbers);
return total/counter;
}

Solutions

Expert Solution

//AverageWithMethods.java

import java.util.Scanner;

public class AverageWithMethods{
    // Method 1
    public static String getNumber(){
        Scanner input = new Scanner(System.in);
        System.out.print("Enter five to ten numbers separated by whitespace : ");
        return input.nextLine();
    }

    //method 2
    public static double getAverage(String numbers){
        int numSpaces = 0;
        double total = 0, counter = 1;
        for (int i = 0; i < numbers.length(); i++) {
            if(numbers.charAt(i)==32)
                numSpaces++;
        }
        while(numSpaces > 0){
            int space = numbers.indexOf(" ");
            String numString = numbers.substring(0,space);
            double num = Double.parseDouble(numString);
            total += num;
            numbers = numbers.substring(space+1);
            counter ++;
            numSpaces--;
        }
        total += Double.parseDouble(numbers);
        return total/counter;
    }

    //method 3
    public static void printAverage(String str, double avg){
        System.out.println("The average of the numbers "+str+" is "+avg);

    }

    //Main Method
    public static void main(String[] args) {
        String numbers = getNumber();
        double average = getAverage(numbers);
        printAverage(numbers, average);
    }
}

Save the file as AverageWithMethods.java
execute these commands.


javac AverageWithMethods.java
java AverageWithMethods


Related Solutions

First, launch NetBeans and close any previous projects that may be open (at the top menu...
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 "AtmSimulator" (without the quotation marks) (not ATMSimluator!) that simulates a simple one-transaction ATM according to the following guidelines. The program should start with an initial account balance, which you can set to any legitimate double value. All output of currency values should include a leading dollar sign and use two...
In java, follow the methods in bold First, launch NetBeans and close any previous projects that...
In java, follow the methods in bold 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 "WeightedAvgDataAnalyzer" (without the quotation marks), that modifies the DataAnalyzer.java in Horstmann Section 7.5, pp. 350-351 according to the specifications below. The input file should be called 'data.txt' and should be created according to the highlighted instructions below. Note that even though you know...
What are the advantages of asking open-ended questions? Are there any advantages to asking close-ended questions?...
What are the advantages of asking open-ended questions? Are there any advantages to asking close-ended questions? What about disadvantages for each question type? Why would we use surveys to collect self-report or victimization data? What makes them the best choice for this type of data collection? Why should we be aware of bias in questionnaire items? What can we do to reduce bias in our questions?
i need a javafx code 1. first create menu bar with option open and save 2....
i need a javafx code 1. first create menu bar with option open and save 2. when user click on open it opens the file only image file 3. when user click on save it saves as new file.
Ms. Jenny Joy is planning to open her first own business project: a little café close...
Ms. Jenny Joy is planning to open her first own business project: a little café close to the university district of the imaginary town of Brightside. She has rented a small, but nice venue for the café. She has worked hard to keep the target opening date of 1 June. There is a very important report missing from her paperwork though; she does not know how much profit she can expect during the first 3 months of operation. She remembers...
Ms. Jenny Joy is planning to open her first own business project: a little café close...
Ms. Jenny Joy is planning to open her first own business project: a little café close to the university district of the imaginary town of Brightside. She has rented a small, but nice venue for the café. She has worked hard to keep the target opening date of 1 June. There is a very important report missing from her paperwork though; she does not know how much profit she can expect during the first 3 months of operation. She remembers...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT