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]
4 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]4 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
__________ has become a widely accepted evaluation standard for training and education related to the security of information sy
kirill115 [55]
NSTISSI NO 4011 has become a widely accepted evaluation standard for training and education related to the security of information system. The standard is directed at information security professionals who are experts in identifying system vulnerabilities, investigating system security, documenting, analyzing and evaluating system security technology policies.<span />
4 0
3 years ago
When assigning a value to a string, which of the following rules needs to be followed?
Alecsey [184]

Answer:

Let's say you need to remember some information, the way moviegoers tried to remember the name of the movie. Instead of storing it in your human memory, you can store information in your computer's memory using Python. This is called assigning a string value to a variable.

To assign a string value to a variable in Python, follow this example. Select each part of the code to see how it works with the movie title example.

movieTitle = "Live. Die. Repeat."

This line of Python code is an example of an assignment statement. In an assignment statement, you tell Python, "This variable is assigned this string value."

Explanation:

4 0
3 years ago
Yo, my Lenovo laptop keeps showing this screen but I can't sign in, can someone help me?
rusak2 [61]
Break your laptop and get a new one
8 0
3 years ago
Read 2 more answers
What statement best describes Konrad Zuse?
vazorg [7]

Answer:

D

Explanation:

Konrad Zuse was a German civil engineer, inventor and computer pioneer. His greatest achievement was the world's first programmable computer; the functional program-controlled Turing-complete Z3 became operational in May 1941.

8 0
4 years ago
Read 2 more answers
In the electric series, which one of the following materials is the most negatively charged? A. Silk B. Sealing wax C. Teflon D.
Snowcat [4.5K]
C) Teflon (:


Good luck!
7 0
4 years ago
Read 2 more answers
Other questions:
  • ll of the following are extra precautions you can take to secure a wireless network EXCEPT ________. Select one: A. turn on secu
    9·1 answer
  • You have just replaced the NIC on a server, making no other changes. It can communicate with all the computers on its network bu
    14·1 answer
  • How do you create a reference page in apa format with all websites?
    5·1 answer
  • owen works in a real-estate office. A contract needs to be signed but the client is out of town. What should Owen do?
    14·1 answer
  • When adding a background to a Web page, it can be _____. a. a solid color b. a fill effect c. an image d. all of the above
    12·1 answer
  • Explain the difference between the legal protections a security officer enjoys and the legal protections a police officer receiv
    12·2 answers
  • Which of the following does Secure Sockets Layer (SSL) use to authenticate a user or system before encrypting a session?
    14·1 answer
  • During the data transmission there are chances that the data bits in the frame might get corrupted. This will require the sender
    6·1 answer
  • In cells D6 through D8, enter formulas to calculate the values of the stocks. The formulas should multiply the number of shares
    11·1 answer
  • 1. The running configuration is also known as the _____________ (Select Two) a. Startup config b. Working configuration c. Curre
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!