Solution by Java