Problem: Write a Java class (call it RomanNumeral) which contains
- public static array of RomanNumeral objects (I'm calling it romanArray) that you initialize to new RomanNumeral objects with the following Strings: "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" (optional: try to initialize in a static initialization block using an array of the Strings, and a for loop) NOTE: the romanArray is NOT an array of String/StringBuilder: will be discussed in meeting#2
- private instance variable called romNumStr (String), initialize to an empty String
- private instance variable called value that you initialize to 0, but will change if the romNumStr changes
- 3 constructors
- has a String parameter that calls the mutator for the romNumStr (passing the String parameter) (see A. below about the mutator)
- has an int parameter that calls the mutator for the value (passing the int parameter) (see B. below about the mutator)
- default constructor (leave or set romNumStr to "" and value to 0)
- ALL of the following methods: (give them indicative names, NOT methodA, etc.)
a mutator (public instance method) to possibly change the romNumStr. In this method, if the parameter isn't null, do the following:
- assign the parameter to romNumStr
- remove leading and trailing white spaces from romNumStr (find the String method to do this! Look up in the textbook or Java API for the String class)
- convert romNumStr to UPPERCASE (find the String method to do this!)
- call a method to calculate the integer value of the Roman Numeral string (see C. below about this method to call)
a mutator (public instance method) to possibly change the value. In this method, if the parameter isn't <= 0, assign the parameter to the value instance variable, and call method F. which generates the correct string for the int value (otherwise leave the value and romNumStr as they were)
private instance method (no parameters) that calculates the integer value of the roman numeral string. Declare a local array of ints called digitValues (allocate the same size as the length of the romNumStr) and do the following:
- Initialize the (integer) value to 0
- Use a for loop to assign to each element of digitValues the equivalent integer value of the corresponding char in romNumStr (you may use a switch statement here). The values of each roman numeral digit are: 'M' = 1000, 'D' = 500, 'C' = 100, 'L' = 50, 'X' = 10, 'V' = 5, 'I' = 1, none of these=0
- If any of the digitValue are 0, change romNumStr to "", assign 0 to the value instance variable and return
- Traverse the digitValues (left to right). For each element (stop at NEXT TO LAST ELEMENT—be sure to use the length variable of the array):
- Add or subtract the current digitValues element based on the following rules:
- If the current digitValues element >= next digitValues element, add the current digitValues element to the value instance variable.
- If the current digitValues element < next digitValues element, subtract the current digitValues element from the value instance variable.
- Add the last digitValues element to the value instance variable
an accessor (public instance method) that returns this romNumStr
an accessor (public instance method) that returns this value instance variable
a private instance method that generates and assigns the String roman numeral equivalent of the value of this RomanNumeral. Use the public static array you declared & initialized in this class called romanArray. Do the following:
- Declare and initialize a local StringBuilder using the default constructor, and a local int (val) to this' value.
- For each element in the romanArray:
- *While (val – (value in the romanArray element)) >= 0, append the (romNumStr in the romanArray element) to the StringBuilder and subtract (value in the romanArray element) from val, then repeat to *
- After you exit the above loop, go to the next element (increment the index)
- After you finish traversing the array, assign to this' romNumStr, the StringBuilder (converted to a String)
a public instance method to add another RomanNumeral to this RomanNumeral, that has a RomanNumeral parameter and a RomanNumeral return value. If the parameter isn't null, add the value in the RomanNumeral parameter (0 if null) to the value of this RomanNumeral (the one you're in) BUT DON'T CHANGE this value NOR THE PARAMETER'S value, and return a new RomanNumeral object calling the constructor with the int parameter (passing the sum).
a toString method (as declared in the Object class) that returns the name of the class, the romNumStr and the value, concatenated in a String.
Write a Java application program in which main is in a separate class and file from the RomanNumeral (I'm calling it HW4). In main, declare three RomanNumeral object variables and initialize to null (I'm calling them roman1, roman2, and roman3). Then in main:
- Assign to roman1 a new RomanNumeral instance passing a random number between 1 and 300.
- To test the mutator and accessor, assign to roman2 a new RomanNumeral (using the default constructor). Then get the int value from roman1 (USING THE ACCESSOR),
- Call the RomanNumeral instance method to add, calling it on roman1 passing roman2 and assigning the return value to roman3.
- Call a static method (that you write, also in the main class) that prints the return values of toString on roman1, roman2, and roman3 (parameters to this method) (no other description, so don't forget this method)
Note: Do not declare any class-scope variables in the main (HW4) class except for a Scanner object. To read to the end of the line, use Scanner's nextLine( ). EACH method that uses an array, a String, or a StringBuilder methods MUST use the length variable (for array) or length( ) method when finding the end of the array of String/StringBuilder (NOT A HARD-CODED NUMBER)! DO NOT CONVERT ANY String or StringBuilder/StringBuffer objects to arrays of char!!!