Question

Given an array of integers return the sum of the values stored in the first and...

Given an array of integers return the sum of the values stored in the first and last index of the array. The array will have at least 2 elements in it

---------------------------------------

public class Class1 {

public static int endSum(int[] values) {
    //Enter code here
}

}

Homework Answers

Answer #1

Please find below code and don't forget to give a Like.

Code: Change the below class name Main to your class name

public class Main
{
   public static void main(String[] args) {
   int total;
   int[] arr={1,2,3,4};
   if(arr.length>=2){
   total=endSum(arr);
   System.out.println("Total of element at first and last index is:"+total);
   }
   else{
   System.out.println("Array should have atleast 2 elements");
   }
  
   }
   public static int endSum(int[] values){
   int total=0;
   total=values[0]+values[values.length-1];
   return total;
   }
}

Output and screenshot of code:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write an application that stores the following nine integers in an array: 10, 15, 19, 23,...
Write an application that stores the following nine integers in an array: 10, 15, 19, 23, 26, 29, 31, 34, 38. Display the integers from first to last, and then display the integers from last to first. ----------------------------------------------- public class NineInts {     public static void main (String args[]) {         // Write your code here     } } ------------------------ Please use JAVA
Write a loop that sets each array element to the sum of itself and the next...
Write a loop that sets each array element to the sum of itself and the next element, except for the last element which stays the same. Be careful not to index beyond the last element. Ex: Initial Scores: 10, 20, 30, 40 Scores after loop: 30, 50, 70, 40 Import. java.util.Scanner; public class StudentScores { public static void main (String [] args){ Scanner scnr = new Scanner (System.in); final int SCORES_SIZE = 4; int [] bonusScores = new int[SCORES_SIZE]; int...
JAVA What values are stored in variables a and b in the code below? public class...
JAVA What values are stored in variables a and b in the code below? public class StackQuestion { public static void main(String[] args) { Stack s = new Stack(); s.push(1); s.push(2); s.push(3); s.pop(); s.pop(); s.push(4); s.push(5); s.pop(); s.pop(); int a = s.pop(); s.push(6); int b = s.pop(); } } What numbers are stored in variable a and b when the code below executes? public class QueueQuestion { public static void main(String[] args) { Queue s = new Queue(); s.enqueue(1); s.enqueue(2);...
Write recursive method to return true if a given array of integers, named numbers, with n...
Write recursive method to return true if a given array of integers, named numbers, with n occupied positions is sorted in ascending (increasing) order, or returns false otherwise. Array can be empty or not. //PRECONDITION: Varible n denotes the number of occupied positions in the array and must be non-negative. Employee class has method getSalary() that returns employee's salary. // An empty array and an array with single element in it, are sorted. Method isSortedRec must be recursive and returns...
Takes two ints as parameters. Determines the remainder when the first int is divided by the...
Takes two ints as parameters. Determines the remainder when the first int is divided by the second. If the remainder is 0 or 1 return true otherwise false. ----------------------------------------------- public class Class1 { public static boolean closeEnough (int i, int j) {     //Enter code here } }
Complete following function which receives an array of integers and the length of the array, and...
Complete following function which receives an array of integers and the length of the array, and then returns the sum of all the positive numbers in the array. For example, if an array that is passed to this function contains following numbers: -1, 2, 0, 3, 4, -3, 0, 2, 0, and then the return value of the function should be 11. Will this function be working correctly? Yes or No? int sumPositive(int a[],int length) { int s=0;     for(int...
JAVA /** * numDistinctElements returns the number of distinct elements in a given array of doubles....
JAVA /** * numDistinctElements returns the number of distinct elements in a given array of doubles. * * Some examples: * numDistinctElements(new double[] { }) is 0 * numDistinctElements(new double[] { 1, -4, -7, 7, 8, 11 }) is 6 * numDistinctElements(new double[] { -7, -4, -7, 3, 8, 8 }) is 4 */ Code Below: public static int numDistinctElements (double[] list) { return 0; }
Find the sum of squares 1^2 + 2^2 + ... n^2 using iteration and recursion *...
Find the sum of squares 1^2 + 2^2 + ... n^2 using iteration and recursion * I'm guessing we need to modify it Here's the code unmodified public class Sum { //Non recursive sum public static long sum1 (int n) { long sum = 1L; for (int i = 2; i <= n; ++i) sum = sum + i *i ; return sum; } //Recursive sum public static long sum2 (int n) { if (n < 2)return 1L; return sum2(n...
This is JAVA Write the method findMin that will return the smallest number in an array...
This is JAVA Write the method findMin that will return the smallest number in an array of integers. Examples: findMin({29,2,32,12}) -> 2 public int findMin(int[] numbers) {    }
import java.util.Scanner; public class FindMinLength { public static int minLength(String[] array) { int minLength = array[0].length();...
import java.util.Scanner; public class FindMinLength { public static int minLength(String[] array) { int minLength = array[0].length(); for (int i = 0; i < array.length; i++) { if (array[i].length() < minLength) minLength = array[i].length(); } return minLength; } public static void main(String[] args) { Scanner in = new Scanner(System.in); String[] strings = new String[50]; for (int i = 0; i < strings.length; i++) { System.out.print("Enter string " + (i + 1) + ": "); strings[i] = in.nextLine(); } System.out.println("Length of smallest...