CPSC 120B Programming Assignment #3
Due Wednesday, November 20, 2002
In this assignment you will write a class that models a number
represented by Roman numerals.
The Basic Class and Test Program (80%)
The RomanNumeral class must have the following instance
The class must have the following public methods:
- the numeral -- a string of characters representing a valid Roman numeral
(you may consider a numeral valid if it is composed only of valid Roman
numeral characters -- M, D, C, L, X, V, and I) or the empty string
- a boolean indicating whether the object is a valid Roman numeral
Your class should also have a few private support methods -- for example,
it should have one that finds and returns the value of a character in a Roman
numeral (in case you forgot, the values are as follows: M - 1000; D - 500; C - 100; L - 50; X = 10; V - 5; I - 1).
- RomanNumeral (String roman) -- a constructor that takes a String
as a parameter, normalizes it by converting it to all upper case letters,
then checks to make sure all characters in the string are
valid characters in a Roman numeral (valid characters are M, D, C, L, X, V, and
I). If all characters are valid, the boolean instance variable is set to true
and the String instance data member is set to the normalized value of the
parameter. If there are any invalid characters a message should be
printed, the boolean instance variable
set to false and the String representing the numeral set to empty ("").
- boolean isValid() -- returns the value of the boolean
instance data (true if the object is a valid Roman numeral;
- int toArabic() -- returns the value of the Roman numeral as an Arabic
- String toString() -- returns the normalized Roman numeral
NOTE on finding the value of a Roman numeral: Roman numerals are generally
written with the letters in decreasing order of value from left to right and
the value of the numeral is just the sum of values of the letters. For example
MMXVII is 2017 (1000 + 1000 + 10 + 5 + 1 + 1). There is one set of exceptions --
the number 4 is written IV, 9 is IX, 40 is XL, 90 is XC, 400 is CD, and 900 is
CM. In these cases, the first letter has smaller value than the second and the
value of the first is subtracted rather than added. In this assignment, you may
assume that if all letters are valid the numeral is properly formed. Hence
to find its value in the Arabic number system, you just need to go through
the string keeping track of the current letter and the next. If the current
letter has value less than the next, subtract its value; otherwise add.
Your driver program that tests the RomanNumeral class should have a
loop that keeps asking the user to enter Roman numerals (as long as the
user wishes). For each numeral entered, the program should print out
whether or not it is a valid numeral and print both the numeral (normalized)
and its value as an Arabic number.
An Enhancement (20%)
Add a second constructor to the class that takes an integer as a parameter
then, if the integer is valid (greater than 0 but less than or equal to 4000),
determines the Roman numeral equivalent for the integer.
To find the Roman numeral corresponding
to an integer you basically need to build up a string (through concatenation) --
determine how many 1000's are in the number and concatenate that many Ms to
the string, then the number of 100's (take care of the special cases of 900
and 400), and so on.
Modify your test program to thoroughly test the new constructor.
As always, use good programming style. This includes "old" business
such as meaningful identifiers, constants,
white space, proper indentation PLUS the new idea of breaking your work up
into smaller tasks and writing methods to do those tasks. Document the
program!!! And, finally, keep in mind the Academic Integrity policies
in the course. Come see me for any help you need on this assignment.
As described above you may assume that if all the letters in the Roman
numeral are legitimate, then the numeral is well formed. Enhance the class
so it detects improperly formed numerals (such as IIV).
Turn in a printout of your RomanNumeral.java and your test program. Tar
the directory of your files and email it to email@example.com with
cpsc120 prog3 in the subject line.