# ASSIGNMENT

ASSIGNMENT 2
CCCS 300
Due: See Mycourse
Please read the entire PDF before starting. You must do this assignment individually.
It is very important that you follow the directions as closely as possible. The directions, while perhaps tedious, are
designed to make it as easy as possible for the TAs to mark the assignments by letting them run your assignment,
in some cases through automated tests. While these tests will never be used to determine your entire grade, they
speed up the process significantly, which allows the TAs to provide better feedback and not waste time on
Up to 30% can be removed for bad indentation of your code as well as omitting comments, or poor coding
structure.
To get full marks, you must:
In particular, make sure that all classes and method names are spelled correctly and coding standard is
followed. Otherwise, you will receive a
50% penalty.
Make sure that your code compiles.
Non-compiling code will receive a 0.
Write your name and student ID as a comment in all .java files you hand in
The purpose of each variable should be obvious from the name
A comment every line is not needed, but there should be enough comments to fully understand your
program
Part 1 (0 points): Warm-up
Do NOT submit this part, as it will not be graded. However, doing these exercises might help you to do the second
part of the assignment, which will be graded. If you have difficulties with the questions of Part 1, then we suggest
that you consult the TAs during their office hours; they can help you and work with you through the warm-up
questions. You are responsible for knowing all of the material in these questions.
Warm-up Question 1 (0 points)
Write a method swap which takes as input two int values x and y. Your method should do 3 things:
1. Print the value of x and y
2. Swap the values of the variables x and y, so that whatever was in x is now in y and whatever was in y is
now in x
3. Print the value of x and y again.
Page 2
For example, if your method is called as follows: swap(3,4) the effect of calling your method should be
the following printing inside swap: x is:3 y is:4 inside swap: x is:4
y is:3
Warm-up Question 2 (0 points)
Consider the program you have just written. Create two integer variables in the main method. Call them x and
y. Assign values to them and call the swap method you wrote in the previous part using x and y as input
parameters.
After calling the swap() method—inside the main method— print the values of x and y. Are they different than
before? Why or why not?
Warm-up Question 3 (0 points)
Write a method that takes three integers x, y, and z as input. This method returns true if z is equal to 3 or if z is
equal to the sum of x and y, and false otherwise.
Warm-up Question 4 (0 points)
Let’s write a method incrementally:
1. Start by writing a method called getRandomNumber that takes no inputs, and returns a random double
between 0 (included) and 10 (excluded).
2. Now, modify it so that it returns a random int between 0 and 10 (still excluded).
3. Finally, let the method take two integers min and max as inputs, and return a random integer greater
than or equal to min and less than max.
Warm-up Question 5 (0 points)
Create a file called Counting.java, and in this file, declare a class called Counting. This program takes as input
from the user (using args) a positive integer and counts up until that number. eg:
> run Counting 10
I am counting until 10: 1 2 3 4 5 6 7 8 9 10
Warm-up Question 6 (0 points)
For this question you have to generalize the last question. The user will give you the number they want the
computer to count up to and the step size by which it will do so.
> run Counting 25 3
I am counting to 25 with a step size of 3:
1 4 7 10 13 16 19 22 25
Warm-up Question 7 (0 points)
Write a method getFirstHalf() that takes as input a String and returns a String composed of the first half of the
characters from the specified String. For example, getFirstHalf(“cucumber”) returns the String “cucu”, while
getFirstHalf(“apple”) returns the String “ap” (thus, if the number of characters is odd, you should round down).
Warm-up Question 8 (0 points)
Write a method alphaString() which takes two Strings as input and returns the String between the two that
comes first in the alphabet. For example, alphaString(“banana”, “apple”) returns the String “apple”, while
alphaString(“snake”, “squirrel”) returns the String “snake”.
Warm-up Question 9 (0 points)
Write a method replaceAll() which takes as input a String and two characters. The method returns the String
composed by the same characters of the given String where all occurrences of the first given character are

