# public class TestAverage {public static void main(String[] args) {int test1, test2, test3, sum;Scanner reader = new Scanner(System.in);System.out.println("Enter test 1");test1 = reader.nextInt();System.out.println("Enter test 2");test2 = reader.nextInt();System.out.println("Enter test 3");test3 = reader.nextInt();sum = test1 + test2 + test3;System.out.println("The sum is " + sum);System.out.println("The average is " + sum / 3);}}There is one logic error in this code explain what is wrong and how to fix it.

Question
205 views

public class TestAverage {
public static void main(String[] args) {
int test1, test2, test3, sum;
System.out.println("Enter test 1");
System.out.println("Enter test 2");
System.out.println("Enter test 3");
sum = test1 + test2 + test3;
System.out.println("The sum is " + sum);
System.out.println("The average is " + sum / 3);
}
}

There is one logic error in this code explain what is wrong and how to fix it.

check_circle

Step 1

The objective of the program is to calculate the average, now since the variable which holds the average value has been declared as an int type, it will not hold the decimal places, hence if the average computes to a have a fractional value it will not be seen in the answer. Hence we need to declare the sum as a variable which can hold fractional values, which is float type. After the declaring the sum variable to be of float type we will get the correct result.

Step 2

The correct program is shown below:

import java.util.*;

public class TestAverage {

public static void main(String[] args) {

int test1, test2, test3;

float sum;

System.out.println("Enter test 1");

System.out.println("Enter test 2");

...

See Solution

### Want to see this answer and more?

Solutions are written by subject experts who are available 24/7. Questions are typically answered within 1 hour.*

See Solution
*Response times may vary by subject and question.
Tagged in 