Write a class named Accumulator containing: An instance variable named sum of type integer. A constructor that accepts an integer parameter, whose value is used to initialize the sum instance variable. A method named getSum that returns the value of sum. A method named add that accepts an integer parameter. The value of sum is increased by the value of the parameter.

Write a class named Accumulator containing: An instance variable named sum of type integer. A constructor that accepts an integer parameter, whose value is used to initialize the sum instance variable. A method named getSum that returns the value of sum. A method named add that accepts an integer parameter. The value of sum is increased by the value of the parameter.

LANGUAGE: JAVA

CHALLENGE:

Write a class named Accumulator containing:
An instance variable named sum of type integer.
A constructor that accepts an integer parameter, whose value is used to initialize the sum instance variable.
A method named getSum that returns the value of sum.
A method named add that accepts an integer parameter. The value of sum is increased by the value of the parameter.

SOLUTION:


public class Accumulator{
   private int sum;

   public Accumulator (int sum){
      this.sum = sum;
   }

   public int getSum(){
      return sum;
   }

   public void add (int value){
      sum += value;
   }
}

Add comment