Page 3
replaces by the second given character. For example, replaceAll(“squirrel”, ’r’ , ’s’) returns the String
“squissel”, while replaceAll(“squirrel”, ’t’, ’a’) returns the String “squirrel”.

Page 4
Part 2
The questions in this part of the assignment will be graded.
General rules for the hangman word game: (http://en.wikipedia.org/wiki/Hangman_%28game%29)
“Hangman is a guessing game for two or more players. One player thinks of a word, phrase or sentence
and the other(s) tries to guess it by suggesting letters within a certain number of guesses.
The game is
over when: The guessing player completes the word or guesses the whole word correctly, or the number
of tries is exhausted.
Programming Question #1: Hangman Game (only to guess the word)
1) The word to guess can have repeated letters.
2) The word to guess is
not case sensitive, and only up to a maximum of 10 characters.
3) The guessing player has
10 tries to find the letters.
4) The player
can guess the entire word in one shot, at any time of the game, or S/he must can guess
and write
one letter at a time.
Recommended skeleton of the structure of your program (refer to screen shots for examples):
1) A player enters the word to guess. Remember a word can only have letters.
2) Print 20 blank lines to clear hide the word to guess from the screen, before guessing player starts.
3) Guessing player (the other player) is shown with
stars for the number of letters in the word to
guess. Your program also shows the number of guesses left as well as a list of the letters still not
picked (See sample run output below).
4) Guessing player is prompted for a letter.
5) As long as the input is more than one character long, is not a letter or is a letter that was already
tried, your program should keep on prompting the user for valid input. Incorrect entries should
not be counted as guesses.
6) Once a valid letter is entered, your program (a) updates the list of letters (alphabet) still left to try
by removing this letter from the list if there are no more characters of the same letter in the word,
(b) reduces the number of guesses left by one, and (c) checks whether the letter is part of the word
to guess. If it is, then replace the star with the letter in the appropriate position.
Steps 3 to 6 are repeated till all letters in the word are found or reaches 10 guesses.
7) Your program name must be “Hangman” and all program code must be in the main() method.
strings, selections, and loops. Your program shouldn’t use arrays.
Your program ends with a summary of the game (See sample output).

