Professional Documents
Culture Documents
}
//Constructor con parámetros
public Complejo(double real, double imag) {
this.real = real;
this.imag = imag;
}
//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
parteReal + parteImaginaria * i
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
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))
*/
//Atributos de clase
//Constructores
public Complejo(){
this.parteReal=0;
this.parteImaginaria=0;
this.parteReal=real;
this.parteImaginaria=imaginaria;
//Accesadores
return parteReal;
}
return parteImaginaria;
//Métodos
//Suma: (a,b)+(c,d)=(a+c,b+d)
this.parteImaginaria += otro.getParteImaginaria();
//Resta: (a,b)-(c,d)=(a-c,b-d)
this.parteReal -= otro.getParteReal();
this.parteImaginaria -= otro.getParteImaginaria();
//Producto: (a,b)*(c,d)=(ac-bd,ad+bc)
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))
double parteReal =
(((this.parteReal*otro.parteReal)+(this.parteImaginaria*otro.parteImaginaria))
/((Math.pow(otro.parteReal, 2)+(Math.pow(otro.parteImaginaria,2)))));
/((Math.pow(otro.parteReal, 2)+(Math.pow(otro.parteImaginaria,2)))));
this.parteReal= parteReal;
this.parteImaginaria=parteImaginaria;
//Métodos auxiliares
@Override
n1.suma(n2);
System.out.println(n1+"\n");
n1.resta(n2);
System.out.println(n1+"\n");
+ "* ("+n2.getParteReal()+"+("+n2.getParteImaginaria()+"i))");
n1.producto(n2);
System.out.println(n1+"\n");
+ "* ("+n2.getParteReal()+"+("+n2.getParteImaginaria()+"i))");
n1.cociente(n2);
System.out.println(n1+"\n");
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 {
/** Constructor */
this.posX = posX;
this.posY = posY;
return posX;
}
public void setPosX(double px){
posX = px;
return posY;
posY = py;