annotate src/edu/unc/utils/RomanNumeral.java @ 2:e16016635b2a

Uploaded
author timpalpant
date Mon, 13 Feb 2012 22:12:06 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
1 package edu.unc.utils;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
2
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
3 // Rudimentary Class for doing Arabic Integer -> Roman Numeral conversion
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
4 // Adapted by Timothy Palpant
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
5 // File : gui/componenents/calculators/Roman.java
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
6 // Description: A static method for converting binary integers to Roman numbers.
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
7 // Illustrates: Static inner value class, StringBuffer, throw exceptions.
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
8 // Author : Fred Swartz - 2006-12-29 - Placed in public domain.
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
9 public class RomanNumeral {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
10
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
11 // This could be alternatively be done with parallel arrays.
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
12 // Another alternative would be Pair<Integer, String>
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
13 final static RomanValue[] ROMAN_VALUE_TABLE = {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
14 new RomanValue(1000, "M"),
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
15 new RomanValue( 900, "CM"),
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
16 new RomanValue( 500, "D"),
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
17 new RomanValue( 400, "CD"),
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
18 new RomanValue( 100, "C"),
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
19 new RomanValue( 90, "XC"),
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
20 new RomanValue( 50, "L"),
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
21 new RomanValue( 40, "XL"),
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
22 new RomanValue( 10, "X"),
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
23 new RomanValue( 9, "IX"),
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
24 new RomanValue( 5, "V"),
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
25 new RomanValue( 4, "IV"),
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
26 new RomanValue( 1, "I")
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
27 };
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
28
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
29 public static String int2roman(int n) {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
30 if (n >= 4000 || n < 1) {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
31 throw new NumberFormatException("Numbers must be in range 1-3999");
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
32 }
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
33 StringBuilder result = new StringBuilder(10);
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
34
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
35 //... Start with largest value, and work toward smallest.
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
36 for (RomanValue equiv : ROMAN_VALUE_TABLE) {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
37 //... Remove as many of this value as possible (maybe none).
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
38 while (n >= equiv.intVal) {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
39 n -= equiv.intVal; // Subtract value.
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
40 result.append(equiv.romVal); // Add roman equivalent.
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
41 }
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
42 }
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
43 return result.toString();
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
44 }
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
45
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
46 private static class RomanValue {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
47 //... No need to make this fields private because they are
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
48 // used only in this private value class.
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
49 int intVal; // Integer value.
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
50 String romVal; // Equivalent roman numeral.
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
51
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
52 RomanValue(int dec, String rom) {
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
53 this.intVal = dec;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
54 this.romVal = rom;
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
55 }
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
56 }
e16016635b2a Uploaded
timpalpant
parents:
diff changeset
57 }