I have a project from school that is finished and graded. I got a perfect score, but the teacher cares a lot more about the output than the efficiency. I know that efficiency is not always important. In my case, the job is done, and I could move on, but I just feel like my code is unsightly, and I have SO MANY VARIABLES that are for my for loops to count and what not that I just have names everywhere, and I’m wondering if there is an easier way to do it. This is in Java, but if there are better examples of certain aspects in other languages, I can read them too.
Additionally, this is no priority, but I know that some people like to refactor code and it would be cool to see a better way to do this.
Last note, I know that I could have used the built in functions of min and max, but part of the project was to not use them. If however, you want to include them refactored, that would be awesome, as I would be able to see how they work.
/* Kylea Watkins
* November 9, 2016
*/
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
public class Hurricanes2 {
public static void main(String[] args)throws IOException {
//declare and initialize variables
int arrayLength = 59;
int [] year = new int[arrayLength];
String [] month = new String[arrayLength];
int [] pressure = new int[arrayLength];
int [] knots = new int[arrayLength];
String [] name = new String[arrayLength];
File fileName = new File("hurcdata2.txt");
Scanner inFile = new Scanner(fileName);
//INPUT - read data in from the file
int index = 0;
while (inFile.hasNext())
{
year[index] = inFile.nextInt();
month[index] = inFile.next();
pressure[index] = inFile.nextInt();
knots[index] = inFile.nextInt();
name[index] = inFile.next();
index++;
}
inFile.close();
int a = 0;
int b = 0;
int c = 0;
int d = 0;
int e = 0;
int [] category = new int[arrayLength];
//PROCESSING - calculate and convert values
// convert windspeed from knots to MPH
for(int numberss = 0; numberss < arrayLength; numberss++) {
knots[numberss] = (int)(knots[numberss] * 1.15);
}
int x = 0;
// determine category
for(double speed : knots) {
if (speed <= 95) {
category[x] = 1;
a++;
}
else if (speed <= 110) {
category[x] = 2;
b++;
}
else if (speed <= 129) {
category[x] = 3;
c++;
}
else if (speed <= 156) {
category[x] = 4;
d++;
}
else {
category[x] = 5;
e++;
}
x++;
}
// count number of each category
//Find min, max and average for category, windspeed and pressure
int sumCat = 0;
int avgCat = 0;
int minCat = 1;
int maxCat = 5;
sumCat = a + b*2 + c*3 + d*4 + e*5;
avgCat = sumCat/59;
int sumWind = 0;
int avgWind;
Integer minWind = Integer.MAX_VALUE;
for(int i = 0; i < arrayLength; i++) {
if (knots[i] < minWind) {
minWind = knots[i];
}
}
Integer maxWind = Integer.MIN_VALUE;
for(int numb = 0; numb < arrayLength; numb++) {
if (knots[numb] > maxWind) {
maxWind = knots[numb];
}
}
for (int windSpeed : knots) {
sumWind += windSpeed;
}
avgWind = (int)((double)sumWind/59);
int sumPressure = 0;
int avgPressure;
Integer minPressure = Integer.MAX_VALUE;
for(int counter = 0; counter < arrayLength; counter++) {
if (pressure[counter] < minPressure) {
minPressure = pressure[counter];
}
}
Integer maxPressure = Integer.MIN_VALUE;
for(int count = 0; count < arrayLength; count++) {
if (pressure[count] > maxPressure) {
maxPressure = pressure[count];
}
}
for(int nums : pressure) {
sumPressure += nums;
}
avgPressure = (int)((double)sumPressure/59);
//Output - print table using printf to format the columns
System.out.println(" Hurricanes 1980 - 2006");
System.out.println();
System.out.println("Year Hurricane Category Pressure (mb) Wind Speed (mph)");
System.out.println("=====================================================================");
for(int counting = 0; counting < arrayLength; counting++) {
System.out.printf("%4d %12s %8d %15d %18d \n", year[counting], name[counting], category[counting], pressure[counting], knots[counting]);
}
System.out.println("=====================================================================");
System.out.printf("%7s %18d %15d %18d \n", "Average", avgCat, avgPressure, avgWind);
System.out.printf("%7s %18d %15d %18d \n", "Maximum", maxCat, maxPressure, maxWind);
System.out.printf("%7s %18d %15d %18d \n", "Minimum", minCat, minPressure, minWind);
System.out.println();
System.out.println("Number of Category 1: " + a);
System.out.println("Number of Category 2: " + b);
System.out.println("Number of Category 3: " + c);
System.out.println("Number of Category 4: " + d);
System.out.println("Number of Category 5: " + e);
}//end main()
}//end Hurricanes2
hurcdata2.txt (1.3 KB)
The attachment is the file that I’m reading from.