An Introduction to Java using Turtles Barb Ericson

An Introduction to Java using Turtles Barb Ericson ericson@cc. gatech. edu Feb 2005 Georgia Institute of Technology

Learning Goals • Create and initialize objects – Using the new operator – Passing values to initialize the new object • Declare variables – To use objects again • Send messages to objects to ask them to do something – Objects can do certain behaviors – You ask an object to do a behavior by sending it a message – Objects can refuse to do what you ask • Use turtles to draw simple shapes Georgia Institute of Technology

Computers as Simulators • “The computer is the Proteus of machines. Its essence is its universality, its power to simulate. Because it can take on a thousand forms and serve a thousand functions, it can appeal to a thousand tastes. ” Seymour Papert in Mindstorms Georgia Institute of Technology

Creating a Simulation • Computers let us simulate things – We do this by creating models of the things we want to simulate – We need to define what types of objects we will want in our simulation and what they can do • Classes define the types and create objects of that type • Objects act in the simulation Georgia Institute of Technology

We will work with Turtle Objects • We have to define what we mean by a Turtle to the computer – We do this by writing a Turtle class definition • Turtle. java – We compile it to convert it into something the computer can understand • Turtle. class Georgia Institute of Technology

History of Turtles • Seymour Papert at MIT in the 60 s – By teaching the computer to do something the kids are thinking about thinking • Develop problem solving skills • Learn by constructing and debugging something – Learn by making mistakes and fixing them Georgia Institute of Technology

Using Turtles • Classes created at Georgia Tech – As part of a undergraduate class • Add book. Classes to your classpath to use these classes Georgia Institute of Technology

Creating Objects in Java • In Java to create an object of a class you use new Class(value, …); – Starts with the new operator • Must be all lowercase – followed by the class name • Usually starts with an uppercase letter – followed by an open parenthesis • Above the 9 on the keyboard – Followed by any values used to set up the new object • There don’t have to be any values – Followed by a close parenthesis • Above the 0 on the keyboard – Followed by a semicolon (to end the statement) • To the right of the L on the keyboard Georgia Institute of Technology

Creating Objects • If you just do – new World(); • You will create a new World object and it will display – But you will not have any way to refer to it again – Once you close the window the object can be garbage collected • The memory can be reused • We need a way to refer to the new object – to be able to work with it again Georgia Institute of Technology

Saving a Reference to an Object • To save a reference to an object we need to declare an object variable and set the variable to reference a new object – Class name = new Class(value, …); • Examples – World world 1 = new World(); – Turtle turtle 1 = new Turtle(world 1); Georgia Institute of Technology

Turtle Basics • The world starts off with a size of 640 by 480 – With no turtles – World world 1 = new World(); • The turtle starts off facing north and in the center of the world by default – You must pass a World object when you create the Turtle object • Or you will get an error – Turtle turtle 1 = new Turtle(world 1); Georgia Institute of Technology

Creating Several Objects • You can create several World objects – World world 2 = new World(); • You can create several Turtle objects – Turtle turtle 2 = new Turtle(world 2); – Turtle turtle 3 = new Turtle(world 2); – One turtle is on top of the other in the world Georgia Institute of Technology

Moving a Turtle • Turtles can move forward – turtle 3. forward(); – The default is to move by • 100 steps (pixels) • You can also tell the turtle how far to move – turtle 2. forward(50); Georgia Institute of Technology

Turning a Turtle • Turtles can turn – Right • turtle 3. turn. Right(); • turtle 3. forward(); – Left • turtle 2. turn. Left(); • turtle 2. forward(50); Georgia Institute of Technology

Turning a Turtle • Turtles can turn by a specified amount – A positive number turns the turtle the right • turtle 3. turn(90); • turtle 3. forward(100); – A negative number turns the turtle to the left • turtle 2. turn(-90); • turtle 2. forward(70); Georgia Institute of Technology

Draw a Square • You can draw a square by doing • • turtle 1. forward(100); turtle 1. turn. Right(); Georgia Institute of Technology

Challenge • Create a World object – Don’t forget to declare a variable to hold a reference to it • Create a turtle object – Don’t forget to declare a variable to hold a reference to it • Use the turtle to draw a – Rectangle (but, not a square) – Diamond – Hexagon Georgia Institute of Technology

The Pen • Each turtle has a pen – The default is to have the pen down to leave a trail – You can pick it up: • turtle 1. pen. Up(); • turtle 1. turn(-90); • turtle 1. forward(70); – You can put it down again: • turtle 1. pen. Down(); • turtle 1. forward(100); Georgia Institute of Technology

Setting the Pen Width • You can change the width of the trail the pen leaves – World world 1 = new World(); – Turtle turtle 1 = new Turtle(world 1); – turtle 1. set. Pen. Width(5); – turtle 1. forward(100); Georgia Institute of Technology

Setting the Pen Color • Use set. Pen. Color to set the color of the pen – turtle 1. set. Pen. Color(java. awt. Color. RED); • There are several predefined colors – In the package java. awt • A package is a group of related classes – In the class Color • To use them you can use the full name – java. awt. Color. RED Georgia Institute of Technology

Setting Colors • You can change the pen color – turtle. set. Pen. Color(java. awt. Color. RED); • You can change the turtle color – turtle 1. set. Color(java. awt. Color. BLUE); • You can change the turtle’s body color – turtle 1. set. Body. Color(java. awt. Color. CYAN); • You can change the turtle’s shell color – turtle 1. set. Shell. Color(java. awt. Color. RED); Georgia Institute of Technology

More Turtle Behaviors • Turtles can move to a specific location – turtle 1. move. To(400, 10); • Of course, you can create many turtles and move them all – Run Turtle. Test • Creates 1000 turtles and puts them in an array of turtles. Has each turn by a random amount from 0 to 359 and go forward by 100 Georgia Institute of Technology

Objects can Refuse • Turtles won’t move completely out of the boundaries of the world – World world 2 = new World(); – Turtle turtle 2 = new Turtle(world 2); – turtle 2. forward(600); Georgia Institute of Technology

Objects send Messages • Objects don’t “tell” each other what to do – They “ask” each other to do things • Objects can refuse to do what they are asked – The object must protect it’s data • Not let it get into an incorrect state • A bank account object shouldn’t let you withdraw more money that you have in the account Georgia Institute of Technology

Summary • Create and initialize objects – new Class(); – new Class(value); • Declare variables – Class name = new Class(); – Class name = new Class(value); – To use objects again • Send messages to objects to ask them to do something – obj. Ref. message(); – obj. Ref. message(value); • Create a method – visibility return. Type name(Type name, …) – To reuse a block of statements Georgia Institute of Technology
- Slides: 25