Objectives

  • Define a class that uses a simple array and an ArrayList of objects
  • Implement common ArrayList algorithms to perform a given task

Overview

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 Problem Statement EarthquakeRegion

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:

  • Declare an instance variable for the ArrayList of Earthquak objects
  • Write a 0parameter constructor that will be used to instantiate the instance variable
  • Write a method with the header:
public void addEarthquake(Earthquake newEarthquake)
  • This method will contain code that checks to be sure the EarthQuake parameter is not storing null (if it is, then this method will just return without anything being added). All nonnull Earthquake objects will be added to the collection of Earthquakes.
  • Write a method with the header:
public String toString()
  • This method will contain code to return a String representation of the EarthquakeRegion object. Note that to do this, you'll want a String that lists out a complete String representation of each Earthquake object (hint: call the Earthquake's toString method). To make things easier to read, be sure that each Earthquake is listed on their own line in the String (hint 2: You'll want to make use of the newline character '\n' as you've seen done before).
  • Write a method with the header:
public double getMaximumMagnitude()
  • This method will return the magnitude by any of the Earthquakes in the region. Note that the algorithm for finding the maximum value is listed in the textbook.
  • Write a method with the header:
public double getMinimumMagnitude()
  • This method will return the mangitude by any of the Earthquakes in the region. Note that the algorithm for finding the minimum value is listed in the textbook.
  • Write a method with the header:
public double getAverageMagnitude()
  • This method will calculate and return the average magnitude for all Earthquakes in the collection.
  • Write a method with the header:
private int[] calculateMagnitudeBreakdown()
  • Please be aware that this is a helper method and therefore is declared as private. This means that you won't be able to directly test this method (because you can't call private methods from your tester class).
  • This method will return a simple, 5element array with the counts for each of the Earthquake Magnitude Classes based on the following scale:

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

  • The idea is that the first element (with index 0) will store the number of Light earthquakes, the second element (with index 1) will store the number of Moderate earthquakes, etc.
  • This method will need to declare the array variable, then process each Earthquake in the collection to get its magnitude. Based on that value, the code will increment the count inside the appropriate element. For example, if the first Earthquake's magnitude is 4.9, then the code will need to increment the value stored in array element 0.
  • Once all of the Earthquake magnitudes have been processed, this method will return the array holding the category counts.
  • Write a method with the header:
public String getCategoryBreakdown()
  • This method will return a String that lists the category number and the count of earthquakes with magnitudes in that category. For example, if the category breakdown was 3 Light earthquakes, 0 Moderate, 1 Strong, 2 Major, and 1 Great, then the String would be:
Light: 3
Moderate: 0
Strong: 1
Major: 2
Great: 1
  • Your String must match this format exactly (but obviously the counts will not always be the same). Sample output is included at the end of this document.
  • Hint: You should make use of the private helper method to do some of the work here.
  • Write a method with the header:
private String makeStarRow(int number)
  • This method will accept a number of stars (*) to be created, create a String with that number of *'s sidebyside, and then return that String. For example, if this method is called and passed the value 4, then the resulting String would be:
****
  • Write a method with the header:
public String getMagnitudeHistogram()
  • This method will return a String holding a horizontal histogram where each category will be displayed on its own line with an * for every earthquake with that classification. For example, if the category breakdown was 3 Light, 0 Moderate, 1 Strong, 2 Major, and 1 Great, then the String would be:
***

*
**
*
  • Your String must match this format exactly (but obviously the number of *'s will not always be the same). Again, sample output is included at the end of this document.

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!

Sample Output

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
**
*

**

Starter Code

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();
}
}
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.