Skúškové zadanie zo zimného semestra - Polynómy

Created: 2008-06-19 - 16:42

Zadanie
Untitled

import java.lang.reflect.Array;

public class Polynom implements java.lang.Comparable {
    public double[] koeficienty;

    public int stupenPolynomu;

    public Polynom() {
        koeficienty = new double[0];
        koeficienty[0] = 0;
        stupenPolynomu = 0;
    }

    public Polynom(double k, int e) {
        koeficienty = new double[1];
        stupenPolynomu = e;
        koeficienty[0] = k;
    }

    public Polynom(double[] koeficienty) {
        this.koeficienty = new double[koeficienty.length];
        System.arraycopy(koeficienty, 0, this.koeficienty, 0, koeficienty.length);
        stupenPolynomu = koeficienty.length - 1;
    }

    public double vyhodnot(double hodnota) {
        double vysledok = 0;
        for (int i = 0; i < stupenPolynomu; i++) {
            vysledok += koeficienty[i] * (Math.pow(hodnota, i));
        }
        return vysledok;
    }

    public double getKoeficient(int exponent) {
        return koeficienty[koeficienty.length-exponent];
    }

    public int getStupen() {
        if (stupenPolynomu != 0)
            return stupenPolynomu;
        else
            return 0;
    }

    public boolean equals(Object obj) {
        if (obj instanceof Polynom) {
            Polynom test = (Polynom) obj;
            if (test == this)
                return true;
            else
                return false;
        } else
            return false;
    }

    public Polynom pripocitaj(Polynom p) {
        Polynom sucet;
        double[] sucetKoeficientov;
        if (p.koeficienty.length == this.koeficienty.length) {
            sucetKoeficientov = new double[this.koeficienty.length];
            for (int i = 0; i < this.stupenPolynomu+1; i++) {
                sucetKoeficientov[i] = this.koeficienty[i] + p.koeficienty[i];
            }

        } else {
            if (p.koeficienty.length < this.koeficienty.length) {
                sucetKoeficientov = new double[this.koeficienty.length];
                for (int i = 0; i < this.koeficienty.length; i++) {
                    if (i < p.koeficienty.length) {
                        sucetKoeficientov[i] = this.koeficienty[i]
                                + p.koeficienty[i];
                    } else
                        sucetKoeficientov[i] = this.koeficienty[i] + 0;
                }

            } else {
                sucetKoeficientov = new double[p.koeficienty.length];
                for (int i = 0; i < p.koeficienty.length; i++) {
                    if (i < this.koeficienty.length) {
                        sucetKoeficientov[i] = this.koeficienty[i]
                                + p.koeficienty[i];
                    } else
                        sucetKoeficientov[i] = p.koeficienty[i] + 0;

                }

            }
        }
        sucet = new Polynom(sucetKoeficientov);
        return sucet;
    }
    
    public Polynom odpocitaj(Polynom p) {
        Polynom rozdiel;
        double[] rozdielKoeficientov;
        if (p.koeficienty.length == this.koeficienty.length) {
            rozdielKoeficientov = new double[this.koeficienty.length];
            for (int i = 0; i < this.stupenPolynomu+1; i++) {
                rozdielKoeficientov[i] = this.koeficienty[i] - p.koeficienty[i];
            }

        } else {
            if (p.koeficienty.length < this.koeficienty.length) {
                rozdielKoeficientov = new double[this.koeficienty.length];
                for (int i = 0; i < this.koeficienty.length; i++) {
                    if (i < p.koeficienty.length) {
                        rozdielKoeficientov[i] = this.koeficienty[i]
                                - p.koeficienty[i];
                    } else
                        rozdielKoeficientov[i] = this.koeficienty[i] + 0;
                }

            } else {
                rozdielKoeficientov = new double[p.koeficienty.length];
                for (int i = 0; i < p.koeficienty.length; i++) {
                    if (i < this.koeficienty.length) {
                        rozdielKoeficientov[i] = this.koeficienty[i]
                                - p.koeficienty[i];
                    } else
                        rozdielKoeficientov[i] = p.koeficienty[i] + 0;

                }

            }
        }
        rozdiel = new Polynom(rozdielKoeficientov);
        return rozdiel;
    }


    public int compareTo(Object o) {
    	if(o instanceof Polynom){
    		Polynom test = (Polynom) o;
    		if (test.koeficienty == this.koeficienty){
    			for (int i=test.koeficienty.length-1;i>0;i--){
    				if(test.koeficienty[i]>this.koeficienty[i])
    					return -1;
    				if(test.koeficienty[i]<this.koeficienty[i])
    					return 1;
    			}
    			return 0;
    		}
    		if (test.koeficienty.length < this.koeficienty.length )
    			return -1;
    		else
    			return 1;
    	}
    	else return -2;
        
    }
    
    public Polynom derivuj() {
    	double[] derivovane = new double[this.koeficienty.length-1];
    	for (int i = koeficienty.length - 1; i>0 ;i--){
    		derivovane[i-1] = koeficienty[i] * i;
    	}
    	Polynom derivovany = new Polynom(derivovane);
    	return derivovany;
    }
    
    public static Polynom sucet(Polynom p1, Polynom p2){
    	return p1.pripocitaj(p2);
    }

    @Override
    public String toString() {
        String polynom = "";
        if (koeficienty.length != 1) {
            for (int i = stupenPolynomu; i > 0; i--) {
            	if (koeficienty[i]>=0){
            		polynom += " +" + String.valueOf(koeficienty[i]) + "x^"
                    + String.valueOf(i);
            	}
            	else{
            		polynom += " " + String.valueOf(koeficienty[i]) + "x^"
                    + String.valueOf(i);
            	}
            }
        } else
            return String.valueOf(koeficienty[0]) + "x^"
                    + String.valueOf(stupenPolynomu);
        return polynom;
    }

    /* moje testovacie demo
      public static void main(String[] args) { double[] poleKoef = new
      double[7]; double[] poleKoef2 = new double[8]; for (int i = 0; i <
      poleKoef.length; i++) { poleKoef[i] = Math.round(Math.random() * 10);
       } for (int i = 0; i < poleKoef2.length; i++) { poleKoef2[i] =
      Math.round(Math.random() * 10);
       }
     
      Polynom pol1 = new Polynom(poleKoef); Polynom pol2 = new
      Polynom(poleKoef2); System.out.println(pol1); System.out.println(pol2);
      System.out.println(pol2.equals(pol1));
      System.out.println(pol2.equals(pol2));
      System.out.println(pol1.vyhodnot(5));
      System.out.println(pol1.getKoeficient(2));
      System.out.println(pol1.getStupen());
      System.out.println(pol2.odpocitaj(pol1));
      System.out.println(pol1.derivuj());}
    */
    public static void main(String[] args) {
        Polynom p1 = new Polynom(3, 5);
        double[] exps = { 5, 7.5, 0, 2.5, 3 };
        Polynom p2 = new Polynom(exps);
        // Test jednej z chyb implementacie
        for (int i = 0; i < exps.length; i++)
            exps[i] = 0;
        System.out.println("p1: " + p1);
        System.out.println("p2: " + p2);
        System.out.println("p2(x = 8) = " + p2.vyhodnot(8));
        System.out.println("Stupen p2 je " + p2.getStupen());
        System.out.println("p1 ? p2 :" + p1.compareTo(p2));
        Polynom sucet = p1.pripocitaj(p2);
        System.out.println(sucet);
    }
}