Problem Solving with Data Structures using Java: A Multimedia Approach Chapter 4: Objects as Agents: Manipulating Turtles.

Slides:



Advertisements
Similar presentations
Topic 10 Java Memory Management. 1-2 Memory Allocation in Java When a program is being executed, separate areas of memory are allocated for each class.
Advertisements

Yoshi
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
Chapter 1 Inheritance University Of Ha’il.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
ManipulatingPictures-Mod6-part21 Manipulating Pictures, Arrays, and Loops part 2 Barb Ericson Georgia Institute of Technology.
Ade Azurat, Advanced Programming 2004 (Based on LYS Stefanus’s slides) Advanced Programming 2004, Based on LYS Stefanus’s slides Slide 2.1 Multithreading.
Exceptions Don’t Frustrate Your User – Handle Errors KR – CS 1401 Spring 2005 Picture – sysprog.net.
Designing an ADT The design of an ADT should evolve naturally during the problem-solving process Questions to ask when designing an ADT What data does.
SE-1020 Dr. Mark L. Hornick 1 Exceptions and Exception Handling.
 It is possible to declare names for object references and not assign object references to them.  Such names literally refer to nothing at all.  It.
Cosc 1P02 Week 2 Lecture slides
Exceptions. Errors and Exceptions An error is a bug in your program –dividing by zero –going outside the bounds of an array –trying to use a null reference.
Threads A thread is a program unit that is executed independently of other parts of the program A thread is a program unit that is executed independently.
Problem Solving with Data Structures using Java: A Multimedia Approach Chapter 8: Trees of Images.
Georgia Institute of Technology Speed part 3 Barb Ericson Georgia Institute of Technology May 2006.
What is an exception? An exception is: – an event that interrupts the normal processing of the program. –an error condition that violates the semantic.
Exception Handling in Java Exception Handling Introduction: After completing this chapter, you will be able to comprehend the nature and kinds.
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
Introduction to Programming Writing Java Beginning Java Programs.
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
REVIEW On Friday we explored Client-Server Applications with Sockets. Servers must create a ServerSocket object on a specific Port #. They then can wait.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 10.
Copyright © Curt Hill Turtles The beginning of media computation.
Java is an object oriented programming language In this model of programming all entities are objects that have methods and fields Methods perform tasks.
1 Web Based Programming Section 8 James King 12 August 2003.
Manipulating Turtles CS1316: Representing Structure and Behavior.
CSC1401 Classes - 1. Learning Goals Computing concepts Identifying objects and classes Declaring a class Declaring fields Default field values.
An Introduction to Programming and Object Oriented Design using Java 3 rd Edition. Dec 2007 Jaime Niño Frederick Hosch Chapter 18 Integrating user interface.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Introduction to Programming Writing Java Beginning Java Programs.
CreatingSubclasses1 Barb Ericson Georgia Institute of Technology Dec 2009.
Java 3: Odds & Ends Advanced Programming Techniques.
CreatingClasses-SlideShow-part41 Creating Classes part 4 Barb Ericson Georgia Institute of Technology Dec 2009.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009.
Programming and Problem Solving With Java Copyright 1999, James M. Slack Exceptions Handling Exceptions with try and catch The finally-block The throws.
CS305j Introduction to Computing Classes II 1 Topic 24 Classes Part II "Object-oriented programming as it emerged in Simula 67 allows software structure.
Finally! Making the Villagers CS1316: Representing Structure and Behavior.
Georgia Institute of Technology More on Creating Classes part 3 Barb Ericson Georgia Institute of Technology Nov 2005.
Session 7 Introduction to Inheritance. Accumulator Example a simple calculator app classes needed: –AdderApp - contains main –AddingFrame - GUI –CloseableFrame.
And other languages…. must remember to check return value OR, must pass label/exception handler to every function Caller Function return status Caller.
Session 7 More Implications of Inheritance & Chapter 5: Ball World Example.
Chapter 9: Continuing Classes By Matt Hirsch. Table Of Contents 1.Static Fields and Methods 2.Inheritance I. Recycle Code with Inheritance II. Overriding.
COMPOSITE PATTERN NOTES. The Composite pattern l Intent Compose objects into tree structures to represent whole-part hierarchies. Composite lets clients.
Georgia Institute of Technology Comic Strip Analysis and Design Inheritance, Abstract Classes, and Polymorphism part1 Barb Ericson Georgia Institute of.
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
Introduction to Exceptions in Java CS201, SW Development Methods.
Section 3.3 Exceptional Situations. 3.3 Exceptional Situations Exceptional situation Associated with an unusual, sometimes unpredictable event, detectable.
Georgia Institute of Technology More on Creating Classes Barb Ericson Georgia Institute of Technology June 2006.
Problem Solving with Data Structures using Java: A Multimedia Approach Chapter 9: Lists and Trees for Structuring Sounds.
Generics, Exception and Undo Command. A Generic Stack Class AStack uses generic. In the main method, we have 2 instances of AStack, each type is Integer.
Generics, Exceptions and Undo Command
Chapter 10 – Exception Handling
Creating and Modifying Text part 2
CS1316: Representing Structure and Behavior
CS1316: Representing Structure and Behavior
CS1316: Representing Structure and Behavior
Inherited Classes in Java
Finally! Making the Villagers
An Example of Inheritance
More on Creating Classes
Chapter 11 Inheritance and Polymorphism Part 1
More on Creating Classes part 3
Problem Solving with Data Structures using Java: A Multimedia Approach
CMSC 202 Exceptions.
Problem Solving with Data Structures using Java: A Multimedia Approach
The beginning of media computation Followed by a demo
Presentation transcript:

