How to Calculate Age Range Compatibility as an Equation in Java

  • Home /
  • Blog Posts /
  • 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:

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

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;
  }
}

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;
  }
}

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;
  }
}

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);
  }
}

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);
  }
}