Thursday, 4 October 2012

Polynomial

Q3. A polynomial is represented by two fields; one is the degree and second is the coefficient. Thus 5x2+ 2x+3 is degree two and its coefficients are coeff[0]=3, coeff[1]=2 and coeff[2]=5. Design a class Polynomial to represent single-variable polynomials which contain the following functionality:
·       A zero argument constructor that initializes the degree and coefficients to zero
·       A two argument constructor that initializes both the degree and the coefficients array

Code:

public
class Coefficient {
public static int [] coeff;
public static int degree;
public Coefficient(){
degree = 0;
coeff = new int[degree + 1];
for(int i=0; i<degree+1; i++){
coeff[i]=0;
}
}
public Coefficient (int d , int[] input){
degree =d;
coeff = new int [degree +1];
for(int i=0; i<degree+1; i++){
coeff[i]= input[i];
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int deg = 5;
int[] s =(1,2,3,4,5);
}
}