solving first problem in java