|
Please read carefully the questions that follow. Do your research, write
up your responses (on your own computer) to at least one question
from 1~4 AND at least one question from 5~7. Support your answers
with examples, and post them into the current conference.
1. (a) Can you assign an int value to a double variable?
Yes; The value will widen during floating point calculations and
display as double unless explicitly displayed as an integer.
Fragment:
int x = 2
double z = 6
Output:
Calculated without forcing the output: 20.0
Explicit integer: 20
(b)Can you assign a double value to an int variable,
like in the code fragment below? What would happen if you try to compile
such a code fragment?
double
dVal = 15.9;
int
iVal = dVal;
No; The file will not compile and returns
an error. Loss of information and narrowing will occur because the
higher order bits will be removed if the conversion is allowed.
2. What is the output of the following code segment?
int x = 2;
double y = 6.6;
System.out.println( y / x );
System.out.println( (int) y /
x );
OUTPUT:
3.3
3
_ <--Cursor
This output occurs in the first
System.out.println () because during floating point math,
the int type is widened to a become a double
type number which is allowed in Java.
The second
System.out.println () forces double number of 6.6 to
the int number 6 and calculates using integer math, thereby
returning an integer number as seen above.
5. Write a program that prompts for and reads a floating-point number and
evaluates the polynomial 3x4-10x3+13. The program
should display both the number read and the results of evaluating the
polynomial. (See attached file below)
Right
click and Save Target As to download file
6. Write a program to compute the mass of a block of aluminum. The program
should input the dimensions of the block (i.e. length, width and height)
in centimeters. The density of aluminum is 2.7g/cm3.
7. Write a program that prompts for and reads your age in years and
outputs your age in days. Assume that there are 365 days in a year.
Send me email
Visit my website
|