Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.util.ArrayList;
- import java.util.Scanner;
- /**
- * LakerSuperiorCloudCover
- *
- * @author (REDACTED) Last Modified: 10 / 16 / 2016
- *
- * Determines information for the rainfall in the Lake Superior Area
- */
- public class LakeSuperiorCloudCover implements CloudCoverInterface {
- // Variable to use to detect data loading.
- boolean dataIsLoaded = false;
- // Store your data in an array list of arrays using the following code
- // snippet:
- /**
- * The variable array should contain an instance of your array list of
- * arrays.
- */
- // The selection array that contains more arrays for data.
- public ArrayList<Object[]> array = new ArrayList<Object[]>();
- /**
- * loadData (String filename), inherited from CloudCoverInterface. String
- * filename : Incorrect to begin with, it should be file path. Variable
- * which locates the file with data.
- *
- * @return void;
- *
- * Throws FileNotFoundException in the event a file cannot be found.
- * Loads the data from the specified file into the ArrayList array,
- * pertaining to it's own arrays.
- */
- @Override
- public void loadData(String filename) throws FileNotFoundException {
- // Null Check
- if (filename == null || filename.isEmpty()) {
- // Error Code
- dataIsLoaded = false;
- } else {
- // Try catch method for if the file can't be found.
- try (Scanner input = new Scanner(new File(filename))) {
- // While the scanner has more information keep iterating.
- while (input.hasNextLine()) {
- // Converts the next line into an array of strings to be
- // converted into numbers.
- String[] data = input.nextLine().split(" ");
- // First portion of the array is the data for what year the
- // data
- // is for .
- int year = Integer.parseInt(data[0]);
- // Holding creates an array of objects for the data in the
- // string.
- // Objects is used because there is more than one data type.
- Object[] holding = new Object[data.length];
- // Sets the first variable for the year as the year itself.
- holding[0] = year;
- // Iterates through the values assigning them to the array.
- for (int i = 1; i < data.length; i++) {
- // Checks if the value is out of bounds.
- // Example: Negative or above 100 % .
- if ((Double.parseDouble(data[i]) < 0) || (Double.parseDouble(data[i]) > 100)) {
- data[i] = "-1.0";
- }
- // Adds the value to the array.
- holding[i] = Double.parseDouble(data[i]);
- }
- // adds the data array to the main selection array.
- array.add(holding);
- }
- // Sets data loaded to true.
- dataIsLoaded = true;
- // Catch statement for file not found.
- } catch (FileNotFoundException e) {
- // Throws stack trace to determine where exactly it's taking
- // place.
- e.printStackTrace();
- // Cause to simply provide an easy to say explanation for
- // user / client side.
- System.out.println(e.getCause());
- // Outputs the filename input so that things such as typos can
- // be
- // ruled out.
- System.out.println(filename);
- // Sets data loaded to false.
- dataIsLoaded = false;
- }
- }
- }
- /**
- * getAverage, inherited from CloudCoverInterface.
- *
- * @param :
- * Month month : A month enum to determine the average of a
- * specific month.
- * @return result : Double Result The average for a month across the years
- * of data.
- *
- * Determines the average over the years for a specific month.
- */
- @Override
- public double getAverage(Month month) {
- // Placeholder for final return.
- double result;
- // Null Check
- if (month == null) {
- // Error Code
- result = -1;
- }
- /*
- * If the array is null or empty returns a negative value Used a
- * negative number because it's an impossible value where as 0 is a
- * potentially possible value. In a real world program a negative value
- * could be detected and be used to throw an exception stating there is
- * a problem with the data.
- */
- // If data hasn't been loaded, return an error of -1.
- else if (!dataIsLoaded) {
- result = -1;
- } else if (array == null || array.isEmpty()) {
- result = -1;
- }
- // Else handles if not null nor empty.
- else {
- // Sum placeholder to begin making the average.
- double sum = 0;
- // Iterates through the years of data.
- for (int i = 0; i < array.size(); i++) {
- // Check for improper value errors.
- if (array.get(i)[month.ordinal() + 1].equals(-1.0)) {
- // Sum doesn't change, simply "skips" over with an error
- // message.
- System.out.println("ERROR AT: YEAR: " + array.get(i)[0] + " MONTH: " + (month.ordinal() + 1));
- } else {
- /*
- * Adds together the values for that month throughout the
- * years. Ordinal month returns a value less than the months
- * actual value, and value 0 holds the year itself.
- */
- sum += (double) array.get(i)[month.ordinal() + 1];
- }
- }
- // result is final sum divided by the number of years.
- result = sum / array.size();
- }
- // returns result.
- return result;
- }
- /**
- * getAverage, inherited from CloudCoverInterface.
- *
- * @param year
- * : int year. The specific year which the average is made from.
- *
- * @return result : double result. Returns the average across all months for
- * a specified year.
- *
- * Calculates the average for all months in a single year.
- */
- @Override
- public double getAverage(int year) {
- // Placeholder for final return.
- double result = 0;
- /*
- * If the array is null or empty returns a negative value Used a
- * negative number because it's an impossible value where as 0 is a
- * potentially possible value. In a real world program a negative value
- * could be detected and be used to throw an exception stating there is
- * a problem with the data.
- */
- // If data hasn't been loaded, return an error of -1.
- if (!dataIsLoaded) {
- result = -1;
- } else if (array == null || array.isEmpty()) {
- result = -1;
- }
- // Else handles if not null nor empty.
- else {
- // Sum placeholder to begin making the average.
- double sum = 0;
- // Iterates through all the years searching for the correct one.
- for (int i = 0; i < array.size(); i++) {
- // If its the correct year, begin data collection.
- if (year == (int) array.get(i)[0]) {
- // Begin iterating through the data of a year.
- for (int j = 1; j < array.get(i).length; j++) {
- // Check for improper value errors.
- if (array.get(i)[j].equals(-1.0)) {
- // Sum doesn't change, simply "skips" over with an
- // error
- // message.
- System.out.println("ERROR AT: YEAR: " + array.get(i)[0] + " MONTH: " + j);
- } else {
- // Sums the values together.
- sum += (double) array.get(i)[j];
- }
- }
- // Average = sum / number of months.
- result = sum / Month.values().length;
- // Breaks out of the for loop so it isn't reverted back to
- // -1.
- break;
- }
- // if it doesn't match the year result is -1.
- if (year != (int) array.get(i)[0]) {
- // Results now -1.
- result = -1;
- }
- }
- }
- // returns result.
- return result;
- }
- /**
- * getAverage, inherited from CloudCoverInterface.
- *
- * @return result : double result. Returns the average of all data.
- *
- * Calculates the average data across all months and years avalible.
- */
- @Override
- public double getAverage() {
- // Placeholder for final return.
- double result;
- /*
- * If the array is null or empty returns a negative value Used a
- * negative number because it's an impossible value where as 0 is a
- * potentially possible value. In a real world program a negative value
- * could be detected and be used to throw an exception stating there is
- * a problem with the data.
- */
- // If data hasn't been loaded, return an error of -1.
- if (!dataIsLoaded) {
- result = -1;
- } else if (array == null || array.isEmpty()) {
- result = -1;
- }
- // Else handles if not null nor empty.
- else {
- // Sum placeholder to begin making the average.
- double sum = 0;
- // Iterates through all the years
- for (int i = 0; i < array.size(); i++) {
- // Begin iterating through the data of a year.
- for (int j = 1; j < array.get(i).length; j++) {
- // Check for improper value errors.
- if (array.get(i)[j].equals(-1.0)) {
- // Sum doesn't change, simply "skips" over with an error
- // message.
- System.out.println("ERROR AT: YEAR: " + array.get(i)[0] + " MONTH: " + j);
- } else {
- // Sums the values together.
- sum += (double) array.get(i)[j];
- }
- }
- }
- // Average = sum / number of months.
- result = sum / (Month.values().length * array.size());
- // Breaks out of the for loop so it isn't reverted back to -1.
- }
- // returns result.
- return result;
- }
- /**
- * getMax, inherited from CloudCoverInterface.
- *
- * @param month
- * : Month month. A month used to determine the maximum of a
- * specific time frame.
- * @return currentMax : double currentMax. The maximum value of the months
- * found throughout the years of data.
- *
- * Calculates the maximum value that is found in a certain month.
- */
- @Override
- public double getMax(Month month) {
- // Holder Variable for the initial maximum. (None)
- double currentMax = -1;
- // Null Check
- if (month == null) {
- // Error Code
- currentMax = -1;
- }
- /*
- * If the array is null or empty returns a negative value Used a
- * negative number because it's an impossible value where as 0 is a
- * potentially possible value. In a real world program a negative value
- * could be detected and be used to throw an exception stating there is
- * a problem with the data.
- */
- // If data hasn't been loaded, return an error of -1.
- else if (!dataIsLoaded) {
- currentMax = -1;
- } else if (array == null || array.isEmpty()) {
- currentMax = -1;
- }
- // Else handles if not null nor empty.
- else {
- // Iterates through the years.
- for (int i = 0; i < array.size(); i++) {
- // Check for improper value errors.
- if (array.get(i)[month.ordinal() + 1].equals(-1.0)) {
- // Sum doesn't change, simply "skips" over with an error
- // message.
- System.out.println("ERROR AT: YEAR: " + array.get(i)[0] + " MONTH: " + (month.ordinal() + 1));
- } else {
- // If the current max value is less than the value at year i
- // and
- // at
- // the month then :
- if (currentMax < (double) array.get(i)[month.ordinal() + 1]) {
- // Sets current max to the latest maximum value.
- currentMax = (double) array.get(i)[month.ordinal() + 1];
- }
- }
- }
- }
- // Returns the maximum value.
- return currentMax;
- }
- /**
- * getMax, inherited from CloudCoverInterface.
- *
- * @param year
- * : int year. The year for which a a max is determined from.
- *
- * @return currentMax : double currentMax. Value for the maximum value in a
- * year.
- *
- * Calculates a value of the the current maximum of the year
- * specified.
- */
- @Override
- public double getMax(int year) {
- // Holder Variable for the initial maximum. (None)
- double currentMax = -1;
- /*
- * If the array is null or empty returns a negative value Used a
- * negative number because it's an impossible value where as 0 is a
- * potentially possible value. In a real world program a negative value
- * could be detected and be used to throw an exception stating there is
- * a problem with the data.
- */
- // If data hasn't been loaded, return an error of -1.
- if (!dataIsLoaded) {
- currentMax = -1;
- } else if (array == null || array.isEmpty()) {
- currentMax = -1;
- }
- // Else handles if not null nor empty.
- else {
- // Iterates through the years selecting the correct year..
- for (int i = 0; i < array.size(); i++) {
- // If the year is the correct year continue.
- if (year == (int) array.get(i)[0]) {
- // Iterate through all the months in the year.
- for (int j = 1; j < array.get(i).length; j++) {
- // Check for improper value errors.
- if (array.get(i)[j].equals(-1.0)) {
- // Sum doesn't change, simply "skips" over with an
- // error
- // message.
- System.out.println("ERROR AT: YEAR: " + array.get(i)[0] + " MONTH: " + j);
- } else {
- // If the value here is more than the current Max
- // value
- if (currentMax < (double) array.get(i)[j]) {
- // Then set that value as the new current max
- // value.
- currentMax = (double) array.get(i)[j];
- }
- }
- }
- // Break out of the loop improving efficiency, not iterating
- // through the rest of the years.
- // On that note, seriously a HashMap would be much better
- // suited
- // for this.
- break;
- }
- }
- }
- // Returns the current and final maximum for the year.
- return currentMax;
- }
- /**
- * getMax, inherited from CloudCoverInterface.
- *
- * @return currentMax : double currentMax. returns the final current max
- * value for the entire data set.
- *
- * Calculates the maximum value for all the data available.
- */
- @Override
- public double getMax() {
- // Holder Variable for the initial maximum. (None)
- double currentMax = -1;
- /*
- * If the array is null or empty returns a negative value Used a
- * negative number because it's an impossible value where as 0 is a
- * potentially possible value. In a real world program a negative value
- * could be detected and be used to throw an exception stating there is
- * a problem with the data.
- */
- // If data hasn't been loaded, return an error of -1.
- if (!dataIsLoaded) {
- currentMax = -1;
- } else if (array == null || array.isEmpty()) {
- currentMax = -1;
- }
- // Else handles if not null nor empty.
- else {
- // Iterating through the years.
- for (int i = 0; i < array.size(); i++) {
- // Iterating through each month.
- for (int j = 1; j < array.get(i).length; j++) {
- // Check for improper value errors.
- if (array.get(i)[j].equals(-1.0)) {
- // Sum doesn't change, simply "skips" over with an error
- // message.
- System.out.println("ERROR AT: YEAR: " + array.get(i)[0] + " MONTH: " + j);
- } else {
- // If the current max value is less than the value at
- // year i
- // and
- // at the month j being iterated through then :
- if (currentMax < (double) array.get(i)[j]) {
- // Sets current max to the latest maximum value.
- currentMax = (double) array.get(i)[j];
- }
- }
- }
- }
- }
- // Returns the maximum value.
- return currentMax;
- }
- /**
- * getMin(month), inherited from CloudCoverInterface.
- *
- * @return currentMin : double currentMin .Smallest rainfall within a month.
- *
- * Finds the smallest set of rainfall in all the years of a specific
- * month.
- */
- @Override
- public double getMin(Month month) {
- // Holder Variable for the initial maximum. (None)
- double currentMin = Double.MAX_VALUE;
- // Null Check
- if (month == null) {
- // Error Code
- currentMin = -1;
- }
- /*
- * If the array is null or empty returns a negative value Used a
- * negative number because it's an impossible value where as 0 is a
- * potentially possible value. In a real world program a negative value
- * could be detected and be used to throw an exception stating there is
- * a problem with the data.
- */
- // If data hasn't been loaded, return an error of -1.
- if (!dataIsLoaded) {
- currentMin = -1;
- } else if (array == null || array.isEmpty()) {
- currentMin = -1;
- }
- // Else handles if not null nor empty.
- else {
- // Iterates through the years.
- for (int i = 0; i < array.size(); i++) {
- // Check for improper value errors.
- if (array.get(i)[month.ordinal() + 1].equals(-1.0)) {
- // Sum doesn't change, simply "skips" over with an error
- // message.
- System.out.println("ERROR AT: YEAR: " + array.get(i)[0] + " MONTH: " + (month.ordinal() + 1));
- } else {
- // If the current max value is less than the value at year i
- // and
- // at
- // the month then :
- if (currentMin > (double) array.get(i)[month.ordinal() + 1]) {
- // Sets current max to the latest maximum value.
- currentMin = (double) array.get(i)[month.ordinal() + 1];
- }
- }
- }
- }
- // Returns the maximum value.
- return currentMin;
- }
- /**
- * getMin(year), inherited from CloudCoverInterface.
- *
- * @return currentMin : double currentMin .Smallest rainfall within a
- * specific year..
- *
- * Finds the smallest set of rainfall in a specific year.
- */
- @Override
- public double getMin(int year) {
- // Holder Variable for the initial maximum. (None)
- double currentMin = Double.MAX_VALUE;
- /*
- * If the array is null or empty returns a negative value Used a
- * negative number because it's an impossible value where as 0 is a
- * potentially possible value. In a real world program a negative value
- * could be detected and be used to throw an exception stating there is
- * a problem with the data.
- */
- // If data hasn't been loaded, return an error of -1.
- if (!dataIsLoaded) {
- currentMin = -1;
- } else if (array == null || array.isEmpty()) {
- currentMin = -1;
- }
- // Else handles if not null nor empty.
- else {
- // Iterates through the years selecting the correct year..
- for (int i = 0; i < array.size(); i++) {
- // If the year is the correct year continue.
- if (year == (int) array.get(i)[0]) {
- // Iterate through all the months in the year.
- for (int j = 1; j < array.get(i).length; j++) {
- // Check for improper value errors.
- if (array.get(i)[j].equals(-1.0)) {
- // Sum doesn't change, simply "skips" over with an
- // error
- // message.
- System.out.println("ERROR AT: YEAR: " + array.get(i)[0] + " MONTH: " + j);
- } else {
- // If the value here is more than the current Max
- // value
- if (currentMin > (double) array.get(i)[j]) {
- // Then set that value as the new current max
- // value.
- currentMin = (double) array.get(i)[j];
- }
- }
- }
- // Break out of the loop improving efficiency, not iterating
- // through the rest of the years.
- // On that note, seriously a HashMap would be much better
- // suited
- // for this.
- break;
- }
- }
- }
- // Returns the current and final maximum for the year.
- return currentMin;
- }
- /**
- * getMin, inherited from CloudCoverInterface.
- *
- * @return currentMin : double currentMin .Smallest rainfall within all the
- * data.
- *
- * Finds the smallest set of rainfall in all the data provided.
- */
- @Override
- public double getMin() {
- // Holder Variable for the initial maximum. (None)
- double currentMin = Double.MAX_VALUE;
- /*
- * If the array is null or empty returns a negative value Used a
- * negative number because it's an impossible value where as 0 is a
- * potentially possible value. In a real world program a negative value
- * could be detected and be used to throw an exception stating there is
- * a problem with the data.
- */
- // If data hasn't been loaded, return an error of -1.
- if (!dataIsLoaded) {
- currentMin = -1;
- } else if (array == null || array.isEmpty()) {
- currentMin = -1;
- }
- // Else handles if not null nor empty.
- else {
- // Iterating through the years.
- for (int i = 0; i < array.size(); i++) {
- // Iterating through each month.
- for (int j = 1; j < array.get(i).length; j++) {
- // Check for improper value errors.
- if (array.get(i)[j].equals(-1.0)) {
- // Sum doesn't change, simply "skips" over with an error
- // message.
- System.out.println("ERROR AT: YEAR: " + array.get(i)[0] + " MONTH: " + (j));
- } else {
- // If the current max value is less than the value at
- // year i
- // and
- // at the month j being iterated through then :
- if (currentMin > (double) array.get(i)[j]) {
- // Sets current max to the latest maximum value.
- currentMin = (double) array.get(i)[j];
- }
- }
- }
- }
- }
- // Returns the maximum value.
- return currentMin;
- }
- /**
- * coverageToArea, inherited from CloudCoverInterface.
- *
- * @param coverage
- * : double coverage. The percent of the lake covered by clouds.
- * [0, 100]
- * @return area : double area. Returns the surface area that the clouds are
- * covering.
- *
- * Takes the percentage of cloud cover of lake superior and converts
- * it into surface area.
- */
- @Override
- public double coverageToArea(double coverage) {
- // creates the storage variable.
- double area;
- // Checks to see if the coverage is even a number, and that it's more
- // than one.
- if ((Double.isNaN(coverage)) || (coverage < 0)) {
- // If not returns -1 as showing an error has occurred.
- area = -1;
- } else {
- // Everything is working properly, now simply uses mathematics to
- // convert.
- area = CloudCoverInterface.SURFACE_AREA * (coverage / 100);
- }
- // Returns the final surface area.
- return area;
- }
- /**
- * beginYear, inherited from CloudCoverInterface.
- *
- * @return minYear : The year the data begins.
- *
- * Iterates through the data finding what year the data begins.
- */
- @Override
- public int beginYear() {
- // Start off at a value as high as possible
- int minYear = Integer.MAX_VALUE;
- // if data has not been loaded.
- if (!dataIsLoaded) {
- // Shows an error has occurred.
- minYear = -1;
- } else if ((array == null || array.isEmpty())) {
- // Shows an error has occurred.
- minYear = -1;
- } else {
- // Begin for loop for iterating through the arrays.
- for (int i = 0; i < array.size(); i++) {
- // If the current minYear is greater than the year found in the
- // array,
- if (minYear < (int) array.get(i)[0]) {
- // Then set the minYear as the current year in the array.
- minYear = (int) array.get(i)[0];
- }
- }
- }
- // Return the final minimum year.
- return minYear;
- }
- /**
- * endYear, inherited from CloudCoverInterface.
- *
- * @return maxYear: Returns the final year of data in from the given data.
- *
- * Iterates through data finding the final year avalible.
- */
- @Override
- public int endYear() {
- // Start with the lowest value possible.
- int maxYear = Integer.MIN_VALUE;
- // if data has not been loaded.
- if (!dataIsLoaded) {
- // Shows an error has occurred.
- maxYear = -1;
- } else if ((array == null || array.isEmpty())) {
- // Shows an error has occurred.
- maxYear = -1;
- } else {
- // Iterate through the arrays of data.
- for (int i = 0; i < array.size(); i++) {
- // If the current maxYear is less than the year in the array,
- if (maxYear > (int) array.get(i)[0]) {
- // Then set the maxYEar as the current year in the array.
- maxYear = (int) array.get(i)[0];
- }
- }
- }
- // Return the final latest year.
- return maxYear;
- }
- public static void main(String[] args) throws FileNotFoundException {
- LakeSuperiorCloudCover self = new LakeSuperiorCloudCover();
- self.loadData("SuperiorCloudCover1.dat");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement