Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6585151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:37:33+00:00 2026-05-25T16:37:33+00:00

Could any help me start? Using a class that I created before, I need

  • 0

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;

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T16:37:34+00:00Added an answer on May 25, 2026 at 4:37 pm

    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!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone have any tips that could help speed up a process of breaking
I was wondering if any one could help me out; I have a table
I have scoured the help files for Visual Studio and could not locate any
Could any body offer me any reason about that? If we do it like
I was under the impression that I could put any old executable program in
I want to start by saying that I am a big fan of using
Check below for UPDATE, I could reproduce and need help. I have a strange
Could any one give an explanation on how a DHT works? Nothing too heavy,
Could any one suggest good Python-related podcasts out there, it could be anything about
Could any one tell me how to enable SOCKET support in PHP ?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.