Java  1.0
Classes | Public Member Functions | Static Public Member Functions | Private Attributes | Static Private Attributes | List of all members
roman Class Reference

Class for manipulating roman numerals. More...

Collaboration diagram for roman:
Collaboration graph
[legend]

Classes

class  ValueError
 Exception class. More...
 

Public Member Functions

 roman ()
 Empty constructor. More...
 
 roman (String r) throws ValueError
 Constructor from a roman numeral. More...
 
 roman (int i)
 Constructor from an integer. More...
 
String toString ()
 Used to print a roman object. More...
 
int toInt ()
 Used to convert a roman object to integer. More...
 

Static Public Member Functions

static String int2roman (int num)
 Converts an integer number to roman. More...
 
static String int2Roman (int num)
 Converts an integer to roman. More...
 
static int roman2int (String roman) throws ValueError
 Converts a roman numeral to decimal. More...
 
static String validateRoman (String romano)
 Validates a roman numeral. More...
 
static void main (String[] args)
 main function for testing. More...
 

Private Attributes

String rnum = null
 hold the roman numeral of this object More...
 
int inum = 0
 hold the integer representation of the roman numeral of this object More...
 

Static Private Attributes

static String[][] symbols
 table for mapping [decimal power, digit] to roman symbols More...
 
static Map< Character, Integer > d = null
 dictionary for mapping roman symbols to decimals More...
 
static int[] l = null
 list for mapping roman symbols to decimals More...
 
static String[] rSymbols = { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"}
 table for relating decimals to roman symbols: an ordered list of roman numerals More...
 
static int[] decimals = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1}
 table for relating decimals to roman symbols: an ordered list of the decimal representation of roman numerals More...
 

Detailed Description

Class for manipulating roman numerals.

Roman numerals, the numeric system in ancient Rome, uses combinations of letters from the Latin alphabet to signify values. The numbers 1 to 10, 50, 100, 500 e 1000 can be expressed in Roman numerals as follows:

In the Middle Ages, a horizontal line was used above a particular numeral, or parentheses placed around it, to represent one thousand times that numeral.

To compile:

To run:

Author
Paulo Roma
Since
1/1/2013
See also
http://en.wikipedia.org/wiki/Roman_numerals

Definition at line 29 of file roman.java.

Constructor & Destructor Documentation

◆ roman() [1/3]

roman.roman ( )

Empty constructor.

Definition at line 104 of file roman.java.

Referenced by int2Roman(), and main().

◆ roman() [2/3]

roman.roman ( String  r) throws ValueError

Constructor from a roman numeral.

Parameters
rroman numeral.
Exceptions
ValueErrorif the given roman numeral is invalid.

Definition at line 114 of file roman.java.

References inum, rnum, roman2int(), and validateRoman().

◆ roman() [3/3]

roman.roman ( int  i)

Constructor from an integer.

Parameters
idecimal numeral.

Definition at line 130 of file roman.java.

References int2roman(), inum, and rnum.

Member Function Documentation

◆ int2roman()

static String roman.int2roman ( int  num)
static

Converts an integer number to roman.

The decimal numeral system (also called base ten or occasionally denary) has ten as its base. It is the numerical base most widely used by modern civilizations.

Decimal notation often refers to a base-10 positional notation, such as the Hindu-Arabic numeral system; however, it can also be used more generally to refer to non-positional systems, such as Roman.

Parameters
numinteger to be converted to roman.
Returns
string representing the given integer as a roman numeral.

Definition at line 168 of file roman.java.

References baseConverter.itoa(), symbols, and toString().

Referenced by roman().

◆ int2Roman()

static String roman.int2Roman ( int  num)
static

Converts an integer to roman.

Parameters
numinteger to be converted to roman.
Returns
string representing the given integer as a roman numeral.

Definition at line 199 of file roman.java.

References decimals, roman(), and rSymbols.

Referenced by main().

◆ main()

static void roman.main ( String[]  args)
static

main function for testing.

Parameters
argsan integer to be converted to roman.

Definition at line 343 of file roman.java.

References TextIO.getAnyChar(), TextIO.getln(), TextIO.getlnInt(), int2Roman(), TextIO.peek(), TextIO.put(), TextIO.putln(), roman(), toInt(), toString(), and validateRoman().

◆ roman2int()

static int roman.roman2int ( String  roman) throws ValueError
static

Converts a roman numeral to decimal.

Parameters
romanroman numeral to be converted to decimal.
Returns
integer equivalent to the given roman numeral.
Exceptions
ValueErrorif the given roman numeral is invalid.

Definition at line 248 of file roman.java.

References l.

Referenced by roman().

◆ toInt()

int roman.toInt ( )

Used to convert a roman object to integer.

Returns
the current roman integer.

Definition at line 151 of file roman.java.

References inum.

Referenced by main().

◆ toString()

String roman.toString ( )

Used to print a roman object.

Returns
the current roman string.

Definition at line 141 of file roman.java.

References inum, and rnum.

Referenced by int2roman(), and main().

◆ validateRoman()

static String roman.validateRoman ( String  romano)
static

Validates a roman numeral.

Parameters
romanoroman numeral to be validated.
Returns
error code string.

Definition at line 283 of file roman.java.

References d.

Referenced by main(), and roman().

Member Data Documentation

◆ d

Map<Character, Integer> roman.d = null
staticprivate

dictionary for mapping roman symbols to decimals

Definition at line 47 of file roman.java.

Referenced by validateRoman().

◆ decimals

int [] roman.decimals = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1}
staticprivate

table for relating decimals to roman symbols: an ordered list of the decimal representation of roman numerals

Definition at line 88 of file roman.java.

Referenced by int2Roman().

◆ inum

int roman.inum = 0
private

hold the integer representation of the roman numeral of this object

Definition at line 98 of file roman.java.

Referenced by roman(), toInt(), and toString().

◆ l

int [] roman.l = null
staticprivate

list for mapping roman symbols to decimals

Definition at line 63 of file roman.java.

Referenced by roman2int().

◆ rnum

String roman.rnum = null
private

hold the roman numeral of this object

Definition at line 93 of file roman.java.

Referenced by roman(), and toString().

◆ rSymbols

String [] roman.rSymbols = { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"}
staticprivate

table for relating decimals to roman symbols: an ordered list of roman numerals

Definition at line 84 of file roman.java.

Referenced by int2Roman().

◆ symbols

String [][] roman.symbols
staticprivate
Initial value:
= {
{"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX" },
{"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC" },
{"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM" },
{"", "M", "MM", "MMM", "(IV)", "(V)", "(VI)", "(VII)", "(VIII)", "(IX)"},
{"", "(X)", "(XX)", "(XXX)", "(XL)", "(L)", "(LX)", "(LXX)", "(LXXX)", "(XC)"},
{"", "(C)", "(CC)", "(CCC)", "(CD)", "(D)", "(DC)", "(DCC)", "(DCCC)", "(CM)"},
{"", "(M)", "(MM)", "(MMM)", "", "", "", "", "", ""}
}

table for mapping [decimal power, digit] to roman symbols

Definition at line 34 of file roman.java.

Referenced by int2roman().


The documentation for this class was generated from the following file: