Results 1 to 2 of 2
Thread: Arrays trouble
- 04-13-2010, 10:15 PM #1
Member
- Join Date
- Feb 2010
- Posts
- 14
- Rep Power
- 0
Arrays trouble
The assignment is to write a class to work with a rain averages program. The class I had to write finds the highest and lowest months rain total, calculates total for all months, and finds the average. My class compiles with no errors, the problem I am having is the program/main class file wont compile and I am not supposed to make any changes to it. My guess is I did something wrong in my class? Does anybody see an issue. The error tells me that it is having trouble with the get methods.
This is the Rainfall class I wrote:
Java Code:public class Rainfall { private double[] thisYear; //References array data public Rainfall (double[] y) { thisYear = new double[y.length]; for (int index = 0; index < y.length; index++) thisYear[index] = y[index]; } //Get method that returns total rainfall. public double getTotalRainFall() { double totalRainFall = 0.0; //Accumulator //Add up the values from the array for (double value : thisYear) totalRainFall += value; //return the total return totalRainFall; } //Average monthly rainfall public double getAverageRainFall() { return getTotalRainFall() / thisYear.length; } //Find month with most and least amount of rain public double getHighestMonth() { //Highest amount //Store first value in array in the variable highest. double highest = thisYear[0]; //Search array for high value for (int index = 1; index < thisYear.length; index++) { if (thisYear[index] > highest) highest = thisYear[index]; } //Return value thats highest return highest; } public double getLowestMonth() { //Lowest amount double lowest = thisYear[0]; //Search array for low value for (int index = 1; index < thisYear.length; index++) { if (thisYear[index] < lowest) lowest = thisYear[index]; } //Return low value return lowest; } }
This is the program I was supplied with and the class is to work with:
Java Code:public class RainfallDemo { public static void main(String[] args) { // Array with this year's rainfall data 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; // To hold the month with the highest amount int low; // To hold the month with the lowest amount // Create a Rainfall object initialized with // this year's data. Rainfall r = new Rainfall(thisYear); // Display the total rainfall. System.out.println("The total rainfall for this year is " + r.getTotalRainFall()); // Display the average rainfall. System.out.println("The average rainfall for this year is " + r.getAverageRainFall()); // Get and display the month with the highest rainfall. high = r.getHighestMonth(); System.out.println("The month with the highest amount of rain " + "is " + (high+1) + " with " + r.getRainAt(high) + " inches."); // Get and display the month with the lowest rainfall. low = r.getLowestMonth(); System.out.println("The month with the lowest amount of rain " + "is " + (low+1) + " with " + r.getRainAt(low) + " inches."); } }
This is the error report:
Thanks in advanced!----jGRASP exec: javac -g F:\Documents\School Documents\Intro to Java\Labs and Homework\Week 11\RainfallDemo.java
RainfallDemo.java:30: cannot find symbol
symbol : variable getHighestMonth
location: class Rainfall
high = r.getHighestMonth;
RainfallDemo.java:32: cannot find symbol
symbol : method getRainAt(int)
location: class Rainfall
"is " + (high+1) + " with " + r.getRainAt(high) +
RainfallDemo.java:36: possible loss of precision
found : double
required: int
low = r.getLowestMonth();
RainfallDemo.java:38: cannot find symbol
symbol : method getRainAt(int)
location: class Rainfall
"is " + (low+1) + " with " + r.getRainAt(low) +
4 errors
----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.Last edited by gto400no1; 04-13-2010 at 10:18 PM.
- 04-14-2010, 01:20 AM #2
Senior Member
- Join Date
- Mar 2010
- Posts
- 266
- Rep Power
- 4
Similar Threads
-
Arrays.sort... why sorting all arrays in class?
By innspiron in forum New To JavaReplies: 6Last Post: 03-23-2010, 01:40 AM -
HELP: Still having trouble getting arrays :(
By Psyclone in forum New To JavaReplies: 4Last Post: 02-06-2010, 01:05 AM -
Here comes trouble... :-)
By sargehendricks in forum IntroductionsReplies: 1Last Post: 04-23-2009, 03:18 PM -
having some trouble
By Unknown1369 in forum New To JavaReplies: 13Last Post: 07-21-2008, 11:52 PM -
HELP: Trouble with partial filled arrays
By daigre7 in forum New To JavaReplies: 1Last Post: 04-07-2008, 02:05 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks