{"id":871,"date":"2023-09-01T15:52:36","date_gmt":"2023-09-01T13:52:36","guid":{"rendered":"https:\/\/oopm.org\/?page_id=871"},"modified":"2025-01-12T13:34:09","modified_gmt":"2025-01-12T12:34:09","slug":"2-7-using-the-account-object","status":"publish","type":"page","link":"https:\/\/oopm.org\/?page_id=871","title":{"rendered":"2.8 The second program"},"content":{"rendered":"<div class=\"pdfprnt-buttons pdfprnt-buttons-page pdfprnt-top-right\"><a href=\"https:\/\/oopm.org\/index.php?rest_route=wpv2pages871&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=wpv2pages871&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\">In this section, we show how to represent activities of a clerk in the bank system. We may do this by introducing an object representing a clerk. In the example, the clerk makes a few transactions on the <code>account_1010<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code has-custom-color-1-background-color has-background\"><code>aClerk: <strong>obj<\/strong>\n   handle:\n      newBalance: <strong>var<\/strong> float\n      account_1010.interestRate := 0.7\n      newBalance := account_1010.deposit(100)   \n      newBalance := account_1010.deposit(225)\n      account_1010.addInterest\n      newBalance := account_1010.withdraw(111)\n      console.print(\"The new balance is:\" + newBalance) <\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The activities of the clerk is represented by a method, <code>handle<\/code>, of the <code>aClerk<\/code> object. The description of the method <code>handle<\/code> consists of the following items:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>A declaration of a local variable <code>newBalance<\/code>.<\/li>\n\n\n\n<li>An assignment statement <code>account_1010.interestRate := 0.7<\/code>.<\/li>\n\n\n\n<li>Two invocations of the <code>deposit<\/code> method on <code>account_1010<\/code>, with arguments <code>100<\/code> and <code>225<\/code>.\n<ul class=\"wp-block-list\">\n<li>The <code>balance<\/code> is now 325<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>An invocation of the <code>addInterest<\/code> method on <code>account_1010<\/code>.\n<ul class=\"wp-block-list\">\n<li>The <code>balance<\/code> is now 325 + 325 * 0.7% = 327,28<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>An invocation of <code>withdraw<\/code> on <code>account_1010<\/code> with argument <code>111<\/code>.\n<ul class=\"wp-block-list\">\n<li>The <code>balance<\/code> is now 216,28<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>And finally, a statement printing the new <code>balance<\/code> of the account.<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">As mentioned, objects exist within a computer, and the bank clerks need to be able to manipulate the objects. In order to do this, a given object model must be able to receive information from outside the computer and to deliver information to the outside. This is called&nbsp;<em>input<\/em>&nbsp;and&nbsp;<em>output<\/em>&nbsp;(abbreviated<em>&nbsp;I\/O<\/em>) to and from an object model.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">As a simple start on handling I\/O, we assume that our system has an object\u00a0<code>console<\/code>\u00a0that represents a window on the screen of the computer executing the bank system. The <code>console<\/code> object has a method <code>print<\/code> for printing strings and numbers in the associated window.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The clerk executes the following statement as part of <code>handle<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code has-custom-color-1-background-color has-background\"><code>console.print(\"The new balance is: \" + newBalance)<\/code><\/pre>\n\n\n\n<style>\n#sec_2_8 {\n  width: 30%;\n  padding-left: 15px;\n  margin-left: 15px;\n  float: right;\n  background-color: lightgray;\n  font-size: 0.75em;\n}\n<\/style>\n<div id=\"sec_2_8\">The operator <code>+<\/code> concatenates two strings. An expression <code>\"Hello \" + \"world\"<\/code> evaluates to the <code>String \"Hello World\"<\/code>. An operator of type <code>float<\/code> like <code>newBalance<\/code> will be transformed to a <code>String<\/code> representing its value. For details see <script>mkRef(\"String values\")<\/script>. <\/div>\n\n\n\n<p class=\"wp-block-paragraph\">The argument,&nbsp;<code>\"The new balance is: \" + balance<\/code>&nbsp;of <code>print<\/code> evaluates to the string&nbsp;<code>\"The new balance is: 216.28\"<\/code> and this string is printed in the window.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Next we show how to make a program containing <code>aClerk<\/code> and <code>account_1010<\/code> in the form of a program <code>mySecondProgram<\/code>. In addition, <code>mySecondProgram<\/code> contains a statement <code>aClerk.handle<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code has-custom-color-1-background-color has-background\"><code><code>mySecondProgram<span style=\"font-family: inherit; font-size: inherit;\">: <\/span><strong style=\"font-family: inherit; font-size: inherit;\">obj<\/strong><span style=\"font-family: inherit; font-size: inherit;\">         <\/span><\/code>   aClerk: <strong>obj<\/strong>\n      handle:\n         -\"-\n   account_1010: <strong>obj<\/strong>\n      -\"-\n   aClerk.handle<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><em>Notation<\/em>: we use <code>-\"-<\/code> to stand for code that is not shown but has been shown in a previous example. One may think of this symbol as an extended ditto mark. See chapter <script>mkRef(\"Notation\");<\/script>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The description of <code>mySecondProgram<\/code> is an example of a program that may be executed &#8211; it has the following items:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A declaration of the object <code>aClerk<\/code>.<\/li>\n\n\n\n<li>A declaration of the object <code>account_1010<\/code>.<\/li>\n\n\n\n<li>A statement <code>aClerk.handle<\/code>.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">When this program is executed, the object <code>mySecondProgram<\/code> is generated; as part of this generation the two objects <code>aClerk<\/code> and <code>account_1010<\/code> are generated, and finally there is an invocation of <code>aClerk.handle<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The following snapshots illustrate the dynamics of the execution of this program.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The first snapshot shows the state of <code>account_1010<\/code> after <code>mySecondProgram<\/code>, <code>aClerk<\/code> and <code>account_1010<\/code> have been generated, and the statement <code>aCleark.handle<\/code> is being executed:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A <code>handle<\/code> method object has been generated.<\/li>\n\n\n\n<li>The point of execution is before the statement<code> account_1010 := 0.7<\/code> in <code>handle<\/code>.<\/li>\n\n\n\n<li>As can be seen, <code>interestRate = 0.7<\/code> and <code>balance = 0<\/code>.<\/li>\n<\/ul>\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><code>mySecondProgram<span style=\"font-family: inherit; font-size: inherit;\">: <\/span><strong style=\"font-family: inherit; font-size: inherit;\">obj<\/strong><span style=\"font-family: inherit; font-size: inherit;\">         <\/span><\/code>   aClerk: <strong>obj<\/strong>\n      handle:\n         newBalance: <strong>var<\/strong> float\n <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-color-3-color\">--&gt;<\/mark>     account_1010.interestRate := 0.7\n         newBalance := account_1010.deposit(100)   \n         newBalance := account_1010.deposit(225)\n         account_1010.addInterest\n         newBalance := account_1010.withdraw(111)\n         console.print(\"The new balance is:\" + newBalance)\n   account_1010: <strong>obj<\/strong>\n      -\"-\n   aClerk.handle <\/code><\/pre>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:30%\">\n<figure class=\"wp-block-image size-full is-resized\"><img decoding=\"async\" width=\"261\" height=\"136\" src=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/09\/2.8_account_1010-I.jpg\" alt=\"\" class=\"wp-image-7722\" style=\"width:270px\"\/><\/figure>\n<\/div>\n<\/div>\n\n\n\n<p class=\"wp-block-paragraph\">The next snapshot shows the situation after <code>handle<\/code> has executed <code>account_1010.interestRate := 0.7<\/code> and the statement <code>newBalance := account_1010.deposit(100)<\/code> is being executed. <\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The point of execution is at the statement <code>balance := balance + amount)<\/code> in <code>deposit.<\/code><\/li>\n\n\n\n<li>So far we still have <code>balance = 0<\/code>.<\/li>\n<\/ul>\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><code>mySecondProgram<span style=\"font-family: inherit; font-size: inherit;\">: <\/span><strong style=\"font-family: inherit; font-size: inherit;\">obj<\/strong><span style=\"font-family: inherit; font-size: inherit;\">         <\/span><\/code>   aClerk: <strong>obj<\/strong>\n      -\"-\n   account_1010: <strong>obj<\/strong>\n      -\"-\n      deposit(amount: var float):\n<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-color-3-color\">--&gt;<\/mark>      balance := balance + amount\n   aClerk.handle <\/code><\/pre>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:30%\">\n<figure class=\"wp-block-image size-full is-resized\"><img decoding=\"async\" width=\"257\" height=\"133\" src=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/09\/2.8_account_1010-II.jpg\" alt=\"\" class=\"wp-image-7724\" style=\"width:270px\"\/><\/figure>\n<\/div>\n<\/div>\n\n\n\n<p class=\"wp-block-paragraph\">The next snapshot shows the situation after execution of <code>deposit<\/code> has returned. <\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The point of execution is at the statement <code>account_1010.deposit(225)<\/code> in <code>handle<\/code>. <\/li>\n\n\n\n<li>As can be seen, <code>balance<\/code> now has the value 100.<\/li>\n<\/ul>\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><code>mySecondProgram<span style=\"font-family: inherit; font-size: inherit;\">: <\/span><strong style=\"font-family: inherit; font-size: inherit;\">obj<\/strong><span style=\"font-family: inherit; font-size: inherit;\">         <\/span><\/code>   aClerk: <strong>obj<\/strong>\n      handle:\n         newBalance: <strong>var<\/strong> float\n         account_1010.interestRate := 0.7\n<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-color-3-color\">   <\/mark>      newBalance := account_1010.deposit(100)   \n<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-color-3-color\">--&gt;<\/mark>      newBalance := account_1010.deposit(225)\n<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-color-3-color\">   <\/mark>      account_1010.addInterest\n         newBalance := account_1010.withdraw(111)\n         console.print(\"The new balance is:\" + newBalance)\n   account_1010: <strong>obj<\/strong>\n      -\"-\n   aClerk.handle  <\/code><\/pre>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:30%\">\n<figure class=\"wp-block-image size-full is-resized\"><img decoding=\"async\" width=\"255\" height=\"135\" src=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/09\/2.8_account_1010-III.jpg\" alt=\"\" class=\"wp-image-7727\" style=\"width:270px\"\/><\/figure>\n<\/div>\n<\/div>\n\n\n\n<p class=\"wp-block-paragraph\">The final snapshot shows the situation just besfore the statement <code>console.print(\"...\") <\/code>in <code>handle<\/code>.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>As can be seen, the value of a <code>balance<\/code> is now 216.27.<\/li>\n<\/ul>\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><code>mySecondProgra<span style=\"font-family: inherit; font-size: inherit;\">m: <\/span><strong style=\"font-family: inherit; font-size: inherit;\">obj<\/strong><span style=\"font-family: inherit; font-size: inherit;\">         <\/span><\/code>   aClerk: <strong>obj<\/strong>\n      handle:\n         newBalance: <strong>var<\/strong> float\n         account_1010.interestRate := 0.7\n<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-color-3-color\">   <\/mark>      newBalance := account_1010.deposit(100)   \n<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-color-3-color\">   <\/mark>      newBalance := account_1010.deposit(225)\n         account_1010.addInterest\n         newBalance := account_1010.withdraw(111)\n<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-color-3-color\">--&gt;<\/mark>      console.print(\"The new balance is:\" + newBalance)\n   account_1010: <strong>obj<\/strong>\n      -\"-\n   aClerk.handle <\/code><\/pre>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:30%\">\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"255\" height=\"130\" src=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/09\/2.8_account_1010-IV.jpg\" alt=\"\" class=\"wp-image-7729\" style=\"width:270px\"\/><\/figure>\n<\/div>\n<\/div>\n\n\n\n<p class=\"wp-block-paragraph\">The above clerk object is only for an illustrative purpose and a does not represent a real clerk in a bank system. We elaborate on the example later in this book.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Terminology: singular object<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">In this chapter, we have described three objects, <code>account_1010<\/code>, <code>aClerk<\/code>, and <code>mySecondProgram<\/code>. They are all examples of what is called a <em>singular object <\/em>since there is only one of its kind for each of them. In the next section, we introduce the <em>class<\/em> mechanism, which is a template that may be used to generate many objects that have the structure as defined by the template.<\/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=871\" 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>In this section, we show how to represent activities of a clerk in the bank system. We may do this by introducing an object representing a clerk. In the example, the clerk makes a few transactions on the account_1010: The activities of the clerk is represented by a method, handle, of the aClerk object. The [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":175,"menu_order":8,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-871","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\/871","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=871"}],"version-history":[{"count":110,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/871\/revisions"}],"predecessor-version":[{"id":10995,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/871\/revisions\/10995"}],"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=871"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}