We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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?
=D>
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.
So instructions are as follows:
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.
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....
=D> @jeremydouglass
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
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) ....
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? Likevoid 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?
Yes.
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
The real problem here is that if we want to create a new class
D
we also have to remember to modify thedoBrushTeeth
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:
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.
abstract class
can have both non-implemented & implemented methods in its body.interface
's fields are bothstatic
&final
implicitly! b-(abstract class
, the same 1 onlyextends
rule applies, like any regularclass
. :(implements
as many interfaces as we want to. >-)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.
@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
I was still talking about
abstract class
there! :-\"Instance field is a non-
static
field. :-BUp to Java 7, all methods inside an
interface
are implicitlyabstract
.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 bothfinal
&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 besidesabstract
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?
That's extremely unlikely b/c we can have instead
abstract
getters & setters for fields. 8-XAnd 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.