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 data: The class must have the following public methods: 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).

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.

General Instructions

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.

Extra Credit

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

Turn in a printout of your RomanNumeral.java and your test program. Tar the directory of your files and email it to ingram@roanoke.edu with cpsc120 prog3 in the subject line.