Now we're going to take a look at another abstract data type that is a pretty interesting way to give us a simple way to create interesting graphical pictures. The ideas is called Turtle Graphics. So what a turtle is it's an idealized model of a plotting device and it was developed by Seymour Papert quite awhile ago actually as an idea for teaching children about computing. And so what we're going to do, we actually considered using Turtle Graphics for our basic standard draw model but we chose not to but with abstract data types, we can implement it. So we're going to write an abstract data type that lets us write Java programs that manipulate turtles. So we think about a turtle that has a pen and it's actually represented graphically with little bug. And what turtles have, their set of values is a position and an orientation. It's pointed in a certain direction. So it might be in the middle of the screen, again, between zero and one pointing up or X 0.25, Y 0.75 pointing up and to the left or down low pointing almost right. Every turtle at any time has its set of values is its position and its orientation. And the operations that a turtle can perform, well, first of all we have to be able to create a turtle and we create it with XY an angle and same way as for charges. A turtle can turn left a certain amount. We give it a number of degrees counterclockwise that it should turn and it can go forward a certain amount and when it goes forward, it draws a line. Turtles got a pen underneath it. And that's it. Those are the only operations that a turtle can perform. And remarkably, this turns into a sophisticated drawing device. So again, we're going to implement a turtle abstract data type by doing instance variables, constructors, methods and a test client and so we want to implement the test client first. So what we're going to do is implement a test client that draws a triangle. So, we'll create a new terminal down at the left corner pointing to the right. And then we'll instruct it to go forward one unit, and when it goes forward, it draws a line and then we want it to turn left 120 degrees, and now it's pointing that way. We go forward again drawing a line, turn left 120 degrees again, go forward and draws a line to complete the triangle. That's our test client, that's what we expect the turtle to do and we'll have a turn left again just to reorient itself to be ready for the next challenge. Turtle test client, that's the first thing that we implement. So, you can see that with a sequence of turn left and go forward instructions, you can think about creating different interesting kinds of drawings. For example, this client drew the triangle and never had to compute square root of three. So that's why maybe for just thinking about controlling this turtle can get children to think about computation, that was Papert's idea. Okay. So what do we need now? We need a constructor. So a constructor, probably you can imagine, it's very simple similar to the charge constructor. We have our instance variables. All you do is since instant variables have been structured at the same time. In this case, they're not final. The whole point of the turtle is for the values to change and then the constructor just sets them just as before. So these two are very much like our charge client. We have instance variables, we provide variables constructors, set those to the values of the instance variables and every turtle now has its own instance variables. So now we've got, next we have to do the methods. We have two methods; we have to turn left and go forward. So turn left, just add delta to the angle, the one-line implementation. Go forward; that's actually where the math gets done. We keep track of the old x and y coordinates and then the distance that we have to go times the cosine of the angle has to be in radians in Java's math function. So you can do this math to figure out the change in x, y coordinate if you want to go a distance d of the angle and that's just doing that math and now we use standard draw to draw a line from the old position to the new position. And notice this x plus equals y plus equals that changes the value of the instance variables for that turtle. That's what brings it to the next thing. And again, this code in here inside the method is referring to x and y without having declared them, that's because they're instance variables. So those are our methods and now we have the availability of a turtle graphics package with just this simple interface. That's the full implementation summarized just as before. Instance variables, constructor, methods and then a test client that draws a triangle. So here's an example of something we can do with the turtle that's very simple. We can draw an Ngon. We'll take the value N from the command line and so the angle we want is 360 over N. If N was three, it was 120, N is four it will be 90 and so forth and then this just computes the step size that means that the thing will fit inside the unit square and I'll skip that math for now. We create a new turtle x-coordinate halfway through y coordinate zero and then angle over two actually, and then N times we'd just say go forward, turn left and that's it. Amazingly, with so if we say three, that's going to draw the triangle with that orientation. If we say seven or if we say 1440, we get almost a circle. And again, pretty simple small amount of code you might have to do quite a bit of math to figure out where to put these points to draw an N-gun just using standard draw itself. And we can actually get an even more interesting client just by changing this just a little bit. In this case, we suppose that we have kind of a lazy turtle or maybe a turtle that gets tired and we can take as a command line argument, the degree to which the turtle gets tired. So and what that does mean every time we tell it to go a certain distance, it goes just a little bit less according to this decay parameter. And then we spin around the Ngon lots of times but since it's going a little bit less each time, we actually get something that spirals in to the middle and that's kind of a strange look with a triangle with a seven gon that's an already an interesting pattern and that's where the circle with a slow decay. This figure is called a Spira Mirabilis and again, you might think about how you would try to plot that just using a standard draw with Turtle Graphics. It's really just a simple modification of our Ngon code. And this brings us to nature. This pattern actually appears often in nature. It's the shape of a nautilus shell. It's the shape of a hurricane and they can go right or left in actually galaxies or even in architecture, people use Spira Mirabilis. So modeling these kinds of situations is something that we can easily do with a turtle which is an abstraction that we just created for doing drawings. Just before moving on, we're going to give a quick pop quiz on object oriented programming. This code has a serious bug. What is it? If you put a declaration of something that you intend to be an instance variable, it creates a new local variable with the same name and that's different from the instance variable. That's called shadowing. In this case, when you say double x equals x0, the instance variable isn't changed at all. The variable, a local variable x with inside the constructor is changed but then when the constructor exits, that has no effect. And this is called again shadowing and everyone should stand up and repeat this pledge saying "I will not shadow instance variables," that means don't declare them inside the constructor. This is a very insidious and difficult bug and every beginning program where when they write their first few object-oriented programming, will make this mistake and it's a difficult one to trace. The instance variable values just don't change and it's hard to figure out why just looking at the code.