简体   繁体   English

Java中的二次方

[英]Quadratic in Java

Could any help me start? 可以帮助我开始吗?

Using a class that I created before, I need to make a new class that specifically deals with QuadPoly. 使用我之前创建的类,我需要创建一个专门处理QuadPoly的新类。 I think I have the constructors made correctly but i'm not a hundred percent sure. 我想我的构造函数是正确的,但我不是百分之百肯定的。

public class Poly {

private float[] coefficients;
public static void main (String[] args){
    float[] fa = {3, 2, 4};
    Poly test = new Poly(fa);

}

public Poly() {
    coefficients = new float[1];
    coefficients[0] = 0;
}

public Poly(int degree) {
    coefficients = new float[degree+1];
    for (int i = 0; i <= degree; i++)
        coefficients[i] = 0;
}


public Poly(float[] a) {
    coefficients = new float[a.length];
    for (int i = 0; i < a.length; i++)
        coefficients[i] = a[i];
}

public int getDegree() {
    return coefficients.length-1;
}

public float getCoefficient(int i) {
    return coefficients[i];
}

public void setCoefficient(int i, float value) {
    coefficients[i] = value;
}

public Poly add(Poly p) {
    int n = getDegree();
    int m = p.getDegree();
    Poly result = new Poly(Poly.max(n, m));
    int i;

        for (i = 0; i <= Poly.min(n, m); i++) 
            result.setCoefficient(i, coefficients[i] + p.getCoefficient(i));
        if (i <= n) {
            //we have to copy the remaining coefficients from this object
            for ( ; i <= n; i++) 
                result.setCoefficient(i, coefficients[i]);
        } else {
            // we have to copy the remaining coefficients from p
            for ( ; i <= m; i++) 
                result.setCoefficient(i, p.getCoefficient(i));
        }
    return result;
}

public void displayPoly () {
    for (int i=0; i < coefficients.length; i++)
        System.out.print(" "+coefficients[i]);
    System.out.println();
}

private static int max (int n, int m) {
    if (n > m)
        return n;
    return m;
}

private static int min (int n, int m) {
    if (n > m)
        return m;
    return n;
}

public Poly multiplyCon (double c){
    int n = getDegree();
    Poly results = new Poly(n);
    for (int i =0; i <= n; i++){ // can work when multiplying only 1 coefficient
        results.setCoefficient(i, (float)(coefficients[i] * c)); // errors ArrayIndexOutOfBounds for setCoefficient
       }

    return results;
   }

public Poly multiplyPoly (Poly p){
    int n = getDegree();
    int m = p.getDegree();
    Poly result = null;
    for (int i = 0; i <= n; i++){
        Poly tmpResult = p.multiByConstantWithDegree(coefficients[i], i); //Calls new method
        if (result == null){
            result = tmpResult;
        } else {
            result = result.add(tmpResult);
        }
    }
    return result;
}
  public void leadingZero() {
    int degree = getDegree();
    if ( degree == 0 ) return;
    if ( coefficients[degree] != 0 ) return;
    // find the last highest degree with non-zero cofficient 
    int highestDegree = degree;
    for ( int i = degree; i <= 0; i--) {
         if ( coefficients[i] == 0 ) {
              highestDegree = i -1;
         } else {
              // if the value is non-zero
              break;
         }
    }
    float[] newCoefficients = new float[highestDegree + 1];
    for ( int i=0; i<= highestDegree; i++ ) {
           newCoefficients[i] = coefficients[i];
    }
    coefficients =   newCoefficients;
}

 public Poly differentiate(){
    int n = getDegree();
    Poly newResult = new Poly(n);
    if (n>0){   //checking if it has a degree
        for (int i = 1; i<= n; i++){
             newResult.coefficients[i-1]= coefficients[i] * (i); // shift degree by 1 and multiplies
     }
     return newResult;

     } else {
    return new Poly(); //empty
     }
    }


public Poly multiByConstantWithDegree(double c, int degree){ //used specifically for multiply poly
    int oldPolyDegree = this.getDegree();
    int newPolyDegree = oldPolyDegree + degree;
    Poly newResult = new Poly(newPolyDegree);
    //set all coeff to zero
    for (int i = 0; i<= newPolyDegree; i++){
        newResult.coefficients[i] = 0;
    }
    //shift by n degree
    for (int j = 0; j <= oldPolyDegree; j++){
        newResult.coefficients[j+degree] = coefficients[j] * (float)c;
    }

    return newResult;
 }
}

Out of this, I need to create a method that factors a Quadratic in two factors (if it has real roots), or in a constant ”1” polynomial factor and itself, if there are no real roots. 除此之外,我需要创建一个方法,在两个因子(如果它有真正的根)中,或在一个常数“1”多项式因子本身中,如果没有真正的根,则将二次方因子。 The method should return an array of two QuadPoly objects, containing each factor. 该方法应返回包含每个因子的两个QuadPoly对象的数组。

public class QuadPoly extends Poly
{
    private float [] quadcoefficients;
     public QuadPoly() {
     super(2);

}

