Exercise 5.3 from Liang Java Textbook 8E Comprehensive Version