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.
Historical topic: Java appletsModern web browsers do not run Java applets. The Applet API was deprecated for removal before Java 25 and was removed in Java 26. Treat applet examples on this page as historical object-oriented/GUI examples; for new desktop interfaces use Swing where appropriate or JavaFX/OpenJFX.

created 05/26/03


Chapter 42 Programming Exercises


Exercise 1 — Log Table

Modify the program that prints out a table of x and ln(x) so that the table is formatted using HTML. Do this by first printing:

<html>
<body>
<table>
<tr><th>x</th><th>ln(x)</th></tr>

Next, for each iteration of the loop, calculate x and logx and print the line:

System.out.println("<tr><td>" + x + "</td><td>" + logx + "</td></tr>" );

Finally, print:

</table>
</body>
</html>

Run your program and redirect the output to a disk file (see Chapter 21).

C:/>java LogTable > myLogs.html

You should now be able to view your output file with a Web browser. (Find it in your subdirectory and double click on it, or use "File/Open" in your browser's menu.)

Click here to go back to the main menu.


Exercise 2 — Cosine Applet

Add to the sineWave applet so that it graphs the sine function in red and the cosine function in blue, both in the same drawing area.

Click here to go back to the main menu.


Exercise 3 — Improved Graphics

Add to the sineWave applet so that it draws the x and y axis. Also, draw "tick" marks along the x axis at PI/4, PI/2, 3*PI/4, and 2*PI. Draw axes and tick marks in black. Draw the sine in red.

Click here to go back to the main menu.