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

M250 Unit 2 Subsection 6 - Key points of Unit 6...

Avatar for Matt Matt
January 10, 2020

M250 Unit 2 Subsection 6 - Key points of Unit 6: A Bank Account Class (10)

Avatar for Matt

Matt

January 10, 2020
Tweet

More Decks by Matt

Other Decks in Education

Transcript

  1. M250 Unit 2 Subsection 6 A discussion of key points

    relating to subsection 6 – A bank account class And recap of essential unit 2 terminology
  2. As subsection 6 is largely activity-based, this presentation will just

    be a discussion of the various things that are introduced in it.
  3. Textual numbers A message might take an argument that appears

    to be numerical, but is in fact a textual representation of a number. For now, just go with it when I say that if there are double quotes around some text, that text is called a string. Imagine a bank account object with the message setAccountNumber(). The argument for this message would need to be a string, and not actual numbers! Why?
  4. What if an account number was 0347? If we treated

    this as an actual number then the Java compiler would remove the leading zero (the zero that does not affect the value of the number). The account number would therefore be recognised as 347 because this has the same numerical value as 0347. However, the string “0347” (with quotes) is distinctly different from the string “347”. In general, whenever we have attributes that involve numbers -- if we don’t need to use these numbers in any calculations, then they should be set as strings to be the textural representation of numbers rather than the actual numbers. Phone numbers are another good example. We never need to add two phone numbers together. And a phone number can start with 0. So we should treat it as a string and not a numerical value. We’ll learn more about strings in the next unit.
  5. Variables referencing objects When we create an object we usually

    also want to create a variable to reference it. We won’t go into the hows of doing this just yet, but it is important to note something: We don’t send messages to an object. We send messages to the variable holding reference to an object. This line often gets blurred for conciseness, but we must bear it in mind. So, kermit is technically not a Frog. kermit is a variable holding reference to a Frog object. We send messages to kermit – for example kermit.doSomething() - and not directly to the object. We’ll discuss this more in the next unit.
  6. Java is case-sensitive Error messages are super common and they

    happen to everyone. A really common error occurs when we mistype a variable name with an uppercase/lowercase letter when we shouldn’t have. Java is case-sensitive. The variable joker is not the same as the variable Joker.
  7. Imagine we make a variable called kermit that references a

    Frog object. If we tried to execute the message-send Kermit.left(), this wouldn’t work. Java wouldn’t be able to compile this code. It would attempt to look for - and subsequently fail to find - a variable called Kermit, when in fact we only created a variable called kermit. Additionally, identifiers (names for variables, messages, arguments, classes) can’t have spaces in them. They must be one word.
  8. The variable name does not influence the behaviour of the

    object The variable name indeed has no effect whatsoever on what the object does. Appropriate variable names are for our use only, as programmers, because they help establish what the code does and improves readability.
  9. Q: What is the state of the following objects: -

    kermit, an instance of HoverFrog that has just been created. - thisFrogIsBrown, an instance of HoverFrog that has just been created
  10. Q: What is the state of the following objects: -

    kermit, an instance of HoverFrog that has just been created. - thisFrogIsBrown, an instance of HoverFrog that has just been created A: - kermit, since it has just been created, has the values it was initialised to. Colour: green, position: 1, Height: 0. - thisFrogIsBrown, also has the same values as it was also just initalsied. As we can see, the naming of the variable has no effect on the state of the object, nor does it effect its behaviour.
  11. Q: In a message-send, is the receiver or the message

    the object? A: The receiver is the object (or rather the variable holding reference to that object, though let’s blur that distinction just slightly from now on). We send a message TO an object. So messages aren’t objects.
  12. Recap of essential Unit 2 terminology • Initialisation – the

    setting of an object’s initial state upon its creation. All instances of the same class are initialised the same way. • Subclass / superclass – a subclass is a class that is an extension of its superclass. Instances of a subclass have all of the same attributes and protocol as instances of their superclass AND more. • Inheritance – A subclass inherits their common attributes and protocol from its superclass, before expanding them.
  13. Argument – An extra piece information included within the brackets

    of a message that is supplied to an object along with the message. Some messages take more than one argument. Message name – The name of a message -- (the parts of a message excluding any arguments, if there are any.) Getter message – A message beginning with the word get that returns the value of an object’s attribute. E.g. kermit.getPosition() Setter message – A message beginning with the word set that takes (usually) one argument and sets an object’s attribute to this argument. E.g. kermit.setHeight(0)