The following data contains monthly measurements of Lake Superior Cloud Cover.
12 1
13
YEAR JANUARY FEBRUARY MARCH APRIL MAY JUNE JULY AUGUST SEPTEMBER OCTOBER
NOVEMBER DECEMBER
2000 64.94 64.66 62.39 58.90 60.81 68.83 54.74 57.00 54.93 55.97 83.57
72.65
2001 73.48 74.79 64.84 62.33 55.32 51.33 49.45 43.90 58.80 61.55 69.17
77.90
2002 76.45 68.93 59.03 63.33 57.97 54.37 40.48 48.74 53.10 78.55 79.87
69.81
2003 65.97 58.79 58.61 51.60 43.65 41.33 46.32 42.81 58.07 66.03 78.90
70.35
2004 72.48 67.90 71.29 56.30 62.58 50.90 50.42 57.35 50.23 71.68 62.80
77.42
2005 69.06 67.82 56.16 44.63 62.42 51.43 36.90 48.35 44.23 65.32 80.93
81.90
2006 78.00 68.75 53.65 44.33 61.87 41.27 37.94 40.94 55.80 63.58 75.40
72.23
2007 69.55 63.46 64.74 54.17 50.97 45.80 44.68 41.03 60.30 67.61 76.27
80.03
2008 72.81 65.93 53.97 62.53 58.94 63.60 55.61 40.00 58.80 63.65 82.43
75.87
2009 66.06 68.89 56.16 57.53 50.87 55.17 57.74 53.13 44.27 77.45 66.37
78.61
2010 70.58 63.21 45.45 46.40 49.61 64.50 47.65 49.35 62.07 49.35 70.03
78.35
The first line of the data file contains the total number of rows of data in the file and the number of header rows.
The second line contains the number of columns of data.
To Do:
Get the name of the file from a command line argument.
Use a Scanner to read the data into an array.
Use printf to display the data in neat rows and columns.
Answer the following questions:
What was the average cloud cover in May over all years?
What was the average cloud cover in 2006?
What was the overall average cloud cover?
Once read in, the data should be as follows:
YEAR JANUARY FEBRUARY MARCH APRIL MAY JUNE JULY AUGUST SEPTEMBER OCTOBER NOVEMBER DECEMBER 2000 64.94 64.66 62.39 58.90 60.81 68.83 54.74 57.00 54.93 55.97 83.57 72.65 2001 73.48 74.79 64.84 62.33 55.32 51.33 49.45 43.90 58.80 61.55 69.17 77.90 2002 76.45 68.93 59.03 63.33 57.97 54.37 40.48 48.74 53.10 78.55 79.87 69.81 2003 65.97 58.79 58.61 51.60 43.65 41.33 46.32 42.81 58.07 66.03 78.90 70.35 2004 72.48 67.90 71.29 56.30 62.58 50.90 50.42 57.35 50.23 71.68 62.80 77.42 2005 69.06 67.82 56.16 44.63 62.42 51.43 36.90 48.35 44.23 65.32 80.93 81.90 2006 78.00 68.75 53.65 44.33 61.87 41.27 37.94 40.94 55.80 63.58 75.40 72.23 2007 69.55 63.46 64.74 54.17 50.97 45.80 44.68 41.03 60.30 67.61 76.27 80.03 2008 72.81 65.93 53.97 62.53 58.94 63.60 55.61 40.00 58.80 63.65 82.43 75.87 2009 66.06 68.89 56.16 57.53 50.87 55.17 57.74 53.13 44.27 77.45 66.37 78.61 2010 70.58 63.21 45.45 46.40 49.61 64.50 47.65 49.35 62.07 49.35 70.03 78.35
Given below is the code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then
indent using ctrl+i
Please do rate the answer if it was helpful. Thank you
NOTE: Please make sure you place your input file in the correct
folder. If using eclipse, the file should be in the project
directly and NOT INSIDE src folder.
input file: cloud.txt
---------------------
12 1
13
YEAR JANUARY FEBRUARY MARCH APRIL MAY JUNE JULY AUGUST SEPTEMBER
OCTOBER NOVEMBER DECEMBER
2000 64.94 64.66 62.39 58.90 60.81 68.83 54.74 57.00 54.93 55.97
83.57 72.65
2001 73.48 74.79 64.84 62.33 55.32 51.33 49.45 43.90 58.80 61.55
69.17 77.90
2002 76.45 68.93 59.03 63.33 57.97 54.37 40.48 48.74 53.10 78.55
79.87 69.81
2003 65.97 58.79 58.61 51.60 43.65 41.33 46.32 42.81 58.07 66.03
78.90 70.35
2004 72.48 67.90 71.29 56.30 62.58 50.90 50.42 57.35 50.23 71.68
62.80 77.42
2005 69.06 67.82 56.16 44.63 62.42 51.43 36.90 48.35 44.23 65.32
80.93 81.90
2006 78.00 68.75 53.65 44.33 61.87 41.27 37.94 40.94 55.80 63.58
75.40 72.23
2007 69.55 63.46 64.74 54.17 50.97 45.80 44.68 41.03 60.30 67.61
76.27 80.03
2008 72.81 65.93 53.97 62.53 58.94 63.60 55.61 40.00 58.80 63.65
82.43 75.87
2009 66.06 68.89 56.16 57.53 50.87 55.17 57.74 53.13 44.27 77.45
66.37 78.61
2010 70.58 63.21 45.45 46.40 49.61 64.50 47.65 49.35 62.07 49.35
70.03 78.35
CloudCover.java
-----------------
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class CloudCover {
public static void main(String[] args) {
int[] years;
double[][] data;
String filename;
Scanner keyboard = new Scanner(System.in);
if(args.length != 1) //no command line arg?
{
System.out.print("Enter input filename: ");
filename = keyboard.next();
}
else
filename = args[1];
try {
Scanner infile = new Scanner(new File(filename));
int rows = infile.nextInt();
int header = infile.nextInt();
int cols = infile.nextInt();
int numYears = rows - (header + 2);
infile.nextLine();
years = new int[numYears];
data = new double[numYears][cols - 1];
loadData(infile, header, years, data);
double monthAvg = getMonthAverage(5, data);
double yearAvg = getYearAverage(2006, years, data);
double allAvg = getOverallAverage(data);
System.out.printf("The average cloud cover for the month of May is
%.2f\n", monthAvg);
System.out.printf("The average cloud cover for the year 2006 is
%.2f\n", yearAvg);
System.out.printf("The overall average cloud cover is %.2f\n",
allAvg);
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
}
}
static void loadData(Scanner infile, int header, int[] years,
double[][] data)
{
//skip over header lines
for(int i = 0; i < header; i++)
infile.nextLine();
for(int i = 0; i < years.length; i++)
{
years[i] = infile.nextInt();
for(int j = 0; j < data[0].length; j++)
data[i][j] = infile.nextDouble();
}
infile.close();
}
static double getMonthAverage(int month, double[][] data)
{
double avg = 0;
for(int i = 0; i < data.length; i++)
avg += data[i][month-1];
avg /= data.length;
return avg;
}
static double getYearAverage(int year, int[] years, double[][]
data)
{
int row = -1;
//find the row for the year
for(int i = 0; i < years.length; i++)
{
if(years[i] == year)
{
row = i;
break;
}
}
if(row == -1)//not found
return 0;
double avg = 0;
for(int i = 0; i < data[0].length; i++)
avg += data[row][i];
avg /= data[0].length;
return avg;
}
static double getOverallAverage(double[][] data)
{
double avg =0;
for(int i = 0; i < data.length; i++)
for(int j =0; j < data[0].length; j++)
avg += data[i][j];
int n = data.length * data[0].length;
avg /= n;
return avg;
}
}
output
Enter input filename: cloud.txt
The average cloud cover for the month of May is 57.17
The average cloud cover for the year 2006 is 57.81
The overall average cloud cover is 60.76
The following data contains monthly measurements of Lake Superior Cloud Cover. 12 1 13 YEAR JANUARY...