☰ Contents Data That Travels Together

Methods for Repeated Computation

Methods for Repeated Computation

Student: Professor, I am having fun with images, but so far it seems like we’ve just defined data, not actually written any programs that do work.
Prof: You’re right. It’s not an accident – a lot of object-oriented programming is about representing data, so we’ve gotten a bunch of practice with that! But now we’re ready to start defining behavior, not just specific data shapes and values.
Student: Yeah! The good stuff.
Prof: Let’s grab one of our earlier examples – a record representing a social media Post:
Prof: Say we’re setting up a custom timeline where only unpopular posts are shown. We might want a way to check for posts that have very few likes. We could try to check that for each one of the examples by using operators like < and using field lookup with .. But I’m going to show a different way. We are going to add to the record, finally using the space in between those {}. In there, I’m going to put a method definition, which in this case is going to take a number of likes and return true if this post has fewer likes, false if it has more.

Prof: The new stuff going on here:

  • A method definition has a few parts

    • The return type, in this case boolean, declares what type the method must return
    • The parameters are similar to the fields of a record: they each have a name and a type. We can use those names in the method body
    • The method body is between the {} after the parameters. It is one or more statements that compute and return something
    • We can refer both to the parameters (like compareLikes) and the fields (like likes) within a record’s methods.
  • Then, in the examples, we have method calls. These have an object value (in this case all Post values) which we call the receiver, then a ., then the name of a method (in this case hasFewerLikesThan), followed by the arguments in parentheses: the values the parameters take on for this call. It’s a lot like using a constructor, but instead of new ConstructorName, it starts with object.methodName.
Student: OK, so each time we call hasFewerLikesThan, it’s plugging in the argument for the value of compareLikes?
Prof: That’s right. In the first example we’re getting the result of p1.likes < 10 (p1.likes is 3461, not less than 10, so we get false). The next example compares it to 10000 instead, so it’s true. That continues for each example – each method call refers to the fields from the receiver, and the arguments that come in from the call. Similarly, inside a method body, calling a method using just its name (no receiver) calls it with the same receiver.
Student: OK, I think I sort of get it…

Practice

Here is another record with a method, and two examples of calling it.

Each chunk below is copied from the program. Say which part it is.
boolean, the first word of the method:
isXGreaterThan, on the same line:
double compareX:
return x > compareX;:
p.isXGreaterThan(2.0):
p, at the start of that call:
5.0, in the last line: