Vous êtes sur la page 1sur 15

public class Complejo {

private double real;


private double imag;

//Constructor por defecto de la clase


public Complejo() {

}
//Constructor con parámetros
public Complejo(double real, double imag) {
this.real = real;
this.imag = imag;
}

//métodos setters y getters


public double getImag() {
return imag;
}

public void setImag(double imag) {


this.imag = imag;
}

public double getReal() {


return real;
}

public void setReal(double real) {


this.real = real;
}

//sumar dos números complejos


//(a, b) + (c, d) = (a + c, b + d);
public Complejo sumar(Complejo c){
Complejo aux = new Complejo();
aux.real = real + c.real;
aux.imag = imag + c.imag;
return aux;
}

//restar dos números complejos


//(a, b) - (c, d) = (a - c, b - d);
public Complejo restar(Complejo c){
Complejo aux = new Complejo();
aux.real = real - c.real;
aux.imag = imag - c.imag;
return aux;
}

//multiplicar dos números complejos


//(a, b) * (c, d) = (a*c – b*d, a*d + b*c)
public Complejo multiplicar(Complejo c){
Complejo aux = new Complejo();
aux.real = real * c.real - imag * c.imag;
aux.imag = real * c.imag + imag * c.real;
return aux;
}

//multiplicar un número complejo por un número de tipo double


//(a, b) * n = (a * n, b * n)
public Complejo multiplicar(double n){
Complejo aux = new Complejo();
aux.real = real * n;
aux.imag = imag * n;
return aux;
}

//dividir dos números complejos


//(a, b) / (c, d) = ((a*c + b*d) / (c^2 + d^2) , (b*c – a*d) / (c^2 + d^2))
public Complejo dividir(Complejo c){
Complejo aux = new Complejo();
aux.real = (real * c.real + imag * c.imag)/(c.real * c.real + c.imag * c.imag);
aux.imag = (imag * c.real - real * c.imag)/(c.real * c.real + c.imag *
c.imag);
return aux;
}

//método toString
@Override
public String toString() {
return "(" + real + ", " + imag + ")";
}

//método equals
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Complejo other = (Complejo) obj;
if (this.real != other.real) {
return false;
}
if (this.imag != other.imag) {
return false;
}
return true;
}
} //Fin de la Clase Complejo Java

package clasesActividad4;
/*

* Ejercicio 3 Cree una clase llamada Complejo para realizar operaciones aritméticas
con números

* complejos. Estos números tienen la forma:

parteReal + parteImaginaria * i

donde i representa la raiz de -1.

Escribe un programa para probar tu clase. Usa variables reales para almacenar los datos
privados

de la clase. Crea un constructor por defecto que inicialice el número a (0,0) y otro que
permita

inicializar los dos valores al crearlo.

Crea métodos públicos para realizar las siguientes operaciones:

Suma: (a,b)+(c,d)=(a+c,b+d)

Resta: (a,b)-(c,d)=(a-c,b-d)

Producto: (a,b)*(c,d)=(ac-bd,ad+bc)

Cociente: (a,b)/(c,d)=((ac+bd)/(c2+d2),(bc-ad)/(c2+d2))

Crea un método para imprimir un número complejo de la forma (a,b), donde a es la


parte real y b
es la parte imaginaria.

*/

