Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2016
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 23.42 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.util.ArrayList;
  4. import java.util.Scanner;
  5.  
  6. /**
  7.  * LakerSuperiorCloudCover
  8.  *
  9.  * @author (REDACTED) Last Modified: 10 / 16 / 2016
  10.  *
  11.  *         Determines information for the rainfall in the Lake Superior Area
  12.  */
  13. public class LakeSuperiorCloudCover implements CloudCoverInterface {
  14.  
  15.     // Variable to use to detect data loading.
  16.     boolean dataIsLoaded = false;
  17.     // Store your data in an array list of arrays using the following code
  18.     // snippet:
  19.     /**
  20.      * The variable array should contain an instance of your array list of
  21.      * arrays.
  22.      */
  23.     // The selection array that contains more arrays for data.
  24.     public ArrayList<Object[]> array = new ArrayList<Object[]>();
  25.  
  26.     /**
  27.      * loadData (String filename), inherited from CloudCoverInterface. String
  28.      * filename : Incorrect to begin with, it should be file path. Variable
  29.      * which locates the file with data.
  30.      *
  31.      * @return void;
  32.      *
  33.      *         Throws FileNotFoundException in the event a file cannot be found.
  34.      *         Loads the data from the specified file into the ArrayList array,
  35.      *         pertaining to it's own arrays.
  36.      */
  37.     @Override
  38.     public void loadData(String filename) throws FileNotFoundException {
  39.         // Null Check
  40.         if (filename == null || filename.isEmpty()) {
  41.             // Error Code
  42.             dataIsLoaded = false;
  43.         } else {
  44.             // Try catch method for if the file can't be found.
  45.             try (Scanner input = new Scanner(new File(filename))) {
  46.                 // While the scanner has more information keep iterating.
  47.                 while (input.hasNextLine()) {
  48.                     // Converts the next line into an array of strings to be
  49.                     // converted into numbers.
  50.                     String[] data = input.nextLine().split(" ");
  51.                     // First portion of the array is the data for what year the
  52.                     // data
  53.                     // is for .
  54.                     int year = Integer.parseInt(data[0]);
  55.                     // Holding creates an array of objects for the data in the
  56.                     // string.
  57.                     // Objects is used because there is more than one data type.
  58.                     Object[] holding = new Object[data.length];
  59.                     // Sets the first variable for the year as the year itself.
  60.                     holding[0] = year;
  61.                     // Iterates through the values assigning them to the array.
  62.                     for (int i = 1; i < data.length; i++) {
  63.                         // Checks if the value is out of bounds.
  64.                         // Example: Negative or above 100 % .
  65.                         if ((Double.parseDouble(data[i]) < 0) || (Double.parseDouble(data[i]) > 100)) {
  66.                             data[i] = "-1.0";
  67.                         }
  68.                         // Adds the value to the array.
  69.                         holding[i] = Double.parseDouble(data[i]);
  70.                     }
  71.                     // adds the data array to the main selection array.
  72.                     array.add(holding);
  73.                 }
  74.                 // Sets data loaded to true.
  75.                 dataIsLoaded = true;
  76.                 // Catch statement for file not found.
  77.             } catch (FileNotFoundException e) {
  78.                 // Throws stack trace to determine where exactly it's taking
  79.                 // place.
  80.                 e.printStackTrace();
  81.                 // Cause to simply provide an easy to say explanation for
  82.                 // user / client side.
  83.                 System.out.println(e.getCause());
  84.                 // Outputs the filename input so that things such as typos can
  85.                 // be
  86.                 // ruled out.
  87.                 System.out.println(filename);
  88.                 // Sets data loaded to false.
  89.                 dataIsLoaded = false;
  90.             }
  91.         }
  92.     }
  93.  
  94.     /**
  95.      * getAverage, inherited from CloudCoverInterface.
  96.      *
  97.      * @param :
  98.      *            Month month : A month enum to determine the average of a
  99.      *            specific month.
  100.      * @return result : Double Result The average for a month across the years
  101.      *         of data.
  102.      *
  103.      *         Determines the average over the years for a specific month.
  104.      */
  105.     @Override
  106.     public double getAverage(Month month) {
  107.         // Placeholder for final return.
  108.         double result;
  109.         // Null Check
  110.         if (month == null) {
  111.             // Error Code
  112.             result = -1;
  113.         }
  114.         /*
  115.          * If the array is null or empty returns a negative value Used a
  116.          * negative number because it's an impossible value where as 0 is a
  117.          * potentially possible value. In a real world program a negative value
  118.          * could be detected and be used to throw an exception stating there is
  119.          * a problem with the data.
  120.          */
  121.         // If data hasn't been loaded, return an error of -1.
  122.         else if (!dataIsLoaded) {
  123.             result = -1;
  124.         } else if (array == null || array.isEmpty()) {
  125.             result = -1;
  126.         }
  127.         // Else handles if not null nor empty.
  128.         else {
  129.             // Sum placeholder to begin making the average.
  130.             double sum = 0;
  131.             // Iterates through the years of data.
  132.             for (int i = 0; i < array.size(); i++) {
  133.                 // Check for improper value errors.
  134.                 if (array.get(i)[month.ordinal() + 1].equals(-1.0)) {
  135.                     // Sum doesn't change, simply "skips" over with an error
  136.                     // message.
  137.                     System.out.println("ERROR AT: YEAR: " + array.get(i)[0] + " MONTH: " + (month.ordinal() + 1));
  138.                 } else {
  139.                     /*
  140.                      * Adds together the values for that month throughout the
  141.                      * years. Ordinal month returns a value less than the months
  142.                      * actual value, and value 0 holds the year itself.
  143.                      */
  144.                     sum += (double) array.get(i)[month.ordinal() + 1];
  145.                 }
  146.             }
  147.             // result is final sum divided by the number of years.
  148.             result = sum / array.size();
  149.         }
  150.         // returns result.
  151.         return result;
  152.     }
  153.  
  154.     /**
  155.      * getAverage, inherited from CloudCoverInterface.
  156.      *
  157.      * @param year
  158.      *            : int year. The specific year which the average is made from.
  159.      *
  160.      * @return result : double result. Returns the average across all months for
  161.      *         a specified year.
  162.      *
  163.      *         Calculates the average for all months in a single year.
  164.      */
  165.     @Override
  166.     public double getAverage(int year) {
  167.         // Placeholder for final return.
  168.         double result = 0;
  169.         /*
  170.          * If the array is null or empty returns a negative value Used a
  171.          * negative number because it's an impossible value where as 0 is a
  172.          * potentially possible value. In a real world program a negative value
  173.          * could be detected and be used to throw an exception stating there is
  174.          * a problem with the data.
  175.          */
  176.         // If data hasn't been loaded, return an error of -1.
  177.         if (!dataIsLoaded) {
  178.             result = -1;
  179.         } else if (array == null || array.isEmpty()) {
  180.             result = -1;
  181.         }
  182.         // Else handles if not null nor empty.
  183.         else {
  184.             // Sum placeholder to begin making the average.
  185.             double sum = 0;
  186.             // Iterates through all the years searching for the correct one.
  187.             for (int i = 0; i < array.size(); i++) {
  188.                 // If its the correct year, begin data collection.
  189.                 if (year == (int) array.get(i)[0]) {
  190.                     // Begin iterating through the data of a year.
  191.                     for (int j = 1; j < array.get(i).length; j++) {
  192.                         // Check for improper value errors.
  193.                         if (array.get(i)[j].equals(-1.0)) {
  194.                             // Sum doesn't change, simply "skips" over with an
  195.                             // error
  196.                             // message.
  197.                             System.out.println("ERROR AT: YEAR: " + array.get(i)[0] + " MONTH: " + j);
  198.                         } else {
  199.                             // Sums the values together.
  200.                             sum += (double) array.get(i)[j];
  201.                         }
  202.                     }
  203.                     // Average = sum / number of months.
  204.                     result = sum / Month.values().length;
  205.                     // Breaks out of the for loop so it isn't reverted back to
  206.                     // -1.
  207.                     break;
  208.                 }
  209.                 // if it doesn't match the year result is -1.
  210.                 if (year != (int) array.get(i)[0]) {
  211.                     // Results now -1.
  212.                     result = -1;
  213.                 }
  214.             }
  215.  
  216.         }
  217.         // returns result.
  218.         return result;
  219.     }
  220.  
  221.     /**
  222.      * getAverage, inherited from CloudCoverInterface.
  223.      *
  224.      * @return result : double result. Returns the average of all data.
  225.      *
  226.      *         Calculates the average data across all months and years avalible.
  227.      */
  228.     @Override
  229.     public double getAverage() {
  230.         // Placeholder for final return.
  231.         double result;
  232.         /*
  233.          * If the array is null or empty returns a negative value Used a
  234.          * negative number because it's an impossible value where as 0 is a
  235.          * potentially possible value. In a real world program a negative value
  236.          * could be detected and be used to throw an exception stating there is
  237.          * a problem with the data.
  238.          */
  239.         // If data hasn't been loaded, return an error of -1.
  240.         if (!dataIsLoaded) {
  241.             result = -1;
  242.         } else if (array == null || array.isEmpty()) {
  243.             result = -1;
  244.         }
  245.         // Else handles if not null nor empty.
  246.         else {
  247.             // Sum placeholder to begin making the average.
  248.             double sum = 0;
  249.             // Iterates through all the years
  250.             for (int i = 0; i < array.size(); i++) {
  251.                 // Begin iterating through the data of a year.
  252.                 for (int j = 1; j < array.get(i).length; j++) {
  253.                     // Check for improper value errors.
  254.                     if (array.get(i)[j].equals(-1.0)) {
  255.                         // Sum doesn't change, simply "skips" over with an error
  256.                         // message.
  257.                         System.out.println("ERROR AT: YEAR: " + array.get(i)[0] + " MONTH: " + j);
  258.                     } else {
  259.                         // Sums the values together.
  260.                         sum += (double) array.get(i)[j];
  261.                     }
  262.                 }
  263.             }
  264.             // Average = sum / number of months.
  265.             result = sum / (Month.values().length * array.size());
  266.             // Breaks out of the for loop so it isn't reverted back to -1.
  267.  
  268.         }
  269.         // returns result.
  270.         return result;
  271.     }
  272.  
  273.     /**
  274.      * getMax, inherited from CloudCoverInterface.
  275.      *
  276.      * @param month
  277.      *            : Month month. A month used to determine the maximum of a
  278.      *            specific time frame.
  279.      * @return currentMax : double currentMax. The maximum value of the months
  280.      *         found throughout the years of data.
  281.      *
  282.      *         Calculates the maximum value that is found in a certain month.
  283.      */
  284.     @Override
  285.     public double getMax(Month month) {
  286.         // Holder Variable for the initial maximum. (None)
  287.         double currentMax = -1;
  288.         // Null Check
  289.         if (month == null) {
  290.             // Error Code
  291.             currentMax = -1;
  292.         }
  293.         /*
  294.          * If the array is null or empty returns a negative value Used a
  295.          * negative number because it's an impossible value where as 0 is a
  296.          * potentially possible value. In a real world program a negative value
  297.          * could be detected and be used to throw an exception stating there is
  298.          * a problem with the data.
  299.          */
  300.         // If data hasn't been loaded, return an error of -1.
  301.         else if (!dataIsLoaded) {
  302.             currentMax = -1;
  303.         } else if (array == null || array.isEmpty()) {
  304.             currentMax = -1;
  305.         }
  306.         // Else handles if not null nor empty.
  307.         else {
  308.             // Iterates through the years.
  309.             for (int i = 0; i < array.size(); i++) {
  310.                 // Check for improper value errors.
  311.                 if (array.get(i)[month.ordinal() + 1].equals(-1.0)) {
  312.                     // Sum doesn't change, simply "skips" over with an error
  313.                     // message.
  314.                     System.out.println("ERROR AT: YEAR: " + array.get(i)[0] + " MONTH: " + (month.ordinal() + 1));
  315.                 } else {
  316.                     // If the current max value is less than the value at year i
  317.                     // and
  318.                     // at
  319.                     // the month then :
  320.                     if (currentMax < (double) array.get(i)[month.ordinal() + 1]) {
  321.                         // Sets current max to the latest maximum value.
  322.                         currentMax = (double) array.get(i)[month.ordinal() + 1];
  323.                     }
  324.                 }
  325.             }
  326.         }
  327.         // Returns the maximum value.
  328.         return currentMax;
  329.     }
  330.  
  331.     /**
  332.      * getMax, inherited from CloudCoverInterface.
  333.      *
  334.      * @param year
  335.      *            : int year. The year for which a a max is determined from.
  336.      *
  337.      * @return currentMax : double currentMax. Value for the maximum value in a
  338.      *         year.
  339.      *
  340.      *         Calculates a value of the the current maximum of the year
  341.      *         specified.
  342.      */
  343.     @Override
  344.     public double getMax(int year) {
  345.         // Holder Variable for the initial maximum. (None)
  346.         double currentMax = -1;
  347.         /*
  348.          * If the array is null or empty returns a negative value Used a
  349.          * negative number because it's an impossible value where as 0 is a
  350.          * potentially possible value. In a real world program a negative value
  351.          * could be detected and be used to throw an exception stating there is
  352.          * a problem with the data.
  353.          */
  354.         // If data hasn't been loaded, return an error of -1.
  355.         if (!dataIsLoaded) {
  356.             currentMax = -1;
  357.         } else if (array == null || array.isEmpty()) {
  358.             currentMax = -1;
  359.         }
  360.         // Else handles if not null nor empty.
  361.         else {
  362.             // Iterates through the years selecting the correct year..
  363.             for (int i = 0; i < array.size(); i++) {
  364.                 // If the year is the correct year continue.
  365.                 if (year == (int) array.get(i)[0]) {
  366.                     // Iterate through all the months in the year.
  367.                     for (int j = 1; j < array.get(i).length; j++) {
  368.                         // Check for improper value errors.
  369.                         if (array.get(i)[j].equals(-1.0)) {
  370.                             // Sum doesn't change, simply "skips" over with an
  371.                             // error
  372.                             // message.
  373.                             System.out.println("ERROR AT: YEAR: " + array.get(i)[0] + " MONTH: " + j);
  374.                         } else {
  375.                             // If the value here is more than the current Max
  376.                             // value
  377.                             if (currentMax < (double) array.get(i)[j]) {
  378.                                 // Then set that value as the new current max
  379.                                 // value.
  380.                                 currentMax = (double) array.get(i)[j];
  381.                             }
  382.                         }
  383.                     }
  384.                     // Break out of the loop improving efficiency, not iterating
  385.                     // through the rest of the years.
  386.                     // On that note, seriously a HashMap would be much better
  387.                     // suited
  388.                     // for this.
  389.                     break;
  390.                 }
  391.             }
  392.         }
  393.         // Returns the current and final maximum for the year.
  394.         return currentMax;
  395.     }
  396.  
  397.     /**
  398.      * getMax, inherited from CloudCoverInterface.
  399.      *
  400.      * @return currentMax : double currentMax. returns the final current max
  401.      *         value for the entire data set.
  402.      *
  403.      *         Calculates the maximum value for all the data available.
  404.      */
  405.     @Override
  406.     public double getMax() {
  407.         // Holder Variable for the initial maximum. (None)
  408.         double currentMax = -1;
  409.         /*
  410.          * If the array is null or empty returns a negative value Used a
  411.          * negative number because it's an impossible value where as 0 is a
  412.          * potentially possible value. In a real world program a negative value
  413.          * could be detected and be used to throw an exception stating there is
  414.          * a problem with the data.
  415.          */
  416.         // If data hasn't been loaded, return an error of -1.
  417.         if (!dataIsLoaded) {
  418.             currentMax = -1;
  419.         } else if (array == null || array.isEmpty()) {
  420.             currentMax = -1;
  421.         }
  422.         // Else handles if not null nor empty.
  423.         else {
  424.             // Iterating through the years.
  425.             for (int i = 0; i < array.size(); i++) {
  426.                 // Iterating through each month.
  427.                 for (int j = 1; j < array.get(i).length; j++) {
  428.                     // Check for improper value errors.
  429.                     if (array.get(i)[j].equals(-1.0)) {
  430.                         // Sum doesn't change, simply "skips" over with an error
  431.                         // message.
  432.                         System.out.println("ERROR AT: YEAR: " + array.get(i)[0] + " MONTH: " + j);
  433.                     } else {
  434.                         // If the current max value is less than the value at
  435.                         // year i
  436.                         // and
  437.                         // at the month j being iterated through then :
  438.                         if (currentMax < (double) array.get(i)[j]) {
  439.                             // Sets current max to the latest maximum value.
  440.                             currentMax = (double) array.get(i)[j];
  441.                         }
  442.                     }
  443.                 }
  444.             }
  445.         }
  446.         // Returns the maximum value.
  447.         return currentMax;
  448.     }
  449.  
  450.     /**
  451.      * getMin(month), inherited from CloudCoverInterface.
  452.      *
  453.      * @return currentMin : double currentMin .Smallest rainfall within a month.
  454.      *
  455.      *         Finds the smallest set of rainfall in all the years of a specific
  456.      *         month.
  457.      */
  458.     @Override
  459.     public double getMin(Month month) {
  460.         // Holder Variable for the initial maximum. (None)
  461.         double currentMin = Double.MAX_VALUE;
  462.         // Null Check
  463.         if (month == null) {
  464.             // Error Code
  465.             currentMin = -1;
  466.         }
  467.         /*
  468.          * If the array is null or empty returns a negative value Used a
  469.          * negative number because it's an impossible value where as 0 is a
  470.          * potentially possible value. In a real world program a negative value
  471.          * could be detected and be used to throw an exception stating there is
  472.          * a problem with the data.
  473.          */
  474.         // If data hasn't been loaded, return an error of -1.
  475.         if (!dataIsLoaded) {
  476.             currentMin = -1;
  477.         } else if (array == null || array.isEmpty()) {
  478.             currentMin = -1;
  479.         }
  480.         // Else handles if not null nor empty.
  481.         else {
  482.             // Iterates through the years.
  483.             for (int i = 0; i < array.size(); i++) {
  484.                 // Check for improper value errors.
  485.                 if (array.get(i)[month.ordinal() + 1].equals(-1.0)) {
  486.                     // Sum doesn't change, simply "skips" over with an error
  487.                     // message.
  488.                     System.out.println("ERROR AT: YEAR: " + array.get(i)[0] + " MONTH: " + (month.ordinal() + 1));
  489.                 } else {
  490.                     // If the current max value is less than the value at year i
  491.                     // and
  492.                     // at
  493.                     // the month then :
  494.                     if (currentMin > (double) array.get(i)[month.ordinal() + 1]) {
  495.                         // Sets current max to the latest maximum value.
  496.                         currentMin = (double) array.get(i)[month.ordinal() + 1];
  497.                     }
  498.                 }
  499.             }
  500.         }
  501.         // Returns the maximum value.
  502.         return currentMin;
  503.     }
  504.  
  505.     /**
  506.      * getMin(year), inherited from CloudCoverInterface.
  507.      *
  508.      * @return currentMin : double currentMin .Smallest rainfall within a
  509.      *         specific year..
  510.      *
  511.      *         Finds the smallest set of rainfall in a specific year.
  512.      */
  513.     @Override
  514.     public double getMin(int year) {
  515.         // Holder Variable for the initial maximum. (None)
  516.         double currentMin = Double.MAX_VALUE;
  517.         /*
  518.          * If the array is null or empty returns a negative value Used a
  519.          * negative number because it's an impossible value where as 0 is a
  520.          * potentially possible value. In a real world program a negative value
  521.          * could be detected and be used to throw an exception stating there is
  522.          * a problem with the data.
  523.          */
  524.         // If data hasn't been loaded, return an error of -1.
  525.         if (!dataIsLoaded) {
  526.             currentMin = -1;
  527.         } else if (array == null || array.isEmpty()) {
  528.             currentMin = -1;
  529.         }
  530.         // Else handles if not null nor empty.
  531.         else {
  532.             // Iterates through the years selecting the correct year..
  533.             for (int i = 0; i < array.size(); i++) {
  534.                 // If the year is the correct year continue.
  535.                 if (year == (int) array.get(i)[0]) {
  536.                     // Iterate through all the months in the year.
  537.                     for (int j = 1; j < array.get(i).length; j++) {
  538.                         // Check for improper value errors.
  539.                         if (array.get(i)[j].equals(-1.0)) {
  540.                             // Sum doesn't change, simply "skips" over with an
  541.                             // error
  542.                             // message.
  543.                             System.out.println("ERROR AT: YEAR: " + array.get(i)[0] + " MONTH: " + j);
  544.                         } else {
  545.                             // If the value here is more than the current Max
  546.                             // value
  547.                             if (currentMin > (double) array.get(i)[j]) {
  548.                                 // Then set that value as the new current max
  549.                                 // value.
  550.                                 currentMin = (double) array.get(i)[j];
  551.                             }
  552.                         }
  553.                     }
  554.                     // Break out of the loop improving efficiency, not iterating
  555.                     // through the rest of the years.
  556.                     // On that note, seriously a HashMap would be much better
  557.                     // suited
  558.                     // for this.
  559.                     break;
  560.                 }
  561.             }
  562.         }
  563.         // Returns the current and final maximum for the year.
  564.         return currentMin;
  565.     }
  566.  
  567.     /**
  568.      * getMin, inherited from CloudCoverInterface.
  569.      *
  570.      * @return currentMin : double currentMin .Smallest rainfall within all the
  571.      *         data.
  572.      *
  573.      *         Finds the smallest set of rainfall in all the data provided.
  574.      */
  575.     @Override
  576.     public double getMin() {
  577.         // Holder Variable for the initial maximum. (None)
  578.         double currentMin = Double.MAX_VALUE;
  579.         /*
  580.          * If the array is null or empty returns a negative value Used a
  581.          * negative number because it's an impossible value where as 0 is a
  582.          * potentially possible value. In a real world program a negative value
  583.          * could be detected and be used to throw an exception stating there is
  584.          * a problem with the data.
  585.          */
  586.         // If data hasn't been loaded, return an error of -1.
  587.         if (!dataIsLoaded) {
  588.             currentMin = -1;
  589.         } else if (array == null || array.isEmpty()) {
  590.             currentMin = -1;
  591.         }
  592.         // Else handles if not null nor empty.
  593.         else {
  594.             // Iterating through the years.
  595.             for (int i = 0; i < array.size(); i++) {
  596.                 // Iterating through each month.
  597.                 for (int j = 1; j < array.get(i).length; j++) {
  598.                     // Check for improper value errors.
  599.                     if (array.get(i)[j].equals(-1.0)) {
  600.                         // Sum doesn't change, simply "skips" over with an error
  601.                         // message.
  602.                         System.out.println("ERROR AT: YEAR: " + array.get(i)[0] + " MONTH: " + (j));
  603.                     } else {
  604.                         // If the current max value is less than the value at
  605.                         // year i
  606.                         // and
  607.                         // at the month j being iterated through then :
  608.                         if (currentMin > (double) array.get(i)[j]) {
  609.                             // Sets current max to the latest maximum value.
  610.                             currentMin = (double) array.get(i)[j];
  611.                         }
  612.                     }
  613.                 }
  614.             }
  615.         }
  616.         // Returns the maximum value.
  617.         return currentMin;
  618.     }
  619.  
  620.     /**
  621.      * coverageToArea, inherited from CloudCoverInterface.
  622.      *
  623.      * @param coverage
  624.      *            : double coverage. The percent of the lake covered by clouds.
  625.      *            [0, 100]
  626.      * @return area : double area. Returns the surface area that the clouds are
  627.      *         covering.
  628.      *
  629.      *         Takes the percentage of cloud cover of lake superior and converts
  630.      *         it into surface area.
  631.      */
  632.     @Override
  633.     public double coverageToArea(double coverage) {
  634.         // creates the storage variable.
  635.         double area;
  636.         // Checks to see if the coverage is even a number, and that it's more
  637.         // than one.
  638.         if ((Double.isNaN(coverage)) || (coverage < 0)) {
  639.             // If not returns -1 as showing an error has occurred.
  640.             area = -1;
  641.         } else {
  642.             // Everything is working properly, now simply uses mathematics to
  643.             // convert.
  644.             area = CloudCoverInterface.SURFACE_AREA * (coverage / 100);
  645.         }
  646.         // Returns the final surface area.
  647.         return area;
  648.     }
  649.  
  650.     /**
  651.      * beginYear, inherited from CloudCoverInterface.
  652.      *
  653.      * @return minYear : The year the data begins.
  654.      *
  655.      *         Iterates through the data finding what year the data begins.
  656.      */
  657.     @Override
  658.     public int beginYear() {
  659.         // Start off at a value as high as possible
  660.         int minYear = Integer.MAX_VALUE;
  661.  
  662.         // if data has not been loaded.
  663.         if (!dataIsLoaded) {
  664.             // Shows an error has occurred.
  665.             minYear = -1;
  666.         } else if ((array == null || array.isEmpty())) {
  667.             // Shows an error has occurred.
  668.             minYear = -1;
  669.         } else {
  670.             // Begin for loop for iterating through the arrays.
  671.             for (int i = 0; i < array.size(); i++) {
  672.                 // If the current minYear is greater than the year found in the
  673.                 // array,
  674.                 if (minYear < (int) array.get(i)[0]) {
  675.                     // Then set the minYear as the current year in the array.
  676.                     minYear = (int) array.get(i)[0];
  677.                 }
  678.             }
  679.         }
  680.         // Return the final minimum year.
  681.         return minYear;
  682.     }
  683.  
  684.     /**
  685.      * endYear, inherited from CloudCoverInterface.
  686.      *
  687.      * @return maxYear: Returns the final year of data in from the given data.
  688.      *
  689.      *         Iterates through data finding the final year avalible.
  690.      */
  691.     @Override
  692.     public int endYear() {
  693.         // Start with the lowest value possible.
  694.         int maxYear = Integer.MIN_VALUE;
  695.         // if data has not been loaded.
  696.         if (!dataIsLoaded) {
  697.             // Shows an error has occurred.
  698.             maxYear = -1;
  699.         } else if ((array == null || array.isEmpty())) {
  700.             // Shows an error has occurred.
  701.             maxYear = -1;
  702.         } else {
  703.             // Iterate through the arrays of data.
  704.             for (int i = 0; i < array.size(); i++) {
  705.                 // If the current maxYear is less than the year in the array,
  706.                 if (maxYear > (int) array.get(i)[0]) {
  707.                     // Then set the maxYEar as the current year in the array.
  708.                     maxYear = (int) array.get(i)[0];
  709.                 }
  710.             }
  711.         }
  712.         // Return the final latest year.
  713.         return maxYear;
  714.     }
  715.  
  716.     public static void main(String[] args) throws FileNotFoundException {
  717.         LakeSuperiorCloudCover self = new LakeSuperiorCloudCover();
  718.         self.loadData("SuperiorCloudCover1.dat");
  719.     }
  720. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement