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; ...
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) {
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...
Suppose we have the following Java Interface: public interface Measurable { double getMeasure(); // An abstract method static double average(Measurable[] objects) { // A static method double sum = 0; for (Measurable obj : objects) { sum = sum + obj.getMeasure(); } if (objects.length > 0) { return sum / objects.length; } else { return 0; } } } Write a class, called Person, that has two instance variables, name (as...
I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that accepts one integer parameter and returns the value raised to the third power as an integer. o Write a method called randominRange that accepts two integer parameters representing a range. The method returns a random integer in the specified range inclusive. 2. o Write a method...
Write an implementation of the following Java method: /* search(ArrayList, int key) -> int method is passed an array list of integers and an integer key method searches for the key in the array list and returns: the index of the key if it is in the array list -1 otherwise */