Cake objects, variables & functions

baby show er cakesThe very first things we need to talk about are Cake objects. Objects are simply arbitrary collections of variables and functions, and they are defined in classes.

VARIABLES

Woah, woah, hold up! What’s a variable? Its name is a clue  – a variable is simply a small holder or address for a value that might change. Say you have a target number of cakes to make – 8. You don’t want to set that value of cakes in stone in case more people turn up to demand cakes, or some people drop out of the cake party. If you store that “8” in a variable, then you can refer to it elsewhere by name rather than having to remember “8”, change it as needed, and always be sure that whenever you refer to it by name, the computer will go and look up the latest value and tell you what it is.

Before you create a variable, you have to decide what type of variable you’d like. Processing is quite picky, and won’t let you mix and match different types easily. Remember: a lot of coding is about being really *really* specific and precise!

The most common sorts of variables that we’ll be dealing with are:

  • whole numbers (e.g. 1, 7, 0, 999) or ‘integers’ are defined as an int
  • a bunch of text (e.g. “hello”, “cake”, “chocolate is good for you”) is defined as a String
  • true or false values – known as a boolean (we’ll meet these again a bit later, but they are useful when you want to switch between two options because they can only hold the value “true” or “false”, nothing else)

You create variables in Processing by telling it what type you’d like, giving the variable a name, and actually giving it a value using a single “=” sign, like this:

int size = 8;
String cakeFlavour = "chocolate";
boolean doYouLikeCakes = true;

Sometimes, you know you want a particular variable to store information in, but you don’t know its value – so you can just declare the type and name and worry about what to put in it later:

int size;
String cakeFlavour;

This makes what you are defining more abstract, and therefore more flexible.

Part of being precise (and computers not being that smart) means you need to tell Processing when you’ve finished writing a bit of code by adding a semicolon – which is why each line above ends with a “;“. When the computer actually read what you’ve typed, it will ignore the line breaks, so you have to use a different symbol to help the computer understand what you mean. (Other languages use different ways of determining when you’ve finished a coding statement – e.g. Python uses new lines and tabs!).

Variables are essentially an object’s attributes or characteristics – so every Cake we make will have a size and a flavour, for example.

FUNCTIONS

Functions (also known as methods) are actions that a Cake can perform – for example you might want a function for each Cake to increase its size because little Johnny from next door has turned up unexpectedly and is wanting cake:

void makeBigger()
{
  size = size + 1;
}

This function just takes whatever value is in the “size” variable, adds 1 to it, and sets the result back into the “size” variable – so each time you call this function, the cake object’s size increases by 1.

A common method for objects in Processing is for them to have their own draw() method, which simplifies the headache of having to try and remember how to draw a bunch of different objects individually; if each one has its own draw() method, then you can just ask each object to go off and draw() itself (go draw() yourself!) rather than faffing about trying to work out what type of object you’ve got and doing a different sort of drawing for each one.

*tip* The “void” bit in a function declaration just means that the function doesn’t return anything back to whatever called it. You can have different sorts of return types for functions, because you might want to send some information back to the caller – for example, if you wanted to tell whoever asked you to increase your cake size what the new size is, you could do this:

int makeBigger()
{
  size = size + 1;
  return size;
}

The “return” bit simply sends the value size back to whoever called makeBigger() function on the cake object. Other common reasons for defining a return value – especially a boolean (remember those? gotta be either “true” or “false”) – is to indicate whether a function has been successful or not.

*Caution* the computer will check that the value you return (here an int) is the same type as the type in the method declaration (i.e. the bit before the method name). Here we have matchy matchy ints so the computer should be pleased with us and not go bing.

CAKE OBJECTS

Now we know about Cake variables and functions, let’s make some Cake objects! First declare a Cake class using curly brackets to enclose all our Cake-related info like this:

class Cake
{
   String flavour;
   int size;
}

Here I’ve arbitrarily decided that all Cakes should have 2 particular variables: a flavour (which we’re just storing as a String i.e. a bit of text), and a size (which we’re storing as an int i.e. a whole number). I reckon those are two pretty important properties of any given cake :). Let’s arbitrarily agree that the “size” variable denotes the diameter of the Cake in centimetres.

