Docsity

Object Oriented Programming -Programming Assignment 1 | COP 3330, Assignments of Computer Science

Toggle sidebar
bg1
Object Oriented Programming - Programming Assignment #1
Assigned: 1/25/07 (Thursday)
Due: 2/1/07 (Thursday) at 11:55pm WebCT time
Objective
1. Use basic Java input and output statements.
2. Review the if statement and loops from COP 3223.
3. Learn how to use the String class.
Problem: Hangman
You will write a program that implements the game hangman, without the actual picture
of the hangman. The rules of the game are as follows:
1) There is a secret word to guess. (For your implementation, this will be read in from a
file.)
2) The user is displayed blanks representing the letters in the word at the beginning of the
game.
3) For each turn, the user guesses a letter. If there is at least one occurrence of the letter in
the secret word, then for the next turn, each occurrence of the letter is revealed. (Thus, in
the middle of the game, the word shown is a mixture of letters and blanks, the blanks
indicating the locations of letters that haven't been guessed yet by the user.) If there is no
occurrence of the letter guessed, then one is added to a tally keeping track of how many
"bad guesses" have been made.
4) The game ends when either the user has uncovered each letter in the word (in which
case he/she wins), OR when the user has made five bad guesses before each of the letters
in the word have been uncovered (in which case he/she loses).
Obtaining the Secret Word
You will prompt the user at the beginning of the program for two pieces of information:
1) A file that contains a list of words
2) A positive integer indicating which word in the file to use for the game.
The Input File Format
The first line of the file will contain a positive integer n, indicating the number of words
in the file. The next n lines will contain one word each. Each word in the file will be in
all lowercase letters. (You'll have to utilize this fact in your implementation.)
Here is a sample file, words.txt:
pf3
pf4
pf5

Partial preview of the text

Download Object Oriented Programming -Programming Assignment 1 | COP 3330 and more Assignments Computer Science in PDF only on Docsity!

Object Oriented Programming - Programming Assignment

Assigned: 1/25/07 (Thursday)

Due: 2/1/07 (Thursday) at 11:55pm WebCT time

Objective

  1. Use basic Java input and output statements.
  2. Review the if statement and loops from COP 3223.
  3. Learn how to use the String class. Problem: Hangman You will write a program that implements the game hangman, without the actual picture of the hangman. The rules of the game are as follows:
  1. There is a secret word to guess. (For your implementation, this will be read in from a file.)
  2. The user is displayed blanks representing the letters in the word at the beginning of the game.
  3. For each turn, the user guesses a letter. If there is at least one occurrence of the letter in the secret word, then for the next turn, each occurrence of the letter is revealed. (Thus, in the middle of the game, the word shown is a mixture of letters and blanks, the blanks indicating the locations of letters that haven't been guessed yet by the user.) If there is no occurrence of the letter guessed, then one is added to a tally keeping track of how many "bad guesses" have been made.
  4. The game ends when either the user has uncovered each letter in the word (in which case he/she wins), OR when the user has made five bad guesses before each of the letters in the word have been uncovered (in which case he/she loses). Obtaining the Secret Word You will prompt the user at the beginning of the program for two pieces of information:
  5. A file that contains a list of words
  6. A positive integer indicating which word in the file to use for the game. The Input File Format The first line of the file will contain a positive integer n , indicating the number of words in the file. The next n lines will contain one word each. Each word in the file will be in all lowercase letters. (You'll have to utilize this fact in your implementation.) Here is a sample file, words.txt :

baseball trigonometry stapler phone tree Implementation Details Since arrays haven't formally been taught yet, in order to receive full credit on this assignment you must NOT use arrays and use Strings only. Output Details For each turn of the game, you will show the user, the current state of the board. Separate each letter with a blank. In the beginning, the current state of the board is all underscore characters, one per letter in the word. For example, if the secret word was "baseball", then in the beginning the user would see


After the user guesses a letter, if they guess a letter in the word, then in the subsequent display, print out the capital version of that letter in the appropriate slot(s). In general, once a letter has been guessed, its capital version should always be printed out instead of an underscore on all subsequent boards. If the user guesses 'b' for his/her first letter, then the subsequent board to be displayed should look like this: B _ _ _ B _ _ _ If the user guesses 'e' for their second letter, then the next board displayed should look like this: B _ _ E B _ _ _ If the user makes five wrong guesses before the board is completed, print out: Sorry, you have not guessed the correct word in time. The correct word was X X X X. You lose. If the user guesses the word correctly before guessing five incorrect letters, then print out: Congratulations, you win! Here is the final board: X X X X where the X's represent the letters in the whole word.

T _ E E

What is your guess? r Good job! Congratulations, you win! Here is the final board: T R E E Sample Run B Welcome to the Hangman Program! What is the name of the file with all of the words? words.txt What number word, from 1 to 5, do you want to use? 3 **Here is the board:


What is your guess?** w Sorry, that letter is not in the word. Here is the board:


What is your guess? x Sorry, that letter is not in the word. Here is the board:


What is your guess? y Sorry, that letter is not in the word. Here is the board:

_ _ _ _ _ _ _

What is your guess? z Sorry, that letter is not in the word. Here is the board:


What is your guess? b Sorry, that letter is not in the word. Sorry, you have not guessed the correct word in time. The correct word was S T A P L E R. You lose. Deliverables You must submit a solution to the program described above in a file named Hangman.java over WebCT. Restrictions You must use a Java 5.0 compiler to develop your program. Your program should include a header comment with the following information: your name, course number and semester, assignment title, and date. Also, make sure you include ample comments throughout your code describing the major steps in solving the problem. Grading Details Your program will be graded upon the following criteria:

  1. Your correctness
  2. Your implementation โ€“ credit will be deducted if you use arrays. You will only be given full credit if you use String object(s) and String methods as needed.
  3. Your modularity โ€“ If you write your whole program in main, you will get some credit deducted. You should write at least one static method for this assignment.
  4. Your programming style and use of white space. (Even if you have a plan and your program works perfectly, if your programming style is poor or your use of white space is poor you could get 10% or 15% deducted from your grade.)