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
During slideshow mode hitting the b key will do which of these
Lemur [1.5K]
<span>If it's an multiple answer question it's....


</span><span>B.) Blank the screen with black screen (or return to the slide if you are currently blank).</span>
8 0
3 years ago
How do you think weather can affect sailing?<br><br>What would be good sailing weather?​
N76 [4]

Answer:

Weather can affect sailing because if its raining it can sink it also if theres a storm. Good weather that would be good to sail in is if its a little windy because if its just sunny and not windy it wont move. ps you can copy it if u want idc

6 0
3 years ago
Which quality allows programmers to modify a program as and when required
lana66690 [7]
I think Flexibility. Because when you are flexible you are able to manage things at anytime.
5 0
3 years ago
Read 2 more answers
Select the correct answer from each drop-down menu. Select the correct type of address reference.
Reika [66]

<u><em>[ Absolute ]</em></u><em> Addresses do not change if you copy them to a different cell.</em>

<u><em>[ Relative ]</em></u><em> </em><em>Addresses change depending on the cells you copy them to.</em>

8 0
3 years ago
Read 2 more answers
A(n) ______________ ________________ attack keeps the target so busy responding to a stream of automated requests that legitimat
Tanya [424]

Answer:

denial-of-service (DoS) attacks

Explanation:

8 0
3 years ago
Other questions:
  • In which area of engineering does material selection play a vital role a design optimization b forensic engineering c mechanical
    6·1 answer
  • Mike is reading about machine-dependent programming languages. Which languages are machine-dependent programming languages?
    11·1 answer
  • WHICH OF THE FOLLOWING LOOKS JUST LIKE A CD ROM BUT CAN STORE MUCH MORE INFORMATION
    8·2 answers
  • Good afternoon guys !!!
    14·2 answers
  • Which is a good guideline to follow when choosing a background for your slides?. . A. Use a different background for each slide.
    8·1 answer
  • True or false? A medical assistant can check for available exam rooms and providers using an electronic scheduling system.
    8·1 answer
  • ​open-source software is​ ________.
    11·1 answer
  • Give a recursive algorithm that takes as input a string s, removes the blank characters and reverses the string. For example, on
    7·1 answer
  • Write a 3-4 page paper (500-800 words) about your project that explains your project, the type of conditioning you used, and the
    10·1 answer
  • Write a two to three sentence response to the following question:
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!