Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java Program to sort storms: LAST STEP THAT NEEDS TO BE ADDED: *** Add the sort function. Use the selection sort algorithm to sort the

Java Program to sort storms:

LAST STEP THAT NEEDS TO BE ADDED:

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

PROGRAM BELOW:

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[] List = new Storm[MAX_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( fileInput.hasNextLine () ) { CurrentStorm = GetStorm(fileInput); ++totalStorms; // If Storm i category 3 or higher, add to the array if( CurrentStorm.getCategory () >=3 ) { List[nStorms]=CurrentStorm; 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 final int STORMID = 0; final int NAME = 1; final int MAXRECORDS = 2; final int BEGINDATE = 0; final int WIND = 6; final int PRESSURE = 7; // Declare variables int wind = 0, pressure = 0, maxRecords =0; int beginDate = 0, duration = 0; String name; String stormid; String data; Storm NewStorm; // Read the Storm header information String header = in.nextLine(); // Tokenize the header String[] headerElements = header.split(","); stormid = headerElements[STORMID]; name = headerElements[NAME].trim(); maxRecords = Integer.parseInt(headerElements[MAXRECORDS].trim()); // Read first row of Storm data data = in.nextLine(); // 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 < maxRecords ; i++ ) { // Read next row of Storm data data = in.nextLine(); duration +=6; // Tokenize the Storm data dataElements = data.split(","); // Extract the data elements wind = Integer.parseInt(dataElements [WIND].trim()); pressure = Integer.parseInt(dataElements [PRESSURE].trim ()); // Update Storm object NewStorm.setDuration(duration); NewStorm.setWind(wind); NewStorm.setPressure (pressure); } // 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.print(List [k].toString()); } 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; } } } } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions