Lab Exercises

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:

  1. Read a string from the user.
  2. Use the Scanner class to grab the individual words in the string.
  3. Add each word to an ArrayList.
  4. Find the length of the longest word (by checking all words in the ArrayList).
  5. Create a two-dimensional array of characters so that the entry in row k and column n is the n-th character of the k-th word.
  6. Now loop through the first column of the array and write each of the characters to the console, then start a new line write each of the characters in the next column, and so on.


Extend JComponent to draw a digital clock with digits in a 200-point font, like this:

Use timer events to redraw the clock every tenth of a second. To do this, the actionPerformed method of your ActionListener should create a new GregorianCalendar object and use its get methods to get the hour, minute and second. These will be pasted together to form a string, which is then copied to an instance field to be accessed by the paintComponent method. Do not forget to call repaint.

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 GUI that performs exponentiation. The user types two numbers into a text field and then clicks a button. The result of the first number raised to the power of the second number is then displayed in a label. You need to use the BigInteger class to perform exponentiation. All of the code for this application should appear in the main method.

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