Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

public class StormChaser { public static void main ( String [ ] args ) { / / Constants final int MAX _ STORMS = 3

public class StormChaser {
public static void main(String[] args)
{
//Constants
final int MAX_STORMS =300;
Storm[] List = new Storm[MAX_STORMS]; // array of storms
Storm CurrentStorm; // storm returned by GetStorm
int NStorms =0; // number of 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 file until EOF
while( fileInput.hasNextLine())
{
CurrentStorm = GetStorm(fileInput);
Total++;
if(CurrentStorm.getCategory()>=3)
{
List[NStorms++]= CurrentStorm;
}
}
System.out.println("Number of Storms:" + Total).
System.out.println("Hurricanes with category 3 and above: "+ NStorms );
DisplayStorms("First Ten Storms", List, 10);
Sort(List, NStorms);
DisplayStorms ("Top Twenty Storms", List, 20);
fileInput.close();
}
public static Storm GetStorm( Scanner in )
{
// Build a Storm object and return it
int year =0, month =0, day =0, hour =0;
int sequence =0, wind =0, pressure =0;
String name;
int current =0, beginDate =0, duration =0;
int skip =0;
double junk =0.0;
Storm NewStorm;
// Read next record
year = in.nextInt();
month = in.nextInt();
day = in.nextInt();
skip = in.nextInt();
sequence = in.nextInt();
name = in.next();
junk = in.nextDouble();
junk = in.nextDouble();
wind = in.nextInt();
pressure = in.nextInt();
// Make a storm object and initialize 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() && current == sequence )
{
// update storm info
duration +=6;
NewStorm.setDuration(duration);
NewStorm.setWind(wind);
NewStorm.setPressure(pressure);
// get next record
year = in.nextInt();
month = in.nextInt();
day = in.nextInt();
skip = in.nextInt();
sequence = in.nextInt();
name = in.next();
junk = in.nextDouble();
junk = in.nextDouble();
wind = in.nextInt();
pressure = in.nextInt();
}
//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.print(List[k].toString());
System.out.println(title +"
");
}
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;
}
}
}
}
}how to resolve NullPointer in java with the upper code

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_2

Step: 3

blur-text-image_3

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

Essentials of Database Management

Authors: Jeffrey A. Hoffer, Heikki Topi, Ramesh Venkataraman

1st edition

133405680, 9780133547702 , 978-0133405682

More Books

Students also viewed these Databases questions