Note that there are some explanatory texts on larger screens.

plurals
  1. POArrays Java - Trying to remove user input requirements
    primarykey
    data
    text
    <p>I am creating a program that calculates rainfall for the year etc. I had the first block of code below working with the user input, perfectly. However, I am trying to change the program now, so that the array values are specified (I'm basically trying to eliminate the user input). </p> <p>Why isn't the second block of code working? I am getting errors at the bottom for r.getTotalRainFall, r.getAverageRainFall etc. </p> <p>Please note that I had to introduce the array thisYear (this is required). </p> <p>CODE BLOCK #1:</p> <pre><code>import java.util.*; public class Rainfall { Scanner in = new Scanner(System.in); int month = 12; double total = 0; double average; double months[]; public Rainfall() { months = new double[12]; } public void enterMonthData() { for (int n = 1; n &lt;= month; n++) { System.out.print("Enter the rainfall (in inches) for month #" + n + ": "); months[n - 1] = in.nextDouble(); // Input Validation - Cannot accept a negative number while (months[n - 1] &lt; 0) { System.out.print("Rainfall must be at least 0. Please enter a new value."); months[n - 1] = in.nextDouble(); } } } public double getTotalRainFall() { total = 0; for (int i = 0; i &lt; 12; i++) { total = total + months[i]; } return total; } public double getAverageRainFall() { average = total / 12; return average; } /** * Returns the index of the month with the highest rainfall. */ public int getHighestMonth() { int highest = 0; for (int i = 0; i &lt; 12; i++) { if (months[i] &gt; months[highest]) { highest = i; } } return highest; } /** * Returns the index of the month with the lowest rainfall. */ public int getLowestMonth() { int lowest = 0; for (int i = 0; i &lt; 12; i++) { if (months[i] &lt; months[lowest]) { lowest = i; } } return lowest; } public static void main(String[]args) { Rainfall r = new Rainfall(); r.enterMonthData(); System.out.println("The total rainfall for this year is " + r.getTotalRainFall()); System.out.println("The average rainfall for this year is " + r.getAverageRainFall()); int lowest = r.getLowestMonth(); int highest = r.getHighestMonth(); System.out.println("The month with the highest amount of rain is " + (highest+1) + " with " + r.months[highest] + " inches"); System.out.println("The month with the lowest amount of rain is " + (lowest+1) + " with " + r.months[lowest] + " inches"); } } </code></pre> <p>CODE BLOCK #2:</p> <pre><code>package rain; public class Rain { int month = 12; double total = 0; double average; double getRainAt[]; public Rain { getRainAt = new double[12]; } double getTotalRainFall() { total = 0; for (int i = 0; i &lt; 12; i++) { total = total + getRainAt[i]; } return total; } double getAverageRainFall() { average = total / 12; return average; } int getHighestMonth() { int high = 0; for (int i = 0; i &lt; 12; i++) { if (getRainAt[i] &gt; getRainAt[high]) { high = i; } } return high; } int getLowestMonth() { int low = 0; for (int i = 0; i &lt; 12; i++) { if (getRainAt[i] &lt; getRainAt[low]) { low = i; } } return low; } public static void main(String[] args) { // Create an array of rainfall figures. double[] thisYear = {1.6, 2.1, 1.7, 3.5, 2.6, 3.7, 3.9, 2.6, 2.9, 4.3, 2.4, 3.7 }; int high; // The high month int low; // The low month // Create a RainFall object initialized with the figures // stored in the thisYear array. Rainfall r = new Rainfall(thisYear); // Display the statistics. System.out.println("The total rainfall for this year is " + r.getTotalRainFall(); System.out.println("The average rainfall for this year is " + r.getAverageRainFall()); high = r.getHighestMonth(); System.out.println("The month with the highest amount of rain " + "is " + (high+1) + " with " + r.getRainAt(high) + " inches."); low = r.getLowestMonth(); System.out.println("The month with the lowest amount of rain " + "is " + (low+1) + " with " + r.getRainAt(low) + " inches."); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload