210 likes | 388 Views
Simulations. Barb Ericson ericson@cc.gatech.edu Jan 2005. Learning Goals. Understand that object-oriented programs are simulations of turtles of robots of card games of fish. Computers as Simulators.
E N D
Simulations Barb Ericson ericson@cc.gatech.edu Jan 2005 Georgia Institute of Technology
Learning Goals • Understand that object-oriented programs are simulations • of turtles • of robots • of card games • of fish 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
History of Turtles • Seymour Papert at MIT in the 60s • 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 bookClassesFinal to your classpath Georgia Institute of Technology
Turtle Basics • The turtle starts off facing north and in the center of the world by default • World world1 = new World(); • Turtle turtle1 = new Turtle(world1); • Turtles can move forward • turtle1.forward(100); Georgia Institute of Technology
Turning a Turtle • Turtles can turn • Positive turns to right • turtle1.turn(90); • turtle1.forward(100); • Negative turns to the left • turtle1.turn(-90); • turtle1.forward(50); 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: • turtle1.penUp(); • turtle1.turn(-90); • turtle1.forward(70); • You can put it down again: • turtle1.penDown(); • turtle1.forward(100); Georgia Institute of Technology
More Turtle Behaviors • Turtles can move to a specific location • turtle1.moveTo(400,10); • Of course, you can create many turtles and move them all • Run TurtleTest • 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 world2 = new World(); • Turtle turtle2 = new Turtle(world2); • turtle2.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 • Ask don’t tell • Objects can refuse to do what they are asked • The object must protect it’s data • Not let it get into an incorrect state Georgia Institute of Technology
Challenge • Write a method to have a turtle draw some basic shapes • Square • Triangle • Hexagon • Circle • Spiral • Use these methods to draw a simple house Georgia Institute of Technology
Karel J. Robot • Set of classes developed to teach object-oriented programming Georgia Institute of Technology
Using Karel J. Robot • Karel code is in karelpremeir0407 • I modified the directories to match the package name • I also modified the constructor to be public • Follow the on-line book at http://csis.pace.edu/~bergin/KarelJava2ed/ch1/index.html • Add KarelJRobot.jar to the classpath. You may need to add the directory karelpremeir0407 as well. • Edit KarelMain.java to try things out • World.setVisible(true); // not visible to start • World.readWorld(String file) // reads the file • UrRobot karel = new UrRobot(1, 1, East, 0, Color.red); // create a robot at 1,1 facing east with 0 beepers and a red badge Georgia Institute of Technology
Robot Behaviors • move() // moves 1 block • turnLeft() // turns 90 degrees left • pickBeeper() // picks up a beeper • putBeeper() // drops a beeper • userPause(String message) // wait for user Georgia Institute of Technology
World Builder Georgia Institute of Technology
Using the World Builder • Double click on KarelJRobot.jar to bring up the world builder • Use it to position walls and beepers • Save it to a file • Modify KarelMain to read that file Georgia Institute of Technology
Robot Challenges • Create a maze of walls and write a method to get the robot through the maze • Have the robot create a pattern by dropping beepers (like a pyramid) • Have several robots work together to gather beepers Georgia Institute of Technology
Card Games • There are public domain card images available at • http://www.waste.org/~oxymoron/files/cards • Dr. Estell of Ohio Northern University has some card game projects at http://nifty.stanford.edu/2004/EstellCardGame/index.html • CardAssingment.doc Georgia Institute of Technology
Fish • Alyce Brady of Kalamazoo College • Page of resources • http://max.cs.kzoo.edu/AP/MBS/index.html • Aquarium Lab Series • Review of basics and set-up for Marine Biology Case Study • http://max.cs.kzoo.edu/patterns/JavaPatternLabs/AquariumLabSeries/index.shtml • Minnow Project • http://max.cs.kzoo.edu/AP/MBS/RelatedAssignments/Minnow/MinnowProj.html Georgia Institute of Technology
Summary • Object-oriented programs are simulations • Consist of interacting objects who send each other messages • Objects can refuse to do what you ask • Objects belong to classes • An object is an instance of a class • Classes describe the data and operations that all objects of the class will have • A class creates objects • Objects are responsible • They should control their data • They each have their own copy of their data Georgia Institute of Technology