Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java programming Question: Please include any import statements needed Construct

ID: 3663138 • Letter: J

Question

Java programming Question:

Please include any import statements needed

Construct and implement a Roman Numeral class to store a roman numeral (a string) and its computed decimal value. example: Roman numeral: XX Decimal Value : 20 Include a default and an explicit constructor. Add the methods shown below: set(String r) - resets a roman numeral to a specified string.getDecimalValue() - returns the decimal value of the roman numeral. getRomanValue() - returns the roman numeral as a string. equal() - returns true if the roman numeral is equivalent in value to another RomanNumeral object, else returns false. increment() - increments the roman numeral by 1.

Explanation / Answer

class RomanNumeral
{
   RomanNumeral()
   {
   }


   private static int getRomanValue(char letter)
   {
       switch (letter)
       {
           case 'M':
           return 1000;
           case 'D':
           return 500;
           case 'C':
           return 100;
           case 'L':
           return 50;
           case 'X':
           return 10;
           case 'V':
           return 5;
           case 'I':
           return 1;
           default:
           return 0;
       }
   }

   public static int decode(String roman)
   {
       int result = 0;
       String uRoman = roman.toUpperCase(); //case-insensitive
       for (int i = 0; i < uRoman.length() - 1; i++)
       {//loop over all but the last character
           if (getRomanValue(uRoman.charAt(i)) < getRomanValue(uRoman.charAt(i + 1)))
           {
           result -= getRomanValue(uRoman.charAt(i));
           }
           else
           {
           result += getRomanValue(uRoman.charAt(i));
           }
       }
       result += getRomanValue(uRoman.charAt(uRoman.length() - 1));
       return result;
   }

   public static void main(String[] args)
   {
       System.out.println(decode("MCMXC")); //1990
       System.out.println(decode("MMVIII")); //2008
       System.out.println(decode("MDCLXVI")); //1666
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote