package bpo5gr10;

import java.util.ArrayList;
import java.util.List;

public class Polynome {
	public static int getDegreMax() {
		return 8;
	}
	private List<Double> coefs = new ArrayList<>();
	// 2X^3 -5X^1 +7 sera stocké comme
	// [7., -5., 0., 2., 0., 0., 0., 0., 0.]
	
	public Polynome(double coef, int degré) {
		for (int d = 0; d <= getDegreMax(); ++d) {
			coefs.add(0.);
		}
		coefs.set(degré, coef);
	}

	public Polynome(double coef) {
		this(coef,0);
	}

	public Polynome() {
		this(0.);
	}

	public Polynome(Polynome p1) {
		coefs = new ArrayList<>(p1.coefs);
		// coefs = p1.coefs.clone(); // si coefs est un tableau
	}

	public double valeur(double x) {
		double resultat = 0.;
		double xPuissanceI = 1;
		for (int i =0;i<=getDegreMax();++i) {
			resultat += coefs.get(i)*xPuissanceI;
			xPuissanceI *= x;
		}
		return resultat;	
	}

	public Polynome ajouter(Polynome p) {
		Polynome somme = new Polynome();
		for (int i =0;i<=getDegreMax();++i)
			somme.coefs.set(i,this.coefs.get(i) + p.coefs.get(i));
		return somme;
	}

}
