Advertisement
Guest User

Untitled

a guest
Mar 27th, 2015
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.03 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class RomanNumerals
  4. {
  5. public static void main(String []args)
  6. {
  7. //Declarations
  8. Scanner sc = new Scanner(System.in);
  9. String strRoman;
  10. int strValue;
  11. String again = "y";
  12.  
  13. while(again.equalsIgnoreCase("y"))
  14. {
  15. strRoman = "";
  16. strValue = 0;
  17.  
  18. System.out.println("Roman Numeral Converter");
  19. System.out.print("Please Enter a roman numeral number to convert to arabic number format: ");
  20. strRoman = sc.nextLine();
  21. if(isValid(strRoman))
  22. {
  23. strValue = getValueOfRoman(strRoman);
  24. System.out.println(strValue);
  25. }
  26. else
  27. {
  28. System.out.println("Your Value is not Valid.");
  29. }
  30. System.out.println("Restart Program?(y or n)");
  31. again = sc.nextLine();
  32. }
  33. }
  34.  
  35. // Takes the String in and returns the actual value
  36. // This method will call both assignValueArray and evaluateArray
  37. // to do it's work.
  38. public static int getValueOfRoman(String strRoman)
  39. {
  40. int[] valueArray = assignValueArray(strRoman);
  41. int answer = evaluateArray(valueArray);
  42.  
  43. return answer;
  44. }
  45.  
  46. // Returns the value of individual 'Roman' letters
  47. // ie V returns 5, X returns 10 etc.
  48. // Hint: might be a good place for a switch
  49. public static int getValue(char ch)
  50. {
  51. int value = 0;
  52. switch(ch)
  53. {
  54. case 'I': value = 1; break;
  55. case 'V': value = 5; break;
  56. case 'X': value = 10; break;
  57. case 'L': value = 50; break;
  58. case 'C': value = 100; break;
  59. case 'D': value = 500; break;
  60. case 'M': value = 1000; break;
  61. }
  62. return value;
  63. }
  64.  
  65. // Goes thru the String (for loop) and builds an int[] valueArray based on
  66. // the value of each letter.
  67. // Hint: use getValue() in your for loop
  68. public static int[] assignValueArray(String roman)
  69. {
  70. int [] foo = new int[roman.length()];
  71.  
  72. for(int i = 0; i < roman.length(); i++)
  73. {
  74. foo[i] = getValue(roman.charAt(i));
  75. }
  76. return foo;
  77. }
  78.  
  79. // Goes thru the valueArray and evaluates based on 1 of 2 algorithms
  80. public static int evaluateArray(int[]valueArray)
  81. {
  82. int answer = 0;
  83. for(int i = 0; i < valueArray.length; i++) //3 0-2
  84. {
  85. if(i == valueArray.length-1) //to check for last index to avoid overflow //2
  86. {
  87. answer += valueArray[i];
  88. }
  89. else if(valueArray[i+1] > valueArray[i])
  90. {
  91. answer += -(valueArray[i]);
  92. }
  93. else
  94. {
  95. answer += valueArray[i];
  96. }
  97. }
  98.  
  99. return answer;
  100. }
  101.  
  102. // Goes thru the String and checks to make sure that each char is a valid
  103. // roman letter/digit. If any char is not valid returns false; otherwise true.
  104. public static boolean isValid(String roman)
  105. {
  106. roman = roman.toUpperCase();
  107. for(int i=0; i<roman.length(); i++)
  108. {
  109. switch(roman.charAt(i))
  110. {
  111. case 'M':
  112. case 'D':
  113. case 'C':
  114. case 'L':
  115. case 'X':
  116. case 'V':
  117. case 'I':break;
  118. default: return false;
  119. }
  120. }
  121. return true;
  122. }
  123. }
  124. /*
  125. =====Test Cases=====
  126. Roman Numeral Converter
  127. Please Enter a roman numeral number to convert to arabic number format: XIV
  128. 14
  129. Restart Program?(y or n)
  130. y
  131. Roman Numeral Converter
  132. Please Enter a roman numeral number to convert to arabic number format: CMI
  133. 901
  134. Restart Program?(y or n)
  135. y
  136. Roman Numeral Converter
  137. Please Enter a roman numeral number to convert to arabic number format: XXIX
  138. 29
  139. Restart Program?(y or n)
  140. y
  141. Roman Numeral Converter
  142. Please Enter a roman numeral number to convert to arabic number format: XLIX
  143. 49
  144. Restart Program?(y or n)
  145. y
  146. Roman Numeral Converter
  147. Please Enter a roman numeral number to convert to arabic number format: MMDCCCLX
  148. IV
  149. 2864
  150. Restart Program?(y or n)
  151. y
  152. Roman Numeral Converter
  153. Please Enter a roman numeral number to convert to arabic number format: M4IVX
  154. Your Value is not Valid.
  155. Restart Program?(y or n)
  156. y
  157. Roman Numeral Converter
  158. Please Enter a roman numeral number to convert to arabic number format: MQLXI
  159. Your Value is not Valid.
  160. Restart Program?(y or n)
  161. y
  162. Roman Numeral Converter
  163. Please Enter a roman numeral number to convert to arabic number format: XVIII
  164. 18
  165. Restart Program?(y or n)
  166. n
  167. Press any key to continue . . .
  168. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement