Experiments in Java

Problems for Session J4: Boolean Expressions and Conditionals

Problem J4-A: Rethinking precedes

It is possible to write precedes without any if statements. How? By using a complex boolean expression. Write precedes so that it has only one line, a return of an appropriate boolean expression. Make sure to test your method.

Which version of precedes do you prefer? Why?

Problem J4-B: Incrementing a date

Write and test a method, SimpleDate tomorrow(), that returns the day after the current date. For example, if d is August 29, 1987, then d.tomorrow() should be August 30, 1987. If d is July 31, 1995, then d.tomorrow() should be August 1, 1995.

Problem J4-C: Is the date valid?

Write and test a method, isValid, that returns true if the current date is legal and false otherwise. A date is legal if the month is legal and the day is at least one and no more than the number of days in that month.

Problem J4-D: Correcting dates

Write and test a method, void correct, that corrects an invalid date by advancing or backing up the appropriate amount. For example, the 14th month of 1964 should be the second month of 1965, the 0th month of 1931 should be the 12th month of 1930, the 32nd day of June should be the 2nd day of July, the 62nd day of June should be the 1st day of August, and the -4th day of December should be the 26th day of November.

Problem J4-E: Printing dates, revisited

Write and test a method, String toLongString, that returns the string corresponding to the longer version of the date. For example, for the date 12/3/1964 (in U.S. format), it might return December 3, 1964.

Problem J4-F: Printing dates, revisited again

Add a method, setPrintFormat, to SimpleDate. This new method should allow the user to select which date format (U.S. short, U.S. long, European short, etc.) to use. Update toString to check which format it should use.

Problem J4-G: isWinter, revisited

Rewrite isWinter so that it uses precedes appropriately.

Problem J4-H: Selecting colors, revisited

In Experiment J4.8, you found that Java's string comparison is case-sensitive, so "white" and "WHITE" are treated as different strings. Update the ConfigurableGreeting applet so that it is case-insensitive.

Hint: Look at the documentation for the methods provided by java.lang.String.

Problem J4-I: Selecting colors, revisited again

Update ConfigurableGreeting to support another dozen or so colors (of your choice). These colors should be available for both the message and the shadow.

Note: You may find it helpful to create a method that converts strings to colors.

Problem J4-J: Multiple greetings

Update ConfigurableGreeting so that it prints up to three messages, one per line. The applet parameters should be named firstMessage, secondMessage, and thirdMessage. Each will appear in the same font and color. If any of the three messages is missing, you should skip it.

Note: you'll need to take the font size into account when displaying one message per line.

Problem J4-K: Parameterized drawings

Create an applet that draws shapes (squares, circles, etc.) and that permits the HTML page to customize at least three aspects of the drawing.

Problem J4-L: Stick figures

Create a StickFigure applet that permits the HTML page to customize at least three aspects of the stick figure (arm length, shirt color, hat size, etc.).

Copyright (c) 1998 Samuel A. Rebelsky. All rights reserved.

Source text last modified Mon Oct 25 21:31:04 1999.

This page generated on Tue Oct 26 15:37:58 1999 by Siteweaver.

Contact our webmaster at rebelsky@math.grin.edu