solve 3.1-3.3, Java