 public QuadPoly(float [] a) {
    quadcoefficients = new float[a.length];
    for (int i = 0; i <a.length; i ++){
        quadcoefficients[i] = a[i];

    if (quadcoefficients.length > 2){
        throw new IllegalArgumentException ("Must be Quadratic");
    }

}
}
    public QuadPoly(Poly p){
        if (quadcoefficients.length > 2){
         throw new IllegalArgumentException ("Must be Quadratic");
    }
}

 public QuadPoly addQuad (QuadPoly p){
    return new QuadPoly(super.add(p));
}

public Poly multiplyQuadPoly (Poly p){
    if (quadcoefficients.length > 2){
        throw new IllegalArgumentException ("Must be Quadratic");
    }
    Poly newResult = null;
    new Result = multiplyPoly(p);


}
}
}

Edit: 编辑:

Sorry. 抱歉。 This is what I have going on for the factoring so far. 到目前为止,这是我为保理所做的事情。 The big problem with it is that I'm not too sure how to get the inheritance to work properly. 它的一个大问题是我不太确定如何使继承正常工作。

This is my New Factoring. 这是我的新保理。 It doesn't work. 它不起作用。 Can anyone give me some hints to get on the right path? 谁能给我一些提示才能走上正确的道路? I understand that I need to return Poly so i'm replacing the arrays there as you can tell by the first if statement but it won't let me progress as its says it requires (int, float). 我知道我需要返回Poly所以我正在替换那里的数组,因为你可以通过第一个if语句告诉它,但它不会让我进步,因为它说它需要(int,float)。 I've casted it but it still won't allow me. 我已经投了它,但它仍然不允许我。 Thanks 谢谢

    public QuadPoly factor(){
    double a = (double) getCoefficient(0);
    double b = (double) getCoefficient(1);
    double c = (double) getCoefficient(2);
    QuadPoly newCoefficients = new QuadPoly(4);
    double equa = Math.sqrt((b*b) - (4*a*c));
    if (equa > 0){
        newCoefficients.setCoefficient(0, (float) (-b + equa)/(2*a));
        newCoefficients.setCoefficient(1, (float) (-b - equa)/(2*a));
    }
    if (equa ==0){
        newCoefficients[0] = 1;
        newCoefficients[1] = (-b + equa)/(2*a);
    }
    if (equa < 0){
        newCoefficients[0] = 0;
        newCoefficients[1] = 1;
    }
    return (QuadPoly) newCoefficients;

} }

The idea of subclassing Poly into QuadPoly is so that you can reuse as many of the old Poly methods as possible. Poly子类化为QuadPoly的想法是,您可以重用尽可能多的旧Poly方法。 Now, all your old methods use the array float[] coefficients , and your new QuadPoly inherits this field. 现在,所有旧方法都使用数组float[] coefficients ,而新的QuadPoly 继承了这个字段。

Why have you created a new field quadcoefficients[] ? 你为什么要创建一个新的字段quadcoefficients[] It suffices to check in any constructor that there are only 3 members in the array, but to still harness the existing field coefficients[] . 检查任何构造函数是否足以检查数组中只有3个成员,但仍能利用现有的字段coefficients[]

If you do this, all your old methods will still work! 如果你这样做,你所有的旧方法仍然有效! Only, they will return generic Poly . 只有,他们将返回通用Poly Since the QuadPoly must conform to the contract of a Poly , this is probably OK. 由于QuadPoly必须符合Poly的合同,这可能没问题。 The method multiplyCon is the only one that could be guaranteed to return another QuadPoly anyway. 方法multiplyCon是唯一可以保证返回另一个QuadPoly的方法。

You don't seem to have attempted a factorisation yet. 您似乎还没有尝试过因子分解。 Do you have any ideas? 你有什么想法? Well, here's a clue: you'll need to use something like 嗯,这里有一个线索:你需要使用类似的东西

if (DISCRIMINANT >= 0) {
} else{
}

OK you have made a reasonable attempt. 好的,你做了一个合理的尝试。 Inheritance is simple here, all you need is the constructors: 这里继承很简单,你需要的只是构造函数:

class QuadPoly extends Poly{
  public QuadPoly(){ super(2); }
  public QuadPoly(float[] f){
    super(f);
    if(coefficients.length!=2) throw new IllegalArgumentException("not quad");
  }
}

and that's pretty much all! 这就是全部! I hope you can see, that the same code as Poly is used for everything else , and the same field coefficients does all the same work as it did before. 我希望你能看到,与Poly相同的代码用于其他所有代码,并且相同的字段coefficients完成与之前相同的工作。

Now, in the factorisation 现在,在分解中

  1. you have dimmed your double[] newCoefficients as size 1. too small! 你已经将你的double[] newCoefficients调暗为1号太小了!
  2. you have tried to square-root your discriminant without knowing that it is positive! 你试图在不知道积极的情况下纠正你的判别力!
  3. you are returning an array of 2 doubles as your answer. 你将返回2个双打的数组作为你的答案。 you need two Poly s. 你需要两个Poly You haven't provided a method return type for factor 您没有为factor提供方法返回类型

I suggest you use 我建议你用

 public QuadPoly[] factor(){
 }

as the signature. 作为签名。 The rest is just maths! 其余的只是数学!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM