bartleby
search
close search
Hit Return to see all results

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

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.

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

There’s more to the solution above. Sign up now to see!

Sign Up

Not ready to register yet? Check out a sample Q&A here.

Stuck on your homework?

Get expertly curated answers to your homework questions for just $9.99/month

Get As ASAP
Tagged in

Engineering

Computer Engineering

Sorry about that. What wasn’t helpful?