Click run to compile and execute, and note the incorrect program output. Fix the bug in theprogram.public class BeansInJars {public static void main (String [] args) {int numBeans = 500;int numJars = 3;int totalBeans = 0;System.out.print(numBeans + " beans in ");System.out.print(numJars + " jars yields "); totalBeans = numBeans * numJars; System.out.println("totalBeans" + " total"); return;}}

Respuesta :

Answer:

The output of the given incorrect program as follows:

Output:

500 beans in 3 jars yields totalBeans total

Explanation:

The correct program to this question as follows:

Program:

//defining class

public class Main //class-name BeansInJars  

{

//Declaring main method

public static void main(String a[]) //main method

{

int numBeans = 500,numJars = 3, totalBeans = 0; //defining integer variables and assigning value

//use of print function

System.out.print(numBeans); //print values

System.out.print( " beans in ");//print message

System.out.print(numJars ); //print values

System.out.print(  " jars yields ");//print message

totalBeans = numBeans * numJars; //calculating multiplication

System.out.print(totalBeans); //print values

System.out.print(" total");//print message

}

}

Output:

500 beans in 1500 total

Explanation of the above program as follows:

  • In the above java program, a class that is " BeansInJars" is declared inside the class the main method is defined, which contains three integer variables, which are "numBeans, numJars, and totalBeans".
  • In these variables, a value is assigned, that is "500, 3, and 0", and the third variable is used first two variables to calculate their multiplication.
  • Inside the main method, the print function is used, which prints the first variable value then message and then multiplication.

Debugging a program involves locating and fixing the errors in a program.

  • The error in the program is expressing an integer variable as string
  • The fix to the error is by writing the variable properly

Locating the error

To locate the error, we simply run the program.

When the program is run, it gives no error; however, the output is incorrect.

In this case, the error in the program is expressing an integer variable as string

Fixing the error

To fix the error, we simply write the variable properly

Hence, the correct program is:

public class Main {

   public static void main (String [] args) {

       int numBeans = 500;

       int numJars = 3;

       int totalBeans = 0;

       System.out.print(numBeans + " beans in ");

       System.out.print(numJars + " jars yields ");

       totalBeans = numBeans * numJars;

       System.out.println(totalBeans + " total");

       return;

   }

   

}

Read more about debugging at:

https://brainly.com/question/18844825

ACCESS MORE