Page 5
Here are 2 sample runs to illustrate the behavior of the program.
——————————-
Welcome to HANGMAN
——————————-
OK Guessing Player … turn around, while your friend enters the word to guess!
Other Player – Enter your word (up to 10 letters only, not case sensitive):
Talla
<<<< 20 blank lines here >>>>
Word to date: ***** (10 guess(es) left)
Want to solve the puzzle? Enter “Y” to solve the puzzle, or “N” to guess a character:
N
Letters not tried yet: ABCDEFGHIJKLMNOPQRSTUVWXYZ
Which letter should I check for?
A
Word to date: *A*** (9 guess(es) left)
Want to solve the puzzle? Enter “Y” to solve the puzzle, or “N” to guess a character:
N
Letters not tried yet: ABCDEFGHIJKLMNOPQRSTUVWXYZ
Which letter should I check for?
T
Word to date: TA*** (8 guess(es) left)
Want to solve the puzzle? Enter “Y” to solve the puzzle, or “N” to guess a character:
N
Letters not tried yet: ABCDEFGHIJKLMNOPQRS*UVWXYZ
Which letter should I check for?
L
Word to date: TAL** (7 guess(es) left)
Want to solve the puzzle? Enter “Y” to solve the puzzle, or “N” to guess a character:
N
Letters not tried yet: ABCDEFGHIJKLMNOPQRS*UVWXYZ
Which letter should I check for?
a
Word to date: TAL*A (6 guess(es) left)
Want to solve the puzzle? Enter “Y” to solve the puzzle, or “N” to guess a character:
n
Letters not tried yet: *BCDEFGHIJKLMNOPQRS*UVWXYZ
Which letter should I check for?
k
Word to date: TAL*A (5 guess(es) left)
Want to solve the puzzle? Enter “Y” to solve the puzzle, or “N” to guess a character:
n
Letters not tried yet: *BCDEFGHIJ*LMNOPQRS*UVWXYZ
Which letter should I check for?
b
Word to date: TAL*A (4 guess(es) left)
Want to solve the puzzle? Enter “Y” to solve the puzzle, or “N” to guess a character:
n
Letters not tried yet: **CDEFGHIJ*LMNOPQRS*UVWXYZ
Which letter should I check for?
a
–> Not a valid request – either not a letter or already guesses.
Which letter should I check for?
h
Word to date: TAL*A (3 guess(es) left)
Want to solve the puzzle? Enter “Y” to solve the puzzle, or “N” to guess a character:
n
Letters not tried yet: **CDEFG*IJ*LMNOPQRS*UVWXYZ
Which letter should I check for?
j
Word to date: TAL*A (2 guess(es) left)
Want to solve the puzzle? Enter “Y” to solve the puzzle, or “N” to guess a character:
n
Letters not tried yet: **CDEFG*I**LMNOPQRS*UVWXYZ
Which letter should I check for?
e
Word to date: TAL*A (1 guess(es) left)
Want to solve the puzzle? Enter “Y” to solve the puzzle, or “N” to guess a character:
n
Letters not tried yet: **CD*FG*I**LMNOPQRS*UVWXYZ
Which letter should I check for?
b
–> Not a valid request – either not a letter or already guesses.
Which letter should I check for?
n
—————————————————-
Sorry you didn’t find the mystery word!
It was “TALLA”
Goodbye ….
————————————————–
Sample output 1 –
Guessing player does not guess the word

Page 6
——————————-
Welcome to HANGMAN
——————————-
OK Guessing Player … turn around, while your friend enters the word to guess!
Other Player – Enter your word (up to 10 letters only, not case sensitive):
Talla
<<<< 20 blank lines here >>>>
Word to date: ***** (10 guess(es) left)
Want to solve the puzzle? Enter “Y” to solve the puzzle, or “N” to guess a character:
n
Letters not tried yet: ABCDEFGHIJKLMNOPQRSTUVWXYZ
Which letter should I check for?
a
Word to date: *A*** (9 guess(es) left)
Want to solve the puzzle? Enter “Y” to solve the puzzle, or “N” to guess a character:
n
Letters not tried yet: ABCDEFGHIJKLMNOPQRSTUVWXYZ
Which letter should I check for?
a
Word to date: *A**A (8 guess(es) left)
Want to solve the puzzle? Enter “Y” to solve the puzzle, or “N” to guess a character:
n
Letters not tried yet: *BCDEFGHIJKLMNOPQRSTUVWXYZ
Which letter should I check for?
t
Word to date: TA**A (7 guess(es) left)
Want to solve the puzzle? Enter “Y” to solve the puzzle, or “N” to guess a character:
n
Letters not tried yet: *BCDEFGHIJKLMNOPQRS*UVWXYZ
Which letter should I check for?
l
Word to date: TAL*A (6 guess(es) left)
Want to solve the puzzle? Enter “Y” to solve the puzzle, or “N” to guess a character:
n
Letters not tried yet: *BCDEFGHIJKLMNOPQRS*UVWXYZ
Which letter should I check for?
l
—————————————————-
Congratulations!!!
You guessed the mystery word “TALLA” in 5 guesses!
Goodbye ….
————————————————–
Sample output 2 –
Guessing player finds the word

Page 7
RESTRICTION for Question #1:
Cannot use arrays. Must use Strings for word that is to be guessed as well as the list of letters still
left to try. You can use any kind of loops that you like. You can use any of the String methods
such as substring(), charAt(), etc.
Your output should be formatted as closely as the sample screen shots.
What To Submit
Just submit the file Hangman.java in the Assignments dropbox on myCourses.