Question: Introduction This program will read data from a file into a two-dimensional array. Each line of the data file contains the precipitation for one month.

Introduction

This program will read data from a file into a two-dimensional array. Each line of the data file contains the precipitation for one month. The figures for a five-year period start with January 2015. Your program will output a nice table showing the total for each year as well as the average for each month. Your program will also search for the minimum and maximum amounts of precipitation and print out the results after the table.

Task #1 Creating the file connections

1) Accept the assignment, open your repository, select Clone or Download, copy to the clipboard and connect your repository to eclipse by importing a git project.

2) You will see the files Precipitation.java and precip.dat. Notice I have already imported the libraries that you will need to accomplish this in the Preciputation.java file. Scroll to the main method at the bottom of the file. The main method creates an object of type Precipitation that will contain the raw data stored in a 2D array, methods that will process the data and be able to create a report for the five-year period with calculated monthly averages and yearly totals as well as the maximum and minimum amounts of precipitation received in that period.

3) In the main method, declare a Scanner object to connect to the data file called precip.dat.

4) In the main method, declare a PrintWriter object to connect to the output file called precip.out.

Task #2 Declaring and creating arrays

1) Declare 3 arrays as data fields (instance variables) of type double

a) A 2-D array called precipitationAmount containing the data read from the file

b) A 1-D array called yearTotal containing the year totals

c) A 1-D array called monthlyAverage containing the average for each month

2) Write a constructor that creates the all arrays to the correct size, the 2-D array should have a row representing an entire year (months as columns).

Task #3 Fill up the arrays

1) In the readFile method, add the appropriate looping structure to read in the data from the file, convert to numbers, and store it into the indexed elements of the 2-D array. Test by printing the contents of the 2D array after you store it in the loop to the console. It should look like the contents of the data file. Comment out the test line after you verify that you have read in the data correctly.

2) In the calculateMonthlyAverage method, add the appropriate looping structure to calculate the monthly average for each column and store it into the monthlyAverage array. Verify that you have calculated correctly by printing the monthly average to the console after you store it into the array inside the loop. Comment out the test line after you verify the values.

3) In the calculateYearTotal method, add the appropriate looping structure to sum up each row and store it into the yearTotal array. Verify that you have calculated correctly by printing the yearly totals to the console after you store it into the array inside the loop. Comment out the test line after you verify the values.

Task #4 Finding the Minimum and Maximum Precipitation

1) Write a method to find the maximum precipitation. This method should search through the array for the largest amount of precipitation. It will return a string indicating the amount of precipitation along with the month and year in which it occurred. Verify that this method has operated correctly by printing out the data to the console and commenting out the test line after verification.

2) Write a similar method to find the minimum precipitation and verify method operation.

Task #5 Printing out the table

1) The printable method should eventually print to the output file. For testing purposes, you can start by printing to the console. In the printTable method, add the appropriate loops and output statements to print a nicely formatted table. The monthLabel array should be printed across the top of the table along with appropriate labels for the year and the total. The year should appear in the first column with each months precipitation along with the total in the last column of each row. The final row should have a label followed by the average precipitation for each month. Make sure the numbers all have one number before the decimal point, and two decimal places. The columns should line up nicely.

2) After the table is printed, call the findMax method and print out a line indicating the maximum amount of precipitation along with the year and month in which it occurred. Do the same for the minimum amount.

3) Once you have verified that your method gives the desired output, change System.out to outfile and run it again. Verify in file explorer that the output file was created and open it to verify that the content is the desired report.

Expected Output

Year Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec Total

2015 0.88 1.11 2.01 3.64 6.44 5.58 4.23 4.34 4.00 2.05 1.48 0.77 36.53

2016 0.76 0.94 2.09 3.29 4.68 3.52 3.52 4.82 3.72 2.21 1.24 0.80 31.59

2017 0.67 0.80 1.75 2.70 4.01 3.88 3.72 3.78 3.55 1.88 1.21 0.61 28.56

2018 0.82 0.80 1.99 3.05 4.19 4.44 3.98 4.57 3.43 2.32 1.61 0.75 31.95

