How to compute two objects of the same class?

Viewed 45

I'm trying to compute the combined potential between the first and and last SuperHero instance of the _superHeroes array, and display the result. I created a method in the SuperHero class that computed the combined potential between two instances of a class. When I try to perform this method I get the following error:

TestSuperHero.java:20: error: cannot find symbol
    double n = _superHeroes.combinedPotential(_superHeroes[0], _superHeroes[2]);
                           ^
symbol:   method combinedPotential(SuperHero,SuperHero)
location: variable _superHeroes of type SuperHero[]
1 error

I would deeply appreciate some help! Thank you!

   import java.util.*;
    
    public class TestSuperHero
    {
       public static void main(String[] args)
       {
          //Welcome Message
          System.out.println("***** Welcome to the SuperHero Program ******\n");
          
          //Define Array of Three SuperHeros
          SuperHero [] _superHeroes = new SuperHero[3];
          
          //Initialize SuperHero Elements
          _superHeroes[0] = new SuperHero ("Poodle", 1000);
          _superHeroes[1] = new SuperHero ("HangNail", 200);
          _superHeroes[2] = new SuperHero ("WetSlipper", 50);
         
         //Create a Double Variable that will compute combinedPotential on 
         //_superHeroes[0] and _superHeroes[2]
         double n = _superHeroes.combinedPotential(_superHeroes[0], _superHeroes[2]);
       }
        class SuperHero
        {
           //Private Data Members
           private String _name;
           private double _kineticForce;
           
           //Constructor
           public SuperHero(String name, double kineticForce)
           {
              _name = name;
              _kineticForce = kineticForce;
           }
           
           /* Methods */
           
           //getName Accessor
           public String getName()
           {
              return _name;
           }
           
           //getKineticForce Accessor
           public double getKineticForce()
           {
              return _kineticForce;
           }
           
           //setKineticForce Mutator
           public void setKineticForce(double kineticForce)
           {
              _kineticForce = kineticForce;
           }
           
           public double damagePotential()
           {
              final double PI = 3.14159;
              double damagePotential = ((7.0/3.0) * PI) * (Math.pow(getKineticForce() / 2 , 5));
              return damagePotential;  
           }
           
           public double combinedPotential(SuperHero superHeroOne, SuperHero superHeroTwo)
           {
              double combinedPotential;
              double numerator = Math.pow(3 * superHeroOne.getKineticForce(), 2) 
                                - Math.pow(3 * superHeroTwo.getKineticForce(), 2);
              double denominator = superHeroOne.getKineticForce() - superHeroTwo.getKineticForce();
              
              combinedPotential = numerator / denominator;
              
              return combinedPotential; 
           }
           
           public String toString()
           {
              String superHero = "Name of SuperHero: " + getName();
              String kineticForce = "Kinetic Force of " + getName() + ": " + getKineticForce();
              return superHero + "\n" + kineticForce;
           }
        }
2 Answers

To understand the problem: _superHeroes is an array of SuperHero and you are trying to call a method of that class on the array. But the array does not inherrit the methods of the class. Instead you need to call the method on an instance that is stored inside of the array.

_superHeroes[0].combinedPotential(_superHeroes[0], _superHeroes[2]);

Better solution: As the method is not using any fields of the class for calculations, you can also declare the method static and call the static method without the need for an instance:

public static double combinedPotential(SuperHero superHeroOne, SuperHero superHeroTwo) {
    ...
}
...
SuperHero.combinedPotential(_superHeroes[0], _superHeroes[2]);

This should be a static method:

public static double combinedPotential(...) { ... }

which you then call as

SuperHero.combinedPotential(_superHeroes[0], _superHeroes[2]);
Related