Java Reference
In-Depth Information
SR 5.11
Based on the given assumptions, the output would be:
a.
red orange white yellow
b.
black blue green
c.
yellow green
SR 5.12
if
(temperature <= 50)
{
System.out.println (
"
It is cool.
"
);
System.out.println (
"
Dress warmly.
"
);
}
else
if
(temperature > 80)
{
System.out.println (
"
It is warm.
"
);
System.out.println (
"
Dress cooly.
"
);
}
else
{
System.out.println ("It is pleasant.");
System.out.println ("Dress pleasantly.");
}
5.3
Comparing Data
SR 5.13
Because they are stored internally as binary numbers, comparing float-
ing point values for exact equality will be true only if they are the same
bit-by-bit. It's better to use a reasonable tolerance value and consider
the difference between the two values.
SR 5.14
We compare strings for equality using the
equals
method of the
String
class, which returns a boolean result. The
compareTo
method of the
String
class can also be used to compare strings. It returns a positive, 0, or nega-
tive integer result depending on the relationship between the two strings.
SR 5.15
//------------------------------------------------------
// Returns true if this Die equals die, otherwise
// returns false.
//------------------------------------------------------
public boolean
equals(Die die)
{
return
(
this
.faceValue == die.faceValue);
}
SR 5.16
if
(s1.compareTo(s2) < 0)
System.out.println (s1 +
"
\n
"
+ s2);
else
System.out.println (s2 +
"
\n
"
+ s1);
Search WWH ::
Custom Search