Problem Solving with Data Structures using Java: A Multimedia Approach Chapter 4: Objects as Agents: Manipulating Turtles

Chapter Objectives

The Logo Turtle Originally, a robot with pen For children to program graphics, in a day before graphics terminals. Literally, a pen would drop down (with the command penDown) and would draw on the paper below it, as the turtle moved with commands like forward and right. Nowadays, replaced with a graphical representation.

Turtles can go forward and turn; they know heading and position > fred.forward(100); > fred.turn(90); > fred.getHeading() 90 > fred.getXPos() 320 > fred.getYPos() 140

Obviously: Turtles are objects Turtles can do: forward(pixels) turn(degrees) Turtles know: Heading Position

Drawing with Turtles > for (int sides=0; sides <= 4 ; sides++) {fred.forward(100); fred.turn(90);} // Actually did five sides here...

Can we cascade? Will this work? turtle.forward(100).turn(90) Hint: Think about the returns!

Modern turtles: Turtle Geometry and StarLogo diSessa and Abelson’s Turtle Geometry showed that simple turtle geometry could explore complex math, including Einstein’s Theory of Relativity Mitchel Resnick’s StarLogo used thousands of turtles to explore behavior of traffic, ants, and termites.

Exploring ants with turtles Each turtle: -Move randomly - If you find food: Grab it, go home, dropping scent. - If you find scent, turn towards the direction of the scent.

