# Naloga/Programiranje/Objektno programiranje/Razred Polinom/Rešitev (Java)

` public class Polinom {	int st;	Ulomek[] p; 	private Ulomek N = new Ulomek (0); 	//Konstruktorji	public Polinom (int n, Ulomek[] a) {		st=n;		p=new Ulomek [st+1];		if (st>=0) for (int i=0;i<=st;i++) p[i]=a[i];	} 	public Polinom (int n) {		st=n;                p=new Ulomek [st+1];	} 	public Polinom () {		st=-1;	} 	//metode	public int stopnja() {		return st;	} 	public Ulomek vrednost(Ulomek a) {		int n=st;		Ulomek tmp = p[n]; 		for (int i= n-1;i>=0;i--) {			tmp=p[i].vsota(a.zmnozek(tmp));		}		tmp.okrajsaj(); 		return tmp;	} 	public boolean equals(Polinom q) {		if (st!=q.st) return false;		for (int i=0;i<=st;i++) {			if (!p[i].equals(q.p[i])) return false;		}		return true;	} 	private void shiftleft() {		for (int i=0;i<=st;i++) {			p[i]=p[i+1];		}		p[st]=N;	} 	public Polinom vsota(Polinom q) {		int m = Math.min(st,q.st);		int M = Math.max(st,q.st); 		Polinom s = new Polinom(M); 		int i;		for (i=0;i<=m;i++) 			s.p[i]=p[i].vsota(q.p[i]); 		for (int j=i+1;j<=M;j++) {			if (M==st) {				s.p[j]=p[j];			}			else s.p[j]=q.p[j];		}				for (int j=0;s.p[j].equals(N);j++) {			s.shiftleft();			s.st--;		}			return s;	} 	public Polinom zmnozek(Polinom q) {		int n = st+q.st;		Polinom s = new Polinom (n);				for (int k=0;k<=n;k++) {			Ulomek vs = new Ulomek(0);			for (int i=0;i<=k;i++) {				if (i<=st && (k-i)<=q.st)  					vs.pristej( p[i].zmnozek(q.p[k-i]) );			}			s.p[k]=vs;		} 		return s;	} 	public String toString() {		if (st<0) return "0";		else if (st==0) return p[0].toString();		else  {			String res = (p[0]!=null) ? p[0].toString() : "";			if (p[0]!=null && (st>0 && !p[0].equals(N))) res+=" + "; 			res+=(p[1]!=null) ? p[1].toString()+" x" : "";			if (p[1]!=null && (st>1 && !p[1].equals(N))) res+=" + "; 			for (int i=2;i<=st;i++) {				if (p[i]==null || p[i].equals(N)) continue;				res+=p[i].toString()+" x^("+i+")";				if (i<st) res+=" + ";			} 			boolean pogoj=true;			for (int i=0;i<=st;i++) if (p[i]!=null) pogoj=false;			if (pogoj) return null;						return res;		}	} } `