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
Asked Feb 8, 2019
205 views

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_circle

Expert Answer

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;

                        Scanner reader = new Scanner(System.in);

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

                        test1 = reader.nextInt();

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

...

Want to see the full answer?

See Solution

Check out a sample Q&A here.

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.

Related Computer Engineering Q&A

Find answers to questions asked by student like you
Show more Q&A
add
question_answer

Q: Identify the errors in the following program. 2 errorspublic class Test {public static void main(St...

A: The two errors in the given program are:The argument is passed in wrong order in the line nPrintln(5...

question_answer

Q: how can I create comments in python?

A: In python, # character is used to start the single line comments. Place a hash symbol in front of a ...

question_answer

Q: Write a program called StudentTable that prints the below table. Use only print() orprintf(). DO NOT...

A: The "print()" and "printf()" function are available in C language and "println()" function is only a...

question_answer

Q: 1. What is the output of the following code? public class Loopy {public static void main(String[] ar...

A: 1.Explanation for the given java code:In the class "Loopy", variable "x" is declared and initialized...

question_answer

Q: Consider a 4-way set associative cache made up of 64-bit words. The number of words per line is 8 an...

A: Cache size:The cache size can be calculated using the following formula:

question_answer

Q: How Technology can help to develop more effective policy?  (Information technology in Global Economy...

A: Information technology:Information technology(IT) has become a very important part of every business...

question_answer

Q: Consider the sender A transmits 12 data packets to B using stop wait protocol. If every 3rd packet i...

A: Analysis:In stop and wait protocol, the data packets will be transmitted as follows:1, 2, 3, 3, 4, 5...

question_answer

Q: I need  crow ERD Model with PK, FK and Referential integrity Arrow Electronics is a consumer electro...

A: The 8 entities are indicated below with only their unqiue identifier (underlined) and one or more ot...

question_answer

Q: this is a problem that I have how would I go about solving it? Stadium Seating There are three seati...

A: Algorithm: Include required header files.Define main function.Initializes the seat prices for class ...