Guest User

Untitled

a guest
Oct 18th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.70 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Menu {
  4.  
  5. static Scanner scan = new Scanner(System.in);
  6.  
  7. //declare the arrays
  8. static double [] mainArr = new double [] {8.25, 7.3, 10.0, 2.5, 16};
  9.  
  10. static double [] tempArr = new double [] {0,0,0,0,0};
  11.  
  12. static double [] temptempArr = new double [] {0,0,0,0,0};
  13.  
  14. static double [] sumArr = new double [] {0,0,0,0,0};
  15.  
  16.  
  17. //functions
  18. static void say(String msg){
  19. System.out.println(msg);
  20. }
  21.  
  22. static void sayd(double n){
  23. System.out.println(n);
  24. }
  25.  
  26. static int aski(String prompt){
  27. say(prompt);
  28. return scan.nextInt();
  29. }
  30.  
  31.  
  32. static double askd(String prompt ){
  33. say(prompt);
  34. return scan.nextDouble();
  35. }
  36.  
  37. static void evenOut() {
  38. if(mainArr.length>tempArr.length) {
  39. sumArr = new double [mainArr.length];
  40. temptempArr = new double [mainArr.length];
  41. for(int i = 0; i < tempArr.length ; i++) {
  42. temptempArr[i] = tempArr [i];
  43. }//end for
  44. }//end if
  45. else if(mainArr.length<tempArr.length) {
  46. sumArr = new double [tempArr.length];
  47. temptempArr = new double [tempArr.length];
  48. for(int i = 0; i < mainArr.length ; i++) {
  49. temptempArr[i] = mainArr [i];
  50. }//end for
  51. }// end else if
  52. }//end evenOut
  53.  
  54. static void addArr() {
  55. if(mainArr.length>tempArr.length){
  56. for(int i = 0; i < mainArr.length ; i++) {
  57. sumArr[i] = mainArr[i] + temptempArr [i];
  58. }
  59. else if(mainArr.length<tempArr.length){
  60. for(int i = 0; i < tempArr.length ; i++) {
  61. sumArr[i] = temptempArr[i] + tempArr [i];
  62. }
  63. else{
  64. for(int i = 0; i < mainArr.length ; i++) {
  65. sumArr[i] = mainArr[i] + tempArr [i];
  66. }
  67. }
  68. }
  69.  
  70. static void subArr() {
  71. for(int i = 0; i < mainArr.length ; i++) {
  72. sumArr[i] = mainArr[i] - tempArr [i];
  73. }
  74. }
  75.  
  76. static void scaleMain() {
  77. double factor = askd("What would you like to multiply each element of mainArr by?");
  78. for(int i = 0; i < mainArr.length; i++) {
  79. mainArr[i] *= factor;
  80. }
  81. }
  82.  
  83. static void scaleTemp() {
  84. double factor = askd("What would you like to multiply each element of tempArr by?");
  85. for(int i = 0; i < tempArr.length; i++) {
  86. tempArr[i] *= factor;
  87. }
  88. }
  89.  
  90. static void fillMain () {
  91. int amnt = aski("\nHow many elements would you like to put into the mainArr?");
  92. mainArr = new double [amnt];
  93. say("Set element");
  94. for(int i = 0; i < mainArr.length; i++) {
  95. double n = askd ((i+1)+" to:");
  96. mainArr[i] = n;
  97. }
  98. }
  99.  
  100. static void fillTemp () {
  101. int amnt = aski("\nHow many elements would you like to put into the tempArr?");
  102. tempArr = new double [amnt];
  103. say("Set element");
  104. for(int i = 0; i < tempArr.length; i++) {
  105. double n = askd ((i+1)+" to:");
  106. tempArr[i] = n;
  107. }
  108. }
  109.  
  110. static void sayArr(double[] n) {
  111. for (int i = 0; i < n.length ; i++) {
  112. sayd(n[i]);
  113. }
  114. }
  115.  
  116. static void arrayLength () {
  117.  
  118. if(mainArr.length == tempArr.length) {
  119. say("\nThe arrays are equal\n");
  120. }
  121. else {
  122. say("\nThe arrays are NOT equal\n");
  123. }
  124. }
  125.  
  126. static int state = 1;
  127.  
  128.  
  129. public static void main(String[] args) {
  130.  
  131.  
  132.  
  133. //intro
  134. say("Welcome to ArrayCalc");
  135.  
  136.  
  137. thewhileloop:
  138. while(true) {
  139.  
  140.  
  141. say("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
  142.  
  143. //options to choose from
  144. String[] menuOptions = new String[] {
  145. "see the elements of the main and temp arrays",
  146. "fill the mainArr with new values",
  147. "fill the tempArr with new values",
  148. "add the values of both arrays",
  149. "subtract the values of both arrays by eachother",
  150. "scale the values of mainArr \n(multiply each element of the array by a factor)",
  151. "scale the values of tempArr",
  152. "check if the lengths of main and temp arrays are equal"
  153. };
  154.  
  155.  
  156.  
  157.  
  158.  
  159. //display menu selections from an array with a for loop
  160. for (int i = 0; i < menuOptions.length; i++){
  161. say("Type "+(i+1)+" to "+menuOptions[i]+"\n");
  162.  
  163. }
  164.  
  165. say("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
  166.  
  167. //selecting the operation to perform
  168. try {
  169. int select = aski("Input a number from 1 - " + menuOptions.length + " to select an operation \nType in \"99\" to exit the program")-1;
  170.  
  171.  
  172.  
  173. //make a decision based on the input
  174.  
  175.  
  176. selectloop:
  177. switch (select){
  178.  
  179. case 0:
  180. say("you selected to "+menuOptions[0]);
  181.  
  182. say("mainArr:");
  183. sayArr(mainArr);
  184.  
  185. say("tempArr:");
  186. sayArr(tempArr);
  187.  
  188. state = aski("Type in \"1\" to go back to the menu, or \"2\" to exit the program")-1;
  189. switch(state) {
  190. case 0:
  191. break selectloop;
  192.  
  193. case 1:
  194. break thewhileloop;
  195. default:
  196. say("you need to type in either \"1\", or \"2\"");
  197. }
  198. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  199. case 1:
  200. say("you selected to "+menuOptions[1]);
  201.  
  202. fillMain();
  203.  
  204. state = aski("Type in \"1\" to go back to the menu, or \"2\" to exit the program")-1;
  205. switch(state) {
  206. case 0:
  207. break selectloop;
  208.  
  209. case 1:
  210. break thewhileloop;
  211. default:
  212. say("you need to type in either \"1\", or \"2\"");
  213. };
  214. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  215. case 2:
  216. say("you selected to "+menuOptions[2]);
  217.  
  218. fillTemp();
  219.  
  220. state = aski("Type in \"1\" to go back to the menu, or \"2\" to exit the program")-1;
  221. switch(state) {
  222. case 0:
  223. break selectloop;
  224.  
  225. case 1:
  226. break thewhileloop;
  227. default:
  228. say("you need to type in either \"1\", or \"2\"");
  229. }
  230. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  231.  
  232. case 3:
  233. say("you selected to "+menuOptions[3]);
  234.  
  235. evenOut();
  236. addArr();
  237. sayArr(sumArr);
  238.  
  239. state = aski("Type in \"1\" to go back to the menu, or \"2\" to exit the program")-1;
  240. switch(state) {
  241. case 0:
  242. break selectloop;
  243.  
  244. case 1:
  245. break thewhileloop;
  246. default:
  247. say("you need to type in either \"1\", or \"2\"");
  248. }
  249. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  250.  
  251. case 4:
  252. say("you selected to "+menuOptions[4]);
  253.  
  254. evenOut();
  255. subArr();
  256. sayArr(sumArr);
  257. state = aski("Type in \"1\" to go back to the menu, or \"2\" to exit the program")-1;
  258. switch(state) {
  259. case 0:
  260. break selectloop;
  261.  
  262. case 1:
  263. break thewhileloop;
  264. default:
  265. say("you need to type in either \"1\", or \"2\"");
  266. }
  267. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  268.  
  269. case 5:
  270. say("you selected to "+menuOptions[5]);
  271.  
  272. scaleMain();
  273.  
  274. state = aski("Type in \"1\" to go back to the menu, or \"2\" to exit the program")-1;
  275. switch(state) {
  276. case 0:
  277. break selectloop;
  278.  
  279. case 1:
  280. break thewhileloop;
  281. default:
  282. say("you need to type in either \"1\", or \"2\"");
  283. }
  284. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  285.  
  286. case 6:
  287. say("you selected to "+menuOptions[6]);
  288.  
  289. scaleTemp();
  290.  
  291. state = aski("Type in \"1\" to go back to the menu, or \"2\" to exit the program")-1;
  292. switch(state) {
  293. case 0:
  294. break selectloop;
  295.  
  296. case 1:
  297. break thewhileloop;
  298. default:
  299. say("you need to type in either \"1\", or \"2\"");
  300. }
  301. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  302.  
  303. case 7:
  304. say("you selected to "+menuOptions[6]);
  305.  
  306. arrayLength();
  307.  
  308. state = aski("Type in \"1\" to go back to the menu, or \"2\" to exit the program")-1;
  309. switch(state) {
  310. case 0:
  311. break selectloop;
  312.  
  313. case 1:
  314. break thewhileloop;
  315. default:
  316. say("you need to type in either \"1\", or \"2\"");
  317. }
  318. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  319.  
  320. case 98:
  321.  
  322. break thewhileloop;
  323. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  324.  
  325. default:
  326. say("You needed to choose a number from 1 to "+ menuOptions.length + "\n");
  327.  
  328. state = aski("Type in \"1\" to go back to the menu, or \"2\" to exit the program")-1;
  329. switch(state) {
  330. case 0:
  331. break selectloop;
  332.  
  333. case 1:
  334. break thewhileloop;
  335. default:
  336. say("you need to type in either \"1\", or \"2\"");
  337. }
  338.  
  339. }//end switch(select)
  340. }//end try
  341.  
  342. catch(Exception e) {
  343.  
  344. scan.nextLine();
  345.  
  346. long timer1 =0;
  347. long timer2 =0;
  348. long tottime = 0;
  349.  
  350. say("Sorry, but that's an invalid character\n\nreturning to menu in:");
  351. int time = 3;
  352. timer1 = System.currentTimeMillis();
  353.  
  354. while(time >= 0) {
  355. tottime = timer2 - timer1;
  356. timer2= System.currentTimeMillis();
  357. if(tottime>=500) {
  358. if(time >0) {
  359. say(""+time);
  360. }
  361. timer1= System.currentTimeMillis();
  362. time--;
  363. }
  364.  
  365. }//end while
  366.  
  367.  
  368.  
  369. }//end catch
  370.  
  371. }//end while
  372.  
  373. say("This arrayCalc program was coded by Namu \nThank you for your time");
  374.  
  375. } //end main
  376.  
  377. }
Add Comment
Please, Sign In to add comment