Quadratic in Java
<p>Could any help me start?</p>

<p>Using a class that I created before, I need to make a new class that specifically deals with QuadPoly. I think I have the constructors made correctly but i'm not a hundred percent sure.</p>

<pre><code>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 &lt;= degree; i++)
        coefficients[i] = 0;
}


public Poly(float[] a) {
    coefficients = new float[a.length];
    for (int i = 0; i &lt; 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 &lt;= Poly.min(n, m); i++) 
            result.setCoefficient(i, coefficients[i] + p.getCoefficient(i));
        if (i &lt;= n) {
            //we have to copy the remaining coefficients from this object
            for ( ; i &lt;= n; i++) 
                result.setCoefficient(i, coefficients[i]);
        } else {
            // we have to copy the remaining coefficients from p
            for ( ; i &lt;= m; i++) 
                result.setCoefficient(i, p.getCoefficient(i));
        }
    return result;
}

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

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

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

public Poly multiplyCon (double c){
    int n = getDegree();
    Poly results = new Poly(n);
    for (int i =0; i &lt;= 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 &lt;= 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 &lt;= 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&lt;= highestDegree; i++ ) {
           newCoefficients[i] = coefficients[i];
    }
    coefficients =   newCoefficients;
}

 public Poly differentiate(){
    int n = getDegree();
    Poly newResult = new Poly(n);
    if (n&gt;0){   //checking if it has a degree
        for (int i = 1; i&lt;= 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&lt;= newPolyDegree; i++){
        newResult.coefficients[i] = 0;
    }
    //shift by n degree
    for (int j = 0; j &lt;= oldPolyDegree; j++){
        newResult.coefficients[j+degree] = coefficients[j] * (float)c;
    }

    return newResult;
 }
}
</code></pre>

<p>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. The method should return an array of two QuadPoly objects, containing each factor.</p>

<pre><code>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 &lt;a.length; i ++){
        quadcoefficients[i] = a[i];

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

}
}
    public QuadPoly(Poly p){
        if (quadcoefficients.length &gt; 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 &gt; 2){
        throw new IllegalArgumentException ("Must be Quadratic");
    }
    Poly newResult = null;
    new Result = multiplyPoly(p);


}
}
}
</code></pre>

<p>Edit:</p>

<p>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.</p>

<p>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). I've casted it but it still won't allow me. Thanks</p>

<pre><code>    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 &gt; 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 &lt; 0){
        newCoefficients[0] = 0;
        newCoefficients[1] = 1;
    }
    return (QuadPoly) newCoefficients;
</code></pre>

<p>}</p>
