You are on page 1of 12

Kyle McCormick | 02/29/2024 | COP2250 | Project 1 ‘Stormchaser’

CODE:___________________________________________________________
StormChaser.java:_________________
package com.mycompany.stormchaser;
/**
*
* @author kylemccormick
* @starterfileauthor jjsullivan
*/
import java.io.*;
import java.util.Scanner;

public class StormChaser {


public static void main(String[] args) {
// Constants
final int MAX_STORMS = 300;
Storm[] list = new Storm[MAX_STORMS]; // creating new
array
Storm currentStorm; // type of storm returned, no dupes
int nStorms = 0; // list ids
int total = 0; // total number of storms, NO DUPES,
Scanner fileInput;

// open/read hurricane.data
try {
System.out.println("Opening hurricane.data...");
fileInput = new Scanner(new File("hurricane.data"));
} catch (FileNotFoundException e) {
System.err.println("FileNotFoundException: " +
e.getMessage());
return;
}
// sys printout confirmation
System.out.println("File opened...");
System.out.println("Reading ...");

// read and store until eof


while (fileInput.hasNextLine()) {
currentStorm = GetStorm(fileInput);
++total;
if (currentStorm.getCategory() >= 3) {
list[nStorms] = currentStorm;
nStorms++; // nstorms++
}
}
// sys.outs for total storms, cat 3+s, first and top
tens
System.out.println("Total number of hurricanes: " +
total);
System.out.println("Category 3 hurricanes and above: " +
nStorms);
System.out.println("-----------------------------------
");
DisplayStorms("The First Ten Storms", list, 10);
Sort(list, nStorms);
System.out.println("-----------------------------------
");
DisplayStorms("The Top Ten Storms", list, 10);
fileInput.close();
}
// getstorm public scanner
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 and get next int


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

// storm object
beginDate = year * 10000 + month * 100 + day;
duration = 6;
newStorm = new Storm(beginDate, duration, name, wind,
pressure);
current = sequence; // stores new data until eof

while (in.hasNextLine() && current == sequence) {


// update storm info
duration += 6;
newStorm.setDuration(duration);
newStorm.setWind(wind);
newStorm.setPressure(pressure);
// wind, duration, pressure sets until next record

// 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(); // per line of
hurricane.data
}
// and return the new storm object, fin
return newStorm;
}

public static void DisplayStorms(String title, Storm[] list,


int nStorms) {
// display nStorms, DO THIS AFTER YOU FINISH THE FIRST
SECTION
// title and column with spacing
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++) { // nstorms again, no
++
System.out.print(list[k].toString());
}
System.out.println("\n");
}

public static void Sort(Storm[] stormList, int n) {


// sort list for storms; while, for, than if statement
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;
}
}
}
}
}
Storm.java:_______________________
package com.mycompany.stormchaser;

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;

// end with SS since its already formatted


public Storm(int bdate, int dur, String sname, int w, int p)
{
beginDate = bdate;
duration = dur;
name = sname;
setWind(w);
pressure = p;
SaffirSimpson();
}
// duration
public void setDuration(int d) {
duration = d;
}
// wind
public void setWind(int w) {
double temp = w * KnotsToMPH;
if (temp > wind)
wind = (int) temp;
SaffirSimpson();
}
// pressure
public void setPressure(int p) {
if (pressure == 0) ;
pressure = p;
if (p > 0 && p < pressure)
pressure = p;
SaffirSimpson();
}
public void SaffirSimpson() { // DO NOT TOUCH, already done
// 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() { // simple return


return category;
}

public String toString() {


return String.format("%9d %8d %10s %4d %9d %10d\n",
beginDate,
duration,
name, category, wind, pressure);
}
}OUTPUT:________________________________________________________
_
Building StormChaser 1.0-SNAPSHOT
--------------------------------[ jar ]-------------------------
--------

--- exec-maven-plugin:3.0.0:exec (default-cli) @ StormChaser ---


Opening hurricane.data...
File opened...
Reading ...
Total number of hurricanes: 326
Category 3 hurricanes and above: 62
-----------------------------------
The First Ten Storms

Begin Date Duration Name Category Maximum Minimum


(hours) Winds (mph) Press. (mb)
---------------------------------------------------------------
19670905 408 CHLOE 3 109 996
19750824 198 CAROLINE 4 114 1002
19750828 174 DORIS 3 109 1005
19750913 282 ELOISE 4 126 1004
19750923 276 GLADYS 4 138 980
19760806 108 BELLE 4 120 992
19760820 372 EMMY 3 103 1002
19760926 210 GLORIA 3 103 1008
19770829 120 ANITA 4 172 1011
19780830 162 ELLA 4 138 980

-----------------------------------
The Top Ten Storms

Begin Date Duration Name Category Maximum Minimum


(hours) Winds (mph) Press. (mb)
---------------------------------------------------------------
19800731 276 ALLEN 5 189 1008
19880909 276 GILBERT 5 184 995
19890910 366 HUGO 5 161 974
19750824 198 CAROLINE 4 114 1002
19750913 282 ELOISE 4 126 1004
19750923 276 GLADYS 4 138 980
19760806 108 BELLE 4 120 992
19770829 120 ANITA 4 172 1011
19780830 162 ELLA 4 138 980
19790825 330 DAVID 4 172 985

----------------------------------------------------------------
--------
BUILD SUCCESS
----------------------------------------------------------------
--------
Total time: 1.215 s
Finished at: 2024-03-02T14:03:24-05:00

You might also like