Question
Java Code I am trying to get the results to print within the [ ] for each response except for the quitting part. Below is
Java Code I am trying to get the results to print within the [ ] for each response except for the quitting part. Below is my code.
import java.util.*;
public class Calculator {
public static void main(String[] args){
Scanner choose = new Scanner(System.in);
Random input = new Random();
int choice=0;
int size;
String prompt;
boolean right = false;
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? ");
num1=getOperand("Enter the values in the first array, separated by spaces: ",size);
num2=getOperand("Enter the values in the second array, separated by spaces: ",size);
add(num1,num2);
break;
case 2:
size=getOperand("How many values are in the arrays? ");
num1=getOperand("Enter the values in the first array, separated by spaces: ",size);
num2=getOperand("Enter the values in the second array, separated by spaces: ",size);
subtract(num1,num2);
break;
case 3:
size=getOperand("How many values are in the arrays? ");
num1=getOperand("Enter the values in the first array, separated by spaces: ",size);
num2=getOperand("Enter the values in the second array, separated by spaces: ",size);
multiply(num1,num2);
break;
case 4:
size=getOperand("How many values are in the arrays? ");
num1=getOperand("Enter the values in the first array, separated by spaces: ",size);
num2=getOperand("Enter the values in the second array, separated by spaces: ",size);
divide(num1,num2);
break;
case 5:
size=getOperand("How many values are in the arrays? ");
num1=getOperand("Enter the values in the first array, separated by spaces: ",size);
num2=getOperand("Enter the values in the second array, separated by spaces: ",size);
dotProduct(num1,num2);
break;
case 6:
size=getOperand("How many values should be in the random array? ");
int lowerLimit=getOperand("What is the lower limit for the random number? ");
int upperLimit=getOperand("What is the upper limit for the random number? ");
random(lowerLimit,upperLimit,size);
break;
case 7:
System.out.println("Good Bye");
break;
}
}else{
right=true;
System.out.println("I'm Sorry,"+choice+" is invalid");
}
}while(choice!=7);
}
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=new Scanner(System.in);
System.out.print(prompt);
double option[]=new double[size];
for(int i=0;i option[i]=choose.nextDouble(); } return option; } public static int getOperand(String prompt){ Scanner choose=new Scanner(System.in); System.out.print(prompt); int option=choose.nextInt(); return option; } public static double[] add(double[] num1,double[] num2){ Scanner choose=new Scanner(System.in); double[] answer=new double[num1.length]; System.out.println("The result is [ ] "); for(int i=0;i answer[i]=num1[i]+num2[i]; System.out.println(answer[i]); } return answer; } public static double[] subtract(double[] num1,double[] num2){ Scanner choose=new Scanner(System.in); double[] answer=new double[num1.length]; System.out.println("The result is [ ] "); for(int i=0;i answer[i]=num1[i]-num2[i]; System.out.println(answer[i]); } return answer; } public static double[] multiply(double[] num1,double[] num2){ Scanner choose=new Scanner(System.in); double[] answer=new double[num1.length]; System.out.println("The result is[ ] "); for(int i=0;i answer[i]=num1[i]*num2[i]; System.out.println(answer[i]); } return answer; } public static double[] divide(double[] num1,double[] num2){ Scanner choose=new Scanner(System.in); double[] answer=new double[num1.length]; System.out.println("The result is [ ] "); for(int i=0;i answer[i]=num1[i]/num2[i]; if(num2[i]!=0) { System.out.println(answer[i]); } if(num2[i]==0) { System.out.println("Nan"); } } return answer; } public static double dotProduct(double[] num1,double[] num2){ Scanner choose=new Scanner(System.in); double[] answer=new double[num1.length]; System.out.println("The result is [ ] "); double sum=0; for(int i=0;i answer[i]=num1[i]*num2[i]; sum=sum+answer[i]; } System.out.println(sum); return sum; } public static int[] random(int lowerLimit,int upperLimit,int size){ Scanner choose=new Scanner(System.in); Random rand=new Random(); int[] randNum=new int[size]; System.out.println("The result is [ ] "); for(int i=0;i randNum[i]=rand.nextInt(upperLimit)+lowerLimit; System.out.println(randNum[i]); } return randNum; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started