Want to learn how to brush your teeth? First you’ll need to learn how to program a computer.


Wake up – 3:59 AM. You always tell people you rise and grind before 4. You stretch your shoulders, stretch your wrists, stretch your fingers. You can’t afford getting Carpel Tunnel Syndrome. You jog to your ergonomic reading chair and light an unscented candle. The scents are a distraction. Read the same seventeen pages of poetry as yesterday. Still doesn’t make any sense. You jog to the nearest pet. Pet the pet. That’s what they’re made for. You jog to the bathroom. This is it. This is what you’ve been waiting for all morning. It’s your opportunity; seize it. You brush your teeth.

What does that mean, anyway? There is a lot of information coded inside that phrase. You’re referring to a toothbrush, not a paintbrush. You’re going to add a minty cleaning goo to your toothbrush. You’re going to add a “pea-sized” bit of delicious minty cleaning goo. You’re not going to swallow that tempting minty cleaning paste either. You’re going to brush all of your teeth, not just your favorites. You’re going to keep brushing at your teeth (gently) for three(ish) minutes. Finally, you’re going to floss. Wait, do you floss at the end or at the beginning?

When we say we’re going to “brush our teeth” we’re actually describing a very personalized ritual that has a lot of inputs and outputs that can change based on the situation. On top of that, brushing our teeth is generally though of as a part of another routine, which is “getting ready for bed” or “getting ready for the day”. What we’re describing is a function. If we had a computer that told us what our morning routine was, we might have a function that looks something like this:

Routine today = getRoutine(Saturday)

This is saying that today is a routine that is the result of our getRoutine function when we tell it that today is Saturday. If it was Tuesday the routine might be completely different, but it would still be the routine of today. And if we wanted to see what the logic for determining the routine of the day was, it might look like this:

Routine getRoutine(String Day) {
   wakeUp()
   eat(Breakfast)

   if (Day is NOT Saturday or Sunday)
      work()

   eat(Lunch)

   if (Day is NOT Saturday or Sunday)
      work()

   eat(Dinner)  
   goSleep()
}  

That’s what every day of your entire life might look like! Don’t think about that too much. If we zoomed into the wakeUp function, that might look like this:

void wakeUp() {
   getUp()
   jogRecklessly()
   readPoetry()
   brushTeeth()
   shower()
   getDressed()
}

This is how computers work – they interpret commands organized into functions called by other functions. Things can get very abstract very quickly, so lets focus on just the brushTeeth() function. Teaching a computer what it means to “brush our teeth” will teach us how to communicate with a computer.

Before we begin let’s take an easy solution for setting up a programming environment: search for java online compiler. I’m using the one from W3Schools. There should be a section for code, output, and some sort of “run” button. If you click that button, you should see some kind of confirmation text in the output panel; usually something like “Hello World!”.


The Anatomy of a Function

Look at this junk. All these characters on a page. What do all these words mean? Why are some colored in? The reason why it’s so hard to get into programming is because there are so many things that you need to know at the very beginning. I can promise you, however, that once you get the base level of knowledge down, it starts to build up pretty quickly. We went over the basic, general stuff in the last section. Lets start to break down these words here.

A program is a list of commands wrapped in functions. A line of code is our basic building block, which you’ll see somewhere on the page of the online compiler:

System.out.println("Hello World!");

The whole line of code here is what tells the output to say “Hello World!” Here’s what each section means:

  • System.out.
    • This is where the computer can find the function that writes the message to the screen. If you imagine the function is a tool inside of a drawer, this is like telling the computer “in the kitchen, next to the fridge…”
  • println(…)
    • This is the name of the function that writes the message to the screen. “Print” is a bit of a misnomer, it refers to printing text to a terminal. Keep in mind that you might need to discard some of your intuition as you start talking computer.
    • Inside of the parenthesis are variables, the parameters for the functions. They have types (like numbers and text) that we’ll get into later.
    • The documentation for this function can be found here, but its not critical that you understand this all right now.
  • “Hello World!”
    • This is the variable that is passed into the function println
    • Because this is a text variable, it’s wrapped in quotes.
  • ;
    • In most programming languages Semicolons indicate that this is the end of a line of code. It’s like a period for a programming sentence – the computer stops looking for more text and executes what it sees.

That’s one line of code done! Every other line of code is going to have similar components.

Now if we look around the function, we’ll see that before and after it are brackets like these {...}. To a computer, brackets mean to group the code inside together. There are a few uses for this, but in this context they’re describing the interior of a function. In this case, we’re looking at:

public static void main(String[] args) {...}

