Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 1997-2013 Curt Hill Turtles The beginning of media computation.

Similar presentations


Presentation on theme: "Copyright © 1997-2013 Curt Hill Turtles The beginning of media computation."— Presentation transcript:

1 Copyright © 1997-2013 Curt Hill Turtles The beginning of media computation.

2 What is a turtle? It will show up as a icon in a window We direct the turtle to move forward or backward as well as turn left or right –Among other things When it moves it may leave a line as a trail It is a line drawing tool Copyright © 1997-2013 Curt Hill

3 A Turtle in a World Copyright © 1997-2013 Curt Hill

4 Two Objects There are two objects that are needed for this –A world –A turtle The world provides the window and background –The sandbox for one or more turtles The turtle –Our drawing object Copyright © 1997-2013 Curt Hill

5 What is an object? In Java there are two kinds of variables –Primitives and objects A primitive represents one simple value –An int containing 4 –The character ‘A’ An object bundles one or more values into one Copyright © 1997-2013 Curt Hill

6 Objects Objects have properties and methods A property is a variable that may be a variable or another object A method is a means to make the object do something –Obtain or change property values –A constructor is a kind of method that initializes the object Copyright © 1997-2013 Curt Hill

7 Primitive Life Cycle We declare We assign values to them They are only known in the method that they are used within –Such as the main method Objects have a slightly different life cycle Copyright © 1997-2013 Curt Hill

8 Object Life Cycle Declare the handle Allocate (or instantiate) using new or assignment –Declaration makes primitives exist but objects need another step Assign and use the values Copyright © 1997-2013 Curt Hill

9 Terminology An object is a kind of variable A class is a type of object Example: String s = “S”; String is the class s is the object “S” is the value Copyright © 1997-2013 Curt Hill

10 Declaration Objects are declared just like primitives Class_Name Variable_name ; Example: Turtle george; george is now an object handle Cannot be used until allocated An import statement may be needed to bring the class name within the scope Copyright © 1997-2013 Curt Hill

11 Two ways to initialize Assign from an existing object Turtle george = sam; where sam is an existing turtle Use the new keyword Turtle bob=new Turtle(myWorld); The Turtle(myWorld) part is a constructor myWorld is the world that the turtle will be in Copyright © 1997-2013 Curt Hill

12 Constructors Recall that a constructor is a special method of a class It has the same name as the class It is often overloaded by different sets of parameters One of the constructors is always executed for a newly created object It initializes the new object Copyright © 1997-2013 Curt Hill

13 Preparation Eclipse knows about the Java Runtime Environment (JRE) This includes all Java standard objects such as Scanner It does not know about Turtles because they are not standard Java items Eclipse will allow new items to be added to a single project or to the JRE –It is easier to just modify the JRE Copyright © 1997-2013 Curt Hill

14 JRE Modification Download TurtleGraphics.jar from class web site –Remember where you put it Select Window and then Preferences Choose preferences Open Java and Installed JREs in preferences Edit the JRE Click Add External JARs Find the downloaded TurtleGraphics.jar Copyright © 1997-2013 Curt Hill

15 Lab Computers Some of you may be running Eclipse in a computer lab Sometimes these are periodically cleaned –Every night –During a reboot In such cases you should put the TurtleGraphics.jar file on your space: –Flash drive –Your space on another drive Copyright © 1997-2013 Curt Hill

16 Downloading Copyright © 1997-2013 Curt Hill

17 Window Preferences Copyright © 1997-2013 Curt Hill

18 Preferences Copyright © 1997-2013 Curt Hill

19 Open Java in Preferences Copyright © 1997-2013 Curt Hill

20 Choose Installed JREs Copyright © 1997-2013 Curt Hill

21 Select and Edit Copyright © 1997-2013 Curt Hill

22 After Edit Button Copyright © 1997-2013 Curt Hill

23 Add External Jar Copyright © 1997-2013 Curt Hill

24 Finishing up Once TurtleGraphics.jar has been selected select the OK or Finish button until back at the main Eclipse window This modifies the JRE and we should not need to do it a second time One more thing may be needed, the forbidden access error needs to be disabled Copyright © 1997-2013 Curt Hill

25 Java Errors Copyright © 1997-2013 Curt Hill

26 Import Statement We have told Eclipse that we need TurtleGraphics to be part of the JRE There are a large number of things in the JRE that require an import to be used The import for this is: import turtlegraphics.*; This follows the package statement Copyright © 1997-2013 Curt Hill

27 Creating a World Each World will become a screen Turtles must be created within a world Create the world: World myWorld = new World(); –World is class name –myWorld is variable name and could be any name, but will be used with Turtle constructor –World() is the constructor Copyright © 1997-2013 Curt Hill

28 Creating Turtles Once myWorld has been made we can make turtles Create a turtle: Turtle sam = new Turtle(myWorld); –Turtle is a class name –sam is the name of one turtle –myWorld is the name of the previously created world We may create as many as we like Copyright © 1997-2013 Curt Hill

29 Turtle Methods Making a turtle to exist is no big deal –We would like the turtle to do something –This takes methods For now just two forward(200); –Moves forward 200 pixels turn(90) –Turn right 90 degrees –Left uses a negative Copyright © 1997-2013 Curt Hill

30 Eclipse Again One of the very many advantages of Eclipse over DrJava is the code completion option When we type in the name of a variable, that is a class, and then a dot –Eclipse suggests some methods that could be used See the following screen: Copyright © 1997-2013 Curt Hill

31 Code Completion Copyright © 1997-2013 Curt Hill

32 Forward chosen Copyright © 1997-2013 Curt Hill

33 The new code Copyright © 1997-2013 Curt Hill

34 Fill in the value Copyright © 1997-2013 Curt Hill

35 Getting Started Create a new project Create a new class Add import Add five lines of code within main Copyright © 1997-2013 Curt Hill

36 Whole but Short Program Copyright © 1997-2013 Curt Hill package turtle; import turtlegraphics.*; public class FirstTurtle { public static void main(String[] args) { World w = new World(); Turtle t = new Turtle(w); t.forward(100); t.turn(90); t.forward(100); }

37 Run It After the program is complete run it A image similar to the next screen should occur Copyright © 1997-2013 Curt Hill

38 The Run Copyright © 1997-2013 Curt Hill

39 One last thought Do not name your project or class or package the same thing as any class you will use In particular do not use: –Turtle –World –Color –Picture –Among others If you do you may not be able to use any of these objects Copyright © 1997-2013 Curt Hill

40 Try It Now we have a glorified etch-a- sketch –Soon we will have much more Create a new program Put in the commands to draw a square –4 forwards –4 turns of 90 I will and you can follow me in this Copyright © 1997-2013 Curt Hill


Download ppt "Copyright © 1997-2013 Curt Hill Turtles The beginning of media computation."

Similar presentations


Ads by Google