Java Reference
In-Depth Information
5.3 Multiple Alternatives
5.3.1 Sequences of Comparisons
Many computations require more than a single if/else decision. Sometimes, you
need to make a series of related comparisons.
The following program asks for a value describing the magnitude of an earthquake
on the Richter scale and prints a description of the likely impact of the quake. The
Richter scale is a measurement for the strength of an earthquake. Every step in the
scale, for example from 6.0 to 7.0, signifies a tenfold increase in the strength of the
quake. The 1989 Loma Prieta earthquake that damaged the Bay Bridge in San
Francisco and destroyed many buildings in several Bay area cities registered 7.1 on
the Richter scale.
Multiple conditions can be combined to evaluate complex decisions. The correct
arrangement depends on the logic of the problem to be solved.
ch05/quake/Earthquake.java
1 /**
2 A class that describes the effects of an earthquake.
3 */
4 public class Earthquake
5 {
6 /**
7 Constructs an Earthquake object.
8 @param magnitude the magnitude on the Richter
scale
9 */
10 public Earthquake(double magnitude)
11 {
12 richter = magnitude;
13 }
14
15 /**
16 Gets a description of the effect of the earthquake.
17 @return the description of the effect
193
194
Search WWH ::




Custom Search