Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quadratic in Java

Tags:

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

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). 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;

}

like image 944
John Smith Avatar asked Dec 06 '25 06:12

John Smith


1 Answers

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.

Now, in the factorisation

  1. you have dimmed your double[] newCoefficients as size 1. too small!
  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. you need two Polys. You haven't provided a method return type for factor

I suggest you use

 public QuadPoly[] factor(){
 }

as the signature. The rest is just maths!

like image 105
Sanjay Manohar Avatar answered Dec 08 '25 19:12

Sanjay Manohar