public class Complejo {

//Atributos de clase

private double parteReal;

private double parteImaginaria;

//Constructores

public Complejo(){

this.parteReal=0;

this.parteImaginaria=0;

public Complejo(double real, double imaginaria){

this.parteReal=real;

this.parteImaginaria=imaginaria;

//Accesadores

public double getParteReal() {

return parteReal;
}

public double getParteImaginaria() {

return parteImaginaria;

//Métodos

//Suma: (a,b)+(c,d)=(a+c,b+d)

public void suma(Complejo otro){

this.parteReal = this.getParteReal() + otro.getParteReal();

this.parteImaginaria += otro.getParteImaginaria();

//Resta: (a,b)-(c,d)=(a-c,b-d)

public void resta(Complejo otro){

this.parteReal -= otro.getParteReal();

this.parteImaginaria -= otro.getParteImaginaria();

//Producto: (a,b)*(c,d)=(ac-bd,ad+bc)

public void producto(Complejo otro){

double parteReal = ((this.parteReal*otro.getParteReal())-


(this.parteImaginaria*otro.getParteImaginaria()));

double parteImaginaria =
((this.parteReal*otro.parteImaginaria)+(this.parteImaginaria*otro.parteReal));
this.parteReal= parteReal;

this.parteImaginaria=parteImaginaria;

//Cociente: (a,b)/(c,d)=((ac+bd)/(c2+d2),(bc-ad)/(c2+d2))

public void cociente(Complejo otro){

double parteReal =
(((this.parteReal*otro.parteReal)+(this.parteImaginaria*otro.parteImaginaria))

/((Math.pow(otro.parteReal, 2)+(Math.pow(otro.parteImaginaria,2)))));

double parteImaginaria = (((this.parteImaginaria*otro.parteReal)-


(this.parteReal*otro.parteImaginaria))

/((Math.pow(otro.parteReal, 2)+(Math.pow(otro.parteImaginaria,2)))));

this.parteReal= parteReal;

this.parteImaginaria=parteImaginaria;

//Métodos auxiliares

@Override

public String toString() {

return "(" + parteReal + ","


+ parteImaginaria + ")";

public static void main(String[] args) {

Complejo n1 = new Complejo(9,5);

Complejo n2 = new Complejo(7,8);

System.out.print("Operación de SUMA de los números complejos: "+ n1

+ " - "+n2 +" = ");

n1.suma(n2);

System.out.println(n1+"\n");

System.out.print("Operación de RESTA de los números complejos: "+ n1 + " + "+ n2


+ " = ");

n1.resta(n2);

System.out.println(n1+"\n");

System.out.println("Operación de PRODUCTO de los números complejos: ("+


n1.getParteReal()+"+("+n1.getParteImaginaria() +"i)) "

+ "* ("+n2.getParteReal()+"+("+n2.getParteImaginaria()+"i))");
n1.producto(n2);

System.out.println(n1+"\n");

System.out.println("Operación de COCIENTE de los números complejos: ("+


n1.getParteReal()+"+("+n1.getParteImaginaria() +"i)) "

+ "* ("+n2.getParteReal()+"+("+n2.getParteImaginaria()+"i))");

n1.cociente(n2);

System.out.println(n1+"\n");

public class Complejo{


private double real;
private double imaginario;
public Complejo (){
real = 0.0;
imaginario = 0.0;
}
public Complejo (double real, double im){
this.real = real;
imaginario = im;
}
public void getComplejo(double r, double i){
real = r;
imaginario = i;
}
public void setReal(double numeroReal){
real = numeroReal;
}
public void setImaginario(double numeroImaginario){
imaginario = numeroImaginario;
}
public double getReal(){
return real;
}
public double getImaginario(){
return imaginario;
}
public Complejo suma(Complejo sumando){
Complejo resultado;
resultado = new Complejo();
resultado.setReal(this.real + sumando.getReal() );
resultado.setImaginario(this.imaginario +
sumando.getImaginario());
return resultado;
}
public Complejo resta (Complejo resta){
Complejo resultado;
resultado = new Complejo();
resultado.setReal(this.real - resta.getReal() );
resultado.setImaginario(this.imaginario -
resta.getImaginario());
return resultado;
}
public Complejo multiplicacion (Complejo producto){
Complejo resultado;
resultado = new Complejo();
resultado.getComplejo(((this.real * producto.getReal())-
(this.imaginario * producto.getImaginario())),
((this.real * producto.getImaginario() ) +
(this.imaginario * producto.getReal() )));
return resultado;
}
public Complejo division(Complejo divisor){
Complejo resultado;
resultado = new Complejo();
resultado.getComplejo((((this.real * divisor.getReal()) +
(this.imaginario * divisor.getImaginario()))/
((divisor.getReal() * divisor.getReal())+
(divisor.getImaginario() * divisor.getImaginario()))),
(((this.real * divisor.getImaginario() ) -
(this.imaginario * divisor.getReal()
))/((divisor.getReal() *
divisor.getReal())+(divisor.getImaginario() *
divisor.getImaginario()))));
return resultado;
}
}

import java.util.*;
public class Main{
private static Scanner teclado = new Scanner (System.in);
public static int Menu(){
int opcion;
System.out.println();
System.out.println("calculadora de numeros complejos");
System.out.println();
System.out.println("1)SUMAR");
System.out.println("2)RESTAR");
System.out.println("3)MULTIPLICAR");
System.out.println("4)DIVIDIR");
System.out.println("5)SALIR");
System.out.println();
System.out.print(" OPCION: ");
opcion = teclado.nextInt();
return opcion;
}
public static void main(String args[]){
Complejo calculadora,complejo1,complejo2,resultado;
Process p;
calculadora = new Complejo();
double real, imag;
int opcion;
do{
opcion = Menu();

switch(opcion){
case 1:
System.out.print("Escribe el primer real: ");
real = teclado.nextDouble();
System.out.print("Escribe el primer imaginario:
");
imag = teclado.nextDouble();
complejo1 = new Complejo (real, imag);
System.out.print("Escribe el segundo real: ");
real = teclado.nextDouble();
System.out.print("Escribe el segundo imaginario:
");
imag = teclado.nextDouble();
complejo2 = new Complejo (real, imag);
resultado = complejo1.suma(complejo2);
if (resultado.getImaginario() < 0.0)
System.out.println("El resultadoultado es: " +
resultado.getReal() +"+ i"+ -
resultado.getImaginario());
else
System.out.println("El resultadoultado es: " +
resultado.getReal() +"+ i"+
resultado.getImaginario());
break;
case 2:
System.out.print("Escribe el primer real: ");
real = teclado.nextDouble();
System.out.print("Escribe el primer imaginario:
");
imag = teclado.nextDouble();
complejo1 = new Complejo (real, imag);
System.out.print("Escribe el segundo real: ");
real = teclado.nextDouble();
System.out.print("Escribe el segundo imaginario:
");
imag = teclado.nextDouble();
complejo2 = new Complejo (real, imag);
resultado = complejo1.resta(complejo2);
if (resultado.getImaginario() <0.0)
System.out.println("El resultadoultado es: " +
resultado.getReal() +"+ i"+ -
resultado.getImaginario());
else
System.out.println("El resultadoultado es: " +
resultado.getReal() +"+ i"+
resultado.getImaginario());
break;
case 3:
System.out.print("Escribe el primer real: ");
real = teclado.nextDouble();
System.out.print("Escribe el primer imaginario:
");
imag = teclado.nextDouble();
complejo1 = new Complejo (real, imag);
System.out.print("Escribe el segundo real: ");
real = teclado.nextDouble();
System.out.print("Escribe el segundo imaginario:
");
imag = teclado.nextDouble();
complejo2 = new Complejo (real, imag);
resultado = complejo1.resta(complejo2);
resultado = complejo1.multiplicacion(complejo2);
if (resultado.getImaginario() <0.0)
System.out.println("El resultadoultado es: " +
resultado.getReal() +"+ i"+ -
resultado.getImaginario());
else
System.out.println("El resultadoultado es: " +
resultado.getReal() +"+ i"+
resultado.getImaginario());
break;
case 4:
System.out.print("Escribe el primer real: ");
real = teclado.nextDouble();
System.out.print("Escribe el primer imaginario:
");
imag = teclado.nextDouble();
complejo1 = new Complejo (real, imag);
System.out.print("Escribe el segundo real: ");
real = teclado.nextDouble();
System.out.print("Escribe el segundo imaginario:
");
imag = teclado.nextDouble();
complejo2 = new Complejo (real, imag);
resultado = complejo1.resta(complejo2);
resultado = complejo1.division(complejo2);
if (resultado.getImaginario() <0.0)
System.out.println("El resultadoultado es: "+
resultado.getReal() +"+ i"+ -
resultado.getImaginario());
else
System.out.println("El resultadoultado es: " +
resultado.getReal() +"+ i"+
resultado.getImaginario());
break;
case 5:
System.out.println("adios");
break;
default:
break;
}
}while(opcion!=5);
}
}
2 public class Rectangulo implements Cloneable{
3 private double x;
4 private double y;
5 private double ancho;
6 private double alto;
7
8 /* Constructor con parámetros para los 4 atributos */
9 public Rectangulo(double x1, double y1, double w, double h) {
10 x=x1;
11 y=y1;
12 ancho=w;
13 alto=h;
14 }
15
16 /* Constructor sin parámetros. Sobrecarga del constructor */
17 public Rectangulo() {
18 x=0;
19 y=0;
20 ancho=1;
21 alto=1;
22 }
23 /* Constructor con parámetros para alto y ancho. Sobrecarga del constructor */
24 public Rectangulo(double w, double h) {
25 x=0;
26 y=0;
27 ancho=w;
28 alto=h;
29 }
30 public void imprimir(){
31 System.out.println("Esta en (" + x + ", " + y + "). Ancho: " + ancho +
32 ", alto: " + alto);
33 }
34 public double calcularArea(){
35 return (ancho*alto);
36 }
37 public void desplazar(double dx, double dy){
38 x+=dx;
39 y+=dy;
40 }
41 public boolean estaDentro(Punto miPunto){
42 double x1 = (double)miPunto.getPosX();
43 double y1 = (double)miPunto.getPosY();
44 if((x1>=x)&&(x1<=x+ancho)&&(y1>=y)&&(y1<=y+alto)){
45 return true;
46 }
47 else{
48 return false;
49 }
50 }
51 public boolean estaDentro(Rectangulo otroRectangulo){
52 boolean inP1, inP2, inP3, inP4;
53 double coordX, coordY;
54 Punto vertice;
55 // Punto 1
56 coordX = otroRectangulo.x;
57 coordY = otroRectangulo.y;
58 vertice = new Punto(coordX, coordY);
59 inP1 = this.estaDentro(vertice);
60 // Punto 2
61 coordX = otroRectangulo.x + otroRectangulo.ancho;
62 coordY = otroRectangulo.y;
63 vertice = new Punto(coordX, coordY);
64 inP2 = this.estaDentro(vertice);
65 // Punto 3
66 coordX = otroRectangulo.x;
67 coordY = otroRectangulo.y + otroRectangulo.alto;
68 vertice = new Punto(coordX, coordY);
69 inP3 = this.estaDentro(vertice);
70 // Punto 4
71 coordX = otroRectangulo.x + otroRectangulo.ancho;
72 coordY = otroRectangulo.y + otroRectangulo.alto;
73 vertice = new Punto(coordX, coordY);
74 inP4 = this.estaDentro(vertice);
75 if (inP1 && inP2 && inP3 && inP4){
76 return true;
77 }
78 else{
79 return false;
80 }
81 }
82 public double getX(){
83 return x;
84 }
85 public void setX(double x){
86 this.x = x;
87 }
88 public double getY(){
89 return y;
90 }
91 public void setY(double y){
92 this.y = y;
93 }
94 public double getAncho(){
95 return ancho;
96 }
97 public void setAncho(double ancho){
98 this.ancho = ancho;
99 }
100 public double getAlto(){
101 return alto;
102 }
103 public void setAlto(double alto){
104 this.alto = alto;
105 }
106 }
107
108
109
110
111
112
113
114
115
116
117
118
119

class Punto {

private double posX = 0, posY = 0;

/** Constructor */

public Punto(double posX, double posY){

this.posX = posX;

this.posY = posY;

/** Getters and setters */

public double getPosX(){

return posX;

}
public void setPosX(double px){

posX = px;

public double getPosY(){

return posY;

public void setPosY(double py){

posY = py;

public void imprime(){

System.out.println("X: " + posX + ", Y: " + posY);

Vous aimerez peut-être aussi