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
What new deal programs were created to build dams to control flooding and generate electric power?
rewona [7]
<span>TVA (Tennessee Valley Authority).

</span><span>Recovery-1933 program created to provide jobs. Built dams in order to generate cheap electricity and control flooding in TN, GA, AL, and KY. Sparked controversy because companies that provided electricity would now have to compete with the government. Eliminated flooding, gave thousands electricity for the first time, improved transportation and forced 15,000 families to move.</span>
3 0
3 years ago
Blood Alcohol Level (BAL) is the ratio between the alcohol consumed and the blood in the body. A. True B. False
horsena [70]
I think its true I'm not sure
3 0
3 years ago
Read 2 more answers
Cookies are to improve the access of internet information. Therefore for the sake of you own privacy and security you should not
lina2011 [118]
The answer is a. True
3 0
3 years ago
Read 2 more answers
Exchanging which type of data uses the least bandwidth?
Over [174]
Voice. voice uses the least bandwidth
5 0
3 years ago
Read 2 more answers
Write the definition of a class Phone containing: 1. A data member named model of type string. 2. A data member named partNumber
Nady [450]

Answer:

class Phone(object):

   def __init__(self, model, partNumber, retailPrice):

       self.model = model

       self.part_number = partNumber

       self.retail_price = retailPrice

   def phone_specs(self):

       print( "Phone model: {}\nPart number: {}\nRetail price: {}".format( self.model, self.part_number, self.retail_price))

phone1 = Phone("Nokia", "asd234", 200.0)

phone1.phone_specs()

Explanation:

A class is a blueprint of a data structure used to create objects of the same data types and methods. The Phone class is an object that creates an instance of the phone1 object. The phone_specs method is used to display the details of the phone1 object.

4 0
3 years ago
Other questions:
  • What are the 7 basic components found in a computer tower
    5·2 answers
  • Customer Premises Equipment (CPE) includes all devices connected to the PSTN, where the ownership and the responsibility for mai
    15·1 answer
  • _____ are considered to be the most important element in a computer-based information system.
    8·1 answer
  • (20 POINTS AND BAINLIEST)
    9·1 answer
  • Please as soon as possible
    13·1 answer
  • How do u type faster
    5·1 answer
  • Create a file named homework_instructions.txt using VI editor and type in it all the submission instructions from page1 of this
    7·1 answer
  • Define a function in Scheme (or relation in Prolog) that checks whether a set of elements (represented as a list) is a subset of
    10·1 answer
  • What can I do If my Texas Instrument TI-84 calculator is not giving accurate answers?
    6·1 answer
  • 30 POINTS FOR THE CORRECT ANSWERS
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!