-2

This method takes in an array of planets and calculates their net x force component on the planet called upon. I get symbol not found error on compile for a1

public double calcForceExertedByX(Planet[] a1){
    double forceX;
    for (int i = 0; i < a1.length(); i++){
      forceX = forceX + 6.667e-11 *(mass*a1[i].mass)/( xxPos - a1[i].xxPos)*(xxPos - a1[i].xxPos);
    }
    return forceX;
}

below is the complete code, as requested.

public class Planet {
  public double xxPos;
  public double yyPos;
  public double xxVel;
  public double yyVel;
  public double mass;
  public String imgFileName;
    public Planet(double xPs, double yPs, double xVl, double
    yVl, double m, String imge){
    xxPos = xPs;
    yyPos = yPs;
    xxVel = xVl;
    yyVel = yVl;
    mass = m;
    imgFileName = imge;

  }
  public Planet(Planet p){
    xxPos = p.xxPos;
    yyPos = p.yyPos;
    xxVel = p.xxVel;
    yyVel = p.yyVel;
    mass = p.mass;
    imgFileName = p.imgFileName;
  }
  public double calcDistance(Planet p1){
    double distx = xxPos - p1.xxPos;
    double disty = yyPos - p1.yyPos;
    double dist = Math.sqrt(distx*distx + disty*disty);
    return dist;
  }
  public double calcForceExertedBy(Planet p1){
    double force = 6.667e-11 *(mass*p1.mass)/(this.calcDistance(p1)*this.calcDistance(p1));
    return force;
  }
  public double calcForceExertedByX(Planet[] a1){
    double forceX;
    for (int i = 0; i < a1.length(); i++){
      forceX = forceX + 6.667e-11 *(mass*a1[i].mass)/( xxPos - a1[i].xxPos)*(xxPos - a1[i].xxPos);
    }
    return forceX;
  }
  public double calcForceExertedByY(Planet[] a1){
    double forceY;
    for (int i = 0; i <a1.length(); i++){
      forceY = forceY + 6.667e-11 *(mass*a1[i].mass)/( yyPos - a1[i].yyPos)*(yyPos - a1[i].yyPos);
    }
    return forceY;
  }
}

Windows 7 JAVAc 11.01

Tom Hawtin - tackline
  • 145,806
  • 30
  • 211
  • 305

1 Answers1

2

I am guessing the error that is trying to be referenced is that Planet[] a1 does not have method length(). Arrays use .length rather than a method call.

Also forceX is not initialised.

Tom Hawtin - tackline
  • 145,806
  • 30
  • 211
  • 305