Java hw5 refactored