This is a function, a shorthand for a computer to execute a series of commands inside the brackets. Lets look at what this function is saying:

  • public
    • This is the access modifier for the function. You’ll probably only need to know public and private – it’s just a way to keep information secret if needed.
  • static
    • This is another complicated concept you won’t need to understand yet. We need to know a lot about objects to make sense of this, but think of it like a universal concept that we can call from anywhere.
  • void
    • This is the return type of the function. In this case, we use void because we don’t care what this function has to say at the end of it.
  • main(...)
    • This is the name of the function. Every time we call a function inside our code, it’ll run this collection of commands.
    • main has a meaning that is specific to the Java language – the computer will look for this function to run it first, and we won’t call main elsewhere.

Inside of the function declaration is the list of variable parameters. All that matters is that you know how to pass variables into functions.

But if you’re really curious about String[] args
  • String[]
    • This is the variable type. A String is a collection of characters in double quotes like “text”. We’ll use these later.
    • We can also have objects and arrays as return types. Objects (denoted by the name of the object) are a representation of a group of variables, and an array (denoted by the straight brackets []) is a collection of the same type of variable.
    • This is an array of strings. If we had a string array of toothpaste brands, it might be [“crest”, “colgate”, “toms”].
  • args
    • This is the list of arguments (parameters) for the function.
    • In the context of main, the arguments here are being read in from the command line. Don’t worry about that right now.

Those are the definitions! Hopefully now the code that is present makes sense. It might take a few reads for it to stick. Whenever you’re feeling confident, we can get to putting it to use.


Lets try making our own function for brushing our teeth:

public static void brushTeeth() {...}

  • public static
    • This is a public and static function that can be called anywhere, although that’s not relevant at this program size.
  • void
    • We don’t need a return type for this function.
  • brushTeeth()
    • This is the name of the function, and right now there are no variables that we need. Otherwise they would be included inside the parenthesis.
    • Programmers tend to write variable and funciton names with camelCase for readability.
  • {...}
    • The commands will end up inside of the brackets here.
    • Lets move that println command into the brushTeeth function have have it say something like “Brushing Teeth…”

With all of that in place, we can call our new function inside of the main function, and after we run it we’ll see our new output:

At its heart, programming is just a lot of functions nested inside eachother

Lets give our function a variable, like a length of time to brush. Inside the parenthesis for the declaration of brushTeeth lets add a variable. These are the variable types we’ll be using:

  • int short for integer, a whole number.
  • String a string of text, wrapped in double quotes like “text”.
  • boolean a true or false value
  • void which means nothing, or no return value.
But there are a few more worth knowing about…
  • float floating point, think decimal. The article might just confuse you.
  • char character, a single character in single quotes like ‘a’.
  • array a group of another type of variable inside brackets, like int[]

In this situation, we can make an int called minutes inside the function declaration.

public static void brushTeeth(int minutes) {…}

. Whatever value we enter into the function can be called by name later. Lets display that number in the output by adding it to the println function:

System.out.println("Brushing Teeth for: "+minutes+" minutes");

We can string together strings (for lack of a better word) by using the plus symbol. There are a handful of ways to do string interpolation but I think this one is the easiest to understand.

Once we’ve put in the effort to understand what it is that we want, we can ask for it correctly. That’s applicable to a lot of things in life. What else can we describe about brushing?


Objects and Classes

The first thing that jumps to mind is the tool for our task, the humble yet mighty toothbrush, the cavity’s crusher and enamel’s enamored. In order to teach the computer we have to ask ourselves: What does it mean to be a toothbrush?

  • It has some kind of bristles.
  • It might have toothpaste on it.

We’ll need to create a new class (we already have one, the Main class) to model this to the computer. Lets call it a Toothbrush. We can use string variable type to model the bristle and boolean to model if it has toothpaste or not.

class Toothbrush {
   String bristleType;
   boolean hasToothpaste;
}

Then, in order to set these values when we create a toothbrush we need to make a special type of function inside the class called a constructor.

public Toothbrush(String bristle, boolean toothpaste) {
    	bristleType = bristle;
        hasToothpaste = toothpaste;
    }

It’s a public function with the same name as the class, and the variables inside the function directly relate to the variables we want to set. Classes can have multiple constructors for different situations. Would a toothbrush ever have toothpaste to start? Maybe you prep your toothpaste the night before to be nicely dry-aged for the morning. What do I know. For the rest of us, we can suffice with a constructor that sets that hasToothpaste variable to false like this:

public Toothbrush(String bristle) {
    	bristleType = bristle;
        hasToothpaste = false;
    }

Then, to use this constructor and create this object in our code, we can go to the main function and call

Toothbrush brush = new Toothbrush("soft");

This designates an object called brush that is a Toothbrush (instead of a String or any other type) and is a new Toothbrush whose bristle is described by the constructor as “soft”. You could make a “hard” bristle toothbrush if you’d like, but those can damage the gums and are not recommended for daily use.

What makes it new?

