Copy and paste the following Java program into a text editor

Copy, and paste the following Java program into a text editor.

a) Save your code as \"twoandonehalf.java\", compile and run the program. The programmer wanted to see numbers that were computed to full precision. What happened?

b) The Java compiler added something in line 2. Try to reverse that without changing the type of x, y, or z.

c) Now, replace line 1 with the following definition.

Evidently, the value of z is not 10 times the value of x. What is the cause of the problem?

Solution

a) As the variable r is delcared as int,the resul though would be a float value would be truncated to int. Hence y will be truncated,y will b etruncated and hence finally z value

b)Withoit changing the type of variable, to rverse the chnage compiler has done, we need to do type-casting.

Hence change line to

float x = (float)y/2;       // line 2

c)It won\'t be 10 times because z will only hold the int part of (10*x)

Copy, and paste the following Java program into a text editor. a) Save your code as \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site