Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

public class Average { public static void main(String[] args) { int n = 1.0; Sca

ID: 3761880 • Letter: P

Question

public class Average {

public static void main(String[] args) {

int n = 1.0;

Scanner scanner = new Scanner(system.in);

System.out.println("This program calculates " + "the average (mean) of entered numbers.");

int x;

do {

System.out.println("Enter another number " + "(or 0 to stop and show the average): ");

x += scanner.next();

n++;

} while (x != 0);

System.out.println("Your final average is: %d" + x / n);

}

}

Find and fix all the bugs in this program. Turn in to Canvas a file called Average.java containing the correct version of this code with in-line comments explaining each fix

Explanation / Answer

/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Average
{
   public static void main (String[] args) throws java.lang.Exception
   {
       // your code goes here
       int n = 0; //initializing this to zero for maintaining the count
       Scanner scanner = new Scanner(system.in);
       System.out.println("This program calculates " + "the average (mean) of entered numbers.");
       int x = 0;//initializing x to zero
       do {
           System.out.println("Enter another number " + "(or 0 to stop and show the average): ");
           x += scanner.nextInt(); //getting the next int from console
           n++;
       } while (x != 0);
       System.out.println("Your final average is: " + x*1.0/n);
   }
}