Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class Menu {
- static Scanner scan = new Scanner(System.in);
- //declare the arrays
- static double [] mainArr = new double [] {8.25, 7.3, 10.0, 2.5, 16};
- static double [] tempArr = new double [] {0,0,0,0,0};
- static double [] temptempArr = new double [] {0,0,0,0,0};
- static double [] sumArr = new double [] {0,0,0,0,0};
- //functions
- static void say(String msg){
- System.out.println(msg);
- }
- static void sayd(double n){
- System.out.println(n);
- }
- static int aski(String prompt){
- say(prompt);
- return scan.nextInt();
- }
- static double askd(String prompt ){
- say(prompt);
- return scan.nextDouble();
- }
- static void evenOut() {
- if(mainArr.length>tempArr.length) {
- sumArr = new double [mainArr.length];
- temptempArr = new double [mainArr.length];
- for(int i = 0; i < tempArr.length ; i++) {
- temptempArr[i] = tempArr [i];
- }//end for
- }//end if
- else if(mainArr.length<tempArr.length) {
- sumArr = new double [tempArr.length];
- temptempArr = new double [tempArr.length];
- for(int i = 0; i < mainArr.length ; i++) {
- temptempArr[i] = mainArr [i];
- }//end for
- }// end else if
- }//end evenOut
- static void addArr() {
- if(mainArr.length>tempArr.length){
- for(int i = 0; i < mainArr.length ; i++) {
- sumArr[i] = mainArr[i] + temptempArr [i];
- }
- else if(mainArr.length<tempArr.length){
- for(int i = 0; i < tempArr.length ; i++) {
- sumArr[i] = temptempArr[i] + tempArr [i];
- }
- else{
- for(int i = 0; i < mainArr.length ; i++) {
- sumArr[i] = mainArr[i] + tempArr [i];
- }
- }
- }
- static void subArr() {
- for(int i = 0; i < mainArr.length ; i++) {
- sumArr[i] = mainArr[i] - tempArr [i];
- }
- }
- static void scaleMain() {
- double factor = askd("What would you like to multiply each element of mainArr by?");
- for(int i = 0; i < mainArr.length; i++) {
- mainArr[i] *= factor;
- }
- }
- static void scaleTemp() {
- double factor = askd("What would you like to multiply each element of tempArr by?");
- for(int i = 0; i < tempArr.length; i++) {
- tempArr[i] *= factor;
- }
- }
- static void fillMain () {
- int amnt = aski("\nHow many elements would you like to put into the mainArr?");
- mainArr = new double [amnt];
- say("Set element");
- for(int i = 0; i < mainArr.length; i++) {
- double n = askd ((i+1)+" to:");
- mainArr[i] = n;
- }
- }
- static void fillTemp () {
- int amnt = aski("\nHow many elements would you like to put into the tempArr?");
- tempArr = new double [amnt];
- say("Set element");
- for(int i = 0; i < tempArr.length; i++) {
- double n = askd ((i+1)+" to:");
- tempArr[i] = n;
- }
- }
- static void sayArr(double[] n) {
- for (int i = 0; i < n.length ; i++) {
- sayd(n[i]);
- }
- }
- static void arrayLength () {
- if(mainArr.length == tempArr.length) {
- say("\nThe arrays are equal\n");
- }
- else {
- say("\nThe arrays are NOT equal\n");
- }
- }
- static int state = 1;
- public static void main(String[] args) {
- //intro
- say("Welcome to ArrayCalc");
- thewhileloop:
- while(true) {
- say("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
- //options to choose from
- String[] menuOptions = new String[] {
- "see the elements of the main and temp arrays",
- "fill the mainArr with new values",
- "fill the tempArr with new values",
- "add the values of both arrays",
- "subtract the values of both arrays by eachother",
- "scale the values of mainArr \n(multiply each element of the array by a factor)",
- "scale the values of tempArr",
- "check if the lengths of main and temp arrays are equal"
- };
- //display menu selections from an array with a for loop
- for (int i = 0; i < menuOptions.length; i++){
- say("Type "+(i+1)+" to "+menuOptions[i]+"\n");
- }
- say("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
- //selecting the operation to perform
- try {
- int select = aski("Input a number from 1 - " + menuOptions.length + " to select an operation \nType in \"99\" to exit the program")-1;
- //make a decision based on the input
- selectloop:
- switch (select){
- case 0:
- say("you selected to "+menuOptions[0]);
- say("mainArr:");
- sayArr(mainArr);
- say("tempArr:");
- sayArr(tempArr);
- state = aski("Type in \"1\" to go back to the menu, or \"2\" to exit the program")-1;
- switch(state) {
- case 0:
- break selectloop;
- case 1:
- break thewhileloop;
- default:
- say("you need to type in either \"1\", or \"2\"");
- }
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- case 1:
- say("you selected to "+menuOptions[1]);
- fillMain();
- state = aski("Type in \"1\" to go back to the menu, or \"2\" to exit the program")-1;
- switch(state) {
- case 0:
- break selectloop;
- case 1:
- break thewhileloop;
- default:
- say("you need to type in either \"1\", or \"2\"");
- };
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- case 2:
- say("you selected to "+menuOptions[2]);
- fillTemp();
- state = aski("Type in \"1\" to go back to the menu, or \"2\" to exit the program")-1;
- switch(state) {
- case 0:
- break selectloop;
- case 1:
- break thewhileloop;
- default:
- say("you need to type in either \"1\", or \"2\"");
- }
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- case 3:
- say("you selected to "+menuOptions[3]);
- evenOut();
- addArr();
- sayArr(sumArr);
- state = aski("Type in \"1\" to go back to the menu, or \"2\" to exit the program")-1;
- switch(state) {
- case 0:
- break selectloop;
- case 1:
- break thewhileloop;
- default:
- say("you need to type in either \"1\", or \"2\"");
- }
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- case 4:
- say("you selected to "+menuOptions[4]);
- evenOut();
- subArr();
- sayArr(sumArr);
- state = aski("Type in \"1\" to go back to the menu, or \"2\" to exit the program")-1;
- switch(state) {
- case 0:
- break selectloop;
- case 1:
- break thewhileloop;
- default:
- say("you need to type in either \"1\", or \"2\"");
- }
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- case 5:
- say("you selected to "+menuOptions[5]);
- scaleMain();
- state = aski("Type in \"1\" to go back to the menu, or \"2\" to exit the program")-1;
- switch(state) {
- case 0:
- break selectloop;
- case 1:
- break thewhileloop;
- default:
- say("you need to type in either \"1\", or \"2\"");
- }
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- case 6:
- say("you selected to "+menuOptions[6]);
- scaleTemp();
- state = aski("Type in \"1\" to go back to the menu, or \"2\" to exit the program")-1;
- switch(state) {
- case 0:
- break selectloop;
- case 1:
- break thewhileloop;
- default:
- say("you need to type in either \"1\", or \"2\"");
- }
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- case 7:
- say("you selected to "+menuOptions[6]);
- arrayLength();
- state = aski("Type in \"1\" to go back to the menu, or \"2\" to exit the program")-1;
- switch(state) {
- case 0:
- break selectloop;
- case 1:
- break thewhileloop;
- default:
- say("you need to type in either \"1\", or \"2\"");
- }
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- case 98:
- break thewhileloop;
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- default:
- say("You needed to choose a number from 1 to "+ menuOptions.length + "\n");
- state = aski("Type in \"1\" to go back to the menu, or \"2\" to exit the program")-1;
- switch(state) {
- case 0:
- break selectloop;
- case 1:
- break thewhileloop;
- default:
- say("you need to type in either \"1\", or \"2\"");
- }
- }//end switch(select)
- }//end try
- catch(Exception e) {
- scan.nextLine();
- long timer1 =0;
- long timer2 =0;
- long tottime = 0;
- say("Sorry, but that's an invalid character\n\nreturning to menu in:");
- int time = 3;
- timer1 = System.currentTimeMillis();
- while(time >= 0) {
- tottime = timer2 - timer1;
- timer2= System.currentTimeMillis();
- if(tottime>=500) {
- if(time >0) {
- say(""+time);
- }
- timer1= System.currentTimeMillis();
- time--;
- }
- }//end while
- }//end catch
- }//end while
- say("This arrayCalc program was coded by Namu \nThank you for your time");
- } //end main
- }
Add Comment
Please, Sign In to add comment