Question: WorkShop 3 (JAVA) 1. Arrays are easy to use in Java. An array variable must be declared, and the array itself must be allocated via

WorkShop 3 (JAVA)

1. Arrays are easy to use in Java. An array variable must be declared, and the array itself must be allocated via the keyword new. For example, the line below creates an array named x with enough space for 17 integers:

int[] x = new int[17];

Write a program which asks the user how many values they would like to process, then creates an array of the specified length, and reads the specified number of values and stores them in the array you created. You may assume all values are integers.

2. Arrays can be passed to methods as arguments/parameters just like other types of data. In your programs main class, define a static method named printArray that takes an array of integers as its only argument and prints the elements of the array. You should print all the elements of the array on one line, separated by spaces, with no extra space after the last element.

Hint 1: You may want to use the length property of the array to know how many elements to print.

Hint 2: your method should look something like this:

public static void printArray(int[] x) {

// code to print array x goes here

}

3. Write another static method named arrayMean which takes an array as a parameter and returns a floating point value which is the mean value (i.e., the average) of the array elements. If A = {1, 2, 3} then arrayMean(A) should return (1 + 2 + 3)/3 = 2.0. Modify your main method to print the mean of the user input from the first question.

4. A Java array is a great way to represent a vector from regular math. Write another static method named dotProd which takes two arrays as parameters and returns the vector dot product, which is obtained by multiplying each element and computing the sum according to the expression WorkShop 3 (JAVA) 1. Arrays are easy to use in Java. An A[n] B[n] where N is the number of elements in each of the arrays. Note that this method only makes sense if A and B have the same number of elements, and note that this method should not modify either array.

As a test, if A = {1, 2, 3} and B = {4, 5, 6} then dotProd(A,B) should return 1 4 + 2 5 + 3 6 = 32.

Modify your main method to print the dot product of the users input array with itself. Note that this value should never be negative.

An example session with the complete program might look like this:

How many numbers to process?

> 4

> 3 2 1 4

You entered: 3 2 1 4

Mean: 2.5

Self Dot Product: 27

Note: the user input is indicated by beginning the line with >.

N-1 n=0

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!