l 5 Java Turtles

download l 5 Java Turtles

of 18

Transcript of l 5 Java Turtles

  • 7/30/2019 l 5 Java Turtles

    1/18

    CS 141 Turtles!

    CS 141 Introduction to Computer Science

    Its Turtles all the way down...

    1

    1Sunday, September 16, 2012

  • 7/30/2019 l 5 Java Turtles

    2/18

    CS 141 Turtles! 2

    References

    (some slides taken from lectures provided by Barbara

    Ericson, Georgia Tech for her Multimedia Computationcourse)

    2Sunday, September 16, 2012

  • 7/30/2019 l 5 Java Turtles

    3/18

    Intro to JavaCS 141

    ARRRR!!!!!

    Its Talk Like a Pirate Day!

    http://www.talklikeapirate.com/howto.html

    3

    3Sunday, September 16, 2012

    http://www.talklikeapirate.com/howto.htmlhttp://www.talklikeapirate.com/howto.htmlhttp://www.talklikeapirate.com/howto.html
  • 7/30/2019 l 5 Java Turtles

    4/18

    CS 141 Turtles! 4

    Computers as Simulators

    The computer is the Proteus of machines. Its essence

    is its universality, its power to simulate. Because it cantake on a thousand forms and serve a thousand

    functions, it can appeal to a thousand tastes.

    Seymour Papert in Mindstorms

    He can foretell the future, but, in a mytheme familiar

    from several cultures, will change his shape to avoid

    having to; he will answer only to someone who is

    capable of capturing him. From this feature of Proteuscomes the adjectiveprotean, with the general meaning

    of "versatile", "mutable", "capable of assuming many

    forms". "Protean" has positive connotations of

    flexibility, versatility and adaptability. (Wikipedia)

    4Sunday, September 16, 2012

    http://en.wikipedia.org/wiki/Adjectivehttp://en.wikipedia.org/wiki/Adjectivehttp://en.wikipedia.org/wiki/Mythemehttp://en.wikipedia.org/wiki/Mytheme
  • 7/30/2019 l 5 Java Turtles

    5/18

    CS 141 Turtles! 5

    Creating a Simulation

    Computers let us simulate things

    We do this by creating models of the things we wantto simulate

    We need to define what types of objects we will wantin our simulation and what they can do

    Classes define the types and create objects of that type

    Objects act in the simulation

    5Sunday, September 16, 2012

  • 7/30/2019 l 5 Java Turtles

    6/18

    CS 141 Turtles! 6

    Turtles!

    Well simulate a world populated with turtles

    carrying pens

    We have to define what we mean by a Turtle to thecomputer

    We compile it to convert it into something the

    computer can understand

    6Sunday, September 16, 2012

  • 7/30/2019 l 5 Java Turtles

    7/18

    CS 141 Turtles! 7

    History of Turtles

    Seymour Papert at MIT in the 60s

    By teaching the computer to do something the kids arethinking about thinking Develop problem solving skills

    Learn by constructing and debugging something

    Learn by making mistakes and fixing them

    7Sunday, September 16, 2012

  • 7/30/2019 l 5 Java Turtles

    8/18

    CS 141 Turtles! 8

    Creating Objects in Java

    In Java to create an object of a class you use

    new (value, value, );

    Our Turtle objects live in a World object

    new World();

    8Sunday, September 16, 2012

  • 7/30/2019 l 5 Java Turtles

    9/18

    CS 141 Turtles! 9

    Naming is Important

    If you get a new pet one of the first things you do is

    name it

    In programming we name things we want to refer to

    again

    Gives us a way to work with them

    9Sunday, September 16, 2012

  • 7/30/2019 l 5 Java Turtles

    10/18

    CS 141 Turtles! 10

    Saving a Reference to an Object

    To be able to refer to an object again we need to

    specify what type of thing it is and give it a name

    This is also called declaring a variable

    Class name; OR

    Class name = new Class(value, value, );

    The equal sign assigns the value of the variable on theleft to the result of the stuff on the right

    10Sunday, September 16, 2012

  • 7/30/2019 l 5 Java Turtles

    11/18

    CS 141 Turtles! 11

    Turtle Basics

    The world starts off with a size of 640 by 480 with no

    turtles

    World w = new World();

    The turtle starts off facing north and in the center ofthe world by default

    You must pass a World object when you create the

    Turtle object or you will get an error:java.lang.NoSuchMethodException

    Turtle ted = new Turtle(w);

    11Sunday, September 16, 2012

  • 7/30/2019 l 5 Java Turtles

    12/18

    CS 141 Turtles!

    Turtle moves

    turtle.forward(steps);

    turtle.backward(steps);

    turtle.turnRight();

    turtle.turnLeft();

    turtle.turn(degrees);

    turtle.penUp();

    turtle.penDown();

    turtle.moveTo(x, y); (0,0) is at the upper left.

    12

    12Sunday, September 16, 2012

  • 7/30/2019 l 5 Java Turtles

    13/18

    CS 141 Turtles!

    turtle.penWidth(size);

    turtle.setPenColor(Color); // must import java.awt.Color;

    turtle.setColor(Color);

    turtle.setBodyColor(Color);

    13

    13Sunday, September 16, 2012

  • 7/30/2019 l 5 Java Turtles

    14/18

    CS 141 Turtles! 14

    Objects send Messages

    Objects dont 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 its data Not let it get into an incorrect state

    A bank account object shouldnt let you withdraw more

    money that you have in the account

    14Sunday, September 16, 2012

    C i M h d

  • 7/30/2019 l 5 Java Turtles

    15/18

    CS 141 Turtles! 15

    Creating a Method

    The syntax for declaring a method is

    visibility returnType name(parameterList)

    Visibility determines access

    The returnType is the type of thing returned

    Name the method starting with a lowercase word and uppercasing

    the first letter of each additional word (CamelCase)

    15Sunday, September 16, 2012

  • 7/30/2019 l 5 Java Turtles

    16/18

    CS 141 Turtles!

    Drawing Polygons

    rectangle - easy

    equilateral triangle?

    pentagon?

    hexagon?

    a general formula for a regular polygon?

    16

    16Sunday, September 16, 2012

  • 7/30/2019 l 5 Java Turtles

    17/18

    CS 141 Turtles!

    Circles?

    How to draw a circle with a Turtle?

    17

    17Sunday, September 16, 2012

    Ch ll (T th t h )

  • 7/30/2019 l 5 Java Turtles

    18/18

    CS 141 Turtles! 18

    Challenges (Try these at home)

    Create a method for drawing an equilateral triangle

    all sides have the same length

    Pass in the length

    Create a method for drawing a diamond

    Create a method for drawing a house

    Using the other methods

    18Sunday, September 16, 2012