Send Close Add comments: (status displays here)
Got it!  This site uses cookies. You consent to this by clicking on "Got it!" or by continuing to use this website.nbsp; Note: This appears on each machine/browser from which this site is accessed.
Module and class example


1. Module and class example

2. Module and class example
Here is a module and class example for a few programming languages.

3. Problem statement
The module part should define a class Adder. Use your initials for the prefix of the module name. The module name suffix should be Adder. The constructor should set the integer class sum to zero. The class should contain a method sumAdd which takes one integer and adds that value to the sum. The class should contain a method sumGet which should return the current sum.

The main code should access the module, create an instance of the Adder class and then call that instance to add the integer values from 0 to 8 (inclusive) using the sumAdd method of the Adder class. The main code should then use the sumGet method to get and display the sum using a print statement.

4. Links
In addition to class notes, see the following in particular.

5. Python module
Here is a code module in Python [module rmsAdder in rmsAdder.py].


6. Python program
Here is the Python code.

Here is the output of the Python code.


7. Lua module
Here is a code module in Lua [module rmsAdder in rmsAdder.lua].


8. Lua program
Here is the Lua code.

Here is the output of the Lua code.


9. JavaScript module
Here is a code module in JavaScript [module rmsAdder in rmsAdder.js].


10. JavaScript program
Here is the JavaScript code.

Here is the output of the JavaScript code.


11. PHP module
Here is a code module in PHP [module rmsAdder in rmsAdder.php].


12. PHP program
Here is the PHP code.

Here is the output of the PHP code.


13. End of page