Brushing your teeth algorithm

Did anyone tried to write the instructions for the task: How to brush your teeth, which is in Learning Processing book ? If you have, please share your set of instructions.

Comments

  • Why don't you share what you came up with and we'll go from there?

  • Directions unclear: Toothbrush stuck in toaster. Was I supposed to put my shoes on first?

  • I agree with @KevinWorkman -- @Noor_1995, you should go ahead and share yours to start off.

  • edited October 2016

    So instructions are as follows:

    Step 0: Take out the toothbrush from the holder. 
    Step 1: Hold it in your dominant hand.
    Step 2: Open the tap and Rinse your toothbrush bristles.
    Step 3: Rinse your mouth and close the tap. 
    Step 4: Take out the toothpaste container from the holder and open it.
    Step 5: Squeeze toothpaste container from the bottom and put some paste on toothbrush bristles.
    Step 6: Close the toothpaste container and put it back in the holder.
    Step 7: Close your mouth with upper front teeth and lower front teeth joined.
    Step 8: Place your toothbrush on your joined teeth.
    Step 9: Move toothbrush one centimeter up and then move one centimeter down. Repeat 10 times respectively.
    Step 10: Move your toothbrush one centimeter to left and then move one centimeter to right. Repeat 10 times respectively.
    Step 11: Stop and open your mouth 4 cm wide.
    Step 12: Change position of toothbrush bristles to right lower bed of teeth
    Step 13: Move 1 cm further and then move 1 cm back. Repeat 10 times.
    Step 14: Change position of toothbrush bristles to left lower bed of teeth. Repeat Step 12 on teeth.
    Step 15: Change position of toothbrush bristles to right upper set of teeth. Repeat   Step 12.
    Step 16: change position of toothbrush bristles to left upper set of teeth. Repeat Step 12.
    Step 17: Spit the liquid from the mouth.
    Step 18: Open tap to medium flow.  Rinse your mouth and toothbrush bristles.
    Step 19: Close the tap. 
    Step 20: Repeat Steps from 6 to 17.
    Step 21: Open tap to medium flow. Wash your mouth and toothbrush. 
    Step 22: Close the tap. 
    Step 23: Put toothbrush in the holder
    Step 24: Check your teeth in the mirror.
    Step 25: switch off the washroom light and leave the washroom. 
    
  • I have modified the written instructions so they are formatted for the forum and deleted the extra posts.

  • Hey how did you do it ? Can you teach me, I am new here.

  • Click on Discussions link then read the first discussion in the forum

  • This exercise doesn't have a right or wrong answer. It's supposed to make you think about taking a process and breaking it down into smaller steps. Even something that seems obvious to you often can (and should) be broken down even further. Have you tried giving this to your friend and having them try to fine places where your directions are unclear?

    For example, step 0 says to take the toothbrush from the holder. What if I use my foot to take it out? Step 1 says to hold it in my dominant hand. But what if I hold it by the bristles? Or hold it so it's pointing the wrong way? Step 2 says to open the tap. What if I open my shower tap?

    Again, this doesn't mean your steps are wrong. The whole point of the exercise is to get you thinking about breaking things down and making them as clear as possible. If you're doing that, then you're doing it right.

  • I will make it more clear. Thank you for the feedback.

  • Again, the point of the exercise isn't to have a perfect set of instructions. That's impossible, because another person can always poke holes in what you come up with. The point of the exercise is to get you thinking about breaking things down into smaller pieces and trying to understand the assumptions you make when going through a process that seems obvious to you.

  • Hm. I understand your point. Thanks once again.

  • edited October 2016

    It doesn't have a right answer, but that would make it interesting to have several people try to answer in this thread, showing different creative ways of problem solving, or problem spaces, or problem-anticipation. For example, context, setup, dealing with conditions, or subroutines....

    1. If going to bed, or waking up, or just finished a meal:
    2. Remember when you last brushed your teeth.
    3. Can you remember? Was it a while ago?
    4. Do you have a toothbrush? If not, stop planning to brush and start planning to get one. Getting could mean buying, or requesting (if you are a Guest).
    5. If you have a toothbrush, remember what room your toothbrush is in.
    6. If the room is nearby, go there.
    7. If the room is a bathroom, and the bathroom is occupied, wait, otherwise go in.
    8. If you are in the room, remember where your toothbrush is in the room and go to that part of the room.
    9. If the toothbrush is inside something, that thing is called a holder. A holder could be a cup, a case, a cabinet, a drawer, a suitcase, or an unusual thing.
    10. If the holder can be opened, open it.
    11. Now follow the @Noor_1995 instruction list "how to brush your teeth."
  • But that's programming:

    break it down in small steps

    make functions with good name for the steps

    Make classes

    Imagine giving commands to a friend / servant and see, what went wrong

    Try to write code that others can easily read

  • edited October 2016
    1. Are you wearing a retainer, or headgear? If so, remove first.
    2. Are the teeth removable (dentures) or fixed in place? If dentures, remove teeth for cleaning....

    Imagine giving commands to a friend / servant and see, what went wrong

    Imagine giving commands to an intelligent alien who knows nothing about this planet and has no common sense, then see what went wrong.

  • Directions still unclear. Toothbrush still stuck in toaster. But at least now you understand the point: Code is, essentially, directions for your computer. Writing good code is exactly like writing good directions. Once you learn about the kind of directions that your computer can understand exactly, you will be able to instruct it to do exactly what you want.

  • Brushing teeth might also be a type of instruction set that is broadly the same, but individual steps differ based on whose teeth are being brushed!

    In code, that difference might either mean that the brushTeeth function accepts different objects (Adult, Infant, Dog, Hippo), or it might mean that each object has its own distinct brushTeeth method (Adult.brushTeeth(), Hippo.brushTeeth) ....

  • brushTeeth function accepts different objects

    How do you do that? When you talk about objects with distinct brushTeeth() methods, I get that. Class Dog has its own brushTeeth(), for instance. But how do you write a method accepting different object types? You would need to write a different version of the method for every object, right? Like void brushTeeth(Infant i){}, void brushTeeth(Adult a){}, void brushTeeth(Dog d){}, void brushTeeth(Hippo h){}

    I'm still very confused about Abstract objects/classes, and I feel maybe this could be connected in some way. Am I right?

  • But how do you write a method accepting different object types?

    Make an interface called Entity or Being.
    Then make all those "being" classes to implements it:
    https://Processing.org/reference/implements.html

  • What about brushTeeth(Object o)

    and then switch(o.typeOf) {

    Not possible?

  • @GoToLoop suggestion of using an interface is the best way to do it.

    An alternative is to use inheritance with polymorphism e.g. a method in the base class called brushTeeth() overridden in the child classes.

    Passing an Object could work like this

    class A {
      void brushTeeth() {
        println("A brushing");
      }
    }
    
    class B {
      void brushTeeth() {
        println("B brushing");
      }
    }
    
    class C {
    }
    
    Object obj;
    
    void setup() { 
      obj = new A();
      doBrushTeeth(obj);
      obj = new B();
      doBrushTeeth(obj);
      obj = new C();
      doBrushTeeth(obj);
    }
    
    void doBrushTeeth(Object o) {
      if (o.getClass() == A.class) {
        ((A)o).brushTeeth();
      } else if (o.getClass() == B.class) {
        ((B)o).brushTeeth();
      } else if (o.getClass() == C.class) {
        println("No teeth here");
      }
    }
    

    The real problem here is that if we want to create a new class D we also have to remember to modify the doBrushTeeth method (and any similar method) that's why we use intefaces.

  • @GoToLoop So in the documentation example, interface Dot would be your Being, classes CircleDot and SquareDot could be Dog and Hippo, and brushTeeth() would be the method, right?

    But all this would only be useful if I needed to tell an animal to brush its teeth without my knowing what species the animal is of, right?

    Say I want the animal to brush its teeth and I don't know what the animal is. Then how would this appear in code?

  • @quark Wow I didn't know class Object. Opens a new realm!

  • @Moxl Consider this code:

    interface Being{
      public void brushTeeth();
    }
    
    public class Dog implements Being{
      //Constructor
      public Dog(){
      }
      //now a brushTeeth() function is necessary to prevent an error
      public void brushTeeth(){
        println("Dog brushing");
      }
      //Extra functions can be added
      public void bark(){
        println("Woof woof!");
      }
    }  
    
    public class Human implements Being{
      //Constructor
      public Human(){
      }
      //now a brushTeeth() function is necessary to prevent an error
      public void brushTeeth(){
        println("Human brushing");
      }
      //Extra functions can be added
      public void work(){
        println("Human working");
      }
    }  
    
    void setup(){//I don't care for "public" here due to Processing pre-processor  
      Human h = new Human();
      Dog d = new Dog();
      doBrushTeeth(h);
      doBrushTeeth(d);
    }
    
    public void doBrushTeeth(Being b){//accept any being, including humans and dogs
      b.brushTeeth();
    }
    
  • See the doBrushTeeth function - it accepts both dogs and humans because they are both "beings", since they both implement the interface "Being".

  • @Lord_of_the_Galaxy Thanks so much! Okay great, I got it now. So an interface is some sort of "class" with no constructor, just listing method names shared by different classes. You still have to write the corresponding method in every class.

    Can classes implement more than one interface?

  • Yes. Please read up about interfaces in Java for more info.

  • edited February 2017

    You still have to write the corresponding method in every class.

    • abstract class can have both non-implemented & implemented methods in its body.
    • Besides, they can also have instance fields too.
    • In turn, all interface's fields are both static & final implicitly! b-(
    • However, in order for a subclass to inherit from an abstract class, the same 1 only extends rule applies, like any regular class. :(
    • While we can implements as many interfaces as we want to. >-)
    • For more Java OOP knowledge, go to this tutorial link below: ~O)
      http://docs.Oracle.com/javase/tutorial/java/concepts/index.html
  • @GoToLoop Excellent explanation. In fact, you should search about inheritance too in Java.

  • Thanks guys. Great advice.

  • Besides, they can also have instance fields too.
    In turn, all interface's fields are both static & final implicitly!

    @GoToLoop What is an instance field of an interface and the interface's fields? An interface cannot be instantiated so I understand. Are you referring to the fields in the interface inherit by its children? Another question is why are they static and final? Is there a reason to be so? (Not like we have an option so it seems)

    Kf

  • What is an instance field of an interface and the interface's fields?

    I was still talking about abstract class there! :-\"
    Instance field is a non-static field. :-B

    Another question is why are they static and final?

    Up to Java 7, all methods inside an interface are implicitly abstract.
    However, Java doesn't offer abstract fields.
    They can't be simply declared w/o also initializing them in an interface. :(
    If we insist to have them inside an interface, they're implicitly both final & static. 3:-O

  • @GoToLoop Thanks for your explanation. You talk about Java 7 with sad faces... are things different in the future? Like for example... in Java 8?

    Kf

  • @kfrajer, Java 8 interfaces can also have static & default methods besides abstract 1s: ~O)
    http://JournalDev.com/2752/java-8-interface-changes-static-method-default-method

  • I hope Java sometime allows for abstract fields also. It will make things very much easier (for example, you may be able to convert many classes into interfaces then, which will mean we can inherit from multiple interfaces). Do you think they may do this sometime soon, by say, Java 10?

  • edited February 2017

    I hope Java sometime allows for abstract fields also.

    That's extremely unlikely b/c we can have instead abstract getters & setters for fields. 8-X
    And allowing direct access to fields is bad etiquette under OOP paradigm after all. :>

  • @GoToLoop you're the expert, so I guess you're right. And I didn't realise getters and setters can be used. Thanks for the idea.

Sign In or Register to comment.