Shall the example be vehicles or bank accounts?
A bank example is described in the note OO-lec-v6-figures-ok (in Papers in Dropbox). Part of the text from this note may be found here.
Other examples are: train reservation system from Beta Book, section 2.
We must also have an example with an active object.
We actually need several examples, but do we have more than one example here or do we have them in subsequent chapters?
Objects are the basic building blocks being used to create a model of a given application domain. An object is an entity that exists inside a computer and we may use objects to represent information about phenomena in the application domain. Such information may be values describing properties of the phenomena.
Objects may also have behavior in the form of actions that may be executed. Such actions may compute new values from values stored in objects and save these values in objects.
Suppose we want to develop a system for keeping track of motor vehicles — a vehicle registration system. To do this we must create a model of vehicle registrations and other phenomena that a relevant for keeping track of vehicle registrations.
We may represent a vehicle registration by an object – the diagram below illustrates an object that may represent the car of Birger.
Note that the diagram is not the real object, but a picture of the object. The real object exists within a computer. We return to that later.
In order to generate objects in the computer, we must describe the objects in a language that can instruct the computer to generate the objects. As mentioned, such a language is called a programming language.
Below we show the description of an object representing the car of Birger.
BirgersCar: obj licenseNumber: val 20116677 kerbWeight: val 1500
The keyword obj specifies that we define an object.
The name of the object is BirgersCar.
The object has two attributes characterizing the car:
The keyword val is a shorthand for value and specifies that the data-item holds a constant value. We return to this in Chapter XXX.
Hallo Birger
We must define the notion of object-descriptor (or perhaps just use the term block?) and explain that indentation is used to define a block. We must have a figure with lines that shows the block in the example.
As said above, the code/program above, is a description of an object -- it is not the object itself as represented in the computer.
We may give this description to a compiler, which translates it into machine language. We may then execute the resulting machine language. which will generated the object in the computer.
BirgersCar has two data-items but it is also possible to associate so-called method-attributes with an object. A method describes a sequence of actions/statements that may change the state of the object and/or compute new values from the state of the object.
In the example below, we add a data-item payload that represents the weight of possible passengers in the car.
In addition, we add a method called addPassenger that increase the value of payload.
BirgersCar: obj licenseNumber: val 20116677 kerbWeight: val 1500 payload: var integer addPassenger: payload := payload + 80
The payload data-item is specified as a variable using the keyword var (shorthand for variable). A variable is a data-item that may hold different values during the execution of the program. It has an associated type that specifies the kind of values it may hold. For payload, the type is integer, which means that payload may (only) hold integer values.
The body of addPassenger has the form:
payload := payload + 80
which is a so-called assignment statement. The operator := specifies that the variable on the leftside gets a new value as specified by the right-side of :=. In this case the expression payload + 80 computes the value of the payload plus 80 (We assume that the the average weight of each passenger is 80 kg.).
The addPassenger-method of BirgersCar may be executed by a statement of the form:
BirgersCar.addPassenger
In the next example we have added at method removePassenger, that as the name indicates, remove a passenger from the car. In addition, we have added a method print, that prints information about the payload of Birgers car on the computer screen.
BirgersCar: obj licenseNumber: val 20116677 kerbWeight: val 1500 payload: var integer addPassenger: payload := payload + 80 removePassenger: payload := payload - 80 print: print("The payload of Birgers car is " + payload + " kg")
The removePassenger method naturally subtracts 80 from the payload of the car. The print method prints the string "The payload of Birgers car is " plus the value of payload plus the string " kg" on the screen.
A string is a data-type that consists of a sequence of characters and double quotes (") is used to specify a string literal. The data-type string is further described in Section xx.
The following code fragment shows examples of invoking the methods of BirgersCar:
Perhaps the code below should be placed in a object or method. Could be a main method as in most mainstream languages.
BirgersCar.addPassenger BirgersCar.addPassenger BirgersCar.removePassenger BirgersCar.print
Execution of the above code results in the following text being displayed on the screen of the computer:
The payload of Birgers car is 80 kg
We return to methods in Chapter XXX.