Solution of exercise in Java for CodingBat