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 ");...

  • In this assignment you will demonstrate your knowledge by fixing the errors you find in the...

    In this assignment you will demonstrate your knowledge by fixing the errors you find in the program below. Copy out the text from this document and fix the code. Paste the corrected code in the area below, and make a list of the problems you fixed. Import java.util.Scanner; public class Test1 { public static void main(String[] args) {     Scaner input = new Scanner();     System.out.print("Enter an integer: ");     int v = Input.nextInt());     System.out.printLn("You entered " & v);...

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

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

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

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

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

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

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

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