Upgrade to Pro — share decks privately, control downloads, hide ads and more …

M250 Unit 1 Subsection 5 - Basic discussion of...

Avatar for Matt Matt
January 04, 2020

M250 Unit 1 Subsection 5 - Basic discussion of variables, message-sends, classes, and instances (4)

Here we get started on some of the vast terminology of M250.

Avatar for Matt

Matt

January 04, 2020
Tweet

More Decks by Matt

Other Decks in Education

Transcript

  1. M250 Unit 1 Subsection 5 An introductory discussion of variables,

    message- sends, classes, and instances to coincide with the information learned in ‘Exploring objects in the microworld’
  2. As subsection 5 is mainly interactive for the purpose of

    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.
  3. Variable examples If I set x = 50, then whenever

    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.
  4. The name of a variable is its identifier. As mentioned

    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:
  5. Code convention When we discuss how to write Java we

    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.
  6. Conventions such as using camelCase for variable names are so

    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.
  7. A distinction Variables can be assigned a value. For example,

    the variable x = 5. Variables can also hold reference to an object. For example, the variable door = some sort of door. Please note the distinction...
  8. When we talk about a variable referencing an object it

    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.
  9. Message-sends As discussed we can send a message to an

    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.
  10. receiver.message() • The receiver is the name of the object

    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.
  11. Message-send example For example, to tell an object called door

    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.
  12. Classes A class is like a blueprint for object creation.

    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.
  13. The class would have to define: • The attributes of

    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.
  14. Instances Because the class defines the attributes and behaviour of

    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.
  15. We call an object of a class an instance of

    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.
  16. Capitalising class names To distinguish them from variable names, class

    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”.
  17. State-dependant behaviour To change the state of an object we

    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.
  18. Messages that don’t alter an object’s state Some messages just

    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.