2019 0.72 0.90 1.71 2.02 2.33 2.98 2.65 2.99 2.55 1.99 1.05 0.92 22.81

Ave 0.77 0.91 1.91 2.94 4.33 4.08 3.62 4.10 3.45 2.09 1.32 0.77

The maximum precipitation of 6.44 occurred in May of 2015

Precipitaion.java

package cs145.lab02;

import java.util.Scanner; // to read from a file import java.io.File; // to create a file import java.io.PrintWriter; // to write to a file import java.lang.reflect.Array; import java.io.IOException; // to use files import java.text.DecimalFormat; // to format numbers for printing import java.io.BufferedReader; /** This class can be used to process precipitation data for 5 years. It will read 5 years of monthly data from a data file called precip.dat containing 1 data point per line. It will create a report containing a table of the data as well as monthly averages and yearly totals. It will also find the maximum and minimum precipitation and display it with the table in the report. Written by YOUR NAME HERE TODAY'S DATE HERE */ public class Precipitation { private final static int MONTHS = 12; private final static int YEARS = 5; private final static int STARTYEAR = 2015; private final String[] monthLabel = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};

//DECLARE ARRAYS HERE FOR TASK #2

public Precipitation() { //CREATE THE ARRAYS OF THE CORRECT SIZE HERE FOR TASK #2 //(USE THE CLASS CONSTANTS) }

public void readFile(Scanner infile) throws IOException { //CREATE A LOOPING STRUCTURE TO READ DATA FROM THE FILE AND //STORE IT INTO THE 2-D ARRAY FOR TASK #3 infile.close(); calculateMonthlyAverage(); calculateYearTotal(); }

private void calculateMonthlyAverage() { //CREATE A LOOPING STRUCTURE TO CALCULATE THE MONTHLY AVERAGE //FOR EACH COLUMN AND STORE IT INTO THE MONTHLY AVERAGE ARRAY //FOR TASK #3 }

private void calculateYearTotal() { //CREATE A LOOPING STRUCTURE TO CALCULATE THE YEAR TOTAL //FOR EACH ROW AND STORE IT INTO THE YEAR TOTAL ARRAY //FOR TASK #3 } public String findMax() { //TASK #4 //SEARCH THE ARRAY FOR THE LARGEST AMOUNT OF PRECIPITAION //SAVE THE AMOUNT, MONTH, AND YEAR. return "The maximum precipitation"; //RETURN A STRING CONTAINING INFORMATION ABOUT THE MAXIMUM PRECIPITATION }

public String findMin() { //TASK #4 //SEARCH THE ARRAY FOR THE SMALLEST AMOUNT OF PRECIPITAION //SAVE THE AMOUNT, MONTH, AND YEAR. return "The minimum precipitation"; //RETURN A STRING CONTAINING INFORMATION ABOUT THE MINIMUM PRECIPITATION } public void printTable(PrintWriter outfile) { //OUTPUT THE TABLE USING NICELY FORMATTED NUMBERS #.## //AND NEAT COLUMNS FOR TASK #5 outfile.close(); }

public static void main(String[] args) throws IOException { Precipitation wetStuff = new Precipitation();

//TASK #1 //CREATE A Scanner OBJECT CALLED infile TO CONNECT WITH THE INPUT FILE: precip.dat //CREATE A PrintWriter OBJECT CALLED outfile TO CONNECT WITH THE OUTPUT FILE: precip.out

//UNCOMMENT THE LINES BELOW ONCE YOU HAVE CREATED YOUR OBJECTS //wetStuff.readFile(infile); //wetStuff.printTable(outfile); } }

PRECIP.dat

0.88 1.11 2.01 3.64 6.44 5.58 4.23 4.34 4.00 2.05 1.48 0.77 0.76 0.94 2.09 3.29 4.68 3.52 3.52 4.82 3.72 2.21 1.24 0.80 0.67 0.80 1.75 2.70 4.01 3.88 3.72 3.78 3.55 1.88 1.21 0.61 0.82 0.80 1.99 3.05 4.19 4.44 3.98 4.57 3.43 2.32 1.61 0.75 0.72 0.90 1.71 2.02 2.33 2.98 2.65 2.99 2.55 1.99 1.05 0.92

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!