Question
Write a JAVA program to include the following: 1. Get Familiar with the Problem Carefully read the program description and look at the data file
Write a JAVA program to include the following:
1. 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!
2. The Storm Class Type
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 print() methods have been written for you and do not need any further coding. The print() method might need some tweaking for alignment purposes.
3. The GetStorm() Function
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.
4. 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.
5. Be sure to use comments!
YOU MUST USE THESE STARTER CODES:
First starter code:
public class Storm {
private final double KnotsToMPH = 1.15;
// global user-defined types:
private int beginDate;
private int duration;
private String name;
private int category;
private int wind;
private int pressure;
public Storm( 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("%9d %8d %10s %4d %9d %10 ", beginDate, duration, name, category, wind, pressure);
}
}
Second starter code:
import java.io.*;
import java.util.Scanner;
public class StormChaser {
public static void main(String[] args)
{
// Constants
final int MAX_STORMS = 200;
Storm[MAX_STORMS] List; // array of Storms
Storm CurrentStorm; // storm returned by GetStorm
int NStorms = 0; // number in array List
int Total = 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("hurricane.data"));
}
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( )
{
++Total;
if( CurrentStorm.getCategory() >= 3 )
{
List[NStorms++] = CurrentStorm;
}
}
System.out.println( "Number of storms: " );
System.out.println( "Hurricanes with category 3 and above: " + NStorms );
DisplayStorms( "First Ten Storms", List, 10 );
Sort( List, NStorms );
DisplayStorms( "Top Ten Storms", List, 10 );
fileInput.close();
}
public static Storm GetStorm( Scanner in )
{
// Build a Storm object and return it
int year = 0, month, day, hour, sequence, wind, pressure;
String name;
int current, beginDate, duration = 0;
Storm NewStorm;
// Check for end of file
if( !in.hasNextLine() )
{
NewStorm = new Storm(beginDate, duration, name, wind, pressure);
return NewStorm;
}
// Read next record.
// Make a storm object and initialize it with info from the current record
beginDate = year * 10000 + month * 100 + day;
duration = 6;
NewStorm = new Storm(beginDate, duration, name, wind, pressure);
current = sequence;
while( in.hasNextLine() && )
{
//update storm info
//get next record
}
// and 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 Date Duration Name Category Maximum Minimum");
System.out.println(" (hours) Winds (mph) Press. (mb)");
System.out.println("----------------------------------------------------------------");
for( int k = 0; k < NStorms; k++ )
System.out.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
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