Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java Assignment If the user attempts to divide by 0, my divide method is suppose

ID: 3894135 • Letter: J

Question

Java Assignment

If the user attempts to divide by 0, my divide method is supposed to prevent the division by 0 which is undefined and then assign Double.NaN to the relevan array element.

I also need to have comma's in between each of the responses for each part of the array answer.

Here is my code.

import java.util.*;

public class Calculator {

public static void main(String[] args) {

Scanner choose = new Scanner(System.in);

int choice = 0;

int size;

double num1[], num2[];

do {

choice = getMenuOption(choose);

if ((choice <= 7) && (choice > 0)) {

switch (choice)

{

case 1:

size = getOperand("How many values are in the arrays? ", choose);

num1 = getOperand("Enter the values in the first array, separated by spaces: ", size, choose);

num2 = getOperand("Enter the values in the second array, separated by spaces: ", size, choose);

add(num1, num2);

break;

case 2:

size = getOperand("How many values are in the arrays? ", choose);

num1 = getOperand("Enter the values in the first array, separated by spaces: ", size, choose);

num2 = getOperand("Enter the values in the second array, separated by spaces: ", size, choose);

subtract(num1, num2);

break;

case 3:

size = getOperand("How many values are in the arrays? ", choose);

num1 = getOperand("Enter the values in the first array, separated by spaces: ", size, choose);

num2 = getOperand("Enter the values in the second array, separated by spaces: ", size, choose);

multiply(num1, num2);

break;

case 4:

size = getOperand("How many values are in the arrays? ", choose);

num1 = getOperand("Enter the values in the first array, separated by spaces: ", size, choose);

num2 = getOperand("Enter the values in the second array, separated by spaces: ", size, choose);

divide(num1, num2);

break;

case 5:

size = getOperand("How many values are in the arrays? ", choose);

num1 = getOperand("Enter the values in the first array, separated by spaces: ", size, choose);

num2 = getOperand("Enter the values in the second array, separated by spaces: ", size, choose);

dotProduct(num1, num2);

break;

case 6:

size = getOperand("How many values should be in the random array? ", choose);

int lowerLimit = getOperand("What is the lower limit for the random number? ", choose);

int upperLimit = getOperand("What is the upper limit for the random number? ", choose);

random(lowerLimit, upperLimit, size);

break;

case 7:

System.out.println("Good Bye");

break;

}

} else {

System.out.println("I'm Sorry," + choice + " is invalid");

}

} while (choice != 7);

choose.close();

}

public static int getMenuOption(Scanner choose) {

int choice;

System.out.println(" ");

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. Quit ");

System.out.println(" ");

System.out.println("What would you like to do? ");

choice = choose.nextInt();

return choice;

}

public static double[] getOperand(String prompt, int size, Scanner choose) {

System.out.print(prompt);

double option[] = new double[size];

for (int i = 0; i < size; i++) {

option[i] = choose.nextDouble();

}

return option;

}

public static int getOperand(String prompt, Scanner choose) {

System.out.print(prompt);

int option = choose.nextInt();

return option;

}

public static double[] add(double[] num1, double[] num2) {

double[] answer = new double[num1.length];

System.out.print("The result is [ ");

if (num1.length == num2.length) {

for (int i = 0; i < num1.length; i++) {

answer[i] = num1[i] + num2[i];

System.out.print(answer[i] + " ");

}

} else {

System.out.println("Invalid length of arrays - Addition operation cannot be performed");

}

System.out.print("] ");

return answer;

}

public static double[] subtract(double[] num1, double[] num2) {

double[] answer = new double[num1.length];

System.out.print("The result is [ ");

if (num1.length == num2.length) {

for (int i = 0; i < num1.length; i++) {

answer[i] = num1[i] - num2[i];

System.out.print(answer[i] + " ");

}

} else {

System.out.println("Invalid length of arrays - Subtraction operation cannot be performed");

}

System.out.print("] ");

return answer;

}

public static double[] multiply(double[] num1, double[] num2) {

double[] answer = new double[num1.length];

System.out.print("The result is [ ");

if (num1.length == num2.length) {

for (int i = 0; i < num1.length; i++) {

answer[i] = num1[i] * num2[i];

System.out.print(answer[i] + " ");

}

} else {

System.out.println("Invalid length of arrays - Multiplication operation cannot be performed");

}

System.out.print("] ");

return answer;

}

public static double[] divide(double[] num1, double[] num2) {

Scanner choose = new Scanner(System.in);

double[] answer = new double[num1.length];

System.out.print("The result is [ ");

if (num1.length == num2.length) {

for (int i = 0; i < num1.length; i++) {

answer[i] = num1[i] / num2[i];

if (num2[i] != 0)

{

System.out.print(answer[i] + " ");

}

if (num2[i] == 0)

{

System.out.print("NaN ");

}

}

} else {

System.out.println("Invalid length of arrays - Division operation cannot be performed");

}

System.out.print(" ] ");

return answer;

}

public static double dotProduct(double[] num1, double[] num2) {

double[] answer = new double[num1.length];

System.out.print("The result is ");

double sum = 0;

if (num1.length == num2.length) {

for (int i = 0; i < num1.length; i++) {

answer[i] = num1[i] * num2[i];

sum = sum + answer[i];

}

} else {

System.out.println("Invalid length of arrays - Dot product operation cannot be performed");

}

System.out.print(sum);

return sum;

}

public static double[] random(int lowerLimit, int upperLimit, int size) {

Random rand = new Random();

double[] randNum = new double[size];

System.out.print("The result is [ ");

for (int i = 0; i < size; i++) {

randNum[i] = lowerLimit + (upperLimit - lowerLimit) * rand.nextDouble();

System.out.print(randNum[i] + " ");

}

System.out.print(" ] ");

return randNum;

}

}

