1. import java.util.Scanner; 2. public class homework 3. { 4. public static void main(String[] args) 5. { 6. 7. /* 8. * name must be printed out at the beginning of your source code 9. * */ 10. //introduction of the program and my mane 11. printIntro(); 12. 13. 14. Scanner kb = new Scanner(System.in);//create the scanner to get the input 15. 16. String answer = "yes";//set the default value of answer 17. 18. while ("yes".equals(answer)) 19. { 20. 21. 22. System.out.println("Enter the list of the operands and the opration:"); 23. 24. //get the input from the use 25. String input = kb.nextLine(); 26. 27. // separate the tokens and store them in variables 28. 29. char op1 = ' '; 30. char op2 = ' '; 31. String operation =""; 32. int flag = 0; 33. 34. for(int i = 0; i < input.length();i++) 35. { 36. 37. if(input.charAt(i)!= ' ') 38. { 39. 40. if(flag == 0) 41. { 42. op1 = input.charAt(i); 43. }else if(flag ==1) 44. { 45. op2 = input.charAt(i); 46. }else if(flag ==2) 47. { 48. operation = input.substring(i); 49. } 50. 51. flag++; 52. } 53. 54. 55. } 56. 57. 58. //(3)call different methods to carry out the task 59. 60. printNumeric(op1,op2,operation); 61. printEnglish(op1,op2,operation); 62. 63. 64. 65. //(4)get the choice of the user 66. System.out.print("Do you have another expression?"); 67. answer = kb.nextLine(); 68. 69. 70. } 71. 72. //when the option the user inputed is not "yes" such as "no" or other words print this sentence 73. System.out.println("Have a good day!! BYE"); 74. 75. } 76. 77. 78. 79. 80. 81. /*this methods gets the operands and the operation and 82. prints the English version: if we call this method 83. printEnglish(2,3, plus), then this method will output: 84. Two plus three = 5 */ 85. public static void printEnglish(char op1, char op2,String operation) 86. { 87. /*1. call the method charToString(op1) to convertlish 88. word for example ‘1’ to one or ‘2’ to two,….*/ 89. String str_num1 = charToString(op1); 90. String str_num2 = charToString(op2); 91. 92. 93. 94. /* 95. 2. call the method operandConversionToNumber(op1) to 96. get its numeric value. For example if op1 is ‘1’ then 97. this method call should return the integer value 1*/ 98. 99. int num1 = operandConversiontoNumber(op1) ; 100. int num2 = operandConversiontoNumber(op2) ; 101. 102. /* 103. 3. call the method operationConversion(operation) to 104. convert the operation to a mathematical operation. For 105. example if you call this method with the string plus 106. then it will return ‘+’*/ 107. char mathematical_operation =operationConversion(operation); 108. 109. 110. /*4. finally call the method calculate to get the result 111. of the operation.*/ 112. double result = calculate(num1,num2, mathematical_operation); 113. if(ZERO == result && operation.equals("divide")||mathematical_operation==' ') 114. { 115. //do nothing because the information has already printed when the method of printEnglish(char op1, char op2,String operation)was called 116. }else if(mathematical_operation=='^') 117. { 118. 119. System.out.println(num1+" to the power of "+num2 +" = "+result ); 120. }else 121. { 122. //if the operation id not divide the result should be an integer 123. if(!operation.equals("divide")) 124. { 125. System.out.println(str_num1+" "+operation+" "+str_num2+" = "+(int)result); 126. }else 127. { 128. System.out.println(str_num1+" "+operation+" "+str_num2+" = "+result); 129. } 130. } 131. 132. 133. 134. } 135. 136. 137. 138. /*this method prints the numeric version which is 2*3 =6*/ 139. public static boolean printNumeric(char op1, char op2, String operation) 140. { 141. 142. 143. //body of this method is similar to the previous 144. //method 145. int num1 = operandConversiontoNumber(op1); 146. int num2 = operandConversiontoNumber(op2); 147. 148. char mathematical_operation =operationConversion(operation); 149. if(mathematical_operation!=' ')//to make sure that the operation has exist 150. { 151. double result = calculate(num1,num2, mathematical_operation); 152. 153. if(ZERO == result && operation.equals("divide")) 154. { 155. System.out.println("Invalid opernad, cannot divide by zero"); 156. }else 157. { 158. //if the operation id not divide the result should be an integer 159. if(!operation.equals("divide")) 160. { 161. System.out.println(num1+" "+mathematical_operation+" "+num2 +" = "+(int)result ); 162. }else 163. System.out.println(num1+" "+mathematical_operation+" "+num2 +" = "+result ); 164. 165. } 166. 167. 168. }else//if the operation is not exist , print this sentence 169. { 170. System.out.println("Invalid operation, "+operation+" does not exist"); 171. } 172. 173. return true; 174. } 175. 176. 177. 178. 179. 180. /*this method gets a number as a character and returns 181. its numeric value as an integer. You must use case 182. statement for this method*/ 183. 184. 185. public static int operandConversiontoNumber(char operand) 186. { 187. int numeric_value = 0; 188. switch(operand) 189. { 190. 191. case '0': numeric_value = 0; break; 192. case '1': numeric_value = 1; break; 193. case '2': numeric_value = 2; break; 194. case '3': numeric_value = 3; break; 195. case '4': numeric_value = 4; break; 196. case '5': numeric_value = 5; break; 197. case '6': numeric_value = 6; break; 198. case '7': numeric_value = 7; break; 199. case '8': numeric_value = 8; break; 200. case '9': numeric_value = 9; break; 201. 202. 203. } 204. return numeric_value; 205. 206. } 207. 208. 209. 210. /*this method gets the operation as a string and 211. return the equivalent operation in math. For example 212. if it receives “plus” the it will return ‘+’ */ 213. public static char operationConversion(String s) 214. { 215. char operation = ' '; 216. 217. if(s.equals("plus")) 218. { 219. operation = '+'; 220. }else if(s.equals("minus")) 221. { 222. operation = '-'; 223. }else if(s.equals("multiply")) 224. { 225. operation = '*'; 226. }else if(s.equals("divide")) 227. { 228. operation = '/'; 229. }else if(s.equals("power")) 230. { 231. operation = '^'; 232. } 233. 234. return operation; 235. 236. } 237. /*this method receives two numbers and the operation and returns the result*/ 238. public static double calculate(int operand1, int operand2, char operation) 239. { 240. double result = 0.0; 241. switch(operation) 242. { 243. case '+' : result = operand1 + operand2; break; 244. case '-' : result = operand1 - operand2; break; 245. case '*' : result = operand1 * operand2; break; 246. case '/' : 247. if(operand2==0) 248. { 249. result = ZERO; 250. }else 251. { 252. result = (double)operand1 / operand2;} 253. break; 254. case '^' : result = Math.pow(operand1, operand2); break; 255. 256. } 257. 258. /*if(operation=='+') 259. { 260. 261. result = operand1 + operand2; 262. }else if(operation=='-') 263. { 264. result = operand1 - operand2; 265. }else if(operation=='*') 266. { 267. result = operand1 * operand2; 268. 269. }else if(operation=='/') 270. { 271. result = operand1 / operand2; 272. }else if(operation=='^') 273. { 274. result = Math.pow(operand1, operand2); 275. }*/ 276. 277. return result ; 278. 279. } 280. /*this method converts a number character to its 281. * English word for example if this method receives ‘1’ 282. it will return “one” */ 283. public static String charToString(char num) 284. { 285. String word = ""; 286. 287. 288. switch(num) 289. { 290. case '0': word = "zero";break; 291. case '1': word = "one";break; 292. case '2': word = "two";break; 293. case '3': word = "three";break; 294. case '4': word = "four";break; 295. case '5': word = "five";break; 296. case '6': word = "six";break; 297. case '7': word = "seven";break; 298. case '8': word = "eight";break; 299. case '9': word = "nine";break; 300. 301. } 302. 303. return word; 304. 305. 306. } 307. /*this method prints the description of this program.*/ 308. public static void printIntro() 309. { 310. System.out.println("My name is ...,This program ..."); 311. } 312. //when op2 is 0 and operation is divide return this number 313. public static final double ZERO = -65535; 314. }