Question

This is a Java program Write a program that reads an unspecified number of integers, determines...

This is a Java program

Write a program that reads an unspecified number of integers, determines home 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 to 2 decimal places. System.out.println(countPositive);

System.out.println(countNegative);

System.out.println(total);

System.out.printf("%.2f",total * 1.0 / count);

Assume inputs are always integer [-912985158, 912985158] and ends with 0.

Input 1 2 -1 3 0

Output 3 1 5 1.25

NOTE: 1- When the input is 0 , that means, user didn't enter anything and so the expected output is single zero 0 . // Do not count zero as positive or negative number!

2- You need to read all the inputs in one single line with one space between each number. Exactly like the provided examples. Do not add extra spaces.

3- The output needs to be in separate lines.

You must use this Driver Class.

class DriverMain{

public static void main(String args[]){

GW3_P4 gw3P4 = new GW3_P4();

gw3P4.solution();

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

\color{blue}\underline{GW3\_P4.java:}

import java.util.Scanner;

public class GW3_P4 {

    public void solution() {
        int total = 0;
        int num, count = 0, countPositive = 0, countNegative = 0;
        Scanner in = new Scanner(System.in);
        while (true) {
            num = in.nextInt();
            if(num == 0) {
                break;
            }
            if(num < 0) {
                countNegative++;
            } else {
                countPositive++;
            }
            total += num;
            count++;
        }
        System.out.println(countPositive);
        System.out.println(countNegative);
        System.out.println(total);
        System.out.printf("%.2f",total * 1.0 / count);
    }

}

\color{blue}\underline{DriverMain.java:}

class DriverMain{

    public static void main(String args[]){
        GW3_P4 gw3P4 = new GW3_P4();
        gw3P4.solution();
    }

}

1 2-1 3 0 1.25 Process finished with e xit code 0

Add a comment
Know the answer?
Add Answer to:
This is a Java program Write a program that reads an unspecified number of integers, determines...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java programming 1. Write a program that reads an unspecified number of integers, determines how many...

    Java programming 1. Write a program that reads an unspecified number of integers, determines how many positive and negative value 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 Here are sample runs: (red indicates a user input) Enter an integer, the input ends if it is 0: 1 2 -1 3 0 The number of positives is...

  • (While-loop controlled by a sentinel value) Write a program that reads an unspecified number of integers....

    (While-loop controlled by a sentinel value) Write a program that reads an unspecified number of integers. Your program ends with the input 0. Determine and display how many positive and negative values have been read, the maximum and minimum values, and the total and average of the input values (not counting the final 0).

  • Correct the five syntax, run-time or logic errors that are found in the CountAndAverageNumbers.java file. Link...

    Correct the five syntax, run-time or logic errors that are found in the CountAndAverageNumbers.java file. Link to the file: CountAndAverageNumbers.javaPreview the document. Make sure you include comments in your code where errors were corrected. If you do not flag each error with a comment, points will be deducted. Upload the corrected .java file here. The output of the corrected file looks like the following: Debug4Output.PNG This program reads an unspecified number of integers, determines how many positive and negative values...

  • (Count positive and negative numbers and compute the average of numbers) Write a program that reads...

    (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. If you entire input is 0, display 'No numbers are entered except 0.' *So I think my code is looping because the...

  • I need to write a program that reads an unspecified number of scores and determines how...

    I need to write a program that reads an unspecified number of scores and determines how many scores are above or equal to the average and how many scores are below the average. Enter a negative number to signify the end of the input. Assume that the maximum number of scores is 100. PreLab07 Analyze scores Write a program that reads an unspecified number of scores and determines how many scores are above or equal to the average and how...

  • Using basic c++ 2. Count the positive and negative numbers using ***while loop*** • Write a...

    Using basic c++ 2. Count the positive and negative numbers using ***while loop*** • Write a program that reads unspecified number of integers , determines how many negative and positive values have been read. Also calculate total and average. Your program will end with input 0. • Output Enter an integer, the input ends if it is 0: 25 34 -89 72 -35 -67 21 48 0 The number of positives is 5 The number of negatives is 3 The...

  • 10) Write a program that inputs five numbers and determines the number of negative numbers input,...

    10) Write a program that inputs five numbers and determines the number of negative numbers input, the number of positive numbers input and the number of zeros input. An easy way to create a counter is given in the example below: int counter 0; initializes the counter counter counter +1;//increments the counter each time is used. Note: Output should read as shown below where # replaces the number of inputs and it is displayed on a table format] (40 pts.)...

  • You can draw the flowchart using the Word's drawing tool, or draw the flowchart on a...

    You can draw the flowchart using the Word's drawing tool, or draw the flowchart on a piece of paper, take a picture, insert it here or save it in the submission folder The program reads an unspecified number of integers until a zero is entered. While the program reads each number it counts the number of positive numbers and the number of negative numbers that have been entered and sum up the values of the numbers entered. After the user...

  • Write a program that reads a file containing an arbitrary number of text integers that are...

    Write a program that reads a file containing an arbitrary number of text integers that are in the range 0 to 99 and counts how many fall into each of eleven ranges. The ranges are 0-9, 10-19, 20-29,..., 90-99 and an eleventh range for "out of range." Each range will correspond to a cell of an array of ints. Find the correct cell for each input integer using integer division. After reading in the file and counting the integers, write...

  • In Java Write a program that reads an arbitrary number of 25 integers that are positive...

    In Java Write a program that reads an arbitrary number of 25 integers that are positive and even. The program will ask the user to re-enter an integer if the user inputs a number that is odd or negative or zero. The inputted integers must then be stored in a two dimensional array of size 5 x 5. Please create 3 methods: 1. Write a method public static int sum2DArray( int [1] inputArray ) The method sums up all elements...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT