Advertisement
richmondn99

Final-Problem55

Jan 23rd, 2017
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.00 KB | None | 0 0
  1.  
  2.  
  3.  
  4. /**
  5. * class of the womework for HWv1v12v17 .
  6. *
  7. * @author (Noah)
  8. * @version (1/12/17)
  9. */
  10.  
  11. import java.util.*; //importing various libraries
  12. import java.lang.*;
  13. import javax.swing.*;
  14. import java.text.*;
  15. //these are left over from the rest of the methods that are included in this for me, class but not included here
  16.  
  17.  
  18. public class HWv1v12v17
  19. {
  20. public static void prob55()
  21. {
  22.  
  23. System.out.println("55."); //print the problem number
  24. Scanner scanin = new Scanner(System.in);
  25.  
  26. int num1;
  27. String Num1="";
  28. String Num2="";
  29. int num2;
  30. String operate;
  31. String operateW = "";
  32. double result = 0;
  33. boolean answ = true;
  34. String again = "";
  35.  
  36. do
  37. {
  38. System.out.println();
  39. do //ask for the user to enter a integer until they enter something thats from 0 to 9
  40. {
  41. System.out.print("Enter a number from 0 to 9: ");
  42. num1 = scanin.nextInt();
  43. System.out.println();
  44. }
  45. while (num1<0 || num1 >= 10);
  46.  
  47. do //ask for the user to input one of the specified operator types. if you look at the while statement, you'll see that it will actually accept more than just the specified versions of the stated operator
  48. {
  49. System.out.print("Enter your operation type (+,-,x,/, or ^): ");
  50. operate = scanin.next();
  51. }
  52. while (!operate.equalsIgnoreCase("+") && !operate.equalsIgnoreCase("add") && !operate.equalsIgnoreCase("addition") && !operate.equalsIgnoreCase("-") && !operate.equalsIgnoreCase("minus") && !operate.equalsIgnoreCase("subtract") && !operate.equalsIgnoreCase("subtraction") && !operate.equalsIgnoreCase("x") && !operate.equalsIgnoreCase("multiply") && !operate.equalsIgnoreCase("times") && !operate.equalsIgnoreCase("/") && !operate.equalsIgnoreCase("divide") && !operate.equalsIgnoreCase("division") && !operate.equalsIgnoreCase("^") && !operate.equalsIgnoreCase("power"));
  53. System.out.println();
  54. if (operate.equalsIgnoreCase("/") || operate.equalsIgnoreCase("divide") || operate.equalsIgnoreCase("division" ))
  55. //if the user has entered a division operator, than this while loop will repeat until it gets an input from 1 to 9 (not zero)
  56. {
  57. do
  58. {
  59. System.out.print("Enter your second single didget number (1 to 9). You cannot divide by zero: ");
  60. num2 = scanin.nextInt();
  61. System.out.println();
  62. }
  63. while (num2 <= 0 || num1 >= 10);
  64. }
  65. else
  66. //else it will accept 0-9
  67. {
  68. do
  69. {
  70. System.out.print("Enter your second single digit number (0 to 9) : ");
  71. num2 = scanin.nextInt();
  72. System.out.println();
  73. }
  74. while (num2 < 0 || num1 >= 10);
  75. }
  76. System.out.println();
  77. switch (num1)//this gets the spelt out version of the first number
  78. {
  79. case 0:
  80. Num1 = "Zero";
  81. break;
  82.  
  83. case 1:
  84. Num1 = "One";
  85. break;
  86.  
  87. case 2:
  88. Num1 = "Two";
  89. break;
  90.  
  91. case 3:
  92. Num1 = "Three";
  93. break;
  94.  
  95. case 4:
  96. Num1 = "Four";
  97. break;
  98.  
  99. case 5:
  100. Num1 = "Five";
  101. break;
  102.  
  103. case 6:
  104. Num1 = "Six";
  105. break;
  106.  
  107. case 7:
  108. Num1 = "Seven";
  109. break;
  110.  
  111. case 8:
  112. Num1 = "Eight";
  113. break;
  114.  
  115. case 9:
  116. Num1 = "Nine";
  117. break;
  118.  
  119. default:
  120. System.out.println("invalid first integer");
  121. }
  122.  
  123. switch (num2)//this makes a spelt out version of the second number
  124. {
  125. case 0:
  126. Num2 = "Zero";
  127. break;
  128.  
  129. case 1:
  130. Num2 = "One";
  131. break;
  132.  
  133. case 2:
  134. Num2 = "Two";
  135. break;
  136.  
  137. case 3:
  138. Num2 = "Three";
  139. break;
  140.  
  141. case 4:
  142. Num2 = "Four";
  143. break;
  144.  
  145. case 5:
  146. Num2 = "Five";
  147. break;
  148.  
  149. case 6:
  150. Num2 = "Six";
  151. break;
  152.  
  153. case 7:
  154. Num2 = "Seven";
  155. break;
  156.  
  157. case 8:
  158. Num2 = "Eight";
  159. break;
  160.  
  161. case 9:
  162. Num2 = "Nine";
  163. break;
  164.  
  165. default:
  166. System.out.println("invalid second integer");
  167. }
  168.  
  169.  
  170. //the following if and elseif statements find the spelt out version of the operation (or operation word, operateW) and sets a double value for the result
  171. //i did not use switch-case because i wasnt sure how to do that with a string
  172. if (operate.equalsIgnoreCase("+") || operate.equalsIgnoreCase("add") || operate.equalsIgnoreCase("addition"))
  173. {
  174. operateW = "added to";
  175. result = num1+num2;
  176. }
  177. else if (operate.equalsIgnoreCase("-") || operate.equalsIgnoreCase("minus") || operate.equalsIgnoreCase("subtract") || operate.equalsIgnoreCase("subtraction"))
  178. {
  179. operateW = "minus";
  180. result = num1-num2;
  181. }
  182. else if (operate.equalsIgnoreCase("x") || operate.equalsIgnoreCase("multiply") || operate.equalsIgnoreCase("times"))
  183. {
  184. operateW = "times";
  185. result = num1*num2;
  186. }
  187. else if (operate.equalsIgnoreCase("/") || operate.equalsIgnoreCase("divide") || operate.equalsIgnoreCase("division" ))
  188. {
  189. operateW = "divided by";
  190. result = (double)num1/ num2;
  191. }
  192. else if (operate.equalsIgnoreCase("^") || operate.equalsIgnoreCase("power"))
  193. {
  194. operateW = "to the power of";
  195. result = Math.pow(num1,num2);
  196. }
  197.  
  198.  
  199. System.out.println(Num1 + " "+ operateW+ " " +Num2+ " equals " + result); //prints the results
  200. System.out.println();
  201. System.out.println();
  202.  
  203. do
  204. {
  205. System.out.println("Would you like to calculate another number? Answer \"Y\" for yes or \"N\" for no:");
  206. again = scanin.next();
  207. }
  208. while (!again.equalsIgnoreCase("y") && !again.equalsIgnoreCase("n"));
  209. System.out.println();
  210. if (again.equalsIgnoreCase("n"))
  211. {
  212. answ = false;
  213. }
  214. }
  215. while (answ);
  216. System.out.println("Goodbye! :)");
  217. }
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement