// ADDING
case 1:
double[] num1 = new double[sizeArray];
double[] num2 = new double[sizeArray];
double[] total = new double[sizeArray];
double aNum1, aNum2, aTotal;
System.out.println("How many values are in the
arrays: ");
sizeArray = keyboard.nextInt();
// ADDS JUST 1 SET OF NUMBERS
if (sizeArray == 1){
aNum1 = getOperand("what is the 1st number to
add: ");
aNum2 = getOperand("what is the 2nd number
to add: ");
// ADDS MULTIPLE SETS OF NUMBERS
else{
num1 = getOperand("Enter the values in the first
array, seperated by spaces: ", sizeArray);
num2 = getOperand("Enter the values in the second
array, seperated by spaces: ", sizeArray);
total = add(num1, num2);
if (counter != 0){
counter = 0;
}
System.out.print("The result is [");
for (int i = 0; i < num1.length - 1; i++){
System.out.print(total[i] + ", ");
count = i;
}
// SUBTRACT MULTIPLE SETS OF NUMBERS
else{
num3 = getOperand("Enter the values in the first
array, seperated by spaces: ", sizeArray);
num4 = getOperand("Enter the values in the second
array, seperated by spaces: ", sizeArray);
total2 = subtract(num3, num4);
if (counter != 0){
counter = 0;
}
System.out.print("The result is [");
for (int i = 0; i < num3.length - 1; i++){
System.out.print(total2[i] + ", ");
count = i;
}
// MULTIPLYING
case 3:
double[] num5 = new double[sizeArray];
double[] num6 = new double[sizeArray];
double[] total3 = new double[sizeArray];
double mNum1 = 0, mNum2 = 0, mTotal = 0;
System.out.println("How many values are in the
arrays: ");
sizeArray = keyboard.nextInt();
// MULTIPLY JUST 1 SET OF NUMBERS
if (sizeArray == 1){
mNum1 = getOperand("what is the 1st number
to multiply: ");
mNum2 = getOperand("what is the 2nd number
to multiply: ");
// MULTIPLY MULTIPLE SETS OF NUMBERS
else{
num5 = getOperand("Enter the values in the first
array, seperated by spaces: ", sizeArray);
num6 = getOperand("Enter the values in the second
array, seperated by spaces: ", sizeArray);
total3 = multiply(num5, num6);
if (counter != 0){
counter = 0;
}
System.out.print("The result is [");
for (int i = 0; i < num5.length - 1; i++){
System.out.print(total3[i] + ", ");
count = i;
}
// DIVIDING
case 4:
boolean dom = true;
double[] num7 = new double[sizeArray];
double[] num8 = new double[sizeArray];
double[] total4 = new double[sizeArray];
double dNum1, dNum2, dTotal;
System.out.println("How many values are in the
arrays: ");
sizeArray = keyboard.nextInt();
// DIVIDE JUST 1 SET OF NUMBERS
if (sizeArray == 1){
dNum1 = getOperand("what is the 1st number to
divide: ");
dNum2 = getOperand("what is the 2nd number
to divide: ");
// DENOMINATOR = 0, THIS WILL TRIGGER A
ERROR MESSAGE
if (dNum2 > 0){
num7 = getOperand("Enter the values in the first
array, seperated by spaces: ", sizeArray);
num8 = getOperand("Enter the values in the second
array, seperated by spaces: ", sizeArray);
// DENOMINATOR = 0, THIS WILL TRIGGER A
ERROR MESSAGE
for (int i = 0; i < num8.length; i++){
if (num8[i] < 1){
dom = false;
}
}
if (dom == true){
total4 = divide(num7, num8);
System.out.print("The result is [");
for (int i = 0; i < num7.length - 1; i++){
System.out.print(total4[i] + ", ");
count = i;
}
else{
System.out.println("Sorry you can't divide by
0!!");
System.out.println("");
}
}
if (counter != 0){
counter = 0;
}
if (dom == false){
dom = true;
}
break;
// DOT PRODUCT
case 5:
System.out.println("How many values are in the
arrays: ");
sizeArray = keyboard.nextInt();
double[] num9 = new double[sizeArray];
double[] num10 = new double[sizeArray];
double total5;
double pNum1 = 0, pNum2 = 0, pTotal = 0;
// MULTIPLY JUST 1 SET OF NUMBERS
if (sizeArray == 1){
pNum1 = getOperand("what is the 1st number
for dot product: ");
pNum2 = getOperand("what is the 2nd number
for dot product: ");
pTotal = pNum1 * pNum2;
System.out.println("The result is: " + pTotal);
System.out.println();
}
// MULTIPLY MULTIPLE SETS OF NUMBERS
else{
num9 = getOperand("Enter the values in the first
array, seperated by spaces: ", sizeArray);
num10 = getOperand("Enter the values in the second
array, seperated by spaces: ", sizeArray);
total5 = dotProduct(num9, num10);
System.out.print("The result is: " + total5);
System.out.println();
}
if (counter != 0){
counter = 0;
}
break;
case 6:
double rNum1 = 0, rNum2 = 0, randNum;
int lnCounter = 0;
System.out.println("How many values are in the
arrays: ");
sizeArray = keyboard.nextInt();
double[] randNums = new double[sizeArray];
// GENERATE RANDOM NUMBER JUST 1 NUMBER
if (sizeArray == 1){
rNum1 = getOperand("what is the lower limit: ");
rNum2 = getOperand("what is the upper limit: ");
// DISPLAY STATEMENT IF USER PUTS WRONG
NUMBER IN
if (choice > 7 || choice < 1) {
counter++;
System.out.println("I'm sorry, " + choice + "
wasn't one of the options");
System.out.println("");
}
if (counter == 3 || choice == 7){
JOptionPane.showMessageDialog(null, "PLEASE
TRY AGAIN LATER");
break;
}
} while(choice != 7);
}
// METHOD TO SHOW THE MENU
public static int getMenuOption(){
Scanner keyboard = new Scanner(System.in);
System.out.println("========= MENU =========");
System.out.println("1. Add");
System.out.println("2. Subtract");
System.out.println("3. Multiply");
System.out.println("4. Divide");
System.out.println("5. Dot Product");
System.out.println("6. Generate Random Array");
System.out.println("7. Exit");
System.out.println("What would you like to do: ");
int choice = keyboard.nextInt();
return choice;
}
public static double[] getOperand(String prompt, int size){
double[] num = new double[size];
Scanner keyboard = new Scanner(System.in);
System.out.println(prompt);
for (int i = 0; i < size; i++){
num[i] = keyboard.nextDouble();
}
return num;
}
public static double getOperand(String prompt){
Scanner keyboard = new Scanner(System.in);
System.out.println(prompt);
double num = keyboard.nextDouble();
return num;
}
public static double[] add(double[] operand1, double[] operand2){
double[] total = new double[operand1.length];
for(int i = 0; i < operand1.length; i++){
total[i] = operand1[i] + operand2[i];
}
return total;
}
public static double[] subtract(double[] operand1, double[] operand2){
double[] total = new double[operand1.length];
for(int i = 0; i < operand1.length; i++){
total[i] = operand1[i] - operand2[i];
}
return total;
}
public static double[] multiply(double[] operand1, double[] operand2){
double[] total = new double[operand1.length];
for(int i = 0; i < operand1.length; i++){
total[i] = operand1[i] * operand2[i];
}
return total;
}
public static double[] divide(double[] operand1, double[] operand2){
double[] total = new double[operand1.length];
for(int i = 0; i < operand1.length; i++){
total[i] = operand1[i] / operand2[i];
}
return total;
}
public static double[] random(double lowerLimit, double upperLimit, int size){
double[] randNum = new double[size];
int decMult = 100;
for(int i = 0; i < size; i++){
randNum[i] = (double) (Math.random() * decMult + 1);
// KEEPS LOOPING TILL THE RANDOM NUMBER IS BETWEEN THE
// SELECTED NUMBERS BY THE USER
do {
// THIS WILL CHANGE THE DECIMAL SO THE USER CAN
CHANGE
// ABOVE 100
if (lowerLimit > 100){
decMult = 1000;
}
// THIS WILL CHANGE THE DECIMAL SO THE USER CAN
CHANGE
// ABOVE 1,000
if (lowerLimit > 1000){
decMult = 10000;
}
// THIS WILL CHANGE THE DECIMAL SO THE USER CAN
CHANGE
// ABOVE 10,000
if (lowerLimit > 10000){
decMult = 100000;
}
} while (randNum[i] < lowerLimit || randNum[i] > upperLimit);
}
return randNum;
}
public static double random(double lowerLimit, double upperLimit){
// THIS WILL CHANGE THE DECIMAL SO THE USER CAN
CHANGE
// ABOVE 100
if (lowerLimit > 100){
decMult = 1000;
}
// THIS WILL CHANGE THE DECIMAL SO THE USER CAN
CHANGE
// ABOVE 1,000
if (lowerLimit > 1000){
decMult = 10000;
}
// THIS WILL CHANGE THE DECIMAL SO THE USER CAN
CHANGE
// ABOVE 10,000
if (lowerLimit > 10000){
decMult = 100000;
}
} while (randomNumber < lowerLimit || randomNumber > upperLimit);
return randomNumber;
}
public static double dotProduct(double[] operand1, double[] operand2){