Ask Question
23 June, 13:53

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

+3
Answers (1)
  1. 23 June, 17:10
    0
    The code to this question can be given as:

    code:

    public class Accumulator / /define class

    {

    private int sum; / /instance variable

    Accumulator (int sum) / /constructor that accepts an integer as a parameter

    {

    this. sum = sum;

    //holding value of sum using this keyword

    }

    int getSum () / /declare function getSum

    {

    return sum;

    //return value

    }

    void add (int value) / /declare function add

    {

    sum = sum + value;

    //add value.

    }

    }

    Explanation:

    In the above code firstly we declare a class that name is already given in the question that is Accumulator. In this class, we declare an integer variable that is sum. Then we declare parameterized constructor. In this constructor, we use this keyword that holds the value of the sum variable. Then we declare the getSum () function. This function returns the sum. Then we declare another function that is add () function. In this function we pass an integer variable value as the parameter. This function does not return any value. In this function, we add value to the sum variable.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Write a class named Accumulator containing: a. An instance variable named sum of type integer. b. A constructor that accepts an integer ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers