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
Tanner is creating a brochure for his Web-hosting company. He designed the layout and placed a picture in the center of the page
Romashka [77]
Resizing is a very simple process. On the photo editing program, simply click on the image, and if resize handles appear around it, click and drag those to the desired size. If the program has a dedicated resizing or rescaling tool, use it and follow the above instructions. Keep the photo proportionate by holding down the shift key while dragging a corner handle. <span>Tanner has to make sure that his photo his of a high enough resolution to resize. A pixellated or blurry photo will only get worse when scaled up. He also has to make sure he doesn't resize it to be too big or it will also end up pixellated.</span>
5 0
4 years ago
What features are needed in a mobile to make it a business solution?
ad-work [718]

Answer:

Connectivity, Security, Productivity

Hope this helps;)

6 0
3 years ago
What can you search on Microsoft Word or Powerpoint Clipart or Google (Clip art) to find more pictures like the ones below but w
elena-14-01-66 [18.8K]
Well if you put sad/mad/scared/tired/ect it should pop up with the same person.
3 0
3 years ago
Which access database object asks a question about the data stored in a database and displays specific fields and records that a
elena-14-01-66 [18.8K]
The Query object is used to asks a question about the data stored in a database and displays the specific fields and records that answer the question.

In code form, the select command is used for queries. 
6 0
3 years ago
Write a computer program to tell if the number is even or odd please
vovikov84 [41]
There you go :) Written in C# you can add using tags yourself I believe

3 0
3 years ago
Other questions:
  • Device that converts sound into electrical signals, which are sent to the computer or other recording device
    6·1 answer
  • Jordan has been asked to help his company find a way to allow all the workers to log on to computers securely but without using
    6·1 answer
  • How does climate research depend on digital technology
    12·1 answer
  • The Polish mathematician Wacław Sierpiński described the pattern in 1915, but it has appeared in Italian art since the 13th cent
    8·1 answer
  • When public relations professionals make provocative statements in newsgroups to get people to visit an organization's website o
    10·1 answer
  • Please fill these out. I really need this done.
    5·1 answer
  • Project: Math Tutor Program with Error Handling
    9·2 answers
  • Jenny needs to record the names of 30 students, write down the subjects they studied, and note their grades in each subject afte
    8·1 answer
  • Many large companies use the word(s), _____, to refer to the huge network of computers that meets their diverse computing needs.
    12·1 answer
  • One of the most notable impacts of IT on business is improved
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!