Home > Software design >  Java - method that add element then multiply every element in a list
Java - method that add element then multiply every element in a list

Time:10-25

can I please for a help in this exercise? I exhausted my knowledge, after many hours. I need to write a static method in Java that takes as a parameter list of numbers and returns int value. The value is a result of adding and multiplying alternate numbers in a list. As a addition I CANNOT use the modulo % to take odd or even index from loop.
How can I break the J inner loop, it is not incrementing when I use break, so j index is always 2. I am complete beginner.

As a example: [1, 2, 3, 4, 5] should be as a result: (1 2 * 3 4 * 5) = 27

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

public class Exercise {
    static int addAndMultiply(List<Integer> list) {
        boolean flag = true;
        int sum = 0;
        for (int i = 0; i < list.size(); i  ) {
                for (int j = 1; j < list.size(); j  ) {
                if(flag) {
                    sum = list.get(i)   list.get(j);
                    flag = false;

                }  else {
                    sum  = list.get(i) * list.get(j);
                    flag = true;
                }
            } break;
        }
        return sum;
}
    public static void main(String[] args) {
        List<Integer> numbers = new LinkedList<>();
        Collections.addAll(numbers, 1, 2, 3, 4, 5);
        System.out.println(Exercise.addAndMultiply(numbers));
    }
}

CodePudding user response:

You can use the below implementation of addAndMultiply method to get the desired result as follows:

Logic:

  1. I have first initialised the result with the first element of the list and then save the loopSize as size of the list.
  2. After that , I have checked whether the loopSize is even or odd, if even then modified the result by adding the last element of the list and reducing the loopSize by 1.
  3. Now iterating the list from i=1 to the loopSize with an increment of i 2 and adding the result after multiplying the consecutive numbers as (list.get(i) * list.get(i 1)).

Code:

static int addAndMultiply(List<Integer> list) {
    int result = list.get(0);
    int loopSize = list.size();
    if ((loopSize & 1) !=1) {
        result = result   list.get(loopSize - 1);
        loopSize = loopSize - 1;
    }
    for (int i = 1; i < loopSize; i = i   2) {
        result = result   (list.get(i) * list.get(i   1));
    }
    return result;
}

Testcases:

Input: 1, 2, 3, 4, 5
Output: 27

Input: 1, 2, 3, 4, 5, 6
Output: 33

Input: 1,2
Output: 3

CodePudding user response:

You can do it this way :

static int addAndMultiply(List<Integer> list) {
    int result = list.get(0);
    for (int i = 1 ; i < list.size() ; i = i 2) {
        if (i == list.size()-1) {
            result  = list.get(i);
        } else {
            result  = list.get(i) * list.get(i 1);
        }
    }
    return result;
}
  • Related