Java task from interview