Read about loops and the Java for and while statements in Chapter 6 of the text. Unfortunately, the char data type is not covered in the text, but it has been discussed in lecture.
Please note that you are expected to employ good functional decomposition in these and all future programs that you write for this course!
Remember the lab exercise on writing a four-letter word backwards? Well, at that time, we could only do a fixed length word, because we did not know how to deal with any general word. But now we do, so here it is, again.
Write a program that asks the user for a word, and writes that word backwards. You may assume that the user types in a single word (i.e., no spaces, no punctuation, etc.)
Once you have written this program and it seems to work okay, convert this so that it asks for another word after it converts one word, and keeps doing this until the user types "quit" or "exit" as the word.
If you have the time, change the program again, and ask the user "Really quit?" when it gets a "quit" or "exit", and quits only if the user says "y" in response. Otherwise it should continue looping.
Here is a sample run:
Enter a word: frog The word frog backwards is gorf. Enter a word: programming The word frog backwards is gnimmargorp. Enter a word: 1881 The word 1881 backwords is 1881. Enter a word: quit Really quit? n whew. Enter a word: exit Really quit? y Good bye!
Save this program as Backwards2.java
Remember the guessing game program we wrote a few weeks ago? This is the one where you think of a number, and the computer guesses it for you. In the assignment, we had fixed the maximum to 15. Now we can relax that restriction and make it a very general guessing game. In this game, you ask the user what is the upper bound on the number that the user is thinking of. Once the user tells you the upper bound (we assume the the minimum is still 1) the computer should say within how many guesses it can solve it, and then continues to make its guesses (and the user does the usual `too big', `too small' stuff until the computer guesses it. Here is a sample run:
I am a pretty good guesser.
You can think of a number, and I will guess it for you in a few
tries.
Tell me an upper bound to your number: 100
Aaah - I think I can guess it in 7 or less tries!
My guess #1 is 50.
Is this too (b)ig or too (s)mall or (c)orrect? s
My guess #2 is 75.
Is this too (b)ig or too (s)mall or (c)orrect? b
My guess #3 is 62.
Is this too (b)ig or too (s)mall or (c)orrect? c
Yay! told ya, I am gooood!
If you have a decent solution for the last version of this program, use it. Otherwise see the posted solution.
You will first need to come up with a reasonable functional decomposition. For example, you will most likely need a function that finds out how many guesses you would take. (hint: use the same basic principle that you divide the number of options by 2 at every stage). You should not need to use a function for each of the guesses, it may be harder to do this. Rather, you can put each of the guesses inside the body of the loop that goes makes one guess at each iteration.
Save this program as Iguess2.java
Write a Java application to simulate the Blackjack card game. Here are the rules of the game:
Assumptions and Simplifications:
Sample Runs:
Here are some sample runs.
Welcome to Blackjack!
You are dealt a 10.
You are dealt a 6.
Your total is 16.
Would you like a hit (y or n)? y
You are dealt a Queen.
Your total is 26.
You went BUST!
You lose!!
Welcome to Blackjack!
You are dealt a 6.
You are dealt a Queen.
Your total is 16.
Would you like a hit (y or n)? n
Your total is 16.
Good-bye.
Welcome to Blackjack!
You are dealt a Ace.
You are dealt a King.
Your total is 11.
Would you like a hit (y or n)? y
You are dealt a 2.
Your total is 13.
Would you like a hit (y or n)? y
You are dealt a 3.
Your total is 16.
Would you like a hit (y or n)? y
You are dealt a 5.
Your total is 21.
BLACKJACK!
You win!!
Make use of the += operator to accumulate the total valuation of the players hand. Write interesting and useful functions!
Save your program in a file named Cards.java.
For the ambitious:
Modify your program so that if the player has not automatically won or lost and he declines to take a hit, you compare his total to a randomly generated dealer total (in the range 16 to 23, inclusive). If the dealer's total is greater than the player's and less than or equal to 21, the dealer wins, otherwise the player wins.
Don't bother adding this feature unless your basic program is working perfectly!
For this lab, you should submit two files: Iguess2.java and Cards.java. Attach these files to an e-mail sent to a201@cs.indiana.edu with lab7 as the subject.