CS 1063, Fall 2005
Earthquake class

The lectures for Week 11 had you work on an Earthquake class along with a EarthquakeTest class.

Question: If you keep all the elses below, could you delete the portions in blue below and have it still work correctly? answer.

File: Earthquake.java
// Earthquake: give effects at different richters
public class Earthquake {
   public double myRichter; // richter scale value
   
   // constructor
   public Earthquake(double richter) {
      myRichter = richter;
   }
   
   // method to set new value (similar to constructor)
   public void setMagnitude(double richter) {
      myRichter = richter;
   }
   
   // helper method, could call from outside
   public String getEffect() {
      String output = ""; // needed for nervous compiler
      // could leave off "else"s below
      if (myRichter >= 8.0) output = "Most structures fall";
      else if (myRichter >= 7.0 && myRichter < 8.0) 
         output = "Many buildings destroyed";
      else if (myRichter >= 6.0 && myRichter < 7.0) 
         output = "Many buildings considerably damaged; some collapse";
      else if (myRichter >= 4.5 && myRichter < 6.0) 
         output = "Damage to poorly constructed buildings";
      else if (myRichter >= 3.5 && myRichter < 4.5) 
         output = "Felt by many people, no destruction";
      else if (myRichter >= 0 && myRichter < 3.5) 
         output = "Generally not felt by people";
      else if (myRichter < 0) 
         output = "Negative numbers are not valid";
      return output;
   }
   
   // toString method, uses getEffect
   public String toString() {
      return "Richter: " + myRichter +
         ", effect: " + getEffect();
   }
}

Here is a test class.

File: EarthquakeTest.java
public class EarthquakeTest {
   public static void main(String[] args) {
      Earthquake e = new Earthquake(9.0);
      e.setMagnitude(8.0);
      System.out.println(e);
      e.setMagnitude(7.5);
      System.out.println(e);
      e.setMagnitude(7.0);
      System.out.println(e);
      e.setMagnitude(6.5);
      System.out.println(e);
      e.setMagnitude(6.0);
      System.out.println(e);
      e.setMagnitude(5.0);
      System.out.println(e);
      e.setMagnitude(4.5);
      System.out.println(e);
      e.setMagnitude(4.0);
      System.out.println(e);
      e.setMagnitude(3.5);
      System.out.println(e);
      e.setMagnitude(2.0);
      System.out.println(e);
      e.setMagnitude(0.0);
      System.out.println(e);
      e.setMagnitude(-1.0);
      System.out.println(e);
   }
}

Here is the resulting output.

Results of a run
Richter: 8.0, effect: Most structures fall
Richter: 7.5, effect: Many buildings destroyed
Richter: 7.0, effect: Many buildings destroyed
Richter: 6.5, effect: Many buildings considerably damaged; some collapse
Richter: 6.0, effect: Many buildings considerably damaged; some collapse
Richter: 5.0, effect: Damage to poorly constructed buildings
Richter: 4.5, effect: Damage to poorly constructed buildings
Richter: 4.0, effect: Felt by many people, no destruction
Richter: 3.5, effect: Felt by many people, no destruction
Richter: 2.0, effect: Generally not felt by people
Richter: 0.0, effect: Generally not felt by people
Richter: -1.0, effect: Negative numbers are not valid