solve 4.6-4.7, Java