Question
Get Familiar with the Problem Carefully read the program description and look at the data file to gain an understanding of what is to be
- Get Familiar with the Problem
Carefully read the program description and look at the data file to gain an understanding of what is to be done. Make sure you are clear on what is to be calculated and how. That is, study the file and program description and ponder!
Think!
- The Storm Class Type
Now concentrate on the Storm class that we define to hold the summary information for one storm.
- First, look at the definition of Storm in the Storm.java file.
- You must complete the methods in the Storm class and use them effectively throughout your program.
- Note: The SaffirSimpson() method uses wind and pressure to calculate the category of a Storm.
- Whenever you update wind, you must convert it from knots (how it is stored in the data file) to miles per hour.
- The SaffirSimpson() and toString() methods have been written for you and do not need any further coding. The toString() method might need some tweaking for alignment purposes.
- The GetStorm() Function
This is the most challenging method in the program! Here are some points to consider as you try to design this function.
- Take another look at the data file - and notice that typically there are several records associated with one storm. It will be the job of GetStorm() to process these records, and build a Storm object, containing the relevant summary information.
- Work out the while loop needed to process the correct records. For example, process records until the sequence number changes, or ....
- Remember that this method returns a Storm object. It will have to use the new operator to allocate memory for this object.
- Add the sort function
Use the selection sort algorithm to sort the Storms. Remember, we are sorting the array of Storms but the key is the Storm category.
- Final cleanup and testing
Clean up any small bugs and add comments for documentation.
First Starter File
/* * Class: Storm.java * Purpose: Store hurricane data for individual storm objects */
public class Storm { private final double KnotsToMPH = 1.15;
private String stormID; private int beginDate; private int duration; private String name; private int category; private int wind; private int pressure;
public Storm( String stormid, int bdate, int dur, String sname, int w, int p ) {
}
public void setDuration( int d ) {
}
public void setWind( int w ) {
}
public void setPressure( int p ) {
}
public void SaffirSimpson() { // Compute storm category, using the Saffir-Simpson scale if(pressure <= 920 && wind >= 156) { category = 5; // Category 5 } if(pressure > 920 && wind < 156) { category = 4; // Category 4 } if(pressure > 945 && wind < 113) { category = 3; // Category 3 } if(pressure > 965 && wind < 96) { category = 2; // Category 2 } if(pressure > 980 && wind < 83) { category = 1; // Category 1 } if(wind < 64) { category = -1; // Tropical Storm } if(wind < 34) { category = -2; // Tropical Depression } if(pressure == 0) { category = 0; // Missing pressure } }
public int getCategory() {
}
public String toString() { return String.format("%s %9d %7d %-10s %2d %6d %8d ", stormID, beginDate, duration, name, category, wind, pressure); }
} Second starter File
/* * Class: StormChaser.java * Purpose: Main program to read hurricane data file, create Storm objects, * and keep those that are category 3 or higher. */
import java.io.*; import java.util.Scanner;
public class StormChaser { public static void main(String[] args) { // Constants final int MAX_STORMS = 300;
// array of Storms Storm CurrentStorm; // storm returned by GetStorm int nStorms = 0; // number in array List int totalStorms = 0; // total number of storms in the input file Scanner fileInput; // Openning hurricane data file try{ System.out.println("Openning hurricane data file..."); fileInput = new Scanner(new File("hurricanedata1950to2015.txt")); } catch(FileNotFoundException e){ System.err.println("FileNotFoundException: " + e.getMessage()); return; } System.out.println( "File opened successfully..."); System.out.println( "Reading file..." ); // Read Storm data from file until EOF while( ) { CurrentStorm = GetStorm(fileInput); ++totalStorms; // If Storm i category 3 or higher, add to the array if( ) { nStorms++; } } System.out.println( "Number of storms: " + totalStorms ); System.out.println( "Hurricanes with category 3 and above: " + nStorms ); DisplayStorms( "First Twenty Storms", List, 20 ); Sort( List, nStorms ); DisplayStorms( "Top Twenty Storms", List, 20 ); fileInput.close(); }
public static Storm GetStorm( Scanner in ) { // Create constants as array indexes for data elements // Declare variables Storm NewStorm; // Read the Storm header information // Tokenize the header
// Read first row of Storm data // Tokenize the Storm data String[] dataElements = data.split(","); // Extract the data elements beginDate = Integer.parseInt(dataElements[BEGINDATE].trim()); wind = Integer.parseInt(dataElements[WIND].trim()); pressure = Integer.parseInt(dataElements[PRESSURE].trim()); duration = 6; // Create Storm object NewStorm = new Storm(stormid, beginDate, duration, name, wind, pressure); for( int i = 1; i < ; i++ ) { // Read next row of Storm data data = in.nextLine(); // Tokenize the Storm data dataElements = data.split(","); // Extract the data elements // Update Storm object } // Return the new storm object return NewStorm; }
public static void DisplayStorms( String title, Storm[] List, int NStorms ) { // display NStorms storms // print some title and column headings System.out.println(title); System.out.println( " Begin Duration Maximum Minimum "); System.out.println( "Storm ID Date (hours) Name Category Winds (mph) Press. (mb)"); System.out.println( "-------------------------------------------------------------------------"); for( int k = 0; k < NStorms; k++ ) { // Print out one Storm } System.out.println (" "); }
public static void Sort( Storm StormList[], int n ) { // bubble sort the list of Storms int pass = 0, k, switches; Storm temp; switches = 1; while( switches != 0 ) { switches = 0; pass++; for( k = 0; k < n - pass; k++ ) { if( StormList[k].getCategory() < StormList[k+1].getCategory() ) { temp = StormList[k]; StormList[k] = StormList[k+1]; StormList[k+1] = temp; switches = 1; } } } } }
please keep the notes provided in the code, thank you
and add notes to things you have changed
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started