JAVA:
Suppose you have the following array declaration and initialization:
int [ ] values;
values = new int[18];
Suppose there is a static method called sumAll that is sent an integer array. The sumAll method computes and returns the sum of all the integers in the array that it is sent (assuming the array is filled). Choose the best example of calling this method:
Group of answer choices
a) sumAll( values[0], values.length )
b) sumAll( values[0 .. 17] )
c) sumAll( values[18] )
d) sumAll( values )
Dear Student,
The option D is correct.
d) sumAll( values )
during method calling we just pass the array name as an argument.
below i have given an example.
==================================================================
//This is the public class
public class Test
{
// method for sum of elements in an array
public static int sumAll(int values[])
{
int sum = 0; //
initialize sum
int i;
// Iterate through all
elements and add them to sum
for (i = 0; i <
values.length; i++)
sum += values[i];
return sum;
}
// This is the main method
public static void main(String[] args)
{
int[] values = new int[]
{1,2,3,4};
System.out.println("Sum of given array is
" + sumAll(values));//this bold
highlighted is the method calling
}
}
==================================================================
Sample Output:
Sum of given array is 10
==================================================================
Kindly Check and Verify Thanks..!!!
JAVA: Suppose you have the following array declaration and initialization: int [ ] values; ...
In JAVA, use this declaration and initialization: int theList[] = new int[4]; theList[0] = 1; theList[1] = -7; theList[2] = 10; theList[3] = 23; Write the method aNumLess(int list[], int x) which returns the number of data values in array theList which are less than value x.
1. Write a complete program based on public static int lastIndexOf (int[] array, int value) { for (int i = array.length - 1; i >= 0; i--) { if (array [i] == value) { return i; } } return -1; write a method called lastindexof that accepts an array of integers and an integer value as its parameters and returns the last index at which the value occurs in the array. the method should return -1 if the value is...
Write a static method named sumOf that takes an array of int values as an argument (you may safely assume that the array contains at least 1 element). This method should return the sum of all values in the array. Examples: Given the following array declaration and definition. int[] theArray = {0, 0, 0, 0, 0, 0}; sumOf(theArray) will return 0 Given the following array declaration and definition. int[] theArray = {1, 1, 1, 1, 1, 1}; sumOf(theArray) will return...
Which of the following statements is true about this array declaration? Declare Integer numbers [5] = 123,456,789,321, 654 This is an error: there should be 6 integers in the array. This is an array declaration and initialization. This is an error; the values of the array should be 1, 2, 3, 4,5 0 None of these statements are true.
Given an array of integers shown below, int myArray()={1,2,3,4,5,6,7,8,9,10) Complete the following method that computes and returns the average of all the values of the array. Paragraph B I U public static double averageOfArray(int[] myArray) {
Valentine’s Day Question(JAVA) Suppose you had a list with 30 valentines and you stored their names in an array (you hopeless romantic). Then if you had 5 people ask to be your valentine, you would need to make a new array of size 35 (30 + 5) and copy over the original valentines before being able to add the new valentines to the array. This question uses the same logic. Write a program that has a method called doubleSize. The...
Array manipulation (a) Write Java code for a method exchange (int [] a, int i, int j) that exchanges the values stored at indices i and j in the array a. You do not need to worry about cases where either i or j is an invalid index. Give the best estimate you can for its time complexity (b) In an ordered array of n items, how can we determine whether or not an item belongs to the list using...
JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a parameter. This method calculates and returns the largest even number in the list. If there are no even numbers in the array, the method should return 0. You can assume that the array is not empty. For example: Test Result int[] values = {1, 4, 5, 9}; System.out.println(getMaxEven(values)); 4 System.out.println(getMaxEven(new int[]{1, 3, 5, 9})); 0 public static int --------------------------------------------------------------------------------- 2. Write a static method...
1.Write code for a Java method, printArray, that takes an int array as parameter and prints it out, one element per line. public static void printArray (int[] values){ Methods that do NOT return a result have “void” as return type. Notice how an array parameter type is passed, int [] }// end printArray 2.Write code for a Java method that takes in as input parameter an integer number, say num, and returns a double array of size num with all...
(JAVA) Given an array of unique positive integers, write a function findSums that takes the array input and: 1. Creates a hashtable called “hashT” and inserts all the elements of the input array in the hashtable. 2. Finds pairs of elements in the hashtable whose sum is another element (sum) in the hashtable and print the pairs in the console. 3. Returns another hashtable names “sums” of those sum elements. For example: Input: [1,5,4,6,7,9] Output: [6,5,7,9] Explanation: 6 = 1...