Question

Write in JAVA Get Familiar with the Problem Carefully read the program description and look at...

Write in JAVA

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)

MUST USE THESE 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 )

{

}

public void setPressure( int 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\n",

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 ("\n");

}

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;

}

}

}

}

}

hurricanedata1950to2015.txt

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.io.*;
import java.util.Scanner;

public class StormChaser {
    public static void main(String[] args)
    {
        final int MAX_STORMS = 200;

        Storm[] List = new Storm[MAX_STORMS];
        Storm CurrentStorm;    
        int NStorms = 0;       
        int Total = 0;         
        Scanner fileInput;

    
        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 != MAX_STORMS )
        {
            CurrentStorm = GetStorm(fileInput);
            ++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 )
    {

        int year = 0, month, day, hour, sequence, wind = 0, pressure = 0;
        String name = "";
        int current = 0, beginDate = 0, duration = 0;
        Storm NewStorm;
        double junk = 0.0;

     
        if( !in.hasNextLine() )
        {
            NewStorm = new Storm(beginDate, duration, name, wind, pressure);
            return NewStorm;
        }
      
        year = in.nextInt();
        month = in.nextInt();
        day = in.nextInt();
        hour = in.nextInt();
        sequence = in.nextInt();
        name = in.next();
        junk = in.nextDouble();   
        junk = in.nextDouble();   
        wind = in.nextInt();
        pressure = in.nextInt();


      
        beginDate = year * 10000 + month * 100 + day;
        duration = 0;
        NewStorm = new Storm(beginDate, duration, name, wind, pressure);
        current = sequence;


        while( in.hasNextLine() && sequence == current )
        {
          
            duration += 6;
            NewStorm.setDuration(duration);
            NewStorm.setWind(wind);
            NewStorm.setPressure(pressure);


            //get next record
            year = in.nextInt();
            month = in.nextInt();
            day = in.nextInt();
            hour = in.nextInt();
            sequence = in.nextInt();
            name = in.next();
            junk = in.nextDouble();  
            junk = in.nextDouble();   
            wind = in.nextInt();
            pressure = in.nextInt();

        }

     
        return NewStorm;
    }

    public static void DisplayStorms( String title, Storm[] List, int NStorms )
    {
        System.out.println(title + "\n");
        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 ("\n");
    }

    public static void Sort( Storm[] StormList, int N )
    {
        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;
                }
            }
        }
    }
}
-----------------------------------------------------------------------------------------------------------
public class Storm {
    private final double KnotsToMPH = 1.15;

    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 )
    {
        beginDate = bdate;
        duration = dur;
        name = sname;
        wind = (int) (w * KnotsToMPH);
        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;
        }

        SaffirSimpson();
    }

    private void SaffirSimpson()
    {

        if(pressure <= 920 && wind >= 156)
        {
            category = 5;
        }
        if(pressure > 920 && wind < 156)
        {
            category = 4;
        }
        if(pressure > 945 && wind < 113)
        {
            category = 3;
        }
        if(pressure > 965 && wind < 96)
        {
            category = 2;
        }
        if(pressure > 980 && wind < 83)
        {
            category = 1;
        }
        if(wind < 64)
        {
            category = -1;
        }
        if(wind < 34)
        {
            category = -2;
        }
        if(pressure == 0)
        {
            category = 0;
        }
    }

    public int getCategory()
    {
        return category;
    }

    public String toString()
    {
        return String.format("%9d %8d   %10s %4d %9d %10d\n", beginDate, duration, name, category, wind, pressure);

    }

}
Storm Chaser- C:\Users. Su apnil StormChaser - StormChaser Asrc Storm java-IntelliJ IDEA 2017 2.5 Eile Edit View Navigate Cod

