Results 1 to 2 of 2

Thread: Arrays trouble

  1. #1
    gto400no1 is offline Member
    Join Date
    Feb 2010
    Posts
    14
    Rep Power
    0

    Default 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:
    ----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.
    Thanks in advanced!
    Last edited by gto400no1; 04-13-2010 at 11:18 PM.

  2. #2
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    Your problems:
    1. Weird. Should really not have happened.
    2. (and also 4.). You're supposed to implement getRainAt() method, and you didn't, so it yells at you. Implement the method.
    3. The testing program expects an integer, and you're returning a double. Return an integer instead.

Similar Threads

  1. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 02:40 AM
  2. HELP: Still having trouble getting arrays :(
    By Psyclone in forum New To Java
    Replies: 4
    Last Post: 02-06-2010, 02:05 AM
  3. Here comes trouble... :-)
    By sargehendricks in forum Introductions
    Replies: 1
    Last Post: 04-23-2009, 04:18 PM
  4. having some trouble
    By Unknown1369 in forum New To Java
    Replies: 13
    Last Post: 07-22-2008, 12:52 AM
  5. HELP: Trouble with partial filled arrays
    By daigre7 in forum New To Java
    Replies: 1
    Last Post: 04-07-2008, 03:05 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •