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
_____________________________________________________is a pre-designed format to help users with the creation of a document, lik
Naily [24]
C template......................................
8 0
2 years ago
You are a software engineer at a company where management routinely encourages you and your colleagues to use pirated software.
s2008m [1.1K]

Answer:

That’s highly illegal. You should notify them that it is illegal. And, if they continue, you should notify the legal team. If it continues, you either need to leave the company or notify the proper authorities.

7 0
2 years ago
Write a chemical reaction to show that nitric acid contains nitrogen​
sleet_krkn [62]

Answer:

HNO3 &NO2

Explanation:

7 0
3 years ago
Which of the following code correctly registers a handler with a button btOK?a. btOK.setOnAction(e -> System.out.println("Han
Bad White [126]

Answer:

The correct answer is C:

btOK.setOnAction((ActionEvent e) -> System.out.println("Handle the event"));

Explanation:

The button produces an action once clicked. The setOnAction method indicates what will happen when the button is clicked. Action Event is a type of event that gets processed by EventHandler, which provides the action to be performed by clicking the button (printing out "Handle the event").

4 0
3 years ago
Keisha wants to change the default printer before printing an e-mail. Which steps will accomplish this task?
Angelina_Jolie [31]

Answer:

The steps that will accomplish the task are;

On the File tab, clicking Print and choosing the desired printer from the Printer drop-down list

Explanation:

In steps required to change the default printer on Microsoft Outlook before printing as e-mail are as follows;

1) Select the File tab on the Menu bar

2) Click on the Print option on the File's Menu

3) From the Print window displayed under the File Menu in Microsoft Outlook select the desired printer from the Printer options drop-down list

The steps that will accomplish the task are On the File tab, clicking Print and choosing the desired printer from the Printer drop-down list.

8 0
3 years ago
Other questions:
  • Plz help ASAP
    10·1 answer
  • Blank determines the overall brightness or darkness of an entire image
    5·1 answer
  • What should be done with statements or sections which are unclear?
    12·2 answers
  • In general, digital to analog modulation equipment is less expensive than the equipment for encoding digital data into a digital
    8·1 answer
  • You are almost finished updating a Website. As part of the update, you have converted all pages from HTML 4.0 to HTML5. The proj
    7·1 answer
  • Write any four difference between email and effects​
    14·1 answer
  • In this exercise, you will get some practice with the __add__ method by implementing it for a class called ContactBook. This cla
    12·1 answer
  • What are 3 software programs for mobile computing?
    10·1 answer
  • I have this questions i need to make it in a report format pages of atleast 3 pages and maximum of 5 pages​
    7·1 answer
  • Need help ASAP.<br> I am so lost.
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!