Question

Write a program that will accept from the user a text file containing hurricane data along...

Write a program that will accept from the user a text file containing hurricane data along with an output filename. The data consists of the year, the number of storms, the number of hurricanes, and the damage in millions of US Dollars. The first line contains the word “Years” followed by the first year listed and the last year listed separated by tabs. The following lines contain the data separated by tabs. A sample text file is available in this submission. The program must have a method named parseData (below) which reads a file containing text. parseData will take two File parameters. You may create a main method for testing but the main method will not be evaluated when grading this assignment. parseData will be called directly. public static void parseData(File inputFile, File outputFile){ //Your code here } The program will read in the data from the file into four different arrays: one for each of the data items listed. The array length can be determined from the first line of the input data that states the year range for the data. The program will then calculate the average number of storms per year, average number of hurricanes per year, and average damage costs per year. The averages will be to two decimal places based on the input data given. The program will also identify the year or years with the most number of storms and the year or years with the most number of hurricanes. You will use two methods to generate this information. One method being calculateAverage and the other being findHighest. These methods will be called from the parseData method. public static double calculateAverage (int[] data){ //Your code here } public static String findHighest(int[] storm, int[] year){ //Your code here } The program will write the results to the given outputFile parameter. The first line of output will write a header that includes the year ranges for the data given. You will then write the average number of storms, average number of hurricanes, average amount of damage, years with the most numbers of storms, and years with the most number of hurricanes, each on a separate line. Please note that the output of the damage data should show the actual number not the number in millions of US Dollars (If the output is 2, this is really $2,000,000.). Output for the sample file: For the years: 2015 to 1940: The average number of storms was 10.97. The average number of hurricanes was 6.09. The average damage was $4,996,355,263.16. The year(s) with the highest number of storms is: 2005. The year(s) with the highest number of hurricanes is: 2005 1969. Years 2015 1940 2015 12 4 590 2014 9 6 232 2013 13 2 1510 2012 19 10 75000 2011 19 7 21000 2010 21 12 12356 2009 11 3 77 2008 16 8 24945 2007 15 6 50 2006 10 5 500 2005 28 15 115520 2004 15 9 45235 2003 16 7 3580 2002 12 4 1220 2001 15 9 5260 2000 15 8 26 1999 12 8 5532 1998 14 10 3165 1997 8 3 100 1996 13 9 3600 1995 19 11 3729 1994 7 3 973 1993 8 4 57 1992 7 4 26500 1991 8 4 1500 1990 13 8 57 1989 11 7 7670 1988 12 5 59 1987 7 3 1 1986 6 4 17 1985 10 7 3725 1984 13 5 66 1983 4 3 2000 1982 6 2 2 1981 12 7 25 1980 11 9 300 1979 9 5 3050 1978 12 5 20 1977 6 5 10 1976 10 6 100 1975 9 6 490 1974 11 4 150 1973 8 4 18 1972 7 3 2102 1971 13 6 212 1970 10 5 454 1969 18 15 1421 1968 8 4 11 1967 8 6 200 1966 11 7 15 1965 6 4 1445 1964 12 6 515 1963 9 7 13 1962 5 3 2 1961 11 8 406 1960 7 4 393 1959 11 7 22 1958 10 7 11 1957 8 3 152 1956 8 4 25 1955 12 9 960 1954 11 8 781 1953 14 6 1 1952 7 6 3 1951 10 8 0 1950 13 11 31 1949 13 7 59 1948 9 6 18 1947 9 5 136 1946 6 3 12 1945 11 5 80 1944 11 7 165 1943 10 5 17 1942 10 4 27 1941 6 4 8 1940 8 4 9

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

CODE:

import java.util.Scanner; // importing the Scanner class
import java.io.*; // importing package to read files

//driver Class
public class StormAnalysis {
   // main method
   public static void main(String[] args) throws IOException {
       String inputFile = ""; // an input file which will be taken as input
       String outputFile = ""; // an output file which will be written to

       Scanner scan = new Scanner(System.in); //creating a scannner object
       System.out.println("Enter input file name: "); // prompt to input a file
       inputFile = scan.nextLine(); // storing the file name

       System.out.println("Enter output file name: "); // prompt to input a output file
       outputFile = scan.nextLine(); // storing the file name

       File in = new File(inputFile); // creating an input file object
       File out = new File(outputFile); // creating a output file object
       parseData(in,out);
    }
    // parseData method
    public static void parseData(File inputFile, File outputFile) throws IOException{
        FileReader fileToRead = new FileReader(inputFile);
       BufferedReader br = new BufferedReader(fileToRead);// bufferedraeder instantiated

       String line[]; // the string array
       String str; // the string that reads and stores each line

       str = br.readLine();//reading the line from bufferedreader
       line = str.split(" ");
       int startYear = Integer.parseInt(line[1]);
       int endYear = Integer.parseInt(line[2]);
       int arrLength = startYear - endYear;

       // creating the arrays
       int[] year = new int[arrLength];
       int[] storm = new int[arrLength];
       int[] hurricanes = new int[arrLength];
       int[] damage = new int[arrLength];

       int fileIndex = 0;
       //Reads the file and populates the arrays
       while(((str = br.readLine())!=null) && (fileIndex < arrLength )) {
           //System.out.println(str);  
           line = str.split(" ");
           //System.out.println(line[0] + " " + line[1] + " " + line[2] + " " + line[3]);
           year[fileIndex] = Integer.parseInt(line[0]);
           storm[fileIndex] = Integer.parseInt(line[1]);
           hurricanes[fileIndex] = Integer.parseInt(line[2]);
           damage[fileIndex] = Integer.parseInt(line[3]);
          
           fileIndex++;
  
       }
       br.close();
       //calculates averages and finds highest storms
       double avgStorms = calculateAverage(storm);
       double avgHurricanes = calculateAverage(hurricanes);
       double avgDamage = calculateAverage(damage);
       String highestStorm = findHighest(storm,year);

       //for the decimal formatting
       avgStorms = Math.floor(avgStorms * 1e2) / 1e2;
       avgHurricanes = Math.floor(avgHurricanes * 1e2) / 1e2;
       avgDamage = Math.floor(avgDamage * 1e2) / 1e2;

       // the line that shall be written
       String outputLine = "For the years: " + startYear + " to " + endYear + System.lineSeparator();
       outputLine += "The average number of storms was " + avgStorms + System.lineSeparator();
       outputLine += "The average number of hurricanes was " + avgHurricanes +"." + System.lineSeparator();
       outputLine += "The average damage was $" + avgDamage + "." + highestStorm;
       System.out.println(outputLine);

       //creating the fileWrter object
       FileWriter fileToWrite = new FileWriter(outputFile);
       //writing to the file
       fileToWrite.write(outputLine);
       fileToWrite.close();
    }
    //mehtod to calculate average
    public static double calculateAverage (int[] data) {
       double sum = 0;
       for(int i = 0; i < data.length; i++) {
           sum += data[i];
       }
       return sum / data.length;
   }
   //method to find highest
   public static String findHighest(int[] storm, int[] year) {
       int highest = storm[0];
       int idx = 0;
       String stormHighest = "";
       // loop to find the maximum number of storms
       for(int i = 0; i < storm.length; i++) {
           if(storm[i] > highest ) {
               highest = storm[i];
               idx = i;// storing the index position where we find highest number of storms
           }
       }
       stormHighest = "The year(s) with the highest number of storms is: " + year[idx]; //the year with the highest number of storm
       return stormHighest;  
   }
}

OUTPUT:

For the years: 2015 to 1940 The average number of storms was 11.01 The average number of hurricanes was 6.12. The average dam

Add a comment
Know the answer?
Add Answer to:
Write a program that will accept from the user a text file containing hurricane data along...
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
  • Therefore, for this program you will read the data in the file named weatherdata_2.txt into arrays...

    Therefore, for this program you will read the data in the file named weatherdata_2.txt into arrays for the wind speed and for the temperature. You will then use the stored data to calculate and output the average wind speed and the average temperature. Create a constant with a value of 30 and use that to declare and loop through your arrays and as a divisor to produce your averages. Here is the data file (below). 1. 14 25 2. 12...

  • Using Python, write a program that keeps asking the user for text until they type "exit"...

    Using Python, write a program that keeps asking the user for text until they type "exit" To Exit Or Not Write a program that keeps asking the user for text until they type "exit" Console 1 Enter number 1 3 Enter number 5 5 Enter number 7 Enter number 8 1 9 Enter number 10 9 11 Enter number 12 3 13 Enter number 14 4 15 Enter number 16 exit 17 Bye! 18

  • c program that takes a seridalized text file of a tree of up to 32 nodes...

    c program that takes a seridalized text file of a tree of up to 32 nodes and builds its stick diagram as shown. Example 1: Input text file: 10 6 1@5@@8@@ 12 @ 13 @ 20 19 16 @@@@ Output text file: 10 / 12 6 / 1 - 8 13 - - 5 20 19 - 16 Input text file: 10 8 1@7@@@@ 18 11 @ 16 13 @ 14 @@@23 21 @@@ Output text file: 10 8 18...

  • Please write a C++ program that will accept 3 integer numbers from the user, and output...

    Please write a C++ program that will accept 3 integer numbers from the user, and output these 3 numbers in order, the sum, and the average of these 3 numbers. You must stop your program when the first number from the user is -7. Design Specifications: (1) You must use your full name on your output statements. (2) You must specify 3 prototypes in your program as follows: int max(int, int, int); // prototype to return maximum of 3 integers...

  • 4. The strongest winds in a hurricane are found in the _______________ of the hurricane.     storm...

    4. The strongest winds in a hurricane are found in the _______________ of the hurricane.     storm surge center of the eye outskirts eye wall 5. Most of the damage and deaths associated with hurricanes are caused by ___________. storm surge winds displaced sand panic 6. Hurricanes are normally steered by the ____________________ winds. trade westerly jet stream polar high 7. Severe weather associated with hurricanes is not limited to the winds and storm surge, but also can include _______________.   ...

  • Write a program that reads the integer numbers in a text file (numbers.txt) and stores the...

    Write a program that reads the integer numbers in a text file (numbers.txt) and stores the numbers to a binary file (binaryNumber.dat). (5 pts) The number.txt file contains the following numbers: 1 2 3 4 5 6 7 8 9 0 7. Write a program that reads the integer numbers in a text file (numbers.txt) and stores the numbers to a binary file (binaryNumber.dat). (5 pts) The number.txt file contains the following numbers: 1 2 3 4 5 6 7...

  • Write a program that reads the integer numbers in a text file (numbers.txt) and stores the...

    Write a program that reads the integer numbers in a text file (numbers.txt) and stores the numbers to a binary file (binaryNumber.dat). (5 pts) The number.txt file contains the following numbers: 1 2 3 4 5 6 7 8 9 0 IN JAVA PLZ 7. Write a program that reads the integer numbers in a text file (numbers.txt) and stores the numbers to a binary file (binaryNumber.dat). (5 pts) The number.txt file contains the following numbers: 1 2 3 4...

  • The accompanying data file contains 10 observations for t and yt. picture Click here for the...

    The accompanying data file contains 10 observations for t and yt. picture Click here for the Excel Data File A 3-period moving average is plotted along with the actual series. 15 14 13 12 11 - 10 LA 9 8- 7- 6 5 0 1 2 3 4 6 7 8 9 10 rk Ch 18 Saved Help Save & Exit su Check my we b-1. Use the 3-period moving average to make in-sample forecasts. (Round intermediate calculations to at...

  • Write a program that reads a file containing an arbitrary number of text integers that are...

    Write a program that reads a file containing an arbitrary number of text integers that are in the range 0 to 99 and counts how many fall into each of eleven ranges. The ranges are 0-9, 10-19, 20-29,..., 90-99 and an eleventh range for "out of range." Each range will correspond to a cell of an array of ints. Find the correct cell for each input integer using integer division. After reading in the file and counting the integers, write...

  • DESCRIPTION Complete the program using Java to read in values from a text file. The values...

    DESCRIPTION Complete the program using Java to read in values from a text file. The values will be the scores on several assignments by the students in a class. Each row of values represents a specific student's scores. Each column represents the scores of all students on a specific assignment. So a specific value is a specific student's score on a specific assignment. The first two values in the text file will give the number of students (number of rows)...

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