I can not understand how to do a task with an array in "Java", where to start? I signed up for courses on "Java", they gave me a task, for a bloody day I don’t understand how to do it (((please tell me. Thank you in advance for the explanation).

The task itself:

Input: array with size = 10 can be of one of two data types: int and double. Calculate: sum min/max max positive multiplication modulus of first and last element second largest element As a result you should have methods with following names : sum(int array[]), sum(double[]) min(int array[]), min(double[]) max(int array[]), max(double[]) maxPositive(int array[]), maxPositive(double array[]), multiplication (int array[]), multiplication (double[]) modulus(int array[]), modulus(double[]) secondLargest(int array[]), secondLargest(double[]) 

Closed due to the fact that the issue is too general for participants aleksandr barakin , Alexander Petrov , cheops , sercxjo , Firepro 11 Sep '16 at 11:39 .

Please correct the question so that it describes the specific problem with sufficient detail to determine the appropriate answer. Do not ask a few questions at once. See “How to ask a good question?” For clarification. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • The task here will not be solved for you, better tell me what exactly you do not understand. - Nofate
  • Where to start, I do not understand. Maybe some tutorial on masivam advise to read. Here for example, for a day I was able to solve one problem (for the sum) public static void main(String[] args) { int myArray[] = {56,53,73,123,23,56,12,465,100,303}; int sum=0; for(int i=0; i<myArray.length; i++) { sum=sum+myArray[i]; } System.out.println(sum); public static void main(String[] args) { int myArray[] = {56,53,73,123,23,56,12,465,100,303}; int sum=0; for(int i=0; i<myArray.length; i++) { sum=sum+myArray[i]; } System.out.println(sum); - Hotosho
  • Start by creating the methods proposed in the task. They take arrays as arguments, and inside implement logic - a.chugunov

1 answer 1

Summation code for all elements of the array:

 int [] array = {1,1,3}; int sum = 0; for(int number : array) sum += number; System.out.println(sum); 

The code for calculating the largest and smallest number in the array:

 int [] array = {1,-22,2,5,7,100}; int min = Arrays.stream(array).min().getAsInt(); int max = Arrays.stream(array).max().getAsInt(); System.out.println("Min: " + min); System.out.println("Max: " + max); 

The code for calculating the maximum number that is above 0:

 int [] array = {1,-2,5,110,13}; int max = 0; for(int number : array) if(number>0 && max<number) max = number; System.out.println(max); 

The multiplication code of each element of the array:

 int [] array = {1,2,3,11}; int product = 1; for(int number : array) product *= number; System.out.println("Multiplication: " + product); 

The module of the first and last elements in the array:

 int [] array = {1,5,2,56}; int first = array[0]; int last = array[array.length - 1]; System.out.println("First: " + Math.abs(first)); System.out.println("Last: " + Math.abs(last)); 

The second maximum number in the array:

 int [] array = {1,5,15,61,62}; int largest = array[0]; int secondLargest = array[0]; for (int number : array) if (number > largest) { secondLargest = largest; largest = number; } else if (number > secondLargest) secondLargest = number; System.out.println("Second largest: " + secondLargest); 

That's all, you just need to arrange in the desired architecture.