Java Basic Algorithm Practise