Use Java to implement basic algorithm