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

1500 POINTS FOR THIS QUESTION Here is the assignment: For today\'s lab, you\'ll

ID: 3543984 • Letter: 1

Question

1500 POINTS FOR THIS QUESTION


Here is the assignment:


For today's lab, you'll write several methods that work with arrays, using what you learned in class about how to create them, traverse them, and do some common tasks to them with the Arrays class. You'll also use some techniques from earlier classes, like for loops and parameters with methods and return values.

You'll start with a class, ArrayExercises.java, that you can download from here. Do not create your own new class and do not modify anything in the main method of this one. Part of the assignment is to use the main method exactly as it is written.

You'll need to add 4 new methods (buildArray, printArrayContents, printSumAndAverage, and addOneToOddNumbers) to the ArrayExercises class, after the existing main method. The code in main is enough for you to deduce exactly what parameters and return types each method should have.

The buildArray method needs to create a new array big enough to hold the integers from its 'start' to 'finish' parameters, inclusive, populate it with the integers between start and finish, inclusive, and return the array. If start is 1 and finish is 5, the array should store the numbers 1, 2, 3, 4, and 5.

The printArrayContents method takes two parameters, a string and an array. It should print the string first, then the array's contents. Hint: remember what you learned in class about an EASY way to get an array's contents as a string. You don't need a loop for this method.

The printSumAndAverage method should do just what it says, print out the sum of the numbers in the array and their average. This is similar to previous labs you've done, except that you've got the numbers already stored in an array instead of having to read them from a Scanner object as you need them. Note that the average is printed out with two decimal places.

Finally, the addOneToOddNumbers method should add 1 to any number in the array that is not odd. So an array like 1, 2, 3, 4 would become 2, 2, 4, 4 (the first and third numbers, 1, and 3, were odd, so they each get +1 and become 2 and 4).

When you've implemented all your methods, if you run your program and use 1 and 10 as the start and finish numbers, your output should look just like this:

Keep in mind that we may use different numbers as input when we're testing your code! When your program works, submit it here.


Here is the base code:


Explanation / Answer

package javaapplication1;

import java.util.Arrays;

import java.util.Scanner;


public class ArrayExercises

{

public static int[] buildArray(int start, int finish){

int[] numbers = new int[finish-start+1];

int j=0;

for(int i=start;i<=finish;i++)

numbers[j++] = i;

return numbers;

}

  

public static void printArrayContents(String str, int[] numbers){

System.out.println(str + Arrays.toString(numbers));

}

  

public static void printSumAndAverage(int[] numbers){

int sum=0;

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

sum+=numbers[i];

System.out.println("Sum: "+sum );

System.out.println("Average: "+(float)sum/numbers.length);

}

  

public static void addOneToOddNumbers(int[] numbers){

  

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

if(numbers[i]%2!=0) numbers[i]++;

}

  

// Start with this main() method. Do not change it!

public static void main(String[] args)

{

Scanner scanner = new Scanner(System.in);

System.out.print("Enter the first number: ");

int start = scanner.nextInt();

int finish = start;

while (finish <= start)

{

System.out.print("Enter the last number (must be after first number): ");

finish = scanner.nextInt();

}

  

// initialize an array to contain the numbers 'start' through 'finish', inclusive

int[] numbers = buildArray(start, finish);

  

// print the message passed in, then all the numbers in the array

printArrayContents("Initial array:", numbers);

  

// print the sum and average of the numbers

printSumAndAverage(numbers);

  

// add 1 to all odd numbers

addOneToOddNumbers(numbers);

  

// print the message and the array contents again

printArrayContents("Array without odds:", numbers);

}

}

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