I am very new to programming and I have a problem that I just cannot figure out. Last week I made a menu calculator that called methods to perform arithmetic on double values or to pick a random number. This week I need to change that it so that instead of double values, it performs arithmetic on arrays. Here is an example of what it is supposed to do.
Menu
1. Add
2. Subtract
3. Multiply
4. Divide 5. Dot product
6. Generate random array
7. Quit
What would you like to do? 1
How many values are in the arrays? 3
Enter the values in the first array, separated by spaces:
2 4 6
Enter the values in the second array, separated by spaces:
1 3 5
The result is [3.0, 7.0, 11.0]
This doesn't look like it should be very complicated but I keep getting errors, and when I try to fix them I just get more errors. Things are spiraling out of control and I just have a mess on my hands now, so I think I need to start over. This is what the code for my method calculator looks like. If anyone could give me some hint as to where to begin, I would greatly appreciate it.
Thank you very much if you decide to take the time to read through this mess.
import java.util.Scanner;
public class methodCalculator{
public static int getMenuOption(){
Scanner input = new Scanner(System.in);
int invalid=0;
while (invalid<3){
System.out.println("MENU");
System.out.println("1. +");
System.out.println("2. -");
System.out.println("3. *");
System.out.println("4. /");
System.out.println("5. Generate a random number.");
System.out.println("6. Quit");
System.out.println("What would you like to do?");
int menuSelect = input.nextInt();
//Selects an item from the menu
if (menuSelect==1||menuSelect==2||menuSelect==3||menuSelect==4||menuSelect==5||menuSelect==6){
return(menuSelect);
}
else{
System.out.println("Sorry, "+menuSelect+" is not an option.");
invalid++;
}
}
System.out.println("Too many invalid inputs. Try again later");
System.exit(0);
return(0);
}
public static double getOperand(String prompt){
System.out.println(prompt);
Scanner input = new Scanner(System.in);
return(input.nextDouble());
}
public static double addition(double one,double two){
return(one+two);
}
public static double subtraction(double one,double two){
return(one-two);
}
public static double multiplication(double one, double two){
return(one*two);
}
public static double division(double one, double two){
if(two != 0){
return(one/two);
}
else {
return(Double.NaN);
}
}
public static double random(double upper,double lower){
double upperLimit = getOperand("Enter upper limit.");
double lowerLimit = getOperand("Enter lower limit.");
double randomVal = (lowerLimit + (int)(Math.random() * ((upperLimit - lowerLimit)+1)));
return(randomVal);
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double numONE = 0.0;
double numTWO = 0.0;
double upperLimit = 0.0;
double lowerLimit = 0.0;
int menuOption = getMenuOption();
while(menuOption == 1 || menuOption == 2 || menuOption == 3 || menuOption == 4||menuOption==5||menuOption==6){
if(menuOption == 1 || menuOption == 2 || menuOption == 3 || menuOption == 4){
numONE=getOperand("Enter the first number.");
//Stores input as numONE
numTWO=getOperand("Enter the second number.");
//Stores input as numTWO
}
if(menuOption == 1){
double sum= addition(numONE,numTWO);
System.out.println(sum);
//Adds two numbers
menuOption=getMenuOption();
}
else if(menuOption == 2){
double difference=subtraction(numONE,numTWO);
System.out.println(difference);
//Subtracts second number from first number
menuOption=getMenuOption();
}
else if(menuOption == 3){
double product=multiplication(numONE,numTWO);
System.out.println(product);
//Multiplies two numbers
menuOption=getMenuOption();
}
else if(menuOption == 4){
double quotient=division(numONE,numTWO);
System.out.println(quotient);
menuOption=getMenuOption();
}
else if(menuOption == 5){
double randomVal=random(numONE,numTWO);
System.out.println(randomVal);
//Displays a random integer between an upper and a lower limit
menuOption=getMenuOption();
}
else if(menuOption==6){
System.out.println("Goodbye");
System.exit(0);
}
}
}
}