AHsd - Important Legal
AHsd - Important Legal
AHsd - Important Legal
FV = PV . (1 + INT / 100)YRS
c = (a2 + b2 - 2abcosγ)1/2
volume = Math.PI * r * r * h;
R4.4. Give an example of integer overflow. Would the same example work correctly if
you used floating-point? Give an example of a floating-point roundoff error.
Would the same example work correctly if you used integers? For this exercise,
you should assume that the values are represented in a sufficiently small unit,
such as cents instead of dollars, so that the values don’t have a fractional part.
R4.7. Let n be an integer and x a floating-point number Explain the difference between
n = (int) (x + 0.5);
and
n = (int) Math.round(x);
For what values of x do they give the same results? For what values of x do they
give different results?
R4.8. Explain the difference between 2, 2.0, ‘2’, “2”, and “2.0”?
R4.9. Explain what each of the following two programs segments computes:
x = 2;
y = x + x;
and
s = “2”;
t = s + s;
R4.10. Uninitialized variables can be a serious problem. Should you always initialize
every variable with zero? Explain the advantages and disadvantages of such a
strategy.
R4.13. How do you get the last digit of an integer? The first digit? That is, if n is 23456,
how do you find out that the first digit is 2 and the last digit is 6? Do not convert
the number to a string. Hint: %, Math.log.
R4.14. This chapter contains several recommendations regarding variable and constants
that make programs easier to read and maintain. Summarize these
recommendations.
R4.15. What is a final variable? Can you define a final variable without supplying
its value? (Try it out.)
R4.16. What are the values of the following expressions? In each line, assume that.
double x = 2.5;
double y = -1.5;
int m = 18;
int n = 4;
String s = “Hello”;
String t = “World”;
a. x + n * y – (x + n) * y
b. m/n+m%n
c. 5*x–n/5
d. Math.sqrt(Math.sqrt(n))
e. (int) Math.round(x)
g. s+t
h. s+n
i. 1 – (1 – (1 – (1 – n))))
j. s .substring(1, 3)
k. s.length( ) + t.length( )