Nethack-style text-based role-playing game




COMP 2150
Homework 1: String Manipulation
(25 points)
Notes for Homework 1:
Use only the charAt, equals, length, split, substring, toLowerCase, and toUpperCase methods of the String
class (see the Java API for details) in your solutions.
Please be sure your method headers are exactly as specified in the assignment. This is to streamline grading for
the TAs since we have a large class. You will receive no credit if your method doesn’t follow the required
If you happen to be familiar with regular expressions, don’t use them here – that makes some of this too easy 🙂
(5 pts) You are writing a Nethack-style text-based role-playing game that supports saving the game’s current state
to a text file. You decide to represent items in your character’s inventory using certain symbols in the file:
+ weapon
@ potion
* dark chocolate bar
(Your character is magically prevented from carrying milk chocolate bars.) Write a method that takes a string as a
parameter and returns an array containing the quantity of weapons, potions, and dark chocolate bars in that order.
You can assume that the input string contains only the three characters above.
Required method header:
public static int[] countItems(String s)
Example input and output:
“@@+**[email protected]***”
Output: {2, 3, 5}
Number of People: Individual. Feel free to ask me for help, or visit the Computer Science Learning Center
Due: Mon., Sept. 11 by 5:30 pm
Submission: Zip all your Java source files (you can zip the entire project folder if using an IDE) into a single file and
upload it to the proper folder in the eCourseware dropbox at
Coding Style: Use consistent indentation. Use standard Java naming conventions for
ClassNames, CONSTANT_NAMES. Include a reasonable amount of comments.
Grader: Rong Qi,
[email protected]. Questions about grading? Please contact her first!
2. (10 pts) Natural language processing is a subfield of computer science focusing on how computers can “make
sense” of human languages like English. Before it can be used in NLP, text data must often be “cleaned.” Write
a method that cleans a string by removing the punctuation and common words listed below. You can assume the
words in the input string are separated by single spaces, the input string does not begin or end with a space, and
the input string does not contain any non-letter characters besides spaces and the punctuation below. The method
should return a lower-case, space-delimited version of the input string, with the punctuation and common words
Punctuation marks to be removed: . , ; : ! ? ( )
Words to be removed: a an the is am are and or
Required method header:
public static String cleanText(String s)
Example inputs and outputs:
Omg, so like, the fox, like, totally jumped over the lazy dog!
Output: omg so like fox like totally jumped over lazy dog
Input: I provide this example: a, b, or c…
Output: i provide this example b c
3. (10 pts) You’ve just been hired onto the IT staff at the Best University Ever. You’re working on a website that
collects student information, and you want to make sure that the email addresses they provide are valid university
emails. BUE’s emails are case-insensitive and in this format:
[first initial].[last name][1 or more digits (optional)]@
[1 or more subdomains, each followed by a dot (optional)]
The first initial, last name, and subdomains (if present) cannot be blank, and they can contain only letters. Write a
method that returns whether an email address is valid.
Required method header:
public static boolean isValidEmail(String s)

Examples of valid BUE addresses:
[email protected]
[email protected]
[email protected]
[email protected]
Examples of invalid BUE addresses:
[email protected]
(missing first initial)
[email protected] (too many “first initials”)
[email protected] (first “initial” is too many characters)
[email protected] (wrong university)
[email protected] (no .edu domain – BUE is totally not commercial, or so they claim)
[email protected] (digits must be after the last name)
[email protected] (subdomain contains a non-letter character)
[email protected] (blank subdomain)
[email protected] (last name contains non-letter characters)
blargh (what kind of excuse for an email address is this??)

split(“\\.”) to split a string around dots.