-1

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);
            }
        }
    }
}
PakkuDon
  • 1,627
  • 4
  • 22
  • 21
user3221816
  • 135
  • 2
  • 2
  • 10

1 Answers1

1

I will gave you an example for the addition. Change you addition function to:

public static double[] addition(double[] one,double[] two){
    double[] resultset = new double[one.length];  //creat new array for the results
    for(int i = 0; i<one.length; i++)             //iterate over the arrays to calculate the results
        resultset[i] = one[i] + two[i];           //calculate the result
    return resultset;                             //return resultset array
}

In main replace the two doubles with two arrays and creat a size variable:

    double[] array1 = null;
    double[] array2 = null;
    double upperLimit = 0.0;
    double lowerLimit = 0.0;
    int arraysize = 0;

in main where you read the two ints, make for-loops to read the two arrays:

        if(menuOption == 1 || menuOption == 2 || menuOption == 3 || menuOption == 4){
            //arraysize
            System.out.println("How many values are in the arrays?");
            arraysize = input.nextInt();
            //1st array
            array1 = new double[arraysize];
            System.out.println("Enter the values in the first array, separated by spaces: ");
            input.nextLine();       //consume whitespaces
            String number = input.nextLine();
            for(int i = 0; i<arraysize; i++)
                array1[i] = Integer.parseInt(number.split(" ")[i]);  //split the string at the whitespaces and convert it to an int
            //2nd array
            array2 = new double[arraysize];
            System.out.println("Enter the values in the secound array, separated by spaces: ");
            number = input.nextLine();
            for(int i = 0; i<arraysize; i++)
                array2[i] = Integer.parseInt(number.split(" ")[i]);
        }

and the last step is printing out the result:

        if(menuOption == 1){
            double[] results = addition(array1,array2);
            for(int i = 0; i<arraysize; i++)
                System.out.print(results[i]+" ");
            menuOption=getMenuOption();
        }

Example Input:

MENU
1. +
2. -
3. *
4. /
5. Generate a random number.
6. 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: 
1 2 3
Enter the values in the secound array, separated by spaces: 
4 5 6

Output:

5.0 7.0 9.0

Now its your turn. You only have to creat the other calculation functions and the input will be parsed to an int atm. change it to double.

kai
  • 6,702
  • 22
  • 38
  • Thank you for the point in the right direction. This helped a lot and it turned out great. Much appreciated. – user3221816 Feb 10 '14 at 08:38
  • Actually, the only thing I don't understand is why this line was necessary: – user3221816 Feb 10 '14 at 09:00
  • input.nextLine(); //consume whitespaces I realize it won't work without it, but I do not fully understand why. – user3221816 Feb 10 '14 at 09:00
  • its because the input.nextInt(); it only reads the int value. Look at this: http://stackoverflow.com/questions/7056749/scanner-issue-when-using-nextline-after-nextxxx – kai Feb 11 '14 at 07:11