Advertisement
Guest User

Roman Numeral

a guest
Jul 3rd, 2012
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.01 KB | None | 0 0
  1. package ptc;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class RomanNumeral extends java.lang.Number
  6. {
  7.  
  8.     final static RomanValue[] ROMAN_VALUE_TABLE = {new RomanValue(1000, "M"), new RomanValue(900, "CM"),
  9.             new RomanValue(500, "D"), new RomanValue(400, "CD"), new RomanValue(100, "C"), new RomanValue(90, "XC"),
  10.             new RomanValue(50, "L"), new RomanValue(40, "XL"), new RomanValue(10, "X"), new RomanValue(9, "IX"),
  11.             new RomanValue(5, "V"), new RomanValue(4, "IV"), new RomanValue(1, "I")};
  12.  
  13.     public static String int2roman(int n)
  14.     {
  15.         if (n >= 4000 || n < 1)
  16.         {
  17.             throw new NumberFormatException("Numbers must be in range 1-3999");
  18.         }
  19.         StringBuffer result = new StringBuffer(10);
  20.  
  21.         //... Start with largest value, and work toward smallest.
  22.         for (RomanValue equiv : ROMAN_VALUE_TABLE)
  23.         {
  24.             //... Remove as many of this value as possible (maybe none).
  25.             while (n >= equiv.intVal)
  26.             {
  27.                 n -= equiv.intVal; // Subtract value.
  28.                 result.append(equiv.romVal); // Add roman equivalent.
  29.             }
  30.         }
  31.         return result.toString();
  32.     }
  33.  
  34.     private static class RomanValue
  35.     {
  36.  
  37.         int intVal; // Integer value.
  38.         String romVal; // Equivalent Roman numeral.
  39.  
  40.         RomanValue( int dec, String rom )
  41.         {
  42.             this.intVal = dec;
  43.             this.romVal = rom;
  44.         }
  45.     }
  46.    
  47.     public static void main(String args[])
  48.     {
  49.         Scanner scanner = new Scanner( System.in );
  50.         System.out.println("Enter an integer number to convert to Roman Numeral: ");
  51.         int input = scanner.nextInt();
  52.         System.out.println("You entered:" +input);
  53.        
  54.         String temp = int2roman(input);
  55.         System.out.println(+input+" in Roman Numeral form is: " +temp);
  56.        
  57.     }
  58.  
  59.     @Override
  60.     public double doubleValue()
  61.     {
  62.         // TODO Auto-generated method stub
  63.         return 0;
  64.     }
  65.  
  66.     @Override
  67.     public float floatValue()
  68.     {
  69.         // TODO Auto-generated method stub
  70.         return 0;
  71.     }
  72.  
  73.     @Override
  74.     public int intValue()
  75.     {
  76.         // TODO Auto-generated method stub
  77.         return 0;
  78.     }
  79.  
  80.     @Override
  81.     public long longValue()
  82.     {
  83.         // TODO Auto-generated method stub
  84.         return 0;
  85.     }
  86.    
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement