# Calculate the Surface Area and Volume of a Box with Java ## The challenge

Write a function that returns the total surface area and volume of a box as an array: `[area, volume]`

## The solution in Java

We know that the formula to calculate the area of a box is as follows:

`2(h × W) + 2(h × L) + 2(W × L)`

We also know that the formula to calculate the volume of a box is as follows:

`W x h x H`

Therefore, we can write the following code:

```.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%}```public class SurfaceAreaAndVolume {
public static int[] getSize(int w,int h,int d) {

int area = ((h * w)*2) + ((h * d)*2) + ((w * d)*2);
int volume =  w*h*d;

return new int[] {area, volume};
}
}
```Code language: Java (java)```

## Test cases to validate our code

It is important to have some tests to validate that our code works correctly with different inputs.

``````import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertArrayEquals;
import org.junit.runners.JUnit4;

public class FinalTest {

@Test
public void Test1() {
assertArrayEquals(new int[] {88,48},SurfaceAreaAndVolume.getSize(4, 2, 6));
assertArrayEquals(new int[]{6,1}, SurfaceAreaAndVolume.getSize(1, 1, 1));
assertArrayEquals(new int[]{10,2}, SurfaceAreaAndVolume.getSize(1, 2, 1));
assertArrayEquals(new int[]{16,4}, SurfaceAreaAndVolume.getSize(1, 2, 2));
assertArrayEquals(new int[]{600,1000}, SurfaceAreaAndVolume.getSize(10, 10, 10));
}

@Test
public void RandomTest() {
Rg rg = new Rg((int) 69 * 10000);
int x = rg.a();
int y = rg.b();
int z = rg.c();
assertArrayEquals(Preloaded.getSize(x, y, z), SurfaceAreaAndVolume.getSize(x, y, z));
}
}
```Code language: Java (java)```
Tags:
Subscribe
Notify of 