public class Monome implements Comparable
{	private double coef;
	private int degre;
	
	/** Construit le monome nul */
	public Monome() {
		coef =0;
		degre =0;}
		
	/** Construit le monome ax^n */
	public Monome(double a, int n) throws ArithmeticException {
		if (n<0) throw new ArithmeticException("Le degré d'un monome doit être un nombre positif.");
		else {coef =a;
		degre =n;
	}	}
		
	/** renvoie le coefficient du monome */
	public double getCoef()	{
		return coef;}
		
	/** renvoie le degré du monome */
	public int getDegre(){
		return degre;}
		
	/** renvoie la valeur du monome pour le paramètre x*/
	public double calcul(double x){
		double puissance=1;
		for (int i=1;i<=degre;i++) {puissance=puissance*x;} /* calcul x^n*/
		return puissance*coef;}
		
         public String toString(){
	 	String string_coef;
	 	if (coef==0) return "0";
		else if (coef==1) string_coef="";
			else if (coef==-1) string_coef="-";
				else string_coef=coef+"";
	 	if (degre==0) if (coef==1) return "1";
					else return string_coef;
				else if (degre==1) return string_coef+"x";
					else return string_coef+"x^"+degre;
			
		}
	 
	/** 2 Monomes sont égaux si leurs coefficients et leurs degrés sont égaux*/
         public boolean equals(Monome m){
	 	return (getDegre()==m.getDegre() && getCoef()==m.getCoef());}
	 
	 /** On compare les monomes selon leur degre puis leur coefficient */
         public int compareTo(Object o){
	 	if (!(o instanceof Monome)) return 1;
		else if (getDegre()>((Monome)o).getDegre()) return -1;
			else if (getDegre()<((Monome)o).getDegre()) return 1;
				else if (getCoef()>((Monome)o).getCoef()) return -1;
					else return 1;}
	 
	/** Construit un nouveau monome qui est la dérivée du monome passé en paramètre*/
	 public static Monome derivee(Monome m) {
	 	if (m.getDegre()==0) return new Monome();
		else return new Monome(m.getCoef()*m.getDegre(),m.getDegre()-1);}
	 
	 public static void main(String[] args) {
		double x;
		Monome m= new Monome(Keyboard.readDouble("Entrer le coefficient du monome:"),Keyboard.readInt("Entrer le degré du monome:"));
		System.out.println(m);
		do {x=Keyboard.readDouble("Entrer la valeur de x :");
			System.out.println(m.getCoef()+"*"+x+"^"+m.getDegre()+"="+m.calcul(x));
			}
		while (x!=0);
		}
}

