Download presentation
Presentation is loading. Please wait.
Published byΝέστωρ Καραμήτσος Modified over 5 years ago
1
The beginning of media computation Followed by a demo
Turtles The beginning of media computation Followed by a demo Eclipse version Copyright © Curt Hill
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 © Curt Hill
3
A Turtle in a World Copyright © 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 © 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 © 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 © 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 © 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 © 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 © 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 © 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 © 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 © 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 In the past JRE modification worked best Copyright © Curt Hill
14
Adding to Project Download TurtleGraphics.jar from class web site
Remember where you put it! From within Eclipse: Select Project and then Properties (You may also right click project in Package Explorer) Choose Java Build Path Click on Classpath Click Add External JARs Find the downloaded TurtleGraphics.jar Finish with Apply and Close Copyright © Curt Hill
15
Downloading Copyright © Curt Hill
16
Right Click Project Copyright © Curt Hill
17
Click Java Build Path Copyright © Curt Hill
18
Click Classpath Copyright © Curt Hill
19
Click Add External Jar Find Turtlegraphics.jar
Copyright © Curt Hill
20
Then click Apply and Close
Copyright © Curt Hill
21
Finishing up Once TurtleGraphics.jar has been selected select the OK or Finish button until back at the main Eclipse window This modifies the project and we will need to do this for every new project Unless I find a better way One more thing may be needed, the forbidden access error needs to be disabled Copyright © Curt Hill
22
TurtleGraphics Problems
This library is quite old Sometimes it uses deprecated calls and objects A deprecated item is slated for removal The compile has options on whether to give errors or warnings on these We need to disable these This is done with the menu Windows Preferences Copyright © Curt Hill
23
Java Errors Copyright © Curt Hill
24
One More Thing In the Project creation dialogs it will ask if you want to Create a Module Always click Don’t Create Creation of this Module seems to interfere with using turtlegraphics in a class Copyright © Curt Hill
25
Don’t Create Copyright © 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 © 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 © 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 © 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 © 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 © Curt Hill
31
Code Completion Copyright © Curt Hill
32
Forward chosen Copyright © Curt Hill
33
The new code Copyright © Curt Hill
34
Fill in the value Copyright © Curt Hill
35
Getting Started Create a new project Create a new class Add import
Add five lines of code within main Copyright © Curt Hill
36
Whole but Short Program
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); } Copyright © Curt Hill
37
Run It After the program is complete run it
A image similar to the next screen should occur Copyright © Curt Hill
38
The Run Copyright © 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 © Curt Hill
40
Try It Now we have a glorified etch-a-sketch Create a new program
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 © Curt Hill
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.