Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Polynome{
- private double coef[];
- private int degre ;
- // degre = (coef.length -1)
- public Polynome(int d , double [] tab){
- degre=d;
- coef=tab;
- }
- public Polynome Ajouter(Polynome P){
- if((P.coef).length==(this.coef).length){
- for(int i=0;i<(P.coef).length;i++){
- P.coef[i] = P.coef[i]+this.coef[i];
- }
- }
- else {
- if((P.coef).length<(this.coef).length){
- for(int i=0;i<(P.coef).length;i++){
- this.coef[i]=P.coef[i]+this.coef[i];
- }
- }
- else{
- for(int i=0;i<(this.coef).length;i++){
- P.coef[i]=P.coef[i]+this.coef[i];
- }
- }
- }
- int d;
- if((P.degre)>this.degre){
- d=P.degre;
- }
- else{
- d=this.degre;
- }
- double tab[]=new double[(P.coef).length+(this.coef).length];
- if((P.coef).length>=(this.coef).length){
- tab=P.coef;
- }
- else{
- tab=this.coef;
- }
- Polynome A= new Polynome(d,tab);
- System.out.println(A);
- return A;
- }
- public double Evaluer(double x){
- double a=coef[0]+x*coef[1];
- double sum=0 ;
- for(int i=2;i<coef.length;i++){
- sum=sum+((Math.pow(x,i))*coef[i]);
- }
- sum=a+sum;
- System.out.println(sum);
- return sum;
- }
- public String toString(){
- String s="";
- for (int i=1;i<coef.length;i++){
- s+=" + "+coef[i]+"X"+"^"+i;
- }
- return coef[0]+" "+s;
- }
- }
- public class Test{
- public static void main(String [] args){
- double x[]={5,2,2,7,7,1};
- double x1[] ={1,3,6,5};
- Polynome P1 = new Polynome(6,x);
- Polynome P2 = new Polynome( 5,x1);
- System.out.println(P1);
- System.out.println(P2);
- System.out.print("Evaluer x dans P1 :");
- P1.Evaluer(5);
- System.out.print("Evaluer x dans P2 :");
- P2.Evaluer(6);
- P2.Ajouter(P1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement