Java Reference
In-Depth Information
etc., up to the seconds. Finally, report on the console the outcome based
on these boolean values.
Exercise 1.4 (From a linear to a quadratic solver)
We consider a linear equation
y
=
ax
+
b
where
a
and
b
are given
parameters (using
double
type). Write a program that calculates the
root of
ax
+
b
= 0. That is, the
y
-intercept of the line with the
x
-axis. How
to extend it to quadratic equations
ax
2
+
bx
+
c
= 0? Observe numerical
rounding phenomena that emphasize that roots slightly differ from their
mathematical roots because of finite precision of number representations.
Exercise 1.5 (Java syntax for constant declarations)
Explain why the program below does not compile. How should it be
modified in order to compile properly?
class
ConstantExercise
static final int
MAX = 10;
public static void
main( String
[ ]
args )
double
fi ;
static final double
GoldenRatio=(1.0d+Math. sqrt (5) ) /2.0;
for
(
int
i=0;i
<
MAX; i ++)
{
f i=GoldenRatio
∗
Math . cos ( 2 . 0
∗
Math . PI
∗
i/(
double
)MAX) ;
System . out . println (
"i="
+i+
" f(i)="
+fi ) ;
}
}
}
Search WWH ::
Custom Search