solve 5.1-5.2, Java