Next we need to know how to make Cakes. Every class has a special function called a constructor, which pretty much is exactly what it says on the tin: a function to construct Cake objects. The return type of the constructor function is the object itself – i.e. a Cake! So whatever bit of code calls this gets returned a shiny new Cake object :). Yay!

The constructor might need some variables sent off to it (e.g. you might want to specify a flavour), or it might just be able to give you a Cake if you ask it without any more information.

Constructor without variables:

Cake()
{
  // set some default values for flavour and size[/simple_tooltip]
  // since we don't know what is wanted
  flavour = “chocolate”;
  size = 20;
}

*tip* the “//” double forward slash creates a special line known as a comment – because you’ll need to add notes to yourself in code (and possibly swears), the computer ignores anything on a line after a // and anything between a /* and a */ – use the latter for multi-line comments (i.e. lots of swearing).

This constructor creates a rather inflexible sort of Cake: you can only have chocolate and you can only have it 20cm in diameter (oh nooooooooooo). You’d use this sort of constructor when the objects you’re making are all pretty similar (because by default they’ll all start off the same). You could of course change the flavour and size variables after making the Cake, but let’s make it simpler for us to make bazillion different kind of Cakes, from 1cm banana-flavoured dainties to frankly ridiculous 300 cm sized triple chocolate fudge Cake to end all cakes, by adding the ability to send variables (known as ‘arguments’) off to the constructor:

Constructor with variables:

Cake(String f, int s)
{
  // set up our flavour and size variables as requested
  flavour = f;
  size = s;
}

As above, an equals sign “=” means setting a value into a variable. Once we’ve created our Cake object, the “flavour” variable will now contain whatever was sent to the constructor in the “f” variable, and the “size” variable will contain whatever value was sent to the constructor in the “s” variable.

Woop! That’s better! Now when we create our Cake objects, we can specify some of their properties. Now we put our constructor in our class like this:

class Cake
{
  String flavour;
  int size;

  Cake(String f, int s)
  {
    // set up flavour and size 
    flavour = f;
    size = s;
  }
}

Now elsewhere in our code (outside the Cake class), we can create our teeny banana Cake objects like this:

Cake teenyBanana = new Cake("banana", 1);

Notice the ‘new’ keyword. This indicates that we would like a new object please – so the computer goes and looks up the matching class for the object (here the Cake class) and calls the appropriate constructor, and gives us back the object was asked for – here we’ve created a “teenyBanana” Cake variable!

Also note that here our with-variables constructor states that it needs to have a String and an int sent off to it in that order before it will deign to return us a Cake – if we try and call new Cake(1); or new Cake(1, “orange”) then the computer will say no :(.

Along the same lines we can make a new chocolate behemoth Cake object like this:

Cake enormoChoco = new Cake("triple choc fudge", 300);

Now we’ve got some cake objects, we can take a peek at their variables or call their functions using a dot “.” after the object name, followed by either their variable name or the function name. If you’re calling a function, you also add the rounded brackets afterwards, like this:

// variables
String choc = enormoChoco.flavour;
int banSize = teenyBanana.size;
// functions
enormoChoco.draw();
int biggerBan = teenyBanana.makeBigger();

the banana crecheNote that not all objects let you look at their innards this way – because if you can look at it, you can also change it simply by assigning another variable. Imagine the carnage if Little Johnny decided to alter the teenyBanana cake size without telling anyone:

teenyBanana.size = 999;

But discussing making variables private is a topic for another day :). Just remember that you can’t *always* access class variables using the dot notation.

An important part of learning to code is to figure out how best to divide real-word objects up so you can represent them in code, whether it’s a Cake or a space invader or whatever. You’ll often want to create new classes to create new objects, and a good way to start is just figure out a few basic variables and a constructor. You will probably go back later and add more stuff as you get deeper into your code and start creating Cake objects, when you decide you really need a cake with sprinkles or sparklers or whatever.

Right then! We have some Cakes, now let’s take a look at our kitchen equipment to help us do interesting things with them :).

NEXT ARTICLE: CAKE HOLDERS >>

Bookmark the permalink.

Comments are closed