Method troubles returning a value from an array

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 397 Views - Last Post: 05 May 2013 - 07:59 PM Rate Topic: -----

#1 megabit  Icon User is offline

  • D.I.C Head

Reputation: 17
  • View blog
  • Posts: 79
  • Joined: 03-May 13

Method troubles returning a value from an array

Posted 05 May 2013 - 02:07 PM

Hello, having trouble getting this class to work with this SnowfallDemo. I need to create methods that return the values to the SnowfallDemo as you can see.

getTotalSnowFall : returns the total sum of the array
getAverageSnowFall : returns the average of the array
getHighestMonth : returns the index of the month containing the highest snowfall
getLowestMonth : returns the index of the month containing the lowest snowfall
getSnowAt : returns the value that is stored in the index of the high and low

The first four methods I believe I have done perfectly, I'm not sure about the last one getSnowAt, is my logic correct, is it returning the value its supposed to?

Brings me to my next error, at the moment I'm only getting one error in my getSnowAt method. I'm not really sure how to return the value that is indexed in the 'high' and 'low' indexes of the methods..
THe error I have is: "cannot find symbol"
return thisYear[index];
                ^






Here is the SnowfallDemo

public class SnowfallDemo
{
   public static void main(String[] args)
   {
      // Create an array of snowfall 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 SnowFall object initialized with the figures
      // stored in the thisYear array.
      Snowfall r = new Snowfall(thisYear);
      
      // Display the statistics.
      System.out.println("The total snowfall for this year is " +
                         r.getTotalSnowFall());
      System.out.println("The average snowfall for this year is " +
                         r.getAverageSnowFall());
      high = r.getHighestMonth();
      System.out.println("The month with the highest amount of snow " +
                         "is " + (high+1) + " with " + r.getSnowAt(high) +
                         " inches.");
      low = r.getLowestMonth();
      System.out.println("The month with the lowest amount of snow " +
                         "is " + (low+1) + " with " + r.getSnowAt(low) +
                         " inches.");
   }
}




Here is my class


public class Snowfall
{
    private double[] thisYear; //References array data
     
    public Snowfall (double[] y)
    {
        thisYear = new double[y.length];
        for (int index = 0; index < y.length; index++)
            thisYear[index] = y[index];
    }
     
    //Get method that returns total snowfall.
    public double getTotalSnowFall()
    {
        double totalSnowFall = 0.0; //Accumulator
         
        //Add up the values from the array
        for (double value : thisYear)
            totalSnowFall += value;
             
        //return the total
        return totalSnowFall;
    }
     
    //Average monthly snowfall
    public double getAverageSnowFall()
    {
        return getTotalSnowFall() / thisYear.length;
    }
     
    //Find month with most and least amount of snow
    public double getHighestMonth()
    {
        //Highest amount
        //Store first value in array in the variable highest.
        double high = thisYear[0];
         
        //Search array for high value
        for (int index = 1; index < thisYear.length; index++)
        {
            if (thisYear[index] > high)
                high = thisYear[index];
        }
         
        //Return value thats highest
        return highest;
    }
 
    public double getLowestMonth()
    {
        //Lowest amount
        double low = thisYear[0];
         
        //Search array for low value
        for (int index = 1; index < thisYear.length; index++)
        {
            if (thisYear[index] < low)
                low = thisYear[index];
        }
         
        //Return low value
        return lowest;
    } 

    public double getSnowAt()
    {
       return thisYear[index]
    }

}



I was getting more errors but hopefully I have fixed the up for the moment.

Is This A Good Question/Topic? 0
  • +

Replies To: Method troubles returning a value from an array

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10595
  • View blog
  • Posts: 39,247
  • Joined: 27-December 08

Re: Method troubles returning a value from an array

Posted 05 May 2013 - 02:15 PM

You have two problems with your getSnowAt() method. First, it does not have a parameter accepting an index field. So: public double getSnowAt(int index). Second, end your return statement with a semi-colon.

Also, I wanted to clarify your instructions:

Quote

getSnowAt : returns the value that is stored in the index of the high and low


A method can only return one value, so in this instance it really doesn't make sense for getSnowAt() to accept two indices if you are returning a single double.

This post has been edited by macosxnerd101: 05 May 2013 - 03:35 PM
Reason for edit:: Fixed typo

Was This Post Helpful? 1
  • +
  • -

#3 megabit  Icon User is offline

  • D.I.C Head

Reputation: 17
  • View blog
  • Posts: 79
  • Joined: 03-May 13

Re: Method troubles returning a value from an array

Posted 05 May 2013 - 03:31 PM

View Postmacosxnerd101, on 05 May 2013 - 02:15 PM, said:

