13 Replies - 10032 Views - Last Post: 17 August 2013 - 08:16 PM Rate Topic: -----

#1 megabit   User is offline

  • D.I.C Head

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

trouble finding average of ArrayList

Posted 15 August 2013 - 11:31 PM

The trouble I'm having is,
non-static method findAverage(ArrayList) cannot be referenced from a static context



I'm also having trouble getting it work with integers. I get a run time exception.

import java.util.ArrayList;    //Needed to use the ArrayLists

public class TheDriverClass
{
    public static void main(String[] args)
    {
        //make ArrayList to store doubles
        ArrayList<Double> doubleList = new ArrayList<Double>();

        //populate doubleList
        doubleList.add(5.5);
        doubleList.add(7.7);
        doubleList.add(9.9);

        //display the average of the doubleList
        System.out.println("The doubleList average is: ");
        System.out.println(TheHelperClass.findAverage(doubleList));


/* ---- Now for Integers ---- */

        //make ArrayList to store integers
        ArrayList<Integer> integerList = new ArrayList<Integer>();

        //populate integerList
        integerList.add(5.5);
        integerList.add(7.7);
        integerList.add(9.9);

        //display the average of the integerList
        System.out.println("The integerList average is: ");
        System.out.println(TheHelperClass.findAverage(integerList));     
    }
}



The TheHelperClass.java

import java.util.ArrayList;    //Needed to use the ArrayLists

public class TheHelperClass<T extends Number>
{
    public double findAverage(ArrayList theListArray)
    {
        double average, currentNum;
        double sum = 0.0;
        
        //find the average
        for (int i = 0; i < theListArray.size(); i++)
        {
            currentNum = theListArray.get(i).doubleValue();
            sum += currentNum;
        }

        average = sum / theListArray.size();
        return average;
    }
}



I know I can just make findAverage() method static but is there another way without doing that?

This post has been edited by megabit: 15 August 2013 - 11:37 PM


Is This A Good Question/Topic? 0
  • +

Replies To: trouble finding average of ArrayList

#2 schutzzz   User is offline

  • D.I.C Regular
  • member icon

Reputation: 143
  • View blog
  • Posts: 342
  • Joined: 22-April 13

Re: trouble finding average of ArrayList

Posted 16 August 2013 - 12:45 AM

You shouldn't be doing all of this in the main method. Besides that the issue you'll have is you're not creating an instance of the helper class. Here's a look at something that will work for you. I only included the double list in the example i'm showing you.

import java.util.ArrayList;

public class Driver {
    
    ArrayList<Double> list = new ArrayList<>();
    
    Driver() {
        Helper h = new Helper(); // Here we are creating a new instance to our helper class with the reference name 'h'.
        
        list.add(5.5);
        list.add(7.7);
        list.add(9.9);
        System.out.println(h.findAverage(list)); // Access the Helper class with the reference name we declared earlier.
    }
    
    public static void main(String[] arguments) {
        new Driver();
    }
    
}



import java.util.ArrayList;

public class Helper {

    double sum;
    
    public double findAverage(ArrayList arrList) {
        for(int i = 0; i < arrList.size(); i++)
            sum += (double) arrList.get(i);
        return sum / arrList.size();
    }
    
}



On a side note, what is this doubleValue() method you call in line 13 of your helper class?

This post has been edited by schutzzz: 16 August 2013 - 12:51 AM

Was This Post Helpful? 1
  • +
  • -

#3 megabit   User is offline

  • D.I.C Head

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

Re: trouble finding average of ArrayList

Posted 16 August 2013 - 12:55 AM

View Postschutzzz, on 16 August 2013 - 12:45 AM, said:

You shouldn't be doing all of this in the main method. Besides that the issue you'll have is you're not creating an instance of the helper class. Here's a look at something that will work for you. I only included the double list in the example i'm showing you.

import java.util.ArrayList;

public class Driver {
    
    ArrayList<Double> list = new ArrayList<>();
    
    Driver() {
        Helper h = new Helper(); // Here we are creating a new instance to our helper class with the reference name 'h'.
        
        list.add(5.5);
        list.add(7.7);
        list.add(9.9);
        System.out.println(h.findAverage(list)); // Access the Helper class with the reference name we declared earlier.
    }
    
    public static void main(String[] arguments) {
        new Driver();
    }
    
}



import java.util.ArrayList;

public class Helper {

    double sum;
    
    public double findAverage(ArrayList arrList) {
        for(int i = 0; i < arrList.size(); i++)
            sum += (double) arrList.get(i);
        return sum / arrList.size();
    }
    
}



On a side note, what is this doubleValue() method you call in line 13 of your helper class?


Hello, thanks for the reply and the advice I am aplying it now. I need to do all the intialization in the main Driver class though and can't use Helper method. As for the doubleValue() method. It is supposed to convert the intgers to doubles...
Was This Post Helpful? 0
  • +
  • -

