solve 4.1-4.3, Java