You have two problems with your getSnowAt() method. First, it does not have a parameter accepting an index field. So: public double getSnowAt(ind index). Second, end your return statement with a semi-colon.

Also, I wanted to clarify your instructions:

Quote

getSnowAt : returns the value that is stored in the index of the high and low


A method can only return one value, so in this instance it really doesn't make sense for getSnowAt() to accept two indices if you are returning a single double.


Thankyou for the quick reply.
Should public double getSnowAt(ind index) be public double getSnowAt(int index) ? What is ind? Also, in my source code I have closed the method with a semi-colon, there must of been an error in me copying it across.

I thought that the method was having this problem for the reason you stated, but I am really stumped on the solution to returning the correct value for both high and low by using only one method...
Here's the exact wording: getSnowAt - Instance method that returns a specified value in the array. It accepts the element number to return as its argument.

Actually, I just compiled it with public double getSnowAt(int index) and it compiled perfectly thankyou! I did have public double getSnowAt(ind index) in the code earlier but for some reason I took it out.

Now I have another problem.
When I run the SnowFallDemo I get the error.

error: Possible loss of precision
high = r.getHighestMonth();
                        ^
required: int
found: double

error: Possible loss of precision
high = r.getHighestMonth();
                        ^
required: int
found: double



So, I need to change these methods return type to int? I tried changing that but it didn't work. Do I have to add the return type int inside the brackets or something?

Here is the actual error, can't seem to edit my other post.

error: Possible loss of precision
high = r.getHighestMonth();
                        ^
required: int
found: double

error: Possible loss of precision
high = r.getLowestMonth();
                       ^
required: int
found: double


Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10595
  • View blog
  • Posts: 39,247
  • Joined: 27-December 08

Re: Method troubles returning a value from an array

Posted 05 May 2013 - 03:36 PM

Sorry about the typo on the parameter! As for your new error, change the type of high from int to double. That should fix the problem.
Was This Post Helpful? 1
  • +
  • -

#5 megabit  Icon User is offline

  • D.I.C Head

Reputation: 17
  • View blog
  • Posts: 79
  • Joined: 03-May 13

Re: Method troubles returning a value from an array

Posted 05 May 2013 - 03:53 PM

View Postmacosxnerd101, on 05 May 2013 - 03:36 PM, said:

Sorry about the typo on the parameter! As for your new error, change the type of high from int to double. That should fix the problem.


Yeah I tried doing that but I ended up getting more errors than I had before.

I tried changing

public double getHighestMonth()
{
    double high = thisYear[0];
    for (int index = 1; index < thisYear.length index++)
         {
            if (thisYear[index] > high)
                high = thisYear[index];
         }
     return high;
}



to...

public int getHighestMonth()
{
    double int = thisYear[0];
    for (int index = 1; index < thisYear.length index++)
         {
            if (thisYear[index] > high)
                high = thisYear[index];
         }
     return high;
}



But I get more errors, is this what you mean by change the type? Thanks for all your help so far.
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10595
  • View blog
  • Posts: 39,247
  • Joined: 27-December 08

Re: Method troubles returning a value from an array

Posted 05 May 2013 - 03:57 PM

You can't name a variable using a reserved word. So double int implies that you are trying to name your variable int, which is illegal.

The variable that I'm referring to is this one: high = r.getHighestMonth();. Make that a double rather than an int.
Was This Post Helpful? 1
  • +
  • -

#7 megabit  Icon User is offline

  • D.I.C Head

Reputation: 17
  • View blog
  • Posts: 79
  • Joined: 03-May 13

Re: Method troubles returning a value from an array

Posted 05 May 2013 - 04:09 PM

View Postmacosxnerd101, on 05 May 2013 - 03:57 PM, said:

You can't name a variable using a reserved word. So double int implies that you are trying to name your variable int, which is illegal.

The variable that I'm referring to is this one: high = r.getHighestMonth();. Make that a double rather than an int.


Thankyou for our hastily reply. 'Double int' was a spelling mistake on my behalf, and should have been 'int high', sorry about that.

The variable high = r.getHighestMonth(); is in the SnowFallDemo file, and I can only make changes to the Snowfall class, should have stated that earlier. Unless I'm not quiet getting it..
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10595
  • View blog
  • Posts: 39,247
  • Joined: 27-December 08

Re: Method troubles returning a value from an array

Posted 05 May 2013 - 04:19 PM

You want to return the index of the lowest month then, not the actual value at the lowest month. So the return type should be int.
Was This Post Helpful? 1
  • +
  • -

#9 megabit  Icon User is offline

  • D.I.C Head

