Microsoft Word - CCCS_300_A2_FALL.docx
ASSIGNMENT 3
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 administrative details.
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:
• Follow all directions below
– In particular, make sure that all classes and method names are spelled and capitalized exactly as
described in this document. 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
• Indent your code properly
• Name your variables appropriately
– The purpose of each variable should be obvious from the name
• Comment your work
– A comment every line is not needed, but there should be enough comments to fully understand your
program
Page 2
1
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)
Create a method to print the the following image. This method must have one parameter, which is the length
of the sides in number of *’s. This method should use only two for loops, and use if statements within the for
loops to decide whether to draw a space or a star. You can assume the parameter is an odd number.
Draw the outline of a square as follows:
N.B. It is normal that the square does not appear to be a perfect square on screen as the width and the length
of the characters are not equal.
Warm-up Question 2 (0 points)
Change the method you just created to have two parameters, so that you can create rectangles. The first
parameter will be the height of the rectangle, and the second parameter will be the width of the rectangle.
Warm-up Question 3 (0 points)
Write a program to display the (x,y) coordinates up to (9,9) of the upper right quadrant of a Cartesian plane. As
in the previous warm-up question, your solution should use two nested for loops. Your program should also
display the axes, by checking to see if the x-coordinate is zero or if the y-coordinate is zero. Note that when
oth the x and y coordinates are zero, you should print a + character.
For example, the output of your code should look like:
Note that in the above image, all of the coordinates containing 0’s are not displayed, since we are printing axes
instead.
Warm-up Question 4 (0 points)
Page 3
Write a method generateRandomA
ay() that takes as input an integer n and returns an a
ay of size
n. The elements of the a
ay should be random doubles between 0 (included) and 5 (excluded). You can use
Math.random() to do this.
Warm-up Question 5 (0 points)
Write a method getLargestElement() that takes an a
ay of integers as input and returns the largest element
inside the a
ay. For example, if the input is: int[] a
= {1, 5, -3, 15, 4}; then getLargestElement(a
) should
eturn 15.
Warm-up Question 6 (0 points)
Write a method getSum() that takes an a
ay of integers as input and returns the sum of all the elements inside
the a
ay. For example, if the input is:
int[] a
= {1, 5,-3, 15, 4};
then getSum(a
) should return 22.
Warm-up Question 7 (0 points)
Write a method countNegatives() that takes an a
ay of integers as input and returns the number of negative
numbers inside the a
ay. For example, if the input is:
int[] a
= {1,5,-3, 15, -13};
then countNegative(a
) should return 2.
Warm-up Question 8 (0 points)
Write a method getVowels() that takes a String as input and returns an a
ay characters containing all the
vowels from the given string. For example, if the input is:
String s = "kangaroo";
then getVowels(s) should return {‘a’, ‘a’, ‘o’, ‘o’}.
Warm-up Question 9 (0 points)
Create a file called Counting.java, and in this file, declare a class called Counting. This class should ask the user
when the computer should stop counting.
When should I stop counting to?
10 <---- User typed this
I am counting until 10: XXXXXXXXXX10
Warm-up Question 10 (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 by which it will do so.
When should I stop counting to?
25 <----
Which step should I use?
3 <----
I am counting to 25 with a step of 3:
XXXXXXXXXX 25
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 co
ectly, or the number
of tries is exhausted.
Programming Question #1: Hangman2 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. Inco
ect 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
y 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 “Hangman2” and all program code must be in the main() method.
- Your program should use: character a
ays, selections, and loops. Your program shouldn’t use
Strings. However, you can use String to A
ay and vice versa, conversion methods such as:
String reference>.toCharA
ay() method; and String.valueOf(
ay reference>) method to
convert String to Character a
ay and vice versa. You can convert an a
ay to string for printing
(i.e., display) purpose, and A
ays.equals(
ay1 reference>,
ay2 reference>) for equality.
Your program ends with a summary of the game (See sample output).
Highlight
Highlight
Page 5
Here are 2 sample runs to illustrate the behavior of the program.
-------------------------------
Welcome to HANGMAN2
-------------------------------
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 to try: 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 to try: 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 to try: 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 to try: 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 to try: *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 to try: *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 to try: **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 to try: **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 to try: **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 to try: **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
----------------------------------------------------
So
y 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 HANGMAN2
-------------------------------
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 to try: 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 to try: 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 to try: *BCDEFGHIJKLMNOPQRSTUVWXYZ
Which letter should I check for? t