-2

So after I messed with it with the help from you guys I got it to do somewhat of what I need it to do

public class PolynomialTestDriver {
public static void main(String[] args){
    Polynomial myPoly = new Polynomial();   
        myPoly.setCoefficient(6, 2);
        myPoly.setCoefficient(9,3);

        System.out.println(myPoly.getCoeff()+"x^"+myPoly.getDeg());

    }
}

however it will only print out the last myPoly.setCoefficient(); its supposed to do print out

2x^6+3x^9 but it only prints 3x^9

  • 2
    It is quite unclear what you want to achieve. Do you want to write a class that can represent sparse polynomials (i.e. where most of the coefficients of the polynomial are 0)? Or a polynomial where you can set the coefficients individually? However, in the latter case the object construction should be `new Polynomial(3);` – Henry Aug 31 '14 at 07:13
  • where it says new Polynomial(3); it's supposed to indicate how many x^n there are not what the degree and coefficients are – user3247083 Aug 31 '14 at 07:17
  • What you've modeled here can be described more accurately as a monomial. – Chris Martin Aug 31 '14 at 07:38
  • so how do I get it to print out the polynomial? – user3247083 Aug 31 '14 at 07:54
  • 1
    Why did you remove the code for the `Polynomial` class? :( – Chris Martin Aug 31 '14 at 08:28

1 Answers1

0

"... can someone tell me where I went wrong?"

Frankly, you probably went wrong by either 1) not reading the instructions / requirements properly before you started, or 2) not thinking carefully about what a polynomial actually is.

Hint:

Consider this polynomial in X: 3X4 + 2X2 + 4.

  • What is the degree of the entire polynomial?
  • What are the degrees of the terms?
  • What are the coefficients of the terms?
  • For a hypothethical polynomial of degree 4, how many terms can there be?
  • If you want to represent an arbitrary polynomial as a Java class, what quantities do you actually need to store?
  • How do you store them? (Hint: simple scalar variables won't do ...)

Obviously, we can't see your exercises requirements, but I suspect that the Polynomial constructor is supposed to take the degree of the entire polynomial as a parameter, and the setCoefficient method is supposed to set the coefficient of the term with a given degree.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216