CS 124, Spring 2021
Lab 4: More Algorithms

For this lab, you will add one more tool to your Java toolbox: for loops. You should use a for loop when it is appropriate, so there should be at least one in each program. On the other hand, another loop that you need to write is more naturally a while loop. But really the point of the lab is to design and implement some more complex programs. The first program for the lab is an arithmetic quiz, which is an enhancement of an example that was discussed in class. The second requires string manipulation that will be useful in a game of "hangman", which you might work on later in the semester. You should read the lab instructions in full before working on that program!

This lab will be due by the end of the day next Tuesday, and it will be submitted using the program submission website, math.hws.edu/submit, as usual. It will also be accepted late until noon on Saturday, but there will be a 10% penalty for labs that are turned in late without some good reason. You will be required to turn in two programs named ArithmeticQuiz.java, and NotHangman.java.

This lab requires TextIO, so you will need to add that to your Eclipse project. One way to do that is to copy-and-paste the entire textio folder from another project. (If you prefer to use Scanner instead of TextIO, you do that instead.) You will not need JavaFX for this lab (but it would still be OK to work in an Eclipse project that supports JavaFX).

Remember that all programs are required to follow the rules of good programming style!! And remember that you are writing your own solutions to the exercises. Although you can discuss the exercises with other students in general terms, you need to construct your own solutions. If you do that, it is not possible that your programs will look very similar to anyone else's.

Exercise 1: Arithmetic Quiz

In class, one of our examples was a simple addition quiz, where the computer made up ten arithmetic problems and asked the user to solve them. For the first part of the lab, you will write a significantly enhanced version of that program. The name of the file that you turn in for this exercise will be ArithmeticQuiz.java.

It will be a ten-question quiz, where all of the numbers are small integers, which are selected randomly for each problem. You should imagine that you are writing the quiz for grade school children who are just learning to do artithemtic, so you don't want the questions to be difficult.

The quiz should have both addition and subtraction problems. For example, for each problem, you might decide whether to make it an addition problem or a subtraction problem. Or you might make the even numbered problems addition problems and the odd-numbered problems subtraction problems.

For a subtraction problem, the answer should not be a negative number. There are several different ways to ensure that this is the case.

For this quiz, the user should get two chances to answer the question correctly. If their first answer is correct, the user gets 10 points for that problem. If not, the computer should let them try again. If their second answer is correct, the user gets 5 points. If their second answer is also incorrect, the computer should tell them the correct answer.

At the end of the quiz, the computer should tell the user their total score.

You should think about the interaction with the user. The presentation should look nice and should be easy to follow. For example, number the questions. Use blank lines and spaces to format the questions. Use outputs that keep the user informed.

Exercise 2: Implement Some Parts of a Hangman Game

In this exercise, you will write some programming that is needed in the well-known game, Hangman. You will work on just a part of the game. Later, we might extend this work to an implementation of the complete game.

The name of the file that you turn in for this exercise will be NotHangman.java.

You will have to spend some time developing an algorithm for the program. Remember that you do not have to write the entire program all at once! Get something working, and then add features to it.

In Hangman, a player tries to guess a word by guessing letters that might be in the word. The player initially sees a blank space for each letter of the word. As the player guesses letters, the blank spaces are filled in, until the user has guessed all the letters that occur in the word. (In the real game, the user loses if they guess too many letters that don't occur in the word, but that is not part of this assignment.)

Your program should use some particular word, of your choice, as the word that the user has to guess. You can code the word into the program; it will be the same word every time the program is run. You are not at this point trying to make an actual game.

Let's say, for example, that the word is "hippopotamus". As you go through the program, you want to keep outputting the word, showing any letters that have been guessed by the user, but showing a blank for any letter that the user has not yet guessed. Use an underscore character, '_', to represent a blank, unguessed letter. At the beginning, all the possibilities will be blank. (Put a space between consecutive characters to make the output readable!) As the user guesses letters, some of the blanks are changed to letters. Here is approximately what the process should look like:

I am thinking of a word.
Try to guess the letters that occur in the word.

The word so far:  _ _ _ _ _ _ _ _ _ _ _ _ 

Enter your next letter: t
Yes, T is in the word.

The word so far:  _ _ _ _ _ _ _ T _ _ _ _ 

Letters that you have guessed so far: T
Enter your next letter: e
Sorry, E is not in the word.

The word so far:  _ _ _ _ _ _ _ T _ _ _ _ 

Letters that you have guessed so far: TE
Enter your next letter: i
Yes, I is in the word.

The word so far:  _ I _ _ _ _ _ T _ _ _ _ 

Letters that you have guessed so far: TEI
Enter your next letter: o
Yes, O is in the word.

The word so far:  _ I _ _ O _ O T _ _ _ _ 

Letters that you have guessed so far: TEIO
Enter your next letter: n
Sorry, N is not in the word.

The word so far:  _ I _ _ O _ O T _ _ _ _ 

Letters that you have guessed so far: TEION
Enter your next letter: s
Yes, S is in the word.

The word so far:  _ I _ _ O _ O T _ _ _ S 

Letters that you have guessed so far: TEIONS
Enter your next letter: a
Yes, A is in the word.

The word so far:  _ I _ _ O _ O T A _ _ S 

Letters that you have guessed so far: TEIONSA
Enter your next letter: r
Sorry, R is not in the word.

The word so far:  _ I _ _ O _ O T A _ _ S 

Letters that you have guessed so far: TEIONSAR
Enter your next letter: u
Yes, U is in the word.

The word so far:  _ I _ _ O _ O T A _ U S 

Letters that you have guessed so far: TEIONSARU
Enter your next letter: m
Yes, M is in the word.

The word so far:  _ I _ _ O _ O T A M U S 

Letters that you have guessed so far: TEIONSARUM
Enter your next letter: h
Yes, H is in the word.

The word so far:  H I _ _ O _ O T A M U S 

Letters that you have guessed so far: TEIONSARUMH
Enter your next letter: p
Yes, P is in the word.

The word so far:  H I P P O P O T A M U S 

You got it!

To implement this, you need to be able to check whether a string contains a given letter. Fortunately, we have see a String function that makes it easy: Suppose guesses is a String and letter is a char. Then the function

guesses.indexOf(letter)

checks whether the character letter occurs in the string guesses. This function returns an int. The value is -1 if letter does not occur in word. If letter does occur in guesses, then the value is greater than or equal to zero. (In fact, the value gives the position of the first occurrence of letter in word.) So, you can test whether the letter occurs in the string simply by saying

if ( guesses.indexOf(letter) >= 0 ) { 

Your program will need a String to hold the word that the user is trying to guess. You will also need a String to hold the letters that the user has guessed so far. This second string starts out as the empty string, "". When the user inputs a character, add that character onto the string using a command such as

guesses = guesses + userLetter;

You might have to deal with the difference between upper and lower case letters. In my program, I use upper case letters in the word, but the user will likely type lower case letters. the function Character.toUpperCase(ch) returns a new character that is the upper case version of ch if ch is a lower case letter. If ch is not a lower case letter, is returns a copy of ch.

The hardest part of the program is to output the word showing some letters and some blanks. Think carefully about how you are going to do that and how you are going to use the two strings to do it. Hint: You do not need a variable to represent the output such as "_ i _ _ o _ o t _ _ _ _". You can generate the output character-by-character from other data that you have.

The program should continue until all the letters in the word have been guessed. Once the word is complete, you should end the program. (This part is also a bit difficult, and we might discuss it in class once you've had a chance to work on the program for a while. Hint: A flag variable would be useful, to tell whether there are any missing, unguessed letters in the word.)