Skip to content

Commit 39a46e5

Browse files
committed
Add ejer1_enunciado
1 parent cf68607 commit 39a46e5

3 files changed

Lines changed: 168 additions & 0 deletions

File tree

tema1_2_ejer1_enunciado/pom.xml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>es.codeurjc.test</groupId>
7+
<artifactId>tema1_2-ejer1_enunciado</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<properties>
12+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
13+
<maven.compiler.source>1.8</maven.compiler.source>
14+
<maven.compiler.target>1.8</maven.compiler.target>
15+
</properties>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>org.junit.jupiter</groupId>
20+
<artifactId>junit-jupiter-engine</artifactId>
21+
<version>5.6.0</version>
22+
<scope>test</scope>
23+
</dependency>
24+
</dependencies>
25+
26+
<build>
27+
<plugins>
28+
<plugin>
29+
<artifactId>maven-surefire-plugin</artifactId>
30+
<version>2.19.1</version>
31+
<dependencies>
32+
<dependency>
33+
<groupId>org.junit.platform</groupId>
34+
<artifactId>junit-platform-surefire-provider</artifactId>
35+
<version>1.0.2</version>
36+
</dependency>
37+
</dependencies>
38+
</plugin>
39+
</plugins>
40+
</build>
41+
42+
</project>
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package es.codeurjc.test.complex;
2+
3+
import static java.lang.Math.*;
4+
5+
public class Complex {
6+
7+
private double realPart = 0;
8+
private double imaginaryPart = 0;
9+
10+
public Complex(double realPart, double imaginaryPart) {
11+
this.realPart = realPart;
12+
this.imaginaryPart = imaginaryPart;
13+
}
14+
15+
public Complex() {
16+
}
17+
18+
public double getRealPart() {
19+
return this.realPart;
20+
}
21+
22+
public double getImaginaryPart() {
23+
return this.imaginaryPart;
24+
}
25+
26+
public double abs() {
27+
return sqrt(pow(this.realPart, 2.0) + pow(this.imaginaryPart, 2.0));
28+
}
29+
30+
public double phase() {
31+
return atan2(this.imaginaryPart, this.realPart);
32+
}
33+
34+
// return a new Complex object whose value is the conjugate of this
35+
public Complex conjugate() {
36+
return new Complex(this.realPart, -this.imaginaryPart);
37+
}
38+
39+
// return a new Complex object whose value is the reciprocal of this
40+
public Complex reciprocal() {
41+
double scale = pow(this.realPart, 2.0) + pow(this.imaginaryPart, 2.0);
42+
return new Complex(this.realPart / scale, this.imaginaryPart / scale);
43+
}
44+
45+
// return a new Complex object whose value is (this + complex)
46+
public Complex add(Complex complex) {
47+
48+
double newRealPart = this.realPart + complex.realPart;
49+
double newImaginaryPart = this.imaginaryPart + complex.imaginaryPart;
50+
51+
return new Complex(newRealPart, newImaginaryPart);
52+
}
53+
54+
// return a new Complex object whose value is (this - complex)
55+
public Complex minus(Complex complex) {
56+
double newRealPart = this.realPart - complex.realPart;
57+
double newImaginaryPart = this.imaginaryPart - complex.imaginaryPart;
58+
59+
return new Complex(newRealPart, newImaginaryPart);
60+
}
61+
62+
// return a new Complex object whose value is (this * complex)
63+
public Complex times(Complex complex) {
64+
double newRealPart = (this.realPart * complex.realPart)
65+
- (this.imaginaryPart * complex.imaginaryPart);
66+
double newImaginaryPart = (this.realPart * complex.imaginaryPart)
67+
- (this.imaginaryPart * complex.realPart);
68+
69+
return new Complex(newRealPart, newImaginaryPart);
70+
}
71+
72+
// return a new Complex object whose value is (this * alpha)
73+
public Complex times(double alpha) {
74+
return new Complex(alpha * this.realPart, alpha * this.imaginaryPart);
75+
}
76+
77+
// return a new Complex object whose value is (this / complex)
78+
public Complex divides(Complex complex) {
79+
return this.times(complex.reciprocal());
80+
}
81+
82+
83+
@Override
84+
public int hashCode() {
85+
final int prime = 31;
86+
int result = 1;
87+
long temp;
88+
temp = Double.doubleToLongBits(imaginaryPart);
89+
result = prime * result + (int) (temp ^ (temp >>> 32));
90+
temp = Double.doubleToLongBits(realPart);
91+
result = prime * result + (int) (temp ^ (temp >>> 32));
92+
return result;
93+
}
94+
95+
@Override
96+
public boolean equals(Object obj) {
97+
if (this == obj)
98+
return true;
99+
if (obj == null)
100+
return false;
101+
if (getClass() != obj.getClass())
102+
return false;
103+
Complex other = (Complex) obj;
104+
if (Double.doubleToLongBits(imaginaryPart) != Double
105+
.doubleToLongBits(other.imaginaryPart))
106+
return false;
107+
if (Double.doubleToLongBits(realPart) != Double
108+
.doubleToLongBits(other.realPart))
109+
return false;
110+
return true;
111+
}
112+
113+
public String toString() {
114+
return this.realPart + " + " + this.imaginaryPart + "i ";
115+
}
116+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package es.codeurjc.test.complex;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
public class ComplexTest {
8+
9+
10+
}

0 commit comments

Comments
 (0)