ArrayUtilities Class Copy Week12-ArrayUtilities demo, in Week 12 learning materials, for this lab. Paste it into Lab8-ArrayUtilities. Use a class called ArrayUtilities, that already holds a method for...

1 answer below »

ArrayUtilities Class



  • Copy Week12-ArrayUtilities demo, in Week 12 learning materials, for this lab. Paste it into Lab8-ArrayUtilities.

  • Use a class called ArrayUtilities, that already holds a method for adding up the elements in an array. I have created this method while demonstrating arrays in the lesson. This method is called totalOfElements. This method takes an array as a parameter and returns the total of the values in the array.

  • Make another method to fill the array indexes with elements. This method takes an array of integers and Scanner reference variable as parameters. Call this method as insertElement. This method returns an array. Here is the method signature: public int[]insertElement(int dArray[], Scanner scan)

  • To pass an array as a parameter, use the square bracket notation after the data type, for example,

  • to pass an array called myArray as a parameter: int myArray[].

  • to write the return type as an array: int[]

  • to write a return statement: return myArray

  • use a for loop to ask the user for numbers and fill the array with them.

  • Make a method to print the elements in the array. This method takes an array of integers as a parameter. Use a for loop to iterate every index and display the element stored at the specific index/subscript.







  • In the main method, do the following:

  • Declare an array and set the size to user input.

  • Call a method of ArrayUtilities class to fill the array with integers.

  • Print the array by calling another method of ArrayUtilities class.

  • Call a method of ArrayUtilities class to print the sum of the numbers in an array.

  • Here’s the sample run:


Enter size of array


4


Enter number 1: 34


Enter number 2: 24


Enter number 3: 54


Enter number 4: 64






Elements in array:


34


24


54


64


Sum of array elements: 176.0















/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author Amrit */ public class ArrayUtilitiesTester { /** * @param args the command line arguments */ public static void main(String[] args) { ArrayUtilities utility = new ArrayUtilities(); // An array initialized with values. int numbers[] = {2, 4, 6, 8, 10}; // Display the total of array elements. System.out.println("Total of array elements:" + utility.totalOfElements(numbers)); //passing an array as an argument to the method } }


































/*



* To change this license header, choose License Headers in Project Properties.



* To change this template file, choose Tools | Templates



* and open the template in the editor.



*/









/**



*



* @author Amrit



*/



public class ArrayUtilities {




// This method accepts an integer array as an argument.




public int totalOfElements(int myArray[]){




// Loop counter




int index;




// Accumulator, initialized to 0




int total = 0;










// Calculate the total of the array elements.




for(index = 0; index




total = total + myArray[index];




}




return total;




}












}



Answered Same DayApr 12, 2022

Answer To: ArrayUtilities Class Copy Week12-ArrayUtilities demo, in Week 12 learning materials, for this lab....

Kshitij answered on Apr 12 2022
107 Votes
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here