Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions algorithms-modules/algorithms-twoanglesdifference/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>algorithms-twoanglesdifference</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>algorithms-twoanglesdifference</name>

<parent>
<groupId>com.baeldung</groupId>
<artifactId>algorithms-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>

<properties>
<!-- Compile target; run-time JDK can be 25 -->
<maven.compiler.release>21</maven.compiler.release>

<junit.jupiter.version>5.11.0</junit.jupiter.version>
<commons-math3.version>3.6.1</commons-math3.version>
<surefire.version>3.2.5</surefire.version>
</properties>

<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-math3</artifactId>
<version>${commons-math3.version}</version>
</dependency>
<!-- Ensure JUnit 5 is on the test classpath (parent usually manages the version) -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${junit.jupiter.version}</version>
<scope>test</scope>
</dependency>

</dependencies>

<!-- Usually already configured by the parents; harmless to keep -->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>3.26.0</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<release>${maven.compiler.release}</release>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.version}</version>
<configuration>
<useModulePath>false</useModulePath>
</configuration>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* Package to host code for calculating three types of angle difference
*/
package com.baeldung.algorithms.twoanglesdifference;

public class AngleDifferenceCalculator {

/**
* Normalizes an angle to be within the range [0, 360).
*
* @param angle The angle in degrees.
* @return The normalized angle.
*/
public static double normalizeAngle(double angle) {
return (angle % 360 + 360) % 360;
}

/**
* Calculates the absolute difference between two angles.
*
* @param angle1 The first angle in degrees.
* @param angle2 The second angle in degrees.
* @return The absolute difference in degrees.
*/
public static double absoluteDifference(double angle1, double angle2) {
return Math.abs(angle1 - angle2);
}

/**
* Calculates the shortest difference between two angles.
*
* @param angle1 The first angle in degrees.
* @param angle2 The second angle in degrees.
* @return The shortest difference in degrees (0 to 180).
*/
public static double shortestDifference(double angle1, double angle2) {
double diff = absoluteDifference(normalizeAngle(angle1), normalizeAngle(angle2));
return Math.min(diff, 360 - diff);
}

/**
* Calculates the signed shortest difference between two angles.
* A positive result indicates counter-clockwise rotation, a negative result indicates clockwise.
*
* @param angle1 The first angle in degrees.
* @param angle2 The second angle in degrees.
* @return The signed shortest difference in degrees (-180 to 180).
*/
public static double signedShortestDifference(double angle1, double angle2) {
double normalizedAngle1 = normalizeAngle(angle1);
double normalizedAngle2 = normalizeAngle(angle2);
double diff = normalizedAngle2 - normalizedAngle1;

if (diff > 180) {
return diff - 360;
} else if (diff < -180) {
return diff + 360;
} else {
return diff;
}
}
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* Package to host JUnit Test code for AngleDifferenceCalculator Class
*/
package com.baeldung.algorithms.twoanglesdifference;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;



class AngleDifferenceCalculatorTest {

private static final double EPSILON = 0.0001;

@Test
void whenNormalizingAngle_thenReturnsCorrectRange() {
assertEquals(90, AngleDifferenceCalculator.normalizeAngle(450), EPSILON);
assertEquals(30, AngleDifferenceCalculator.normalizeAngle(390), EPSILON);
assertEquals(330, AngleDifferenceCalculator.normalizeAngle(-30), EPSILON);
assertEquals(0, AngleDifferenceCalculator.normalizeAngle(360), EPSILON);
}

@Test
void whenCalculatingAbsoluteDifference_thenReturnsCorrectValue() {
assertEquals(100, AngleDifferenceCalculator.absoluteDifference(10, 110), EPSILON);
assertEquals(290, AngleDifferenceCalculator.absoluteDifference(10, 300), EPSILON);
assertEquals(30, AngleDifferenceCalculator.absoluteDifference(-30, 0), EPSILON);
}

@Test
void whenCalculatingShortestDifference_thenReturnsCorrectValue() {
assertEquals(100, AngleDifferenceCalculator.shortestDifference(10, 110), EPSILON);
assertEquals(70, AngleDifferenceCalculator.shortestDifference(10, 300), EPSILON);
assertEquals(30, AngleDifferenceCalculator.shortestDifference(-30, 0), EPSILON);
assertEquals(0, AngleDifferenceCalculator.shortestDifference(360, 0), EPSILON);
}

@Test
void whenCalculatingSignedShortestDifference_thenReturnsCorrectValue() {
assertEquals(100, AngleDifferenceCalculator.signedShortestDifference(10, 110), EPSILON);
assertEquals(-70, AngleDifferenceCalculator.signedShortestDifference(10, 300), EPSILON);
assertEquals(30, AngleDifferenceCalculator.signedShortestDifference(-30, 0), EPSILON);
assertEquals(70, AngleDifferenceCalculator.signedShortestDifference(300, 10), EPSILON);
}
}

1 change: 1 addition & 0 deletions algorithms-modules/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<module>algorithms-sorting</module>
<module>algorithms-sorting-2</module>
<module>algorithms-sorting-3</module>
<module>algorithms-twoanglesdifference</module>
</modules>

<properties>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8"?>
<!--suppress PyInterpreter -->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@

package com.baeldung.jaxb.gen;

import javax.xml.namespace.QName;
import jakarta.xml.bind.JAXBElement;
import jakarta.xml.bind.annotation.XmlElementDecl;
import jakarta.xml.bind.annotation.XmlRegistry;


Expand All @@ -21,6 +24,8 @@
@XmlRegistry
public class ObjectFactory {

private final static QName _UserRequest_QNAME = new QName("http://www.baeldung.com/jaxb/gen", "userRequest");
private final static QName _UserResponse_QNAME = new QName("http://www.baeldung.com/jaxb/gen", "userResponse");

/**
* Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: com.baeldung.jaxb.gen
Expand All @@ -45,4 +50,30 @@ public UserResponse createUserResponse() {
return new UserResponse();
}

/**
* Create an instance of {@link JAXBElement }{@code <}{@link UserRequest }{@code >}
*
* @param value
* Java instance representing xml element's value.
* @return
* the new instance of {@link JAXBElement }{@code <}{@link UserRequest }{@code >}
*/
@XmlElementDecl(namespace = "http://www.baeldung.com/jaxb/gen", name = "userRequest")
public JAXBElement<UserRequest> createUserRequest(UserRequest value) {
return new JAXBElement<UserRequest>(_UserRequest_QNAME, UserRequest.class, null, value);
}

/**
* Create an instance of {@link JAXBElement }{@code <}{@link UserResponse }{@code >}
*
* @param value
* Java instance representing xml element's value.
* @return
* the new instance of {@link JAXBElement }{@code <}{@link UserResponse }{@code >}
*/
@XmlElementDecl(namespace = "http://www.baeldung.com/jaxb/gen", name = "userResponse")
public JAXBElement<UserResponse> createUserResponse(UserResponse value) {
return new JAXBElement<UserResponse>(_UserResponse_QNAME, UserResponse.class, null, value);
}

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@

package com.baeldung.jaxb.gen;

import java.io.Serializable;
import jakarta.xml.bind.annotation.XmlAccessType;
import jakarta.xml.bind.annotation.XmlAccessorType;
import jakarta.xml.bind.annotation.XmlElement;
import jakarta.xml.bind.annotation.XmlRootElement;
import jakarta.xml.bind.annotation.XmlType;


Expand Down Expand Up @@ -34,12 +32,8 @@
"id",
"name"
})
@XmlRootElement(name = "userRequest")
public class UserRequest
implements Serializable
{
public class UserRequest {

private final static long serialVersionUID = -1L;
protected int id;
@XmlElement(required = true)
protected String name;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@

package com.baeldung.jaxb.gen;

import java.io.Serializable;
import java.util.Calendar;
import javax.xml.datatype.XMLGregorianCalendar;
import jakarta.xml.bind.annotation.XmlAccessType;
import jakarta.xml.bind.annotation.XmlAccessorType;
import jakarta.xml.bind.annotation.XmlElement;
import jakarta.xml.bind.annotation.XmlRootElement;
import jakarta.xml.bind.annotation.XmlSchemaType;
import jakarta.xml.bind.annotation.XmlType;
import jakarta.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import org.w3._2001.xmlschema.Adapter1;


/**
Expand Down Expand Up @@ -42,21 +38,16 @@
"gender",
"created"
})
@XmlRootElement(name = "userResponse")
public class UserResponse
implements Serializable
{
public class UserResponse {

private final static long serialVersionUID = -1L;
protected int id;
@XmlElement(required = true)
protected String name;
@XmlElement(required = true)
protected String gender;
@XmlElement(required = true, type = String.class)
@XmlJavaTypeAdapter(Adapter1 .class)
@XmlElement(required = true)
@XmlSchemaType(name = "dateTime")
protected Calendar created;
protected XMLGregorianCalendar created;

/**
* Gets the value of the id property.
Expand Down Expand Up @@ -127,10 +118,10 @@ public void setGender(String value) {
*
* @return
* possible object is
* {@link String }
* {@link XMLGregorianCalendar }
*
*/
public Calendar getCreated() {
public XMLGregorianCalendar getCreated() {
return created;
}

Expand All @@ -139,10 +130,10 @@ public Calendar getCreated() {
*
* @param value
* allowed object is
* {@link String }
* {@link XMLGregorianCalendar }
*
*/
public void setCreated(Calendar value) {
public void setCreated(XMLGregorianCalendar value) {
this.created = value;
}

Expand Down