Explanation / Answer


Given below is the modified code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you

import java.util.*;
public class Calculator2 {
public static void main(String[] args) {
Scanner choose = new Scanner(System.in);
int choice = 0;
int size;
double num1[], num2[];
do {
choice = getMenuOption(choose);
if ((choice <= 7) && (choice > 0)) {
switch (choice)
{
case 1:
size = getOperand("How many values are in the arrays? ", choose);
num1 = getOperand("Enter the values in the first array, separated by spaces: ", size, choose);
num2 = getOperand("Enter the values in the second array, separated by spaces: ", size, choose);
add(num1, num2);
break;
case 2:
size = getOperand("How many values are in the arrays? ", choose);
num1 = getOperand("Enter the values in the first array, separated by spaces: ", size, choose);
num2 = getOperand("Enter the values in the second array, separated by spaces: ", size, choose);
subtract(num1, num2);
break;
case 3:
size = getOperand("How many values are in the arrays? ", choose);
num1 = getOperand("Enter the values in the first array, separated by spaces: ", size, choose);
num2 = getOperand("Enter the values in the second array, separated by spaces: ", size, choose);
multiply(num1, num2);
break;
case 4:
size = getOperand("How many values are in the arrays? ", choose);
num1 = getOperand("Enter the values in the first array, separated by spaces: ", size, choose);
num2 = getOperand("Enter the values in the second array, separated by spaces: ", size, choose);
divide(num1, num2);
break;
case 5:
size = getOperand("How many values are in the arrays? ", choose);
num1 = getOperand("Enter the values in the first array, separated by spaces: ", size, choose);
num2 = getOperand("Enter the values in the second array, separated by spaces: ", size, choose);
dotProduct(num1, num2);
break;
case 6:
size = getOperand("How many values should be in the random array? ", choose);
int lowerLimit = getOperand("What is the lower limit for the random number? ", choose);
int upperLimit = getOperand("What is the upper limit for the random number? ", choose);
random(lowerLimit, upperLimit, size);
break;
case 7:
System.out.println("Good Bye");
break;
}
} else {
System.out.println("I'm Sorry," + choice + " is invalid");
}
} while (choice != 7);
choose.close();
}
public static int getMenuOption(Scanner choose) {
int choice;
System.out.println(" ");
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. Quit ");
System.out.println(" ");
System.out.println("What would you like to do? ");
choice = choose.nextInt();
return choice;
}
public static double[] getOperand(String prompt, int size, Scanner choose) {
System.out.print(prompt);
double option[] = new double[size];
for (int i = 0; i < size; i++) {
option[i] = choose.nextDouble();
}
return option;
}
public static int getOperand(String prompt, Scanner choose) {
System.out.print(prompt);
int option = choose.nextInt();
return option;
}
public static double[] add(double[] num1, double[] num2) {
double[] answer = new double[num1.length];
System.out.print("The result is [ ");
if (num1.length == num2.length) {
for (int i = 0; i < num1.length; i++) {
answer[i] = num1[i] + num2[i];
if(i != 0)
System.out.print(", ");
System.out.print(answer[i] );
}
} else {
System.out.println("Invalid length of arrays - Addition operation cannot be performed");
}
System.out.print("] ");
return answer;
}
public static double[] subtract(double[] num1, double[] num2) {
double[] answer = new double[num1.length];
System.out.print("The result is [ ");
if (num1.length == num2.length) {
for (int i = 0; i < num1.length; i++) {
answer[i] = num1[i] - num2[i];
if(i != 0)
System.out.print(", ");
System.out.print(answer[i] );
}
} else {
System.out.println("Invalid length of arrays - Subtraction operation cannot be performed");
}
System.out.print("] ");
return answer;
}
public static double[] multiply(double[] num1, double[] num2) {
double[] answer = new double[num1.length];
System.out.print("The result is [ ");
if (num1.length == num2.length) {
for (int i = 0; i < num1.length; i++) {
answer[i] = num1[i] * num2[i];
if(i != 0)
System.out.print(", ");
System.out.print(answer[i] );
}
} else {
System.out.println("Invalid length of arrays - Multiplication operation cannot be performed");
}
System.out.print("] ");
return answer;
}
public static double[] divide(double[] num1, double[] num2) {
Scanner choose = new Scanner(System.in);
double[] answer = new double[num1.length];
System.out.print("The result is [ ");
if (num1.length == num2.length) {
for (int i = 0; i < num1.length; i++) {
if(num2[i] == 0)
answer[i] = Double.NaN;
else
answer[i] = num1[i] / num2[i];
if(i != 0)
System.out.print(", ");
System.out.print(answer[i] );
}
} else {
System.out.println("Invalid length of arrays - Division operation cannot be performed");
}
System.out.print(" ] ");
return answer;
}
public static double dotProduct(double[] num1, double[] num2) {
double[] answer = new double[num1.length];
System.out.print("The result is ");
double sum = 0;
if (num1.length == num2.length) {
for (int i = 0; i < num1.length; i++) {
answer[i] = num1[i] * num2[i];
sum = sum + answer[i];
}
} else {
System.out.println("Invalid length of arrays - Dot product operation cannot be performed");
}
System.out.print(sum);
return sum;
}
public static double[] random(int lowerLimit, int upperLimit, int size) {
Random rand = new Random();
double[] randNum = new double[size];
System.out.print("The result is [ ");
for (int i = 0; i < size; i++) {
randNum[i] = lowerLimit + (upperLimit - lowerLimit) * rand.nextDouble();
System.out.print(randNum[i] + " ");
}
System.out.print(" ] ");
return randNum;
}
}


output
=====
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?
2
Enter the values in the first array, separated by spaces:
3 4
Enter the values in the second array, separated by spaces:
5 6
The result is [ 8.0, 10.0]  
Menu
1. Add
2. Subtract
3. Multiply
4. Divide
5. Dot Product
6. Generate Random array
7. Quit
What would you like to do?
4
How many values are in the arrays?
2
Enter the values in the first array, separated by spaces:
10 20
Enter the values in the second array, separated by spaces:
2 5
The result is [ 5.0, 4.0 ]  
Menu
1. Add
2. Subtract
3. Multiply
4. Divide
5. Dot Product
6. Generate Random array
7. Quit
What would you like to do?
4
How many values are in the arrays?
2
Enter the values in the first array, separated by spaces:
10 20
Enter the values in the second array, separated by spaces:
5 0
The result is [ 2.0, NaN ]  
Menu
1. Add
2. Subtract
3. Multiply
4. Divide
5. Dot Product
6. Generate Random array
7. Quit
What would you like to do?
7
Good Bye

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote