kennith stomps

Ranch Hand

Posts: 160

posted 2 months ago

Curious as to the process a java coder would go through while going about writing this code, or any other code., simply some pointers on the process you do while solving codes, or laying out the format.

An Array Calculator

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

Copyright © 2017, 2018 Sinclair Community College. All Rights Reserved.

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]

Menu

1. Add

2. Subtract

3. Multiply

4. Divide

5. Dot product

6. Generate random array

7. Quit

What would you like to do? 2

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 [1.0, 1.0, 1.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? 3

How many values are in the arrays? 2

Enter the values in the first array, separated by spaces:

4 7

Enter the values in the second array, separated by spaces:

4 12

The result is [16.0, 84.0]

Menu

1. Add

2. Subtract

3. Multiply

4. Divide

5. Dot product

6. Generate random array

7. Quit

Copyright © 2017, 2018 Sinclair Community College. All Rights Reserved.

What would you like to do? 4

How many values are in the arrays? 4

Enter the values in the first array, separated by spaces:

1 16 4 9

Enter the values in the second array, separated by spaces:

5 4 0 4

The result is [0.2, 4.0, NaN, 2.25]

Menu

1. Add

2. Subtract

3. Multiply

4. Divide

5. Dot product

6. Generate random array

7. Quit

What would you like to do? 5

How many values are in the arrays? 3

Enter the values in the first array, separated by spaces:

2 5 3

Enter the values in the second array, separated by spaces:

1 7 4

The result is 49.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? 6

How many values should be in the random array? 5

What is the lower limit for the random number? 1

What is the upper limit for the random number? 10

The result is [1.4949373649360478, 5.728423187666145, 2.118227671384858,

6.172396196295855, 8.608650530556496]

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

Goodbye!

An Array Calculator

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

Copyright © 2017, 2018 Sinclair Community College. All Rights Reserved.

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]

Menu

1. Add

2. Subtract

3. Multiply

4. Divide

5. Dot product

6. Generate random array

7. Quit

What would you like to do? 2

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 [1.0, 1.0, 1.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? 3

How many values are in the arrays? 2

Enter the values in the first array, separated by spaces:

4 7

Enter the values in the second array, separated by spaces:

4 12

The result is [16.0, 84.0]

Menu

1. Add

2. Subtract

3. Multiply

4. Divide

5. Dot product

6. Generate random array

7. Quit

Copyright © 2017, 2018 Sinclair Community College. All Rights Reserved.

What would you like to do? 4

How many values are in the arrays? 4

Enter the values in the first array, separated by spaces:

1 16 4 9

Enter the values in the second array, separated by spaces:

5 4 0 4

The result is [0.2, 4.0, NaN, 2.25]

Menu

1. Add

2. Subtract

3. Multiply

4. Divide

5. Dot product

6. Generate random array

7. Quit

What would you like to do? 5

How many values are in the arrays? 3

Enter the values in the first array, separated by spaces:

2 5 3

Enter the values in the second array, separated by spaces:

1 7 4

The result is 49.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? 6

How many values should be in the random array? 5

What is the lower limit for the random number? 1

What is the upper limit for the random number? 10

The result is [1.4949373649360478, 5.728423187666145, 2.118227671384858,

6.172396196295855, 8.608650530556496]

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

Goodbye!

Norm Radder

Rancher

Posts: 2240

28

posted 2 months ago

Before writing any code, a programmer should create a design for what the program should do. Take a piece of paper and pen and write down the steps needed to solve the problem.

Then refine that list to provide the details needed and then finally write the code in small steps with frequent compiles and executing for testing.

process a java coder would go through while going about writing this code

Before writing any code, a programmer should create a design for what the program should do. Take a piece of paper and pen and write down the steps needed to solve the problem.

Then refine that list to provide the details needed and then finally write the code in small steps with frequent compiles and executing for testing.

posted 2 months ago

How you would develop an assignment like this is a little different than what you would do in the professional world, but there are similarities. Like Norm, I would advise writing down the design (either on paper or digitally). Since most of the design has been done -- you have been given the signatures of the method -- I would probably more on to requirements.

* I will need a Scanner object

* I will need a loop that quits when 7 is entered

* Will I need input verification? (you decide or ask your instructor)

* Should I use several classes or one? (same as above)

Then I would write some pseudocode:

LOOP

Display menu

Get user input

...

WHILE input is not 7

Then decide, Should Display menu be a method? If so, write pseudocode for it. Should Get use input be a method? etc.

Does this help?

* I will need a Scanner object

* I will need a loop that quits when 7 is entered

* Will I need input verification? (you decide or ask your instructor)

* Should I use several classes or one? (same as above)

Then I would write some pseudocode:

LOOP

Display menu

Get user input

...

WHILE input is not 7

Then decide, Should Display menu be a method? If so, write pseudocode for it. Should Get use input be a method? etc.

Does this help?

All things are lawful, but not all things are profitable.