Reference Roman Table

It is useful to remember the following simple rule before starting to solve this problem. A roman numeral can be calculated by adding or subtracting the numeric values of the characters each other by comparing their values. The rule is basically if the value of the character is greater than the value of the next character, the total sum variable is added with the value of the character. Otherwise the subtraction operation is performed.

For example,

So, we can implement this rule as below.

package com.fatihkabakci.Easy.RomanToInteger;/****@authorfkabakci** Problem Description: Given a roman numeral, convert it to an integer.* Input is guaranteed to be within the range from 1 to 3999.** Solution:** Postcondition: num is within the range from 1 to 3999**1. Compare adjacent two characters in regarding to the following rule.*2. If the roman value of a character is greater than the value of another character which is right of that,* Total sum is added with the value of the first character.* otherwise,* Total sum is subtracted by the value of the first character.*3. Go back to 2 until all characters are visited.** Example:** Roman characters: MCMXCVI** Their values:100010010001010051**1. Start comparing with 1000 and 100, because of the comparison 1000 > 100 is true, add total sum with 1000, total = 1000*2. Then 100 and 1000. 100 < 1000, subtract total sum by 100, total = 900*3. Now is 1000 and 10. 1000 > 10, adding total sum with 1000, total = 1900*4. 10 and 100. 10 < 100, again subtracting total sum by 10, total = 1890*5. 100 and 5. 100 > 5, adding total sum with 100, total = 1990*6. 5 and 1. 5 > 1, adding total sum with 5, total = 1995,*7. Lastly,1 and 0 because there is no more character left. 1 > 0, adding sum with 1 more, total = 1996.* */publicclass RomanToInteger {
privateint getValue(char c) {
switch (c) {
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;
}
}
publicint romanToInt(String s) {
int acc = 0, i = 0, l = s.length();
while (i < l) {
char c = s.charAt(i);
char csub = (i + 1 < s.length()) ? s.charAt(i + 1) : "\0";
int v1 = getValue(c);
int v2 = getValue(csub);
if (v1 < v2)
acc = acc - v1;
else
acc = acc + v1;
i = i + 1;
}
return acc;
}
publicstaticvoid main(String[] args) {
RomanToInteger rti = new RomanToInteger();
System.out.println(rti.romanToInt("MCMXCVI"));
}
}

A programming language is a formal constructed language designed to communicate instructions to a machine, particularly a computer. It can be used to create programs to control the behavior of a machine.
Java,C,
C++,C#

Computer hardware is the collection of physical elements that constitutes a computer system. Computer hardware refers to the physical parts or components of a computer such as the monitor, memory, cpu.

Web development is a broad term for the work involved in developing a web site for the Internet or an intranet. Html,Css,JavaScript,ASP.Net,PHP are one of the most popular technologies.
J2EE,Servlet,
JSP,JSF,
ASP

Mobile application development is the process by which application software is developed for low-power handheld devices, such as personal digital assistants, enterprise digital assistants or mobile phones.
J2ME

A computer network or data network is a telecommunications network that allows computers to exchange data. In computer networks, networked computing devices pass data to each other along data connections.

An operating system is software that manages computer hardware and software resources and provides common services for computer programs. The OS is an essential component of the system software in a computer system.
Linux,Windows