Reputation: 17
  • View blog
  • Posts: 79
  • Joined: 03-May 13

Re: Method troubles returning a value from an array

Posted 05 May 2013 - 05:15 PM

View Postmacosxnerd101, on 05 May 2013 - 04:19 PM, said:

You want to return the index of the lowest month then, not the actual value at the lowest month. So the return type should be int.


Yes you are right, I want to return the index that contains the highest value. But how do I do that? I know how to return a specific value in the array if I already know its location, like thisYear[9] would return the value stored in the 9th index of the array... Trying to implement that philosophy for this array leaves me with no answer though. I've tried returning:

public double getHighestMonth()
{  
    double high = thisYear[0]; // 

     for (int index = 1; index < thisYear.length index++)  

         {  

             if (thisYear[index] > high)  

                high = thisYear[index];  

        }  

     return index;  
 } 


I've tried returning the index, and returning everything except the right answer it seems. Should I be creating another variable that stores the number of iterations or something?

This post has been edited by macosxnerd101: 05 May 2013 - 05:45 PM
Reason for edit:: Please use code tags

Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10595
  • View blog
  • Posts: 39,247
  • Joined: 27-December 08

Re: Method troubles returning a value from an array

Posted 05 May 2013 - 05:48 PM

First, start by changing the return type of the method like I suggested. Second, rather than storing double high = thisYear[0];, store int high = 0;. Then compare thisYear[temp] and thisYear[high].
Was This Post Helpful? 1
  • +
  • -

#11 megabit  Icon User is offline

  • D.I.C Head

Reputation: 17
  • View blog
  • Posts: 79
  • Joined: 03-May 13

Re: Method troubles returning a value from an array

Posted 05 May 2013 - 06:17 PM

View Postmacosxnerd101, on 05 May 2013 - 05:48 PM, said:

First, start by changing the return type of the method like I suggested. Second, rather than storing double high = thisYear[0];, store int high = 0;. Then compare thisYear[temp] and thisYear[high].


I have implemented the changes. By compare what do you mean? see if they are equal? also, where what is this year[temp]? I haven't used any value named temp, do you mean thisYear[index]?

Thank you in advance and for previous postings.
Was This Post Helpful? 0
  • +
  • -

#12 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10595
  • View blog
  • Posts: 39,247
  • Joined: 27-December 08

Re: Method troubles returning a value from an array

Posted 05 May 2013 - 06:42 PM

Quote

By compare what do you mean?

You're trying to find the largest (or smallest) element in the array, correct?

Quote

also, where what is this year[temp]? I haven't used any value named temp, do you mean thisYear[index]?

Yes.
Was This Post Helpful? 1
  • +
  • -

#13 megabit  Icon User is offline

  • D.I.C Head

Reputation: 17
  • View blog
  • Posts: 79
  • Joined: 03-May 13

Re: Method troubles returning a value from an array

Posted 05 May 2013 - 07:18 PM

View Postmacosxnerd101, on 05 May 2013 - 06:42 PM, said:

You're trying to find the largest (or smallest) element in the array, correct?



Yes, kind of. I am trying to the find the highest (or lowest) value that is stored in the array. But I am also trying to find the subscript that is associated with that element.

For example:

{    
    int high = thisYear[0];   

    for (int index = 1; index < thisYear.length index++)    

    {    
      if (thisYear[index] > high)    
      high = thisYear[index];    
    }
}


According to my textbook. The following code iterates through the array checking if each element is greater than the current element stored in 'high'. If it is greater it is replaced.

So the element 4.3 which has the subscript 9 will be stored in 'high'. But how do I find its subscript?

The getHighestMonth function returns the subscript that contains the highest (or lowest) element.
The getSnowAt function returns the element high (or low) that is contained in those particular subscripts.

Sorry if that's confusing. I have been working on this for days and just can't figure it out. Literally dreaming about it at night. :P/>
Was This Post Helpful? 0
  • +
  • -

#14 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10595
  • View blog
  • Posts: 39,247
  • Joined: 27-December 08

Re: Method troubles returning a value from an array

Posted 05 May 2013 - 07:20 PM

I already told you what changes you needed to make. See post #10.
Was This Post Helpful? 2
  • +
  • -

#15 megabit  Icon User is offline

  • D.I.C Head

Reputation: 17
  • View blog
  • Posts: 79
  • Joined: 03-May 13

Re: Method troubles returning a value from an array

Posted 05 May 2013 - 07:58 PM

View Postmacosxnerd101, on 05 May 2013 - 07:20 PM, said:

I already told you what changes you needed to make. See post #10.


Thankyou very much for helping me and being so patient. :D I finally figured it out and it is working as expected now! :D :D :D
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2