Program Assignment #2 (20 points)

advertisement
Program Assignment #2 (20 points)
Due: Monday, June 26
Create a class named RomanNum to represent a Roman numeral. Your class should include the
following data members:


An instance variable of type int to represent the value of the number
Static constants to represent values of various digits; the values (and their names) are:
Name Value
I
1
V
5
X
10
L
50
C
100
D
500
M
1000
And the following methods:
 Three constructors:
o A default constructor that sets the instance variable’s value to a random number
between 1 and 3999
o A constructor that takes an int argument (between 1 and 3999) and sets the
instance variable’s value to this number
o A constructor that takes a String argument (that represents a valid Roman numeral
– you do not have to check its validity) and assigns to the instance variable the
equivalent int value
 A toString method that returns the Roman numeral String equivalent of the instance
variable’s value
 Two setValue methods that set a new value for the instance variable; one of these should
take an int argument, and the other a String argument
 A getValue method that returns the value of the instance variable
 A main method that tests your class by:
o Instantiating three RomanNum objects (using each of the three constructors)
o Writing out the values of the numbers as both Roman numerals and integers
o Changing the values of the numbers and writing them out again using both forms
Extra credit option (worth up an additional 20 points):
 Add methods for Roman numeral arithmetic (addition, subtraction, multiplication and
division – 5 points per each successful operation)
 Add code in your main method to demonstrate that these methods work
 Each method should include two versions; one that takes a RomanNum argument and one
that takes an int argument – see the Fraction class in your notes for ideas about this
Computer Science I
Sheller
Cs1sum06p2 – web site, zip
Summer 2006
Page 1
Download