We'll continue working with ArrayLists and simple arrays this week as we try to keep track of some earthquakes in a fictitious region of the world. Because you've been doing a lot of practicing with writing informal test code in previous exercises we're going to leave it to you to follow the pattern from those files and do some informal testing of your own.
The EarthquakeRegion class will keep track of a collection of Earthquake objects. It will provide the ability to get some summary statistics on its quakes by providing the minimum, maximum, and average intensities, as well as providing the magnitude category number both numerically and graphically (as a histogram of *'s on the console).
Be sure to read through all items before you begin.
1. Download the Zipped started file from Moodle and extract its contents to a location where you'll be able to find it later.
2. Use Eclipse to open this Java project (if you've forgotten how to do this, refer to the earlier exercise that describes this process)
Figure: see image.
3. Rename the Eclipse Project to YourLastNameLab11.11b
4. Once opened, you'll find two (2) different class files. Please be aware that you will not make any changes within the Earthquake.java file.
5. The EarthquakeRegion class is present in the Starter file, but as you'll soon find out, it's empty. It will be up to you to implement the methods described here.
6. As you've been doing all along, you should be running informal tests on the new pieces of functionality as you implement them. Because we haven't given you the code, this will be your chance to create them from scratch. Simply add a new package called edu.westga.cs6311.earthquakes.tests. This package will contain a class named RegionTester which will add some Earthquake objects to the EarthquakeRegion and then test out the different methods as they are created. Be sure that this package has an RegionDriver class that defines the main method. Again, refer back to previous exercises where you created similar classes for informal testing.
7. Although you can complete the items in your EarthquakeRegion class in any order you like, I would recommend that you complete them as follows:
public void addEarthquake(Earthquake newEarthquake)
public String toString()
public double getMaximumMagnitude()
public double getMinimumMagnitude()
public double getAverageMagnitude()
private int[] calculateMagnitudeBreakdown()
Magnitude | Class |
< 5.0 | Light |
5.0 <= magnitude < 6.0 | Moderate |
6.0 <= magnitude < 7.0 | Strong |
7.0 <= magnitude < 8.0 | Major |
<= 8.0 | Great |
public String getCategoryBreakdown()
Light: 3
Moderate: 0
Strong: 1
Major: 2
Great: 1
private String makeStarRow(int number)
****
public String getMagnitudeHistogram()
***
*
**
*
Note: Your author shows example algorithms using the 'regular' for loop, however it is more appropriate to use the foreach ('enhanced for') loop when iterating through all elements in a collection like this. Therefore, we ask that any time you process all elements in an ArrayList or simple array, you please use the foreach loop because it makes the code easier to read, write, and debug. Thanks!
Earthquake at Kodiak Island, Alaska with magnitude: 7.9
Earthquake at Arequipa, Peru with magnitude: 7.1
Earthquake at Trion, Georgia with magnitude: 2.6
Earthquake at Amarillo, Texas with magnitude: 4.0
Earthquake at Tanana, Alaska with magnitude: 5.3
Should give minimum magnitude of: 2.6
Actual minimum magnitude: 2.6
Should give maximum magnitude of: 7.9
Actual maximum magnitude: 7.9
Should give average magnitude of: 5.38
Actual average magnitude: 5.38
Category Breakdown
Light: 2
Moderate: 1
Strong: 0
Major: 2
Great: 0
Histogram
**
*
**
Earthquake.java
package edu.westga.cs6311.earthquakes.model;
/**
* Manages information (name and wind speed) about a Hurricane
*
* **********************************************
* *** DO NOT MODIFY THE CODE IN THIS CLASS ***
* **********************************************
* @author CS6311
* @version Fall 2021
*
*/
public class Earthquake {
private String epicenter;
private double magnitude;
/**
* Initializes the epicenter and magnitude to the value of
* the corresponding parameters
* @param epicenter The Earthquake's epicenter
* @param magnitude The Earthquake's magnitude
*/
public Earthquake(String epicenter, double magnitude) {
this.epicenter = epicenter;
this.magnitude = magnitude;
}
/**
* Accessor for Earthquake's epicenter
*
* @return The Earthquake's epicenter
*/
public String getEpicenter() {
return this.epicenter;
}
/**
* Accessor for Earthquake's magnitude
*
* @return The Earthquake's magnitude
*/
public double getMagnitude() {
return this.magnitude;
}
/**
* Returns a String representation of the Earthquake
*
* @return A String representation of the Earthquake
* including its epicenter and magnitude
*/
public String toString() {
return "Earthquake at " + this.epicenter
+ " with magnitude: " + this.magnitude;
}
}
RegionTester.java
package edu.westga.cs6311.earthquakes.tests;
import edu.westga.cs6311.earthquakes.model.Earthquake;
import edu.westga.cs6311.earthquakes.model.EarthquakeRegion;
public class RegionTester {
// Test the earthquake region
public static void main(String[] args) {
EarthquakeRegion region = new EarthquakeRegion();
// Test the add
region.addEarthquake(new Earthquake("Kodiak Island", 7.9));
region.addEarthquake(new Earthquake("Arequipa, Peru", 7.1));
region.addEarthquake(new Earthquake("Trion, Georgia", 2.6));
region.addEarthquake(new Earthquake("Amarillo, Texas", 4.0));
region.addEarthquake(new Earthquake("Tanana, Alaska", 5.3));
// Test the to string
System.out.println(region);
System.out.println();
// Test the minimum magnitude
System.out.println("Should give minimum magnitude of: 2.6");
System.out.println("Actual minimum magnitude: " + region.getMinimumMagnitude());
System.out.println();
// Test the maximum magnitude
System.out.println("Should give maximum magnitude of: 7.9");
System.out.println("Actual maximum magnitude: " + region.getMaximumMagnitude());
System.out.println();
// Test the average magnitude
System.out.println("Should give average magnitude of: 5.38");
System.out.println("Actual average magnitude: " + String.format("%.2f", region.getAverageMagnitude()));
System.out.println();
// Display the category break down
System.out.println("Category Breakdown");
System.out.println(region.getCategoryBreakdown());
System.out.println();
// Test the histogram
System.out.println("Histogram");
System.out.println(region.getMagnitudeHistogram());
System.out.println();
}
}