Advertisement
Crenox

NumberConverter Java Program

Nov 19th, 2014
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.45 KB | None | 0 0
  1. // Sammy Samkough
  2. // NumberConverter
  3. // Spec: To convert numbers to different bases (Binary, Octal, Decimal, Hexadecimal).
  4.  
  5. public class NumberConverter
  6. {
  7. private int decimal; // always stores the decimal equivalent, regardless of base
  8. private int base;
  9. private String strValue;
  10. private boolean b;
  11.  
  12. /** default set to base 10 w/ a value of 0 */
  13. public NumberConverter()
  14. {
  15. decimal = 0;
  16. base = 10;
  17. strValue = "0";
  18. }
  19.  
  20. /** base 10 value is used to set decimal, base and strValue
  21. * note: toBaseX can do this for you also */
  22. public NumberConverter(int value)
  23. {
  24. decimal = value;
  25. base = 10;
  26. strValue = toBaseX(10);
  27. }
  28.  
  29. /** sets the strValue and base based on parameters
  30. * strValue is only stored in uppercase
  31. * decimal is set here as well provided newValue is valid */
  32. public NumberConverter(String newValue, int newBase)
  33. {
  34. strValue = newValue;
  35. base = newBase;
  36. }
  37.  
  38. //**** Accessor Methods ****//
  39. public String getValue()
  40. {
  41. return strValue;
  42. }
  43.  
  44. public int getBase()
  45. {
  46. return base;
  47. }
  48.  
  49. public int getDecimal()
  50. {
  51. return decimal;
  52. }
  53.  
  54. /** sets the strValue and base based on parameters
  55. * decimal is set here as well, with a call to baseXToDec() */
  56. public void setValue(String newValue, int newBase)
  57. {
  58. strValue = newValue;
  59. base = newBase;
  60. decimal = baseXToDec();
  61. b = this.isValid(); // validates the strValue by using the isValid() method
  62. }
  63.  
  64.  
  65. public boolean isValid()
  66. {
  67. boolean valid = true;
  68.  
  69. //Checks for base 2
  70. if(base < 3)
  71. {
  72. for(int i = 0 ; i < strValue.length() - 1 ; i++)
  73. {
  74. if(charToValue(strValue.charAt(i)) < 0 || charToValue(strValue.charAt(i)) > 1)
  75. {
  76. valid = false;
  77. break;
  78. }
  79. else
  80. {
  81. valid = true;
  82. }
  83. }
  84. }
  85. //checks for bases 3 to 9
  86. else if(base < 10 && base > 2)
  87. {
  88. for(int i = 0 ; i < strValue.length() - 1 ; i++)
  89. {
  90. if(charToValue(strValue.charAt(i)) < 0 || charToValue(strValue.charAt(i)) >= 8)
  91. {
  92. valid = false;
  93. break;
  94. }
  95. else
  96. {
  97. valid = true;
  98. }
  99. }
  100. }
  101. //checks for 10 to 16
  102. else if(base >= 10 && base < 17)
  103. {
  104. for(int i = 0 ; i < strValue.length() - 1 ; i++)
  105. {
  106. if(charToValue(strValue.charAt(i)) < 0 || charToValue(strValue.charAt(i)) > 15)//this statement makes sure no value other than the known hex values occur
  107. {
  108. valid = false;
  109. break;
  110. }
  111. else
  112. {
  113. valid = true;
  114. }
  115. }
  116. }
  117. return valid;
  118. }
  119.  
  120. /** base of this object is set to x and strValue is the String value in base x
  121. * strValue is also returned, just for good measure
  122. * @param x the number base to convert to */
  123. public String toBaseX(int x)
  124. {
  125. String strRev = "";
  126. strValue = "";
  127. int div = decimal;
  128. int mod = 0;
  129. base = x;
  130.  
  131. if(b)
  132. {
  133. while(div != 0)
  134. {
  135. mod = div % base;
  136. if(mod >= 10) // the letter's number value
  137. {
  138. strValue += (char)(mod + 55);
  139. }
  140. else // numbers values for numbers 1 to 9
  141. {
  142. strValue += (char)(mod + 48);
  143. }
  144. div = (int)(div / base);
  145. strRev = reverseString(strValue); // reverses the strValue in order to return the accurate format
  146. }
  147. }
  148. else
  149. {
  150. strRev = "invalid";
  151. }
  152.  
  153. return strRev;
  154. }
  155.  
  156. /** take the currently stored strValue and calculate and return the decimal value */
  157. public int baseXToDec()
  158. {
  159. int exp = 0;
  160. decimal = 0;
  161. for(int i = strValue.length() - 1; i >= 0; i--) //goes from right to left on the strValue
  162. {
  163. decimal += charToValue(strValue.charAt(i)) * Math.pow(base, exp);
  164. exp++;
  165. }
  166.  
  167. return decimal;
  168. }
  169.  
  170. /** this is a helper method only
  171. * the integer value of digit is returned
  172. * -1 is the return value for an error
  173. * @param ch a valid digit for the given number base */
  174. private int charToValue(char ch)
  175. {
  176. int value = 0;
  177. switch(ch)
  178. {
  179. // gives values of 0-9 as 0-9 and A-F as 10-15
  180. case '0': value = 0;
  181. break;
  182. case '1': value = 1;
  183. break;
  184. case '2': value = 2;
  185. break;
  186. case '3': value = 3;
  187. break;
  188. case '4': value = 4;
  189. break;
  190. case '5': value = 5;
  191. break;
  192. case '6': value = 6;
  193. break;
  194. case '7': value = 7;
  195. break;
  196. case '8': value = 8;
  197. break;
  198. case '9': value = 9;
  199. break;
  200. case 'A': value = 10;
  201. break;
  202. case 'B': value = 11;
  203. break;
  204. case 'C': value = 12;
  205. break;
  206. case 'D': value = 13;
  207. break;
  208. case 'E': value = 14;
  209. break;
  210. case 'F': value = 15;
  211. break;
  212. // if none of the above occur, then the value is set to -1, which later in the isValid method will spell an error
  213. default: value = -1;
  214. break;
  215. }
  216. return value;
  217. }
  218.  
  219. /** This is a helper method that returns strRev as a reversed version
  220. * @param strFwd the string to be reversed */
  221. public String reverseString(String strFwd)
  222. {
  223. String strReverse = "";
  224.  
  225. for(int i = (strFwd.length() - 1); i >= 0; i--)
  226. {
  227. strReverse += strFwd.charAt(i);
  228. }
  229.  
  230. return strReverse;
  231. }
  232.  
  233. /** a String with the current base and value is returned */
  234. public String toString()
  235. {
  236. return "" + toBaseX(base);
  237. }
  238. }
  239. -----------------------------------------------------------------------------------------------------------------------------------
  240. // Sammy Samkough
  241. // NumberConverter
  242. // Spec: To convert numbers to different bases (Binary, Octal, Decimal, Hexadecimal).
  243.  
  244. import java.util.Scanner;
  245.  
  246. public class NumberConverterClient
  247. {
  248. public static void main(String args[])
  249. {
  250. boolean running = true;
  251. boolean playing = true;
  252. Scanner sc = new Scanner(System.in);
  253. NumberConverter nc = new NumberConverter();
  254. String v = "";
  255. String c = "";
  256. int b;
  257.  
  258. while(running)
  259. {
  260. playing = true;
  261.  
  262. System.out.print("What base do you want to start at? ");
  263. b = sc.nextInt();
  264. System.out.print("\nGood choice! Now enter the value you want converted: ");
  265.  
  266. v = sc.next();
  267. v = v.toUpperCase();
  268.  
  269. nc.setValue(v, b);
  270. System.out.println("\nYour number in binary is: " + nc.toBaseX(2));
  271. System.out.println("Your number in octal is: " + nc.toBaseX(8));
  272. System.out.println("Your number in decimal is: " + nc.toBaseX(10));
  273. System.out.println("Your number in base 12 is: " + nc.toBaseX(12));
  274. System.out.println("Your number in hexadecimal is: " + nc.toBaseX(16));
  275. System.out.println("----------------------------------------------------");
  276. System.out.println("Want to go again? Please choose either yes or no.");
  277.  
  278. while(playing)
  279. {
  280. c = sc.next();
  281. if(c.equalsIgnoreCase("yes"))
  282. {
  283. System.out.println("----------------------------------------------------");
  284. playing = false;
  285. continue;
  286. }
  287. else if(c.equalsIgnoreCase("no"))
  288. {
  289. System.out.println("Bye-bye then!");
  290. playing = false;
  291. running = false;
  292. break;
  293. }
  294. else
  295. {
  296. System.out.println("Improper input. Try again");
  297. System.out.println("");
  298. continue;
  299. }
  300. } // smaller while loop, "playing"
  301. } // large while loop, "running"
  302. } // main method
  303. } // class
  304. /*
  305. What base do you want to start at? 16
  306.  
  307. Good choice! Now enter the value you want converted: FFF
  308.  
  309. Your number in binary is: 111111111111
  310. Your number in octal is: 7777
  311. Your number in decimal is: 4095
  312. Your number in base 12 is: 2453
  313. Your number in hexadecimal is: FFF
  314. ----------------------------------------------------
  315. Want to go again? Please choose either yes or no.
  316. yes
  317. ----------------------------------------------------
  318. What base do you want to start at? 16
  319.  
  320. Good choice! Now enter the value you want converted: BaD
  321.  
  322. Your number in binary is: 101110101101
  323. Your number in octal is: 5655
  324. Your number in decimal is: 2989
  325. Your number in base 12 is: 1891
  326. Your number in hexadecimal is: BAD
  327. ----------------------------------------------------
  328. Want to go again? Please choose either yes or no.
  329. yes
  330. ----------------------------------------------------
  331. What base do you want to start at? 16
  332.  
  333. Good choice! Now enter the value you want converted: BAT
  334.  
  335. Your number in binary is: 101110011111
  336. Your number in octal is: 5637
  337. Your number in decimal is: 2975
  338. Your number in base 12 is: 187B
  339. Your number in hexadecimal is: B9F
  340. ----------------------------------------------------
  341. Want to go again? Please choose either yes or no.
  342. yes
  343. ----------------------------------------------------
  344. What base do you want to start at? 10
  345.  
  346. Good choice! Now enter the value you want converted: 942
  347.  
  348. Your number in binary is: 1110101110
  349. Your number in octal is: 1656
  350. Your number in decimal is: 942
  351. Your number in base 12 is: 666
  352. Your number in hexadecimal is: 3AE
  353. ----------------------------------------------------
  354. Want to go again? Please choose either yes or no.
  355. yes
  356. ----------------------------------------------------
  357. What base do you want to start at? 8
  358.  
  359. Good choice! Now enter the value you want converted: 747
  360.  
  361. Your number in binary is: 111100111
  362. Your number in octal is: 747
  363. Your number in decimal is: 487
  364. Your number in base 12 is: 347
  365. Your number in hexadecimal is: 1E7
  366. ----------------------------------------------------
  367. Want to go again? Please choose either yes or no.
  368. yes
  369. ----------------------------------------------------
  370. What base do you want to start at? 12
  371.  
  372. Good choice! Now enter the value you want converted: A359b
  373.  
  374. Your number in binary is: 110100000110000111
  375. Your number in octal is: 640607
  376. Your number in decimal is: 213383
  377. Your number in base 12 is: A359B
  378. Your number in hexadecimal is: 34187
  379. ----------------------------------------------------
  380. Want to go again? Please choose either yes or no.
  381. yes
  382. ----------------------------------------------------
  383. What base do you want to start at? 2
  384.  
  385. Good choice! Now enter the value you want converted: 1011101
  386.  
  387. Your number in binary is: 1011101
  388. Your number in octal is: 135
  389. Your number in decimal is: 93
  390. Your number in base 12 is: 79
  391. Your number in hexadecimal is: 5D
  392. ----------------------------------------------------
  393. Want to go again? Please choose either yes or no.
  394. yes
  395. ----------------------------------------------------
  396. What base do you want to start at? 16
  397.  
  398. Good choice! Now enter the value you want converted: 16>;3
  399.  
  400. Your number in binary is: invalid
  401. Your number in octal is: invalid
  402. Your number in decimal is: invalid
  403. Your number in base 12 is: invalid
  404. Your number in hexadecimal is: invalid
  405. ----------------------------------------------------
  406. Want to go again? Please choose either yes or no.
  407. fad
  408. Improper input. Try again
  409.  
  410. no
  411. Bye-bye then!
  412. Press any key to continue . . .
  413. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement