# How to Calculate Age Range Compatibility as an Equation in Java

## The challenge

Everybody knows the classic “half your age plus seven” dating rule that a lot of people follow (including myself). It’s the ‘recommended’ age range in which to date someone.

`minimum age <= your age <= maximum age`

Given an integer (1 <= n <= 100) representing a person’s age, return their minimum and maximum age range.

This equation doesn’t work when the age <= 14, so use this equation instead:

```.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%}```min = age - 0.10 * age
max = age + 0.10 * age``````

You should floor all your answers so that an integer is given instead of a float (which doesn’t represent age). `Return your answer in the form [min]-[max]`

### Examples:

``````age = 27   =>   20-40
age = 5    =>   4-5
age = 17   =>   15-20```Code language: PHP (php)```

## The solution in Java code

``````public class Solution {
public static String datingRange(int age) {
var minAge = age <= 14 ? (int)(age - 0.1 * age) : age / 2 + 7;
var maxAge = age <= 14 ? (int)(age + 0.1 * age) : (age - 7) * 2;

return minAge + "-" + maxAge;
}
}
```Code language: Java (java)```

An alternate approach:

``````public class Solution {
public static String datingRange(int age) {
int min, max;
if (age <= 14) {
min = (int) (age * 0.9);
max = (int) (age * 1.1);
} else {
min = (int) (age / 2.0) + 7;
max = (int) (age - 7) * 2;
}
return min + "-" + max;
}
}
```Code language: Java (java)```

Or as a single line solution:

``````public class Solution {
public static String datingRange(int age) {
String minmax = (age<=14) ? Integer.toString((int)(age-0.10*age)) + "-" + Integer.toString((int)(age+0.10*age)) : Integer.toString((int)(age/2+7)) + "-" + Integer.toString((int)((age-7)*2));
return minmax;
}
}
```Code language: Java (java)```

This can be simplified even more:

``````public class Solution {
public static String datingRange(int a) {
return ""+(a<15?(int)(a-.1*a):a/2+7)+"-"+(a<15?(int)(a+.1*a):(a-7)*2);
}
}
```Code language: Java (java)```

## Test cases to validate our Java code

``````import org.junit.Test;
import static org.junit.Assert.assertEquals;
import java.util.Random;

public class Tests {
@Test
public void exampleTests(){
assertEquals("15-20", Solution.datingRange(17));
assertEquals("27-66", Solution.datingRange(40));
assertEquals("14-16", Solution.datingRange(15));
assertEquals("24-56", Solution.datingRange(35));
assertEquals("9-11", Solution.datingRange(10));
}

@Test
public void basicTests(){
assertEquals("33-92", Solution.datingRange(53));
assertEquals("16-24", Solution.datingRange(19));
assertEquals("10-13", Solution.datingRange(12));
assertEquals("6-7", Solution.datingRange(7));
assertEquals("23-52", Solution.datingRange(33));
}

@Test
public void randomTests() {
Random rnd = new Random();
for (int i = 0; i < 50; i++) {
int n = rnd.nextInt((100 - 1) + 1) + 1;
String solution = solution(n);
assertEquals(String.format("Random inputs: Testing for %1\$d = %2\$s", n, solution), solution, Solution.datingRange(n));
}
}

private String solution(int age){
double min, max;
if (age <= 14){
min = age * 0.9;
max = age * 1.1;
}
else{
min = (age/2) + 7;
max = 2 * (age - 7);
}
return String.format("%1\$d-%2\$s", (int)min, (int)max);
}
}
```Code language: Java (java)```
Tags:
Subscribe
Notify of