#4 schutzzz   User is offline

  • D.I.C Regular
  • member icon

Reputation: 143
  • View blog
  • Posts: 342
  • Joined: 22-April 13

Re: trouble finding average of ArrayList

Posted 16 August 2013 - 01:02 AM

You'll need to instantiate the helper class in order to use it's methods. If you are required to code the program in the main method it will still work, for example:

import java.util.ArrayList;

public class Driver {
    
    public static void main(String[] arguments) {
        ArrayList<Double> doubleList = new ArrayList<>();        
        Helper h = new Helper();
        
        doubleList.add(5.5);
        doubleList.add(7.7);
        doubleList.add(9.9);
        System.out.println("The doubleList average is:");
        System.out.println(h.findAverage(doubleList));
    }
    
}



The other way is preferred though.
Was This Post Helpful? 1
  • +
  • -

#5 megabit   User is offline

  • D.I.C Head

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

Re: trouble finding average of ArrayList

Posted 16 August 2013 - 01:11 AM

View Postschutzzz, on 16 August 2013 - 01:02 AM, said:

You'll need to instantiate the helper class in order to use it's methods. If you are required to code the program in the main method it will still work, for example:

import java.util.ArrayList;

public class Driver {
    
    public static void main(String[] arguments) {
        ArrayList<Double> doubleList = new ArrayList<>();        
        Helper h = new Helper();
        
        doubleList.add(5.5);
        doubleList.add(7.7);
        doubleList.add(9.9);
        System.out.println("The doubleList average is:");
        System.out.println(h.findAverage(doubleList));
    }
    
}



The other way is preferred though.


Ok, thankyou, I will remember that for future reference but for now I think I should do it the way it has been specified. I have it working now. Now, my real problem is this: when I create an ArrayList of Integers, it compiles but at run-time it generates an error when the program tries to find the average.

This is what I have.

Create the list.

ArrayList<Integer> integerList = new ArrayList<>();   
  
        Helper i = new Helper();
        
        integerList.add(5);
        integerList.add(7);
        integerList.add(9);

        System.out.println("The integerList average is:");
        System.out.println(i.findAverage(integerList));



and

    public double findAverage(ArrayList arrList)
    {  
        for(int i = 0; i < arrList.size(); i++)  
        {
          sum += (double) arrList.get(i);  
        }
          return sum / arrList.size();  
     }


The error at run time is:

"java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Double"

That is what the doubleValue() method was supposed to do.
I would like the method to be able to find the average of any numeric type such as float, long etc.

I understand why the error happens, but I can't for the life of me find the solution.

This post has been edited by megabit: 16 August 2013 - 01:13 AM

Was This Post Helpful? 0
  • +
  • -

#6 schutzzz   User is offline

  • D.I.C Regular
  • member icon

Reputation: 143
  • View blog
  • Posts: 342
  • Joined: 22-April 13

Re: trouble finding average of ArrayList

Posted 16 August 2013 - 01:15 AM

edit: Looking into doubleValue found in the Number class. doing some research on it and i'll try and get back to you

You could create a seperate method for finding the average of other numeric types I suppose. or you could do a little conversion within the method.

This post has been edited by schutzzz: 16 August 2013 - 01:31 AM

Was This Post Helpful? 1
  • +
  • -

#7 megabit   User is offline

  • D.I.C Head

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

Re: trouble finding average of ArrayList

Posted 16 August 2013 - 01:26 AM

View Postschutzzz, on 16 August 2013 - 01:15 AM, said:

If doubleValue() works, then use it. I've never even heard of it before and I can't find any documentation on that method in the ArrayList docs.

You could create a seperate method for finding the average of other numeric types I suppose. or you could do a little conversion within the method.



Yeah it doesn't work, thats the problem. :bigsmile: I can't find anything much on it either. I need to use this method for all numeric types like float, short etc.. Is there a simple way? Is it possible to cast them all to a double? But then what about longs?
Was This Post Helpful? 0
  • +
  • -

#8 megabit   User is offline

  • D.I.C Head

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

Re: trouble finding average of ArrayList

Posted 16 August 2013 - 01:56 AM

So the problem is.
I am trying to cast an Integer wrapper class to a Double wrapper class which can't done as I have been reading. What I would like to do is be able to cast any numeric wrapper class like
Integer, Double, Float
etc... to the primative data type
double
. I will keep looking.
Was This Post Helpful? 0
  • +
  • -

#9 schutzzz   User is offline

  • D.I.C Regular
  • member icon

Reputation: 143
  • View blog
  • Posts: 342
  • Joined: 22-April 13

Re: trouble finding average of ArrayList

Posted 16 August 2013 - 02:25 AM

