Infosys interview question

To write the code in java.