import java.io.*; public class Complejos { //Atributos float real, imaginario; //Constructores public Complejos (float re, float im) { real = re; imaginario = im; } public Complejos (float im) { imaginario = im; } public Complejos(){} public Complejos(Complejos n) { real = n.getReal(); imaginario = n.getImaginario(); } //Metodos get y set void setReal (float r) { real = r; } void setImaginario(float i) { imaginario = i; } float getReal () { return real; } float getImaginario() { return imaginario; } //Metodo para sumar complejos Complejos sumar (Complejos m) { float r,i; r=real+m.getReal(); i=imaginario+m.getImaginario(); Complejos c=new Complejos(r,i); return c; } //Metodo para sumar dos complejos pero pasandole el complejo en atributos (parte real e imaginaria por separado) Complejos sumar (float a, float b) { Complejos n=new Complejos (a,b); return this.sumar(n); } //Metodo para comparar 2 numeros complejos: para ver si son iguales o no boolean comparar (Complejos m) { boolean v=false; if(real==m.getReal()) if(imaginario==m.getImaginario()) v=true; return v; } //Metodo imprime void Imprime() { if(imaginario<0) System.out.println("El numero complejo es "+real+" "+imaginario+" j"); else System.out.println("El numero complejo es "+real+" + "+imaginario+" j"); } //Metodo main public static void main(String args[])throws Exception { float r,i,re,im; boolean ok = false; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); while(ok == false) { System.out.println("Introduzca parte real del complejo"); r = Float.parseFloat(br.readLine()); try{ float test = (Float) r; r = whateverwazere }Catch(SomeMad casting exception e){ lolol.not.an.int;} } ok = false; while(ok == false) { System.out.println("Introduzca parte imaginaria del complejo"); i = Float.parseFloat(br.readLine()); if(i.isDigit()) ok = true; } ok = false; Complejos k = new Complejos (r,i); k.Imprime(); System.out.println(""); while(ok = false) { System.out.println("Introduzca parte real de otro complejo"); re = Float.parseFloat(br.readLine()); if(re.isDigit()) ok = true; } ok = false; while(ok = false) { System.out.println("Introduzca parte imaginaria de otro complejo"); im = Float.parseFloat(br.readLine()); if(im.isDigit()) ok = true; } Complejos j = new Complejos (re,im); j.Imprime(); System.out.println(""); boolean comparacion=k.comparar(j); if(comparacion) System.out.println("Los numeros son iguales"); else System.out.println("Los numeros son distintos"); System.out.println(""); float sumarara; sumarara=k.getReal()+j.getImaginario(); System.out.println("La suma de la parte real del primero y la parte imaginaria del segundo es: "+sumarara); System.out.println(""); Complejos suma2=j.sumar(2,4); suma2.Imprime(); /* Complejos n = new Complejos (3,5); Complejos m = new Complejos (6,-3); Complejos suma1 = new Complejos (n.sumar(m)); n.Imprime(); m.Imprime(); suma1.Imprime(); */ } }