0

I'm making a program whose basic code is as follows:

public class StatisticalList extends ArrayList {

    public double getMinimumValue() {...}

    public double getMaximumValue() {...}

    public double range(){

          double range = StatisticalList.getMaximumValue() - StatisticalList.getMinimumValue();

          return range;
}

In this I have implemented two methods, getMinimumValue and getMaximumValue. Next I want to implement a method getRange. It seems easy to not do the calculations again and just call the get__Value() methods.

But this gives an error of The method maxValue() is undefined for the type ArrayList. I want to do this type of calculation where I need to use methods in other methods in my program several times. How do I get this to work?

1
  • Since methods are not static you can directly call them e.g. getMaximumValue() Commented Nov 25, 2013 at 6:23

4 Answers 4

3

You are calling getMaximumValue() and getMinimumValue() as static method calls on StatisticalList.

double range = getMaximumValue() - getMinimumValue()

should get you what you are looking for.

Sign up to request clarification or add additional context in comments.

Comments

1

A method defined within a class is accessible everywhere in this class. Moreover, because you have used "public" access modifier, your methods will be accessible from any other class in the package. To access them in classes in different package however, you will need to import that class. Please see the docs here and more on access modifiers here.

And you could also simplify your range() method:

public double range() {
    return (getMaximumValue() - getMinimumValue());
}

Comments

1

In a nutshell:

double range = getMaximumValue() - getMinimumValue();

But let's explain a little bit. First, I am not really sure how this code compiles to you. Calling a method in the form of "classname.methodname" is for static method so when you call StatisticalList.getMaximumValue() the java compiler expects to see a static method there.
And I think you want it as well. You want the getMaximumValue to find the maximal value among the values in the ArrayList. So you should have this method invoked on "this" object. Simply remove the 2 x StatisticalList. from this line and have it as follows:

double range = getMaximumValue() - getMinimumValue();

In addition you should also be careful how you implement get__Value() methods. Your collection extends ArrayList so it is basically ArrayList<Object> which can hold objects from all kind of types. Not only numbers. Changing the class definition to

public class StatisticalList extends ArrayList<Double> {

would probably make much more sense.

Try it out..

Comments

0

Basically the syntax StatisticalList.getMaximumValue() is for calling static method.

And the methods you are trying to call are instance methods.

So, If you want to call them in same class

   double range = getMaximumValue() - getMinimumValue();

If you want to use it some where else you need to have a reference of StatisticalList

StatisticalList  s = new StatisticalList();
double range = s.getMaximumValue() - s.getMinimumValue();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.