In that line of code, new is a reserve word, meaning that it has a special meaning to the computer. We’re telling the computer to make a new object with that constructor, which is different from how we declare new variables.

Try running it again and lets see what happens:

Nothing broke! It’s important to celebrate the little things. With the back-end wired up, lets implement that toothbrush object into our brushTeeth function. You can add a parameter to the function to take in a Toothbrush brush.

public static void brushTeeth(int minutes, Toothbrush brush) {...}

Then make a new println command to display the value we set for the bristle with brush.bristleType (which is a string) just like we did with minutes.

System.out.println("Getting my "+brush.bristleType+" toothbrush");

Calling the name of that object’s variable returns the value for that instance of the object. In this example, it should print “soft”. The last thing left is to pass that brush object we made into the brushTeeth function:

brushTeeth(10, brush);

10 minutes might be a little long, but you can never be sure.

We’ve taught the computer what a toothbrush is and how to use it – now it has no more excuses to not brush!


Conditional Logic

Finally, lets add some toothpaste to this brush. Within the main class, after the brushTeeth function, lets make an addToothpaste function that takes in a toothbrush. What we want it to do is check if the toothbrush doesn’t have toothpaste, and if so, add some. We can use that hasToothpaste variable to check.

public static void addToothpaste(Toothbrush brush) {
   if (brush.hasToothpaste == true)
      System.out.println("No more toothpaste needed");
   if (brush.hasToothpaste == false){
      System.out.println("Adding toothpaste");
      brush.hasToothpaste = true;
   }
}

The if statement checks if the value inside the parenthesis is true or false. Because that hasToothpaste value is a boolean, it will return true or false and use the double equals to compare if true==true. That’s a little redundant, isn’t it? Lets simplify that.

Because hasToothpaste is already going to return true or false, we can remove that comparison. Then, we can use an else statement to describe what to do if that first check is not true. The else block only happens if the first check evaluates to false.

public static void addToothpaste(Toothbrush brush) {
   if (brush.hasToothpaste)
      System.out.println("No more toothpaste needed");
   else {
      System.out.println("Adding toothpaste");
      brush.hasToothpaste = true;
   }
}

Another key component of programming will be keeping an eye out for ways to simplify your own logic. Less cognitive load on your code will make it faster to understand the next time you look at it. Less operations the computer needs to perform will make it finish faster.

Lets add this addToothpaste function with our brush object to the main function and try calling it.

I’ve scrolled the toothbrush object out of view for simplicity’s sake.

It reads a little out of order, doesn’t it? What if we add that addToothpaste function to the brushTeeth function as well, so that this little step becomes a part of the greater step. We can observe that there is no more toothpaste needed because we already added some, and our conditional check is working!

The more I think about it the more that 10 minute brush time feels irresponsible. Lets change it up. Maybe if the brush is soft we brush for 3 minutes, but if the brush has medium bristles we only brush for two. How would that change the structure of our program?

First, we wouldn’t need to pass in that minutes variable into the brushTeeth function because we’re going to calculate it inside instead. Move that declaration of the int minutes variable to the top of the function instead. Set it to 0 to start with. We can set minutes to 3 if it’s soft, and if it’s medium we can set it to 2.

int minutes = 0;
if (brush.bristleType == "soft")
   minutes = 3;
if (brush.bristleType == "medium")
   minutes = 2;  

You might notice that we’re wasting a little bit of time here when we check if the toothbrush has medium bristles once we already know it’s soft. In those sorts of situations we can use an else if to skip the second check if the first one is already true. And if neither of these are true, a final else statement can pick a default value.

int minutes = 0;
if (brush.bristleType == "soft")
   minutes = 3;
else if (brush.bristleType == "medium")
   minutes = 2;  
else  
   minutes = 1;

Lets check out this logic by brushing our teeth with a few more toothbrushes. What if we had a soft, medium, and hard tootbrush that we used once each and then ended with using the soft one again, like we were sanding down something?

Please don’t sand down your teeth.

That brushTeeth function is self-contained enough that all we need to think about at the top level is which toothbrush to use. This is just the tip of the iceberg.

We could add a property for dirty to the toothbrush to indicate whether or not it needs washing. What if we had a toothbrush holder that had a list of toothbrush objects? Maybe we only use that hard toothbrush on Tuesdays. Just to get the tough stains out before pictures at the weekly pickleball game. You could turn the bristleType into an enum, a type of variable whose values can only be those from an approved list. You could get even more efficient by turning the minute calculator into a switch statement. Get creative with your implementations, add more classes and variables, and see if you can map out a whole morning routine.


The more things you can automate by explaining it to a computer the less things you need to worry about. What would your life look like if every day you had a routine handed to you so you didn’t need to stress about forgetting things? What else could you do with that time?



Leave a Reply

Your email address will not be published. Required fields are marked *