Peter the Postman works in the BU post office. This post office has 200 mailboxes numbered 0 to 199. One night, Peter opened every box out of sheer boredom. Then he decided to close every second box (0, 2, 4, 6, ...). Still bored, he went to every third box (0, 3, 6, 9, ...) and toggled it (closed it if it was open, or opened it if it was closed). After that, he toggled every fourth box (0, 4, 8, 12,...). He repeated this routine for every fifth box, every sixth box, and so on. In the final round, he toggled only the last box. Then he went to sleep. In the morning, which mailboxes were open?
Write a program to answer this question. Implement the sequence of mailboxes as an array of booleans (true = open, false = closed). The user gets to specify the number of boxes. The problem is solved with a nested loop: the outer loop repeats once for each pass that Peter makes, and the inner loop is used to toggle every k-th mailbox during the k-th pass. Once this is done, display the list of open boxes (i.e., the positions in the array where true is stored).
You need about 9 lines of code for the entire program.
This is an exercise in the use of ArrayLists and two-dimensional arrays. Write a program that accepts a line of text as input and then displays each "word" vertically. A "word" is a maximal sequence of non-whitespace characters. Display a space between columns.
The following execution snapshots will demonstrate the required functionality:
Enter a line of text: Your powers are weak, old man! Y p a w o m o o r e l a u w e a d n r e k ! r , s |
Enter a line of text: Mudhole? Slimy? My home this is! M S M h t i u l y o h s d i m i ! h m e s o y l ? e ? |
Here are the basic steps:
Extend JComponent to draw a digital clock with digits in a 200-point font, like this:
The hours, minutes and seconds should each appear as a two-digit number (e.g., 05 instead of just 5) so single digits should be prepended with a zero.
Now implement a mouse click listener. Clicking the mouse inside the component should cause the background color to be swapped with the digit color, like this:
Implement a program that displays a dialog box asking the user to enter a non-negative integer. The program converts the input string to a number using Integer.parseInt. If the number is negative, the user will be prompted to try again. What happens if the input string contains a non-digit character? The parseInt method throws an exception and in that case you need to catch the exception and handle it by repeating the input prompt. Check the Java API to see what kind of exception parseInt throws.
Next, your program computes the factorial of the entered number. In case you do not already know: the factorial of a non-negative integer n (written n!) is just the product of the positive integers from 1 to n. For example, 5! = 1 * 2 * 3 * 4 * 5 = 120. Note that if n > 13 then n! is greater than the largest possible int in Java , so you will need to use the BigInteger class.
Finally, write the result to a file. Use JFileChooser to select the file.
Home | Topics | Policies | Labs | Style Guide