Exercise 1.7 from Liang Java Textbook 8E Comprehensive Version