You get the hat after you finish school porbably a few hours after your tama is an adult. Then return to tamatown and go to the school. Left of the chalkboard there is a grad cap and gown. Click both and logout to get it unless you want to do more. Then enter the password from v4 tamatown into your tama and presto- your cap.
When you graduate school and become an adult you got to tamatown and then the school. There Mr.Turtle will say "Have you gotten your graduation suit and hat for the graduation ceromony?" You click on them and then get your code! Hope I helped!!