Okay, I did a little bit of research and found a solution. Take a look at what's being done in these classes. A few changes to both so definitely consider reading up on the topic a little.

import java.util.ArrayList;

public class Driver {
    
    Driver() {
        ArrayList<Object> integerList = new ArrayList<>();
        ArrayList<Object> doubleList = new ArrayList<>();        
        Helper h = new Helper();
        
        integerList.add(5);
        integerList.add(7);
        integerList.add(9);
        System.out.println(h.findAverage(integerList));
        
        doubleList.add(new Double(5.5));
        doubleList.add(new Double(7.7));
        doubleList.add(new Double(9.9));
        System.out.println(h.findAverage(doubleList));
    }
    
    public static void main(String[] arguments) {
        new Driver();
    }
    
}



import java.util.ArrayList;

public class Helper {

    double sum;
    double temp;
    
    public double findAverage(ArrayList arrList) {
        Object[] o = new Object[arrList.size()];
        for(int i = 0; i < arrList.size(); i++) {
            o[i] = arrList.get(i);
            if(o[i].getClass().equals(Integer.class))
                sum += (int) o[i];
            else if(o[i].getClass().equals(Double.class))
                sum += (double) o[i];
        }
        temp = sum;
        sum = 0.0;
        return temp / arrList.size();
    }
    
}



Just some simple research to figure out how to problem solve :)
Was This Post Helpful? 1
  • +
  • -

#10 Ryano121   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1461
  • View blog
  • Posts: 3,289
  • Joined: 30-January 11

Re: trouble finding average of ArrayList

Posted 16 August 2013 - 06:29 AM

Thats a bit of a hack though. ArrayList of Object is generally a sign that you are doing something wrong.

You had the right idea at the beginning through the use of <T extends Number> but it was in the wrong place and you never actually made use of T.

public double findAverage<T extends Number>(ArrayList<T> nums) {
    // you can now use .doublevalue() on each element as the type is restricted to a Number
}

Was This Post Helpful? 1
  • +
  • -

#11 megabit   User is offline

  • D.I.C Head

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

Re: trouble finding average of ArrayList

Posted 16 August 2013 - 07:23 AM

View PostRyano121, on 16 August 2013 - 06:29 AM, said:

Thats a bit of a hack though. ArrayList of Object is generally a sign that you are doing something wrong.

You had the right idea at the beginning through the use of <T extends Number> but it was in the wrong place and you never actually made use of T.

public double findAverage<T extends Number>(ArrayList<T> nums) {
    // you can now use .doublevalue() on each element as the type is restricted to a Number
}

Thanks for the reply. I will try it out and report back, going to go to sleep now though. Nice avatar btw! :tooth:
Was This Post Helpful? 0
  • +
  • -

#12 megabit   User is offline

  • D.I.C Head

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

Re: trouble finding average of ArrayList

Posted 16 August 2013 - 06:42 PM

View PostRyano121, on 16 August 2013 - 06:29 AM, said:

Thats a bit of a hack though. ArrayList of Object is generally a sign that you are doing something wrong.

You had the right idea at the beginning through the use of <T extends Number> but it was in the wrong place and you never actually made use of T.


Thanks schutzzz and Ryano121 for your help, I got it working now. The method header I used was
public <T extends Number> double findAverage(ArrayList<T> nums)
{
    // you can now use .doublevalue() on each element as the type is restricted to a Number
}



However, you see, the task was to create a class that extended Number. Like
public MyClass<T extends Number>

thats why I had it in my code, but it doesn't work with doubleValue() so I don't know, I'm a little confused. When I have
<T extends Number>
in both my class definition and method headers I get the "recompile with -XLint unchecked" warning. But if I include the
<T extends Number>
just in my methods, I don't get any errors. Which way is the most correct if I want to limit the entire class to accept numbers only?
Was This Post Helpful? 0
  • +
  • -

#13 Ryano121   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1461
  • View blog
  • Posts: 3,289
  • Joined: 30-January 11

Re: trouble finding average of ArrayList

Posted 17 August 2013 - 11:16 AM

I'm not sure why you would ever need to have it on the class itself. The only reason I can think of is if they were thinking that you instantiate an object of the class with a generic parameter of say a float which you then use in the average method so that method can only accept a list of floats. To me that seems a bit backwards for this task though.
Was This Post Helpful? 1
  • +
  • -

#14 megabit   User is offline

  • D.I.C Head

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

Re: trouble finding average of ArrayList

Posted 17 August 2013 - 08:16 PM

View PostRyano121, on 17 August 2013 - 11:16 AM, said:

I'm not sure why you would ever need to have it on the class itself. The only reason I can think of is if they were thinking that you instantiate an object of the class with a generic parameter of say a float which you then use in the average method so that method can only accept a list of floats. To me that seems a bit backwards for this task though.


Thanks for replying and helping! Got it all sorted now. :^:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1