Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java Assignment I've got the code working but then realized I also have to have two versions of the getOperand method for method overloading. First

Java Assignment I've got the code working but then realized I also have to have two versions of the getOperand method for method overloading. First version will prompt the user for enough values to fill an array and return the array. This will be used to get the operands for the add, subtract, multiply, divide, and dotProduct method. The second will display a prompt, read what is entered and returns a double value. This is needed to get the inputs for the random method which will need one additional parameter for the size of the random array that should be generated.

Below is the code I have so far.

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.print("The result is [ ");

for(int i=0;i

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

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

}

System.out.print("] ");

return answer;

}

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

Scanner choose=new Scanner(System.in);

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

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

for(int i=0;i

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

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

}

System.out.print("] ");

return answer;

}

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

Scanner choose=new Scanner(System.in);

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

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

for(int i=0;i

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

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

}

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 [ ");

for(int i=0;i

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

if(num2[i]!=0)

{

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

}

if(num2[i]==0)

{

System.out.print("Nan ");

}

}

System.out.print(" ] ");

return answer;

}

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

Scanner choose=new Scanner(System.in);

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

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

double sum=0;

for(int i=0;i

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

sum=sum+answer[i];

}

System.out.print(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.print("The result is [ ");

for(int i=0;i

randNum[i]=rand.nextInt(upperLimit)+lowerLimit;

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

}

System.out.print(" ] ");

return randNum;

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions