Final solution to problems in c++ and java