1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Andrews [41]
3 years ago
8

The following code does not work as intended. It is meant to input two test grades and return the average in decimal format:

Computers and Technology
1 answer:
vredina [299]3 years ago
4 0

Answer:

Type casting error

Explanation:

Logically, the program is correct and it is expected to return the average value. However, test1 and test2 are int values and when you applies any algebraic expression on Int, it will result into Int.

So, in this part (test1 + test2 )/2, two integers are adding and the final result is still an integer and this part will act as an integer even if you multiple or divide with any external number.

For example,

test1 = 4

test2 = 5

test1 + test2 = 9

Now, if you divide it by 2, it will still react as an integer portion. So, 9/2 would result in 4 instead of 4.5. All the calculation is done on the right side before assigning it to a double variable. Therefore, the double variable is still getting the int value and your program is not working correctly.

The solution is to typecast the "(test1 + test2 )/2" portion to double using Double.valueOf(test1 + test2)/2 ;

I have also attached the working code below.

import java.util.Scanner;

public class Main

{

public static void main(String[] args) {

 System.out.println("Hello World");

 Scanner scan = new Scanner (System.in);

 int test1 = scan.nextInt();

       int test2 = scan.nextInt();

       double average = Double.valueOf(test1 + test2)/2 ;

       System.out.println("Answer: " + average);

}

}

You might be interested in
Of the 5 factors that should be evaluated when assessing information's quality (Validity, Reliability, Accuracy, Timeliness, and
Art [367]
Validity is the best asseng information to evaluate the all factors
8 0
2 years ago
Which of the following statements is true?
Lilit [14]

Answer:

Option A is the correct answer choice for the above question.

Explanation:

The computer system needs intercommunication which is done inside the processor to process the task given by the user. There are two types of model is used for intercommunication--

  1. Message passing and
  2. Shared memory

The difference between two is that message passing passes the message on two points at a single unit of time whereas shared memory simultaneous shares the multiple messages. That's why shared memory is faster than message passing.

  • Hence option A is the correct choice because it also refers to the above concept. While the other is not correct because--
  • Option B states that message passing is faster than shared memory which is wrong.  
  • Option C states that message passing is used for large data but shared memory is used for large data.
  • Option D states that shared memory is unavailable in some processor which is wrong.
3 0
2 years ago
Which of the following is NOT a major type of crime reported to the IC3.
mezya [45]
I believe malware fraud, c. 


5 0
3 years ago
The following code processes a file containing five positive numbers. What will the variable $result contain after the code is e
Tanzania [10]

Answer:

highest of five numbers in the file

Explanation:

  • This code reads the file "some-file.txt" and saves the result in variable "somefile"
  • The runs a for loop for a count of 5.
  • In each loop it compares the current value of the file with the value of the variable result and if the result is true it updates the value of the variable result.
  • At the end of this code the variable result will contain the highest value in the file "some-file.txt" and print it.

3 0
3 years ago
How do you change the font in html?<br>&lt;______&gt;google sans"google sans font"&lt;/______&gt;
Allisa [31]
What do you want inside font colour,text,or face
7 0
2 years ago
Other questions:
  • Respond to the following in three to five sentences. Select the workplace skill, habit, or attitude described in this chapter (a
    12·1 answer
  • Which statement best describes the cut and paste option in a word processor?
    6·2 answers
  • What is the output of the code snippet given below?string s = "abcde";int i = 1;while (i &lt; 5){ cout &lt;&lt; s.substr (i, 1);
    11·1 answer
  • Anybody know this question??
    8·1 answer
  • Create a class named Invoicing that includes three overloaded computeInvoice() methods for a book store: see pages 196 for examp
    7·1 answer
  • Algorithm of how to calculate the area of a square.
    12·2 answers
  • You can use a minus sign to make a negative numberlike -2. What happens to each of the following 2++2
    8·1 answer
  • 1. Identify one modern technology and discuss its development and discuss what future changes might occur that could have an eve
    13·1 answer
  • Write the implementation of a class Cse20 Topic that represents a topic in the cse20 class. The class should implement the init
    13·1 answer
  • What is the difference between algorithm and program?
    10·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!