# Cartesian coordinates from degree angle in Java

## The challenge

Write a simple function that takes polar coordinates (an angle in degrees and a radius) and returns the equivalent cartesian coordinates (rounded to 10 places).

For example:

`coordinates(90,1)` `=> (0.0, 1.0)`

`coordinates(45, 1)` `=> (0.7071067812, 0.7071067812)`

## The solution in Java code

Option 1:

```.wp-block-code{border:0;padding:0}.wp-block-code>div{overflow:auto}.shcb-language{border:0;clip:rect(1px,1px,1px,1px);-webkit-clip-path:inset(50%);clip-path:inset(50%);height:1px;margin:-1px;overflow:hidden;padding:0;position:absolute;width:1px;word-wrap:normal;word-break:normal}.hljs{box-sizing:border-box}.hljs.shcb-code-table{display:table;width:100%}.hljs.shcb-code-table>.shcb-loc{color:inherit;display:table-row;width:100%}.hljs.shcb-code-table .shcb-loc>span{display:table-cell}.wp-block-code code.hljs:not(.shcb-wrap-lines){white-space:pre}.wp-block-code code.hljs.shcb-wrap-lines{white-space:pre-wrap}.hljs.shcb-line-numbers{border-spacing:0;counter-reset:line}.hljs.shcb-line-numbers>.shcb-loc{counter-increment:line}.hljs.shcb-line-numbers .shcb-loc>span{padding-left:.75em}.hljs.shcb-line-numbers .shcb-loc::before{border-right:1px solid #ddd;content:counter(line);display:table-cell;padding:0 .75em;text-align:right;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;white-space:nowrap;width:1%}```import java.awt.geom.Point2D;

class CoordinatesInspector {
Point2D.Double coordinates(Double degrees, Double radius) {
return new Point2D.Double(
}
}
```Code language: Java (java)```

Option 2:

``````import java.awt.geom.Point2D;
import java.math.*;

public class CoordinatesInspector {

public Point2D coordinates(Double degrees, Double radius) {
double x = radius * Math.cos(rd);
double y = radius * Math.sin(rd);
BigDecimal xb = new BigDecimal(x).setScale(10, RoundingMode.HALF_UP);
BigDecimal yb = new BigDecimal(y).setScale(10, RoundingMode.HALF_UP);
return new Point2D.Double(xb.doubleValue(),yb.doubleValue());
}

}
```Code language: Java (java)```

Option 3:

``````import java.awt.geom.Point2D;

public class CoordinatesInspector {

public static Point2D coordinates(Double degrees, Double radius) {
return new Point2D.Double(
);
}

}
```Code language: Java (java)```

## Test cases to validate our solution

``````import java.awt.geom.Point2D;
import org.junit.Test;
import static org.junit.Assert.assertEquals;

public class CoordinatesTest {

@Test public void test_1() {
Point2D result = new CoordinatesInspector().coordinates(90.0,1.0);
assertEquals("Incorrect coordinates", new Point2D.Double(0.0,1.0), result);
}

}
```Code language: Java (java)```

``````import java.awt.geom.Point2D;
import static org.junit.Assert.*;
import org.junit.Test;

public class CoordinatesTest {

@Test public void test_1() {
Point2D result = new CoordinatesInspector().coordinates(90.0,1.0);
assertEquals("Incorrect coordinates", new Point2D.Double(0.0,1.0), result);
}

@Test public void test_2() {
Point2D result = new CoordinatesInspector().coordinates(90.0,2.0);
assertEquals("Incorrect coordinates", new Point2D.Double(0.0,2.0), result);
}

@Test public void test_3() {
Point2D result = new CoordinatesInspector().coordinates(0.0,1.0);
assertEquals("Incorrect coordinates", new Point2D.Double(1.0,0.0), result);
}

@Test public void test_4() {
Point2D result = new CoordinatesInspector().coordinates(45.0,1.0);
assertEquals("Incorrect coordinates", new Point2D.Double(0.7071067812,0.7071067812), result);
}

@Test public void test_5() {
Point2D result = new CoordinatesInspector().coordinates(1090.0,10000.0);
assertEquals("Incorrect coordinates", new Point2D.Double(9848.0775301221,1736.4817766693), result);
}

@Test public void test_6() {
Point2D result = new CoordinatesInspector().coordinates(-270.0,1.0);
assertEquals("Incorrect coordinates", new Point2D.Double(0.0,1.0), result);
}

}
```Code language: Java (java)```
Tags:
Subscribe
Notify of