package Kompleksni; import java.lang.Math.*; public class KompleksniBroj { double re; double im; KompleksniBroj(){} KompleksniBroj(double x) { re = x; im = 0; } KompleksniBroj(double x, double y) { re = x; im = y; } public void Saberi (KompleksniBroj z1) { this.re += z1.re; this.im += z1.im; } //NAPOMENA: //Metod sabiranja pozeljnije je napisati ovako: //public KompleksniBroj Saberi (KompleksniBroj z1) //{ // KompleksniBroj kb=new KompleksniBroj(); // kb.re = re + z1.re; // kb.im = im + z1.im; // return kb; //} //Ovim se postize da se ni jedan od sabiraka ne menja, //vec da se samo zbir vrati kao rezultat. //Takodje je pozeljno da su podaci private a metode public. public void Oduzmi (KompleksniBroj z1) { this.re -= z1.re; this.im -= z1.im; } public void Pomnozhi (KompleksniBroj z1) { this.re = (this.re * z1.re) + ((-1) * this.im * z1.im); this.im = (this.re * z1.im) + (this.im * z1.im); } public void Podeli (KompleksniBroj z1) { if ((z1.re == 0) && (z1.im == 0)) System.out.println("Ne mozhe se deliti nulom"); this.re = ((this.re * z1.re) + (this.im * z1.im)) / ((z1.re * z1.re) + (z1.im * z1.im)); this.im = ((this.im * z1.re) + (this.re * z1.im)) / ((z1.re * z1.re) + (z1.im * z1.im)); } public void Konjugovani () { this.im = (-1) * this.im; } public double radius () { return Math.sqrt(this.re * this.re + this.im * this.im); } public double ugao () { return Math.atan(this.im / this.re); } public String toString() { return re + " + (" + im + ")i"; } }