First task. Java implementation.