## CSci 1211 Problem set 4. Due Friday, Feb. 13th at 5pm

#### If you are afraid of bad luck, please submit it earlier :-)

Use the examples from the February 2nd-6th classes (see resources web page).

Please remember to write your program in good programming style: choose meaningful names of variables and don't forget comments! Also, please read the problems carefully and implement them exactly.

### Problem 1 (5 points)

Exercise 4 on p. 110. Please don't use the GUI-based input, just read the data from the user. Use this example (from lab 1) to read a double.

### Problem 2: random rectangle with random color (7 points)

If you haven't completed the Random Rectangles extra credit problem of Assignment 1, you may use this solution.

Modify the RandomRectangles.java file so that the color of each rectangle is randomly selected among 5 different colors. See the Color API for color choices. Do not use Color.white as a possible color of a rectangle (unless you set a different background color).

Hint: randomly generate an integer between 0 and 5. Then use the `switch` statement to set the color based on the integer. Test your program to make sure that all colors are picked (approximately) with the same probability.

### Problem 3: one word palindromes (10 points)

A palindrome is a word or a phrase spelled the same backwards and forwards. For this problem we will focus on one-word palindromes such as `deed`, `eye`, or `Bob` (there are numerous web sites on palindromes, for instance Page O'Palindromes, where you can find more examples).

Write a program that gets one word from the user and decides if it's a palindrome. Hint: break the input string in halves, reverse one half, and then compare them using the String comparison. You might need to handle the cases of even number of letters and odd number of letters separately. Your program must recognize palindromes that have upper-case letters in them (such as Bob or Abba).

### Problem 3.5: Extra credit (up to 7 points)

Extend your program so that it handles palindrome phrases. For this you will need to ignore spaces and punctuation marks. The easiest way to do so is to replace them with an empty string. You might not be able to handle all the cases. Please state clearly which cases your program can handle. Submit your test examples.

Your grade will depend on how many cases your program can handle. Removing just white spaces will earn you 3 points.

### Problem 4 (the last one!): casino slot machine (8 points)

Write a program that simulates a "slot machine" using random number generator. The program generates three random numbers, each can have values 1, 2, and 3 with equal probabilities. If all three numbers are equal, the user wins. More precisely, if all the numbers are 1, the user wins \$1, if all the numbers are 2, the user wins \$10, and if all the numbers are 3, the user wins \$50.

Your program must print out the three numbers and display the message indicating whether the user has won and, if yes, how much.

Run your program several times until the first win. How many tries did it take? Write down the answer in comments.

This page is a part of the course CSci 1211 at UMM.

The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.