Ask Question

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.

+5
Answers (1)
  1. 5 September, 11:55
    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 & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers