簡體   English   中英

Java中的二次方

[英]Quadratic in Java

可以幫助我開始嗎?

使用我之前創建的類,我需要創建一個專門處理QuadPoly的新類。 我想我的構造函數是正確的,但我不是百分之百肯定的。

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;
 }
}

除此之外,我需要創建一個方法,在兩個因子(如果它有真正的根)中,或在一個常數“1”多項式因子本身中,如果沒有真正的根,則將二次方因子。 該方法應返回包含每個因子的兩個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);


}
}
}

編輯:

抱歉。 到目前為止,這是我為保理所做的事情。 它的一個大問題是我不太確定如何使繼承正常工作。

這是我的新保理。 它不起作用。 誰能給我一些提示才能走上正確的道路? 我知道我需要返回Poly所以我正在替換那里的數組,因為你可以通過第一個if語句告訴它,但它不會讓我進步,因為它說它需要(int,float)。 我已經投了它,但它仍然不允許我。 謝謝

    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;

}

Poly子類化為QuadPoly的想法是,您可以重用盡可能多的舊Poly方法。 現在,所有舊方法都使用數組float[] coefficients ,而新的QuadPoly 繼承了這個字段。

你為什么要創建一個新的字段quadcoefficients[] 檢查任何構造函數是否足以檢查數組中只有3個成員,但仍能利用現有的字段coefficients[]

如果你這樣做,你所有的舊方法仍然有效! 只有,他們將返回通用Poly 由於QuadPoly必須符合Poly的合同,這可能沒問題。 方法multiplyCon是唯一可以保證返回另一個QuadPoly的方法。

您似乎還沒有嘗試過因子分解。 你有什么想法? 嗯,這里有一個線索:你需要使用類似的東西

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

好的,你做了一個合理的嘗試。 這里繼承很簡單,你需要的只是構造函數:

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

這就是全部! 我希望你能看到,與Poly相同的代碼用於其他所有代碼,並且相同的字段coefficients完成與之前相同的工作。

現在,在分解中

  1. 你已經將你的double[] newCoefficients調暗為1號太小了!
  2. 你試圖在不知道積極的情況下糾正你的判別力!
  3. 你將返回2個雙打的數組作為你的答案。 你需要兩個Poly 您沒有為factor提供方法返回類型

我建議你用

 public QuadPoly[] factor(){
 }

作為簽名。 其余的只是數學!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM