Avatar of GeradeMalda

GeradeMalda's solution

to Robot Name in the Java Track

Published at May 12 2020 · 0 comments
Test suite

Manage robot factory settings.

When robots come off the factory floor, they have no name.

The first time you boot them up, a random name is generated in the format of two uppercase letters followed by three digits, such as RX837 or BC811.

Every once in a while we need to reset a robot to its factory settings, which means that their name gets wiped. The next time you ask, it will respond with a new random name.

The names must be random: they should not follow a predictable sequence. Random names means a risk of collisions. Your solution must ensure that every existing robot has a unique name.


Since this exercise has difficulty 5 it doesn't come with any starter implementation. This is so that you get to practice creating classes and methods which is an important part of programming in Java. It does mean that when you first try to run the tests, they won't compile. They will give you an error similar to:

 path-to-exercism-dir\exercism\java\name-of-exercise\src\test\java\ExerciseClassNameTest.java:14: error: cannot find symbol
        ExerciseClassName exerciseClassName = new ExerciseClassName();
 symbol:   class ExerciseClassName
 location: class ExerciseClassNameTest

This error occurs because the test refers to a class that hasn't been created yet (ExerciseClassName). To resolve the error you need to add a file matching the class name in the error to the src/main/java directory. For example, for the error above you would add a file called ExerciseClassName.java.

When you try to run the tests again you will get slightly different errors. You might get an error similar to:

  constructor ExerciseClassName in class ExerciseClassName cannot be applied to given types;
        ExerciseClassName exerciseClassName = new ExerciseClassName("some argument");
  required: no arguments
  found: String
  reason: actual and formal argument lists differ in length

This error means that you need to add a constructor to your new class. If you don't add a constructor, Java will add a default one for you. This default constructor takes no arguments. So if the tests expect your class to have a constructor which takes arguments, then you need to create this constructor yourself. In the example above you could add:

ExerciseClassName(String input) {


That should make the error go away, though you might need to add some more code to your constructor to make the test pass!

You might also get an error similar to:

  error: cannot find symbol
        assertEquals(expectedOutput, exerciseClassName.someMethod());
  symbol:   method someMethod()
  location: variable exerciseClassName of type ExerciseClassName

This error means that you need to add a method called someMethod to your new class. In the example above you would add:

String someMethod() {
  return "";

Make sure the return type matches what the test is expecting. You can find out which return type it should have by looking at the type of object it's being compared to in the tests. Or you could set your method to return some random type (e.g. void), and run the tests again. The new error should tell you which type it's expecting.

After having resolved these errors you should be ready to start making the tests pass!


Go through the setup instructions for Java to install the necessary dependencies:


Running the tests

You can run all the tests for an exercise by entering the following in your terminal:

$ gradle test

In the test suites all tests but the first have been skipped.

Once you get a test passing, you can enable the next one by removing the @Ignore("Remove to run test") annotation.


A debugging session with Paul Blackwell at gSchool. http://gschool.it

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.


import static org.assertj.core.api.Assertions.assertThat;

import org.junit.Test;
import org.junit.Ignore;
import org.junit.Before;

public class RobotTest {

    private static final String EXPECTED_ROBOT_NAME_PATTERN = "[A-Z]{2}\\d{3}";
    private Robot robot;

    public void setUp() {
        robot = new Robot();

    public void hasName() {

    @Ignore("Remove to run test")
    public void differentRobotsHaveDifferentNames() {
        assertThat(robot.getName()).isNotEqualTo(new Robot().getName());

    @Ignore("Remove to run test")
    public void resetName() {
        final String name = robot.getName();
        final String name2 = robot.getName();

    private static void assertIsValidName(String name) {
import java.util.ArrayList;
import java.util.concurrent.ThreadLocalRandom;

public class Robot {

  private static final String ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  private static final int MAX_OF_ALPHABET = 25;
  private static final int MAX_RANGE = 9;
  private static final ArrayList <String> namesAlreadyInUse = new ArrayList<>();
  private String myRobotName;

  public Robot() {
    myRobotName = createMyUniqueRobotName();

  public String getName() {
    return myRobotName;

  public void reset() {
    myRobotName = createMyUniqueRobotName();

  private String createMyUniqueRobotName() {
    String name;
      name = createName();
    }while (namesAlreadyInUse.contains(name));
    return name;

  private String createName() {
    return String.valueOf(ALPHABET.charAt(randomNumber(MAX_OF_ALPHABET)))
        + ALPHABET.charAt(randomNumber(MAX_OF_ALPHABET))
        + randomNumber(MAX_RANGE)
        + randomNumber(MAX_RANGE)
        + randomNumber(MAX_RANGE);

  private int randomNumber(int rangeMax) {
    return ThreadLocalRandom.current().nextInt(0, rangeMax);


What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?