here

advertisement
Comp 401 - Assignment 2:
Scanning Numbers and Words
Date Assigned: Wed Sep 1, 2010
Completion Date: Fri Sep 10, 2010
Early Submission Date: Wed Sep 8, 2010
In this assignment, you will get more practice scanning. This work will build on your previous
assignment.
Task
Extend your Java program of the previous assignment to print not only words but also numbers,
where a number is a sequence of digits; and a word, as in the previous assignment, is a
sequence of letters.
You can assume that the first character of the scanned string is a letter or a digit, and that each
token (number/word) is followed by exactly one blank. As before, you can halt the program on
encountering the first unexpected character.
This time, you should print the tokens in order rather than reverse order. Thus, you do not need
to store the tokens in an array. In addition, before you print a token, give an indication of its
type (word or number). Thus, if the String argument is:
“30 foxes 200 rabbits “
You should print:
Number: 30
Word: foxes
Number: 200
Word: rabbits
Constraints
1. You can use all of the Java features allowed in the previous assignment. In
addition you can use the Character.isDigit() method, which indicates if its
character argument is a digit. You can also use the switch statement, in case you
are familiar with it.
Extra Credit
1. Allow an arbitrary number of spaces before and after each number/word.
2. Give error messages in case the user does not enter any number/word or enters
non digits/letters, and keep scanning.
3. Look also for and print (a) the plus token, consisting of the single character, ‘+’
and (b) the minus token, consisting of the single character, ‘-‘.’
4. Finish the assignment by the early completion date.
Submission Instructions
1. Submit, at the start of class, a print out of your code along with screenshots showing the
test cases.
2. Upload the assignment directory in blackboard. In general, for all assignments, you should
do so by midnight of the day the assignment is due. But do not change the code after you
submit it in class.
Good luck!
Download