Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVA I have most of the project done, there a few areas in the stormchaser class that I can't figure out. Such as the //extract

JAVA

I have most of the project done, there a few areas in the stormchaser class that I can't figure out. Such as the //extract data elements and //print out one storm. I don't know if you need the text file for the hurricane data, but if you do, it can be found here. https://www.dropbox.com/s/mng2lk0biphvcd4/hurricanedata1950to2015.txt?dl=0

PLEASE HELP!

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.

What to Hand In

Submit your code and screen output by the due date.

Project Files

The relevant files for this project are:

StormChaser.java (program starter file)

Storm.java (Storm class definition)

hurricanedata1950to2015.txt (data file)

WHAT I HAVE SO FAR (Starter files)

/*

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

{

stormID = stormid;

beginDate = bdate;

duration = dur;

name = sname;

wind = w;

pressure = p;

SaffirSimpson();

}

public void setDuration( int d )

{

duration = d;

}

public void setWind( int w )

{

w = (int)(w * KnotsToMPH);

if (w > wind)

wind = w;

}

public void setPressure( int p )

{

if(pressure <= 0)

pressure = p;

if(p > 0 && p < pressure)

pressure = p;

}

private 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()

{

return category;

}

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[] 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 BEGINDATE = 0;

final int STORMID = 0;

final int NAME = 1;

final int MAXRECORDS = 2;

final int WIND = 6;

final int PRESSURE = 7;

// Declare variables

int wind = 0;

int pressure = 0;

int beginDate = 0;

int duration = 0;

Storm NewStorm;

// Read the Storm header information

String header = in.nextLine();

// Tokenize the header

String[] headerElements = header.split(",");

String stormid = headerElements[STORMID].trim();

String name = headerElements[NAME].trim();

int maxRecords = Integer.parseInt(headerElements[MAXRECORDS].trim());

// Read first row of Storm data

String 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();

// Tokenize the Storm data

dataElements = data.split(",");

// Extract the data elements

// Update Storm object

duration += 6;

NewStorm.setDuration(duration);

NewStorm.setPressure(pressure);

NewStorm.setWind(wind);

}

// 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;

}

}

}

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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

Recommended Textbook for

Data Mining Concepts And Techniques

Authors: Jiawei Han, Micheline Kamber, Jian Pei

3rd Edition

0123814790, 9780123814791

More Books

Students also viewed these Databases questions

Question

How can evaluation of LMD become more than an act of faith?

Answered: 1 week ago