Add a comment
Know the answer?
Add Answer to:
Write in JAVA Get Familiar with the Problem Carefully read the program description and look at...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JAVA project PLEASE complete/ create project with comments in the programming explaining everything Text file Year...

    JAVA project PLEASE complete/ create project with comments in the programming explaining everything Text file Year of storm/ Name of storm/ mmdd storm started/ mmdd storm ended/ magnitude of storm/ //made up data 2004/Ali/1212/1219/1 2003/Bob/1123/1222/3 1980/Sarah/0123/0312/0 1956/Michael/1211/1223/4 1988/Ryan/0926/1019/ 1976/Tim/0318/1010/0 2006/Ronald/0919/1012/2 1996/Mona/0707/0723/1 2000/Kim/0101/0201/1 2001/Jim/1101/1201/3 Text file Class storm{ private String nameStorm; private int yearStorm; private int startStorm; private int endStorm; private int magStorm; public storm(String name, int year, int start, int end, int mag){ nameStorm = name; yearStorm = year; startStorm...

  • This is a java homework for my java class. Write a program to perform statistical analysis...

    This is a java homework for my java class. Write a program to perform statistical analysis of scores for a class of students.The class may have up to 40 students.There are five quizzes during the term. Each student is identified by a four-digit student ID number. The program is to print the student scores and calculate and print the statistics for each quiz. The output is in the same order as the input; no sorting is needed. The input is...

  • Modify the program that you wrote for the last exercise in a file named Baseball9.java that...

    Modify the program that you wrote for the last exercise in a file named Baseball9.java that uses the Player class stored within an array. The program should read data from the file baseball.txt for input. The Player class should once again be stored in a file named Player.java, however Baseball9.java is the only file that you need to modify for this assignment. Once all of the input data from the file is stored in the array, code and invoke a...

  • Java: Please help with my Tester. I'm having a hard time figuring out my error. Thank...

    Java: Please help with my Tester. I'm having a hard time figuring out my error. Thank you. What the tester requires: The AccountTester This class should consists of a main method that tests all the methods in each class, either directly by calling the method from the Tester or indirectly by having another method call a method. User and Bot information will be read from a file. A sample file is provided. Use this file format to aid in grading....

  • In Java Code Needed is Below the Question Using Program 3: Encapsulating Dogs, modify this program...

    In Java Code Needed is Below the Question Using Program 3: Encapsulating Dogs, modify this program to create a database of dogs of your own. You will also include File I/O with this program. You will create 3 classes for this assignment. The first class: Create a Class representing the information that is associated with one item (one dog) of your database. Name this class Dog. Using Lab 3, this will include the information in regard to one dog. The...

  • (JAVA) Use the Pet.java program from the original problem (down below) Compile it. Create a text...

    (JAVA) Use the Pet.java program from the original problem (down below) Compile it. Create a text file named pets10.txt with 10 pets (or use the one below). Store this file in the same folder as your “class” file(s). The format is the same format used in the original homework problem. Each line in the file should contain a pet name (String), a comma, a pet’s age (int) in years, another comma, and a pet’s weight (double) in pounds. Perform the...

  • Copy the following java codes and compile //// HighArray.java //// HighArrayApp.java Study carefully the design and...

    Copy the following java codes and compile //// HighArray.java //// HighArrayApp.java Study carefully the design and implementation HighArray class and note the attributes and its methods.    Create findAll method which uses linear search algorithm to return all number of occurrences of specified element. /** * find an element from array and returns all number of occurrences of the specified element, returns 0 if the element does not exit. * * @param foundElement   Element to be found */ int findAll(int...

  • Problem: Use the code I have provided to start writing a program that accepts a large...

    Problem: Use the code I have provided to start writing a program that accepts a large file as input (given to you) and takes all of these numbers and enters them into an array. Once all of the numbers are in your array your job is to sort them. You must use either the insertion or selection sort to accomplish this. Input: Each line of input will be one item to be added to your array. Output: Your output will...

  • USE JAVA PROGRAMMING Create a program that would collect list of persons using double link list...

    USE JAVA PROGRAMMING Create a program that would collect list of persons using double link list and use a Merge Sort to sort the object by age. Create a class called Person : name and age Create methods that add, and delete Person from the link list Create a method that sorts the persons' objects by age. package mergesort; public class MergeSortExample { private static Comparable[] aux; // auxiliary array for merges public static void sort(Comparable[] a) { aux =...

  • JAVA Write a program which will read a text file into an ArrayList of Strings. Note...

    JAVA Write a program which will read a text file into an ArrayList of Strings. Note that the given data file (i.e., “sortedStrings.txt”) contains the words already sorted for your convenience. • Read a search key word (i.e., string) from the keyboard and use sequential and binary searches to check to see if the string is present as the instance of ArraryList. • Refer to “SearchInt.java” (given in “SearchString.zip”) and the following UML diagram for the details of required program...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT