Exercise 7_4 from Introduction to Java Programming by Y. Daniel Liang, 8th Edition.