Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In this assignment, you will make another calculator. This one will work on arrays rather than single values. Like the basic calculator from the previous

In this assignment, you will make another calculator. This one will work on arrays rather

than single values. Like the basic calculator from the previous assignments, the array

calculator should allow users to add, subtract, multiply and divide the corresponding

values in two arrays. It should also allow users to generate a random array. Finally,

there is a special value that can be computed for two arrays called the dot product that

your calculator should also be able to compute. You can find the definition of the

algebraic dot product of two arrays by googling (Wikipedia is a good choice). Briefly, the

dot product of two arrays is the sum of the product of the corresponding values in the

arrays.

For example, the dot product of [1, 5, 8, 2] and [2, 4, 1, 3] is:

(1 * 2) + (5 * 4) + (8 * 1) + (2 * 3)

which is 36.

Your array should have the same methods as the calculator from Assignment 4, plus a

method to compute the dot product. These methods should deal with double arrays

rather than plain doubles, however. Here is a list of the method signatures:

public static int getMenuOption()

public static double[] getOperand(String prompt, int size);

public static double getOperand(String prompt);

public static double[] add(double[] operand1, double[] operand2)

public static double[] subtract(double[] operand1, double[] operand2)

public static double[] multiply(double[] operand1, double[] operand2)

public static double[] divide(double[] operand1, double[] operand2)

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

public static double dotProduct(double[] operand1, double[] operand2)

You will need two versions of the getOperand method. This is an example of method

overloading. The 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 other version does the same thing as in the Fourth

Assignment Calculator with Methods it displays a prompt and reads and returns a

double value. This is needed to get the inputs for the random method. The random

method will need one additional parameter the size of the random array that should be

generated. Note that the dotProduct method only needs to return a single double value

rather than an array.

Here is an example run of the program:

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]

image text in transcribedimage text in transcribed

Here is my code so far return operandi-operand2; import iavatSRDDBc public static double mutiplyidouble operand1, double operand2k return operand1 operand2; public static int getMe00 int option; public static double divide double operandi, double operand2 while(true) return operand1/operand2; vatensutRriotol"Menuin Addin2. Subtract n3. Multiplyin4. Divide n5. Generate public static double random double lwertinit, double yeperinit Random r new Random return (double ltoextintintupetinit-ewadtiwit))+lewerlimit random numberin6. Quit") vstem.sut priotiol"what would you like to do?"; Scanner s new ScannerlSystem.inl; public static void mainjstring a) return option; else SvstensutRtiotolm sorry, "+option+" wasn't one of the options" public static double gst8sandString prompt) vstemsut Rriatio prompt) Scanner genew ScannerSystem.in; case 1 operandi- gatceerand What is the first number?") operand2 ESTQRROd What is the second number?" break case 2 operandi-SSOR 0d("What is the first number?"); operand2 Btoascand What is the second number?l public static double addidouble operandi, double operand2) return operand1+operand2; break public static double subtract(double operandi, double operand2 case 3 operandi gstkeerandu what is the first number?") operand2 gstkeerand what is the second number?") System.qut.printin Answer: "+multiplyfoperand1 operand2) break; case 4 operandi gstkeerandu what is the first number?") operand2 gatoperand What is the second number?"l; if(operand2 Evstem.sutriothsm sorry, you can't divide by zero." auatem.outpciator Answer: ba return else System.qut.printin"Answer: "+divide(operand1 operand2)) break; case 5 loxwertimit gatoperandWhat is the lower limit?" yRRECLiit getteerod What is the upper limit?") Evstem.out priotin Answer: "+random(lwertimit uppartimit)) break; tautciottol Goodbye" break

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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

Recommended Textbook for

Database And Expert Systems Applications 22nd International Conference Dexa 2011 Toulouse France August/September 2011 Proceedings Part 1 Lncs 6860

Authors: Abdelkader Hameurlain ,Stephen W. Liddle ,Klaus-Dieter Schewe ,Xiaofang Zhou

2011th Edition

3642230873, 978-3642230875

More Books

Students also viewed these Databases questions