{"id":418,"date":"2023-05-14T13:31:34","date_gmt":"2023-05-14T11:31:34","guid":{"rendered":"https:\/\/oopm.org\/?page_id=418"},"modified":"2024-11-18T07:50:54","modified_gmt":"2024-11-18T06:50:54","slug":"2-4-method-attributes","status":"publish","type":"page","link":"https:\/\/oopm.org\/?page_id=418","title":{"rendered":"2.5 Methods"},"content":{"rendered":"<div class=\"pdfprnt-buttons pdfprnt-buttons-page pdfprnt-top-right\"><a href=\"https:\/\/oopm.org\/index.php?rest_route=wpv2pages418&print=pdf\" class=\"pdfprnt-button pdfprnt-button-pdf\" target=\"_blank\"><img decoding=\"async\" src=\"https:\/\/oopm.org\/wp-content\/plugins\/pdf-print\/images\/pdf.png\" alt=\"image_pdf\" title=\"View PDF\" \/><\/a><a href=\"https:\/\/oopm.org\/index.php?rest_route=wpv2pages418&print=print\" class=\"pdfprnt-button pdfprnt-button-print\" target=\"_blank\"><img decoding=\"async\" src=\"https:\/\/oopm.org\/wp-content\/plugins\/pdf-print\/images\/print.png\" alt=\"image_print\" title=\"Print Content\" \/><\/a><\/div>\n<p class=\"wp-block-paragraph\">The <code>aAccount_1010<\/code>&nbsp;has two data-items <code>owner<\/code>, and <code>balance<\/code> that represent properties associated with a bank account. We also need to represent <em>activities<\/em> associated with bank accounts. Such activities may include calculating<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-color-3-color\"> <\/mark>possible interest, and deposit and withdraw of money on an account. An activity may be represented by a method.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A <em>method<\/em> is a template for a<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-color-3-color\"> <\/mark>set of computations. A method may be an attribute of an object just like a data-item. A method has a sequence of statements that when executed carry out a computation that may change the state of the object and\/or compute new values from the state of the object.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the example below, we add a method called&nbsp;<code>addInterest<\/code>&nbsp;that computes the next amount of interest for the account and add it to the&nbsp;<code>balance<\/code>. In addition, we have added a data-item&nbsp;<code>interestRate<\/code>&nbsp;which is the quarterly interest rate for the account. In this simple example, we thus assume that interest is added every quarter.&nbsp;<code>interestRate<\/code>&nbsp;is declared as a variable (<code><strong>var<\/strong><\/code>), since it may change over time.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>account_1010: <strong>obj<\/strong>\n   owner: <strong>val<\/strong> \"John Smith\"\n   balance: <strong>var<\/strong> float\n   interestRate: <strong>var<\/strong> float  \n   addInterest:\n      balance := balance + (balance * interestRate) \/ 100 <\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The description of method&nbsp;<code>addInterest<\/code><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-foreground-color\"> <\/mark>consists of the following assignment statement:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>     balance := balance + (balance * interestRate) \/ 100<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The right-side expression&nbsp;is&nbsp;<code>balance + (balance * interestRate) \/ 100.<\/code>The variable&nbsp;<code>balance<\/code>&nbsp;denotes the current value of&nbsp;<code>balance<\/code>&nbsp;before it is updated by the assignment. The variable&nbsp;<code>interestRate<\/code>&nbsp;similarly denotes the current value of&nbsp;<code>interestRate<\/code>&nbsp;for the account.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Suppose that that&nbsp;<code>balance<\/code>&nbsp;holds the value 350.56 and&nbsp;<code>interestRate<\/code>&nbsp;holds the value 0.7. The execution of the assignment statement then takes place as follows:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>The variables&nbsp;<code>balance<\/code>&nbsp;and&nbsp;<code>interestRate<\/code>&nbsp;are substituted by their current values resulting in an expression:&nbsp;<code>350.56 + (350.56 * 0.7) \/ 100<\/code><\/li>\n\n\n\n<li>This expression is evaluated resulting in the value 353.01<\/li>\n\n\n\n<li>The value 353.01 is assigned to&nbsp;<code>balance<\/code>, which thus holds the value 353.01 after execution of the assignment statement.<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">The&nbsp;<code>addInterest<\/code>-method of&nbsp;<code>account_1010<\/code>&nbsp;may be executed by a statement of the form:<\/p>\n\n\n\n<pre class=\"wp-block-code has-custom-color-1-background-color has-background\"><code>account_1010.addInterest<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Such a statement is called a <em>method invocation<\/em>. As with the assignment to <code>interestRate<\/code> it must be placed in an object. The object executing the method invocation is called the <em>invoker<\/em> of the method. The object where the method is defined is called the <em>context<\/em> of the object.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the next snapshots we have extended <code>myFirstProgram<\/code> to include an assignment of <code>account_1010.interestRate<\/code> and an invocation of <code>account_1010.addInterest<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The first snapshot shows the state of <code>account_1010<\/code> after the two assignments, but before the invocation <code>account_1010.addInterest<\/code>. The invoker is <code>myFirstProgram<\/code> and the context is <code>account_1010<\/code>.<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-7387b849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<pre class=\"wp-block-code\"><code>   myFirstProgram: <strong>obj<\/strong> \n      account_1010: <strong>obj<\/strong>\n         owner: <strong>val<\/strong> \"John Smith\"\n         balance: <strong>var<\/strong> float \n      account_1010.balance := 350.56\n      account_1010.interestRate := 0.7\n      ...\n<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-color-3-color\">--&gt;<\/mark>   account_1010.addInterest\n      ...<\/code><\/pre>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-vertically-aligned-center is-layout-flow wp-block-column-is-layout-flow\">\n<figure class=\"wp-block-image aligncenter size-full\"><img decoding=\"async\" width=\"257\" height=\"131\" src=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/09\/2.5_account_1010-I.jpg\" alt=\"\" class=\"wp-image-7699\"\/><\/figure>\n<\/div>\n<\/div>\n\n\n\n<p class=\"wp-block-paragraph\">The second snapshot shows the situation after the invocation of  <code>account_1010.addInterest<\/code>. <\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-7387b849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<pre class=\"wp-block-code\"><code>   myFirstProgram: <strong>obj<\/strong> \n      account_1010: <strong>obj<\/strong>\n         owner: <strong>val<\/strong> \"John Smith\"\n         balance: <strong>var<\/strong> float \n      account_1010.balance := 350.56\n      account_1010.interestRate := 0.7\n      ...\n      account_1010.addInterest\n<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-color-3-color\">--&gt;<\/mark>   ...<\/code><\/pre>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-vertically-aligned-center is-layout-flow wp-block-column-is-layout-flow\">\n<figure class=\"wp-block-image aligncenter size-full\"><img decoding=\"async\" width=\"254\" height=\"127\" src=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/09\/2.5_account_1010-II.jpg\" alt=\"\" class=\"wp-image-7701\"\/><\/figure>\n<\/div>\n<\/div>\n\n\n\n<p class=\"wp-block-paragraph\">As can be seen, <code>balance<\/code> now has the value 353.01.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">As mentioned, we assume that interest is added quarterly. This implies that some other component in the bank system must invoke this method every quarter. The above invocation is only to illustrate how method invocation works.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n<div style=\"display:flex; gap:10px;justify-content:center\" class=\"wps-pgfw-pdf-generate-icon__wrapper-frontend\">\n\t\t<a  href=\"https:\/\/oopm.org?action=genpdf&amp;id=418\" class=\"pgfw-single-pdf-download-button\" ><img src=\"https:\/\/oopm.org\/wp-content\/plugins\/pdf-generator-for-wp\/admin\/src\/images\/PDF_Tray.svg\" title=\"Generate PDF\" style=\"width:auto; height:45px;\"><\/a>\n\t\t<\/div>","protected":false},"excerpt":{"rendered":"<p>The aAccount_1010&nbsp;has two data-items owner, and balance that represent properties associated with a bank account. We also need to represent activities associated with bank accounts. Such activities may include calculating possible interest, and deposit and withdraw of money on an account. An activity may be represented by a method. A method is a template for [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":175,"menu_order":5,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-418","page","type-page","status-publish","hentry"],"mb":[],"mfb_rest_fields":["title","gutenberg_elementor_mode"],"_links":{"self":[{"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/418","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/oopm.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=418"}],"version-history":[{"count":58,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/418\/revisions"}],"predecessor-version":[{"id":9597,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/418\/revisions\/9597"}],"up":[{"embeddable":true,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/175"}],"wp:attachment":[{"href":"https:\/\/oopm.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=418"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}