5-1) (Count positive and negative numbers and compute the average of numbers) Write a program that reads an unspecified number of integers, determines how manypositive and negative values have been read, and computes the total and average of the input values (not counting zeros). Your program ends with the input 0. Display theaverage as a floating-point numberSAMPLE RUN #4: java AvgwithLoopInteractive SessionHide InvisiblesHighlight: NoneShow Highlighted Only DEnter.an.integer, the.input ends.if.it.is.e:-4433.-12.15.89.45The-number-of. positives is . 6The number.of.negatives.is.3The total.is.335.0The average.is 37.22113.117.45.8

Question
Asked Mar 13, 2019
2187 views

Use Java to answer the question.

5-1) (Count positive and negative numbers and compute the average of numbers)

Write a program that reads an unspecified number of integers, determines how many positive and negative values have been read, and computes the total and average of the input values (not counting zeros). Your program ends with the input 0. Display the average as a floating-point number.

 

 
5-1) (Count positive and negative numbers and compute the average of numbers) Write a program that reads an unspecified number of integers, determines how many
positive and negative values have been read, and computes the total and average of the input values (not counting zeros). Your program ends with the input 0. Display the
average as a floating-point number
SAMPLE RUN #4: java AvgwithLoop
Interactive SessionHide Invisibles
Highlight: None
Show Highlighted Only D
Enter.an.integer, the.input ends.if.it.is.e:-4433.-12.15.89.45
The-number-of. positives is . 6
The number.of.negatives.is.3
The total.is.335.0
The average.is 37.22
113.117.45.8
help_outline

Image Transcriptionclose

5-1) (Count positive and negative numbers and compute the average of numbers) Write a program that reads an unspecified number of integers, determines how many positive and negative values have been read, and computes the total and average of the input values (not counting zeros). Your program ends with the input 0. Display the average as a floating-point number SAMPLE RUN #4: java AvgwithLoop Interactive SessionHide Invisibles Highlight: None Show Highlighted Only D Enter.an.integer, the.input ends.if.it.is.e:-4433.-12.15.89.45 The-number-of. positives is . 6 The number.of.negatives.is.3 The total.is.335.0 The average.is 37.22 113.117.45.8

fullscreen
check_circle

Expert Answer

Step 1

Java code

import java.util.Scanner;

 

public class AvgWithLoop {

      public static void main(String[] args){

            int num;    //to store the input number

            int positiveCount=0;    //to store the count of positive numbers

            int negativeCount=0;    //to store the count of negative numbers

            double total=0.0; //to store the total of all the numbers

            double avg=0.0;   //to store the average of all the numbers

            Scanner s=new Scanner(System.in);   //declare scanner object to take input from user

            System.out.println("Enter an integer, the input ends if it is 0: ");

            while(true){      //non terminating loop

                  num=s.nextInt();  //accept a number and store in num

                  if(num==0){ //if the input number is 0

                        break;      //terminate the loop

                  }

                  if(num>0){  //if it is a positive number

                        positiveCount+=1; //increase the positive count by 1

     &n...

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

Programing Language

Related Computer Science Q&A

Find answers to questions asked by student like you
Show more Q&A
add
question_answer

Q: Design a Turing machine that computes the function f (x) = x − 2 if x > 2   = 0 i...

A: Click to see the answer

question_answer

Q: Create a new file in c++ and save it as lab11_XYZ.cpp (replace XYZ with your initials). Create ANOTH...

A: Program Instructions:In the header file, create a temporary variable temp of the data type of each v...

question_answer

Q: I am trying to read in a text file called "lines.txt" that holds an entire dictionary of words with ...

A: First you need to use the ifstream class object to read from file.

question_answer

Q: How do I define a recursive function with C++? I am supposed to compute the Fibonacci numbers with a...

A: The algorithm for the recursive Fibonacci series is as below:Start with the first element which is 0...

question_answer

Q: What values will print? Use Eclipse to verify it and show the steps. double x = Math.sqrt(4);System....

A: Creating the program exemplifying the Java math operators in a class called MathOperators as shown i...

question_answer

Q: You need to use Eclipse to create this program.

A: Instructions for the first program:Create an array of size 5 and initialize a counter variable named...

question_answer

Q: Visual Basic. Write the definition of a function named quadratic that receives three double paramete...

A: Formula for quadratic equation:

question_answer

Q: I am struggling on storing numbers from the getChoice () function into an array and pulling it back ...

A: Click to see the answer

question_answer

Q: Find a regular expression for the set {anbm : n ≥ 3, m is odd}

A: Regular expression:Given: {an bm: n >=3, m is odd}