Programming Exercises

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.

Chapter 64 Programming Exercises


Exercise 1 — Ideal Weight Calculator with a Slider

Write the ideal weight calculator so that height in inches is entered by using a slider. Use the approximate formula:

W = H2 / 30 , for female

W = H2 / 28 , for male
where W is the ideal weight in pounds, H is the height in inches

Set an action command for each radio button using setActionCommand(String) just as for push buttons. Add an action listener for each button using addActionListener(). Add a change listener for the slider using addChangeListener(). The ideal weight should be displayed in a text field when the user changes either a radio button or the slider.

Pick initial settings for the buttons and slider. When the program starts up display the ideal weight for those settings.


Click here to go back to the main menu.