Java method problem.

Asked 2 years ago, Updated 2 years ago, 81 views

class Adder{
    int value;
    int var1;

    public Adder(int x){
        this.var1 = x;
    }

    public int add(){
        value += var1;
        return getValue();
    }

    public int getValue(){
        return value;
    }
}

class Solution {
    public int solution(int[] arr) {

        Adder adder = new Adder(0);

        for(int x : arr){
            adder.add(x);
        }

        int answer = adder.getValue();
        return answer;
    }
}

It's homework, but I've been doing this and that for 3 hours, but I really don't know what the problem is and how to return it.

java method class

2022-09-20 17:26

1 Answers

Value value is not initialized in the Adder class


2022-09-20 17:26

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.