即使对象是数组也找不到数组

时间:2018-09-02 21:21:35

标签: java arrays object

因此,我尝试将2个多项式加在一起,并创建了ArrayBasedPoly的对象类型Polynomial,并且该对象是系数的array,并且度从0开始。

例如:[1, 0, 3, 4] is 4x^3 + 3x^2 + 1

在我的添加类中,我试图将2个数组加在一起->

例如:p1 = [1, 0, 3, 4], p2 = [-2, -5], Sum[] = [-1, -5, 3, 4]

但是,在add方法中,即使对象是数组,它也不会将p2识别为数组。

编辑:现在我知道我需要一种方法来找到对象p的数组的长度,但是,即使当我有一个单独的方法时,它仍然找不到它?

public class ArrayBasedPoly extends Polynomial
{
   private double [] poly; 

   public ArrayBasedPoly(double [] poly)
   {
      this.poly = poly;
   }

   public ArrayBasedPoly(double coeff, int expon)
   {
      super.coeff = coeff;
      super.expon = expon;
   }

   public ArrayBasedPoly add(ArrayBasedPoly p)
   {
      double [] temp = new double [Math.max(p.length, poly.length)]; //<=== Here
      return null; //temp
   }

   public String toString()
   {
      String s = "";

      if (poly == null)
      {
         return super.toString(); // no poly array
      }

      for(int i = poly.length - 1; i >= 0; i--)
      {
         if (i != poly.length - 1 && poly[i] > 0) //adds + sign
         {
            s += " + ";
         }
         if (poly[i] != 0) // ignores the 0 in the array
         {
            if (i == 1) //if expon is 1, doesn't do the ^1
            {
               s += poly[i] + "x";
            } else if (i > 0) { // normal
               s += poly[i] + "x^" + i;
            } else {
               s += poly[i]; // if expon = 0, just prints out the coeff
            }   
         }   
      }
      return s;
   }

   public static void main (String [] args)
   {
      double [] c = {1, 0, 3, 4};
      double [] c1 = {-2, -5};

      Polynomial p1 = new ArrayBasedPoly (c);
      System.out.println("p1(x) =   " + p1);

      Polynomial p2 = new ArrayBasedPoly (c1);
      System.out.println("p2(x) =   " + p2);

      Polynomial p3 = new ArrayBasedPoly(-4, 1);
      System.out.println("p3(x) =   " + p3);

      Polynomial p = p1.add(p2);//.add(p2);
      System.out.println("p(x) =    " + p);

      Polynomial p4 = p.subtract(p3);
      System.out.println("p4(x) =   " + p4);

      Polynomial p5 = p4.getDerivative();
      System.out.println("p5(x) =   " + p5);

      System.out.println("p5(0) = " + p5.evaluate(0));
      System.out.println("p5(1) = " + p5.evaluate(1));
   }

} //ArrayBasedPoly

public class Polynomial
{
   protected double coeff;
   protected int expon;
   
   public Polynomial()
   {
      coeff = 1;
      expon = 1;
      
   }
   
   public Polynomial(double coeff, int expon)
   {
      this.coeff = coeff;
      this.expon = expon;
   }
   
   public Polynomial(Polynomial p)
   {
      this.coeff = p.coeff;
      this.expon = p.expon;
   }
   
   public int getDegree()
   {
      return expon;
   }
   
   public double getCoeff()
   {
      return coeff;
   }
   
   public double evaluate(double x)
   {
      return Math.pow(x, expon) * coeff;
   }
   
   public Polynomial add (Polynomial p)
   {      
      Polynomial temp = new Polynomial();
      if (expon != p.expon)
      {
         return null;
      }
      temp.coeff = coeff + p.coeff;
      temp.expon = expon;   
      return temp;
   } //add
   
   public Polynomial subtract (Polynomial p)
   {
      Polynomial temp = new Polynomial();
      if (expon != p.expon)
      {
         return null;
      }
      temp.coeff = coeff - p.coeff;
      temp.expon = expon;
      return temp;
   } //subtract
   
   public Polynomial getDerivative()
   {
      Polynomial temp = new Polynomial(coeff, expon);
      if (expon == 1)
      {
         temp.coeff = coeff;
         temp.expon = 0;
         return temp;
      }
      temp.coeff *= temp.expon;
      temp.expon--;
      return temp;
   } //derivative
   
   public String toString()
   {
      if (expon == 0)
      {
         return coeff + "";   
      }
      if (expon == 1)
      {
         return coeff + "x";
      }
      return coeff + "x^" + expon;
   } //toString
   
   public static void main (String [] args)
   {
      //Coefficient, Exponent
      Polynomial p1 = new Polynomial(3, 2); //3x^2
      System.out.println("P1: " + p1);
      System.out.println();
      System.out.println("Derivative of P1: " + p1.getDerivative());
      System.out.println();
      System.out.println("P1 if x=2: " + p1.evaluate(2));
      Polynomial p2 = new Polynomial(2, 2);
      System.out.println();
      System.out.println("P2: " + p2);
      System.out.println();
      System.out.println("P1+P2: " + p1.add(p2));
      System.out.println();
      System.out.println("P1-P2: " + p1.subtract(p2));
      Polynomial p3 = new Polynomial(2, 1);
      System.out.println();
      System.out.println("P3: " + p3);
      System.out.println();
      System.out.println("Derivative of P3: " + p3.getDerivative());
      System.out.println();
      System.out.println("P1+P3: " + p1.add(p3));
   } //Main
   
} //Polynomial

/*
  ----jGRASP exec: java Polynomial
 P1: 3.0x^2
 
 Derivative of P1: 6.0x^1
 
 P1 if x=2: 12.0
 
 P2: 2.0x^2
 
 P1+P2: 5.0x^2
 
 P1-P2: 1.0x^2
 
 P3: 2.0x^1
 
 Derivative of P3: 1.0x^0
 
 P1+P3: null
 
  ----jGRASP: operation complete.
*/

2 个答案:

答案 0 :(得分:0)

这段代码中存在多个问题。

首先,ArrayBasedPoly不应扩展Polynomial,因为它不是它的子类,而是它的数组表示形式。要解决此问题,您应该像这样在ArrayBasedPoly中保留一个多项式数组/列表:

public class ArrayBasedPoly {
    Polynomial[] polyArray;
    // or alternatively: List<Polynomial> polyArray;
    // or even better, use a sorted structure such as skiplist
}

第二,正如Pereira所指出的,您正在混淆这两个类。 多项式类不包含add()。 您只能添加两个ArrayBasePoly。

答案 1 :(得分:0)

  

似乎我犯了一个错误,并且“ add”方法应该   而是使用参数“多项式”,因为在main方法中   对象的类型为多项式。但是,当我使用多项式时   参数并具有getLength()方法,但仍然找不到它。 –   阿格拉蒙

问题出在方法调用中:

 Polynomial p = p1.add(p2);//.add(p2);

因为p1是一个多项式,它将检查THAT类的add方法。您需要强制转换p1。然后,由于该方法需要ArrayBasedPoly,因此您需要强制转换p2。

Polynomial p = ((ArrayBasedPoly) p1).add((ArrayBasedPoly) p2);