Ask Question
20 November, 20:26

Write a program that calculates the average of a stream of non-negative numbers. The user can enter as many non-negative numbers as they want, and they will indicate that they are finished by entering a negative number. For this program, zero counts as a number that goes into the average. Of course, the negative number should not be part of the average (and, for this program, the average of 0 numbers is 0). You must use a method to read in the numbers, keep track of the running sum and count, compute the average, and return the average to the main () method. Note that you must use a loop to do this, as you don't know how many numbers the user will enter ahead of time.

+4
Answers (1)
  1. 20 November, 22:50
    0
    import java. util. Scanner;

    public class SumOfPostiveNumbers {

    public static void main (String[] args) {

    //scanner object to read input from user

    Scanner input = new Scanner (System. in);

    boolean repeat = true;

    String userChoice;

    double average;

    do

    {

    //get the average of the set of numbers using getPositiveNumber method

    average = getPositiveNumber (input);

    //display the output

    System. out. printf ("The average is: %.2f", average);

    //prompt and read if user wishes to repeat the process

    System. out. print ("/n/nDo you want to compute another average (y/n) ? ");

    input. nextLine ();

    userChoice = input. nextLine ();

    //if user enters anything other than y, terminate the program

    if (! (userChoice. equalsIgnoreCase ("y")))

    repeat = false;

    System. out. println ();

    }while (repeat);

    }

    //method that read a stream of integers and returns the average

    public static double getPositiveNumber (Scanner input)

    {

    / / Variables for this method

    double inputValue;

    int numberofNumbers;

    numberofNumbers = 1;

    double average;

    average=0;

    double total;

    total = 0;

    //prompt the user to enter a set of numbers

    System. out. printf ("Enter a stream of non-negative numbers (negative when finished) : ");

    do

    {

    //read the number

    inputValue = input. nextDouble ();

    //if the number is not negative

    if (inputValue > = 0)

    {

    //add it to total

    total = total + inputValue;

    //compute the average

    average = total / numberofNumbers;

    //increment the count of numbers

    numberofNumbers = numberofNumbers + 1;

    }

    } while (inputValue> = 0);

    //return the calculates average

    return average;

    }

    }
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Write a program that calculates the average of a stream of non-negative numbers. The user can enter as many non-negative numbers as they ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers