Question

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 have been read, and computes the total and average of the input values (not counting zeros). The program ends with the input 0. The average is displayed as a double. */ import java.util.Scanner; public class CountAndAverageNumbers { public static void main(String[] args) { int countPositive = 0, countNegative = 0; int count = 0, number; total = 0; Scanner input = new Scanner(System.in); System.out.print("Enter an integer, the input ends if it is 0: "); number = input.nextInt(); while (number = 0) if (number > 0) countPositive++; else if (number < 0) countNegative++; total += number; count--; // Read the next number System.out.print("Enter an integer, the input ends if it is 0: "); number = input.nextInt(); if (count != 0) System.out.println("No numbers are entered except 0"); else { System.out.println("The number of positives is " + countPositive); System.out.println("The number of negatives is " + countNegative); System.out.println("The total is " + total); System.out.println("The average is " + total / count); } } } Output: Enter an integer, the input ends if it is 0: 5 Enter an integer, the input ends if it is 0: 2 Enter an integer, the input ends if it is 0: 18 Enter an integer, the input ends if it is 0: -6 Enter an integer, the input ends if it is 0: -4 Enter an integer, the input ends if it is 0:3 Enter an integer, the input ends if it is 0:-8 Enter an integer, the input ends if it is 0:12 Enter an integer, the input ends if it is 0: 0 The number of positivies is 4 The number of negatives is 3 The total is 20.0 The average is 2.857142857142857.

Download CountAndAverageNumbers.java

This program 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). The program ends with the input 0. The average is displayed as a double.
*/
import java.util.Scanner;

public class CountAndAverageNumbers {
public static void main(String[] args) {
int countPositive = 0, countNegative = 0;
int count = 0, number;
total = 0;

Scanner input = new Scanner(System.in);
System.out.print("Enter an integer, the input ends if it is 0: ");
number = input.nextInt();

while (number = 0)
if (number > 0)
countPositive++;
else if (number < 0)
countNegative++;

total += number;
count--;
  
// Read the next number
System.out.print("Enter an integer, the input ends if it is 0: ");
number = input.nextInt();

if (count != 0)
System.out.println("No numbers are entered except 0");
else {
System.out.println("The number of positives is " + countPositive);
System.out.println("The number of negatives is " + countNegative);
System.out.println("The total is " + total);
System.out.println("The average is " + total / count);
}
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
 package test; import java.util.Arrays; import java.util.Scanner; public class CountAndAverageNumbers {        public static void main(String[] args) {                int countPositive = 0, countNegative = 0,zerocount=0;           int count = 0, number;          int total = 0;          Scanner input = new Scanner(System.in);                 System.out.println("Enter the number of elements for the array:");              int n = input.nextInt(); //Setting the size of array            int arr1[] = new int[n]; //Initializing the array               System.out.println("Enter elements: ");                 for(int i=0;i<arr1.length;i++) {                     arr1[i] = input.nextInt(); //Adding the elements to array               }               System.out.println(Arrays.toString(arr1));              for(int i=0;i<arr1.length;i++) { //This for loop counts for positives if >0 and                   if(arr1[i]>0) { // negatives if <0 and zeros if both the above statements are not satisfied                               countPositive++;                        }else if(arr1[i]<0) {                                countNegative++;                        }else {                                 zerocount++;                    }               }               System.out.println("Number of positive elements are:"+countPositive);           System.out.println("Number of negative elements are:"+countNegative);           System.out.println("Number of zeroes are:"+zerocount);  } } 

Hi,

Instead of directly taking numbers, i have taken an array and stored the elements. Then, looped through the array and counted number of positives and negatives along with zeroes. Please feel free to comment if you need more info or if you find some doubts.

Add a comment
Know the answer?
Add Answer to:
Correct the five syntax, run-time or logic errors that are found in the CountAndAverageNumbers.java file. Link...
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...

  • The files provided in the code editor to the right contain syntax and/or logic errors. In...

    The files provided in the code editor to the right contain syntax and/or logic errors. In each case, determine and fix the problem, remove all syntax and coding errors, and run the program to ensure it works properly. // DebugFive2.java // Decides if two numbers are evenly divisible import java.util.Scanner; public class DebugFive2 { public static void main(String args[]) { int num; int num2; Scanner input = new Scanner(System.in); System.out.print("Enter a number "); num = input.nextInteger() System.out.print("Enter another number ");...

  • The part in bold is giving me an error. Ther error is that I cant convert...

    The part in bold is giving me an error. Ther error is that I cant convert an int to a boolean. The program asks the user to input two integer values and determines whether the first is divisible (without a remainder) by the second import java.util.Scanner; public class CheckDivisible { int isDivisible(int dividend, int divisor) { if(dividend % divisor == 0) return 1; else return 0; } public static void main(String[] args) { int dividend = 0; int divisor =...

  • Open a new file in your text editor, and start a class that will demonstrate a...

    Open a new file in your text editor, and start a class that will demonstrate a working two-dimensional array: import java.util.Scanner; class TwoDimensionalArrayDemo { public static void main(String[] args) { 2. Declare a three-by-three array of integers. By default, the elements will all be initialized to 0. int[][] count = new int[3][3]; 3. Declare a Scanner object for input, variables to hold a row and column, and a constant that can be used to indicate when the user wants to...

  • import java.util.Scanner; // TASK #1 Add the file I/O import statement here /** This class reads...

    import java.util.Scanner; // TASK #1 Add the file I/O import statement here /** This class reads numbers from a file, calculates the mean and standard deviation, and writes the results to a file. */ public class StatsDemo { // TASK #1 Add the throws clause public static void main(String[] args) { double sum = 0; // The sum of the numbers int count = 0; // The number of numbers added double mean = 0; // The average of the...

  • The files provided in the code editor to the right contain syntax and/or logic errors. In...

    The files provided in the code editor to the right contain syntax and/or logic errors. In each case, determine and fix the problem, remove all syntax and coding errors, and run the program to ensure it works properly. // Prompt user for value to start // Value must be between 1 and 20 inclusive // At command line, count down to blastoff // With a brief pause between each displayed value import java.util.*; public class DebugSix3 { public static void...

  • Need help with the UML for this code? Thank you. import java.util.Scanner;    public class Assignment1Duong1895...

    Need help with the UML for this code? Thank you. import java.util.Scanner;    public class Assignment1Duong1895    {        public static void header()        {            System.out.println("\tWelcome to St. Joseph's College");        }        public static void main(String[] args) {            Scanner input = new Scanner(System.in);            int d;            header();            System.out.println("Enter number of items to process");            d = input.nextInt();      ...

  • (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...

  • Please answer both questions. Thanks Question 26 (1 point) What is the output generated by the...

    Please answer both questions. Thanks Question 26 (1 point) What is the output generated by the code segment below, assuming that data is a Scanner object used to read the following text: As the world turns. data.useDelimiter(" "); int count = 0; while (data.hasNext()) { char input data.next().charAt(0); if (! Character.isLetter (input)) { count++; System.out.println (count); A/ Question 27 (1 point) Assuming that the user provides 114 as input, what is the output of the following code snippet? Please make...

  • 8. What is the output of this program if the input: 1 2 3 4 -5...

    8. What is the output of this program if the input: 1 2 3 4 -5 0 26? importjava.util.Scanner; public class Q_09 Your answer: public static void main(String[] args) int n, neg_count = 0, sum = 0; Scanner input = new Scanner(System.in); System.out.println("Enter n:"); n = input.nextInt(); while(n !=0) neg_count++; sum +=n; System.out.println("sum =" + sum); System.out.println("#of negatives:" + neg_count + "\t" +"Total = "+sum);

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