introducing topics, this video is a look at these topics without going through the activities. All the topics discussed here are expanded upon further into the module.
I use the variable x in my programme I am using the value of 50. So x + 2 would evaluate to 52. I can also change the value whenever I want. If I set x = 100, then x + 2 would evaluate to 102. I could even set x = x+2. This would make x equal the sum of itself and 2.
in section 3.2, an identifier should: • Be written in camelCase • Have a name that describes its purpose Although it doesn’t HAVE to do either of these things to compile correctly. This leads us into:
need to bear in mind two things: • The syntax – how should we write the source code so that it compiles correctly. • Convention – how should we write the source code so that it is easier to follow and understand.
widely used by programmers that it would be confusing and detrimental to the readability of the source code if we were to ignore them or use them in the wrong place.
is important to remember that the variable is not the object itself, but that the object is referenced by the variable. So to tell an object called ‘door’ to open, we’d send the appropriate message the the variable ‘door’ which references the object, and not to the object itself. This distinction gets blurred for brevity, and most programmers would happily use the phrase, “send a message to the object” instead of “send a message to the variable referencing the object.” We just need to keep the distinction in mind.
object. The code we write to do so is called a message-send. This is the syntax of a message-send: receiver.message() As we can see, it has three-parts.
we want to send the message to. • The fullstop indicates that the receiver before it is to be sent the message after it. • The message() includes the brackets. Later we’ll see things put between these brackets, but even when there isn’t anything they are required. So don’t forget them.
to open we would use the message-send: door.open(); Note the semi-colon at the end. In Java all statements terminate in a semi-colon to tell the compiler that what comes before it is a statement that needs to be executed. The semi-colon is needed because a message-send is a statement, but it is technically not a part of the message- send.
It defines the attributes and behaviour of a group of objects, so that all objects created from that class (that blueprint) are created the same. So for us to create a Frog object called kermit that has position and colour attributes, we’d need a class that defines frog objects.
Frogs objects: position and colour. • The behaviour Frogs objects: how they respond to the message jump(), and up(), and every other message in it’s protocol.
objects, all new objects created from that class will be the same. Two objects of the Frog class, for example kermit and gribbit, would be created in exactly the same way. They would have the same attributes and the same behaviour. Also initially their attribute values would be identical – colour would be green and position would be 0. So we can say also that they initially have the same state.
that class. So we can say that ‘instances of the same class will have the same behaviour and attributes and initially the same state.’ Their state may however differ. For example, if kermit turns brown and gribbit remains green.
names start with a capital letter by convention. So kermit is an instance of the class Frog and not the class frog. Or we might say, “kermit and gribbit are both instances of the Frog class”.
have to send it a message. For example, the colour of kermit could be set to brown through the use of the message brown(). However, if kermit was already brown before we sent the message then nothing would happen. It wouldn’t be ‘wrong’ to do this, it would just result in a different response (in this case no response). This is termed state-dependant behaviour. The state of an object can determine its behaviour in response to receiving a message.
aren’t intended to alter an object’s state to begin with. Telling a Frog object to croak or jump results in an action, but its state remains the same. Its colour isn’t affected and its position is the same, so its response to any message will be the same as before croak or jump was sent.