100 Turtles public class LotsOfTurtles { public static void main(String[] args){ // Create a world World myWorld = new World(); // A flotilla of turtles Turtle [] myTurtles = new Turtle[100]; // Make a hundred turtles for (int i=0; i < 100; i++) { myTurtles[i] = new Turtle(myWorld); } //Tell them all what to do for (int i=0; i < 100; i++) { // Turn a random amount between 0 and 360 myTurtles[i].turn((int) (360 * Math.random())); // Go 100 pixels myTurtles[i].forward(100); }

Making a circle

How did that work? We created an array to hold 100 turtles, then created 100 turtles in that array and in the World myWorld. We told each to turn a random amount. If random is truly uniform (no peaks), then the turtles should pretty evenly cover the space from 0 to 360 degrees. By going forward 100, we create a circle of turtles with radius 100.

Thought Experiment What’s the difference between this: Turtle [] myTurtles = new Turtle[100]; And this? for (int i=0; i < 100; i++) { myTurtles[i] = new Turtle(myWorld); } What are each doing?

More than one Turtle at once

If you have more than one turtle, how do they find each other? How does C3PO find R2D2 after R2 wanders off randomly? > World tattoine = new World(); > Turtle r2d2 = new Turtle(tattoine); > r2d2.turn((int) (360 * Math.random())); > r2d2.forward((int) (Math.random() * 100)); > Turtle c3po = new Turtle(tattoine); > c3po.turnToFace(r2d2); > c3po.forward((int) (c3po.getDistance( r2d2.getXPos(),r2d2.getYPos())));

Putting Turtles on Pictures > Picture canvas = new Picture(400,400); > Turtle mabel = new Turtle(canvas); > for (int sides=1; sides <= 4 ; sides++) {mabel.forward(100); mabel.turn(90);} > canvas.show();

Using Turtles to compose Pictures > Picture t = new Picture("D:/cs1316/Medi aSources/Turtle.jpg"); > mabel.drop(t) > canvas.repaint();

A more complicated composition > Picture monster = new Picture(FileChooser.getMediaPath( "mscary.jpg")); > Picture newBack = new Picture(400,400); > Turtle myTurtle = new Turtle(newBack); > myTurtle.drop(monster); newBack.show(); > myTurtle.turn(180); > myTurtle.drop(monster); > newBack.repaint();

Even more monsters > Picture frame = new Picture(600,600); > Turtle mabel = new Turtle(frame); > for (int i = 0; i < 12; i++) { mabel.drop(monster); mabel.turn(30); }

Adding new methods to Turtle

Testing our new method

Thought Experiment We can have two methods with the same name? How did Java know which one to use?

Making more complex pictures: Using main() public class MyTurtlePicture { public static void main(String [] args) { FileChooser.setMediaPath("D:/cs1316/MediaSour ces/"); Picture canvas = new Picture(600,600); Turtle jenny = new Turtle(canvas); Picture lilTurtle = new Picture(FileChooser.getMediaPath("Turtle.jpg")); for (int i=0; i <=40; i++) { if (i < 20) {jenny.turn(20);} else {jenny.turn(-20);} jenny.forward(40); jenny.drop(lilTurtle.scale(0.5)); } canvas.show(); } } Also: Note use of setMediaPath and getMediaPath

Result:

Thought Experiments Is this myTurtlePicture a class? An object? Can we access variables from the Interactions Pane? Can we return values to the Interactions Pane? When is it useful to use a main()?

Explaining public, and static, and void, and main, and String [] args public static void main(String [] args); Public: This method can be accessed by any other class. Static: This is a method that can be accessed through the class, even if no instances of the class exist. Void: This method doesn’t return anything. String [] args: If called from the Command Line (outside DrJava), inputs could be provided. They’d show up as strings in this array.

Creating an animation with FrameSequencer FrameSequencer stores out Pictures to a directory, and can show/replay the sequence. new FrameSequencer(dir): dir where the Pictures should be stored as JPEG frames.addFrame(aPicture): Adds this Picture as a frame.show(): Show the frames as they get added.play(fps): Play the sequence, at fps frames per second

Using FrameSequence Welcome to DrJava. > FrameSequencer f = new FrameSequencer("D:/Temp"); > f.show() There are no frames to show yet. When you add a frame it will be shown > Picture t = new Picture("D:/cs1316/MediaSources/Turtle.jpg"); > f.addFrame(t); > Picture barb = new Picture("D:/cs1316/MediaSources/Barbara.jpg"); > f.addFrame(barb); > Picture katie = new Picture("D:/cs1316/MediaSources/Katie.jpg"); > f.addFrame(katie); > f.play(1); // display one frame per second

Making a turtle drawing animate Welcome to DrJava. > MyTurtleAnimation anim = new MyTurtleAnimation(); > anim.next(20); > anim.replay(500);

Animated turtle /** * An example class for creating a turtle * animation Mark Guzdial Barb Ericson */ public class MyTurtleAnimation { private Picture canvas; private Turtle jenny; private FrameSequencer f; public MyTurtleAnimation() { canvas = new Picture(600,600); jenny = new Turtle(canvas); f = new FrameSequencer("C:/Temp/"); }

Animated turtle public void next() { Picture lilTurtle = new Picture(FileChooser.getMediaPath("Turtle.jpg")); jenny.turn(-20); jenny.forward(30); jenny.turn(30); jenny.forward(-5); jenny.drop(lilTurtle.scale(0.5)); f.addFrame(new Picture(canvas)); // Try this as // f.addFrame(canvas); } public void next(int numTimes) { for (int i=0; i < numTimes; i++) { this.next(); } public void show() { f.show(); } public void play(int framesPerSecond) { f.show(); f.play(framesPerSecond); } public static void main(String[] args) { MyTurtleAnimation anim = new MyTurtleAnimation(); anim.next(20); anim.play(16); }

Declarations public class MyTurtleAnimation { Picture canvas; Turtle jenny; FrameSequencer f; We’re going to need a canvas, a Turtle, and a FrameSequencer for each instance of MyTurtleAnimation. That’s what the instances know These are called instance variables

A constructor public MyTurtleAnimation() { FileChooser.setMediaPath("D:/cs1316/MediaSources/"); canvas = new Picture(600,600); jenny = new Turtle(canvas); f = new FrameSequencer("D:/Temp/"); } A method with the same name of the class is called to initialize (or construct) the new instance. Notice the use of setMediaPath We don’t need to declare canvas, jenny, or f—the instance knows those already

Each step of the animation public void next(){ Picture lilTurtle = new Picture(FileChooser.getMediaPath("Turtle.jpg")); jenny.turn(-20); jenny.forward(30); jenny.turn(30); jenny.forward(-5); jenny.drop(lilTurtle.scale(0.5)); f.addFrame(canvas.copy()); } Do one stage of the drawing.

Try it! Why do we call.copy on the canvas? Try it without it! What does the result suggest to you about how FrameSequencer instances store their frames internally? Try it like this: > MyTurtleAnimation anim = new MyTurtleAnimation(); > anim.next(20); // Generate 20 frames > anim.play(2); // Play them back, two per second Try it like this: > MyTurtleAnimation anim = new MyTurtleAnimation(); > anim.next(20); // Generate 20 frames > anim.play(2); // Play them back, two per second

Being able to play it public void next(int numTimes){ for (int i=0; i < numTimes; i++) {this.next();} } public void show(){ f.show(); } public void replay(int framesPerSecond){ f.show(); f.play(franesPerSecond); } }

What happened when you removed the.copy()? Think about the internal data structure of FrameSequencer. It’s a list of references to Picture objects.

Without the copy, all frames reference one Picture. So however the last Picture ends up, that’s what all the frames show.

With the copy, different Pictures for each frame

Making a slow moving turtle How could we make a turtle that moves slowly? It would be a special kind-of Turtle, so a subclass of Turtle is needed. We override forward to make it go slow. We need to pause execution. We need to sleep But something can go wrong during sleep, so we need to handle an exception.

Handling exceptions Weird things can happen outside of your program’s control. The network fails. The disk you’re trying to write to is full. We use try-catch to deal with extraordinary events. try { //code that can throw the exception } catch (Exception e) { System.err.println("Exception: " + e.getMessage()); System.err.println("Stack Trace is:"); e.printStackTrace(); } try { //code that can throw the exception } catch (Exception e) { System.err.println("Exception: " + e.getMessage()); System.err.println("Stack Trace is:"); e.printStackTrace(); }

Threads and sleep Your program executes within a thread. Yes, you can have more than one thread, so more than one thing can be happening at once. Threads can be told to sleep, but if something happens that interrupts that sleep, an exception occurs. Java requires you to catch all exceptions that might get thrown.

SlowWalkingTurtle /** * A class that represents a slow walking turtle Mark Guzdial Barb Ericson */ class SlowWalkingTurtle extends Turtle { /** Constructor that takes the model display modelDisplay the thing that displays the model */ public SlowWalkingTurtle (ModelDisplay modelDisplay) { // let the parent constructor handle it super(modelDisplay); }

/** * Method to have the turtle go forward then wait a moment * and then go on. pixels the number of pixels to go forward */ public void forward(int pixels) { super.forward(pixels); try { Thread.sleep(500); // Wait a half sec } catch (Exception e) { System.err.println("Exception: " + e.getMessage()); System.err.println("Stack Trace is:"); e.printStackTrace(); }

Using our slow turtle Works just the same. Just slowly. > World earth = new World() > SlowWalkingTurtle mark = new SlowWalkingTurtle(earth) > mark.forward(100); mark.forward(100); > World earth = new World() > SlowWalkingTurtle mark = new SlowWalkingTurtle(earth) > mark.forward(100); mark.forward(100);

JavaDoc on SimpleTurtle

Thought Experiment Why SimpleTurtle (and SimplePicture)? Hint: Think about information hiding