Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need to answer these in Java, 1. Write a method with the signature \"float sum

ID: 3685256 • Letter: I

Question

I need to answer these in Java,

1. Write a method with the signature "float sumArray(float[] data)" that takes an array of floats, and returns their sum.

2. Write a method with the signature "boolean[] createAlternating(int length)" that takes an integer representing the size of an array, and then returns a new boolean array of that size where each index alternates between true and false (e.g., index 0 is true, index 1 is false, index 2 is true, and so on).

3.

Does the following code execute properly? If not, what should be changed?

int[] numbers = {4, 2, 1, 6, 9, 56, 12, 4, 3, 12, 6};

//print out each number in the array.

for (int i = 1; i <= numbers.length; i++)

  System.out.println(numbers[i]);

Explanation / Answer

1. Write a method with the signature "float sumArray(float[] data)" that takes an array of floats, and returns their sum.

public static float sumArray(float[] data)

{

float sum = 0;

for(int i = 0; i < data.length; i++)

sum += data[i];

return sum;

}

2. Write a method with the signature "boolean[] createAlternating(int length)" that takes an integer representing the size of an array, and then returns a new boolean array of that size where each index alternates between true and false (e.g., index 0 is true, index 1 is false, index 2 is true, and so on).

public static boolean[] createAlternating(int length)

{

boolean[] Array = new boolean[length];

Array[0] = true;

for(int i = 1; i < length; i++)

Array[i] = !Array[i-1];

return Array;

}

3. Does the following code execute properly? If not, what should be changed?

int[] numbers = {4, 2, 1, 6, 9, 56, 12, 4, 3, 12, 6};

//print out each number in the array.

for (int i = 1; i <= numbers.length; i++)

  System.out.println(numbers[i]);

In java, the array index starts with 0, and will proceed upto its length-1.

Thats what your only problem with the code is. So, instead of your for loop, just use this:

for(int i = 0; i < numbers.length; i++) //Which means, starting at index 0, and moving till length-1. This will work.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote