Solution to Problem 1

Coded in Java.