introduction to object oriented programming in alice and java n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Introduction to Object-Oriented Programming in Alice and Java PowerPoint Presentation
Download Presentation
Introduction to Object-Oriented Programming in Alice and Java

Loading in 2 Seconds...

play fullscreen
1 / 65

Introduction to Object-Oriented Programming in Alice and Java - PowerPoint PPT Presentation


  • 104 Views
  • Uploaded on

Introduction to Object-Oriented Programming in Alice and Java. Barb Ericson Georgia Institute of Technology Aug 2006. Learning Goals. To introduce computation as simulation To introduce Alice To talk about the history of using turtles to teach programming

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Introduction to Object-Oriented Programming in Alice and Java' - caesar-wilder


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
introduction to object oriented programming in alice and java

Introduction to Object-Oriented Programming in Alice and Java

Barb Ericson

Georgia Institute of Technology

Aug 2006

Georgia Institute of Technology

learning goals
Learning Goals
  • To introduce computation as simulation
  • To introduce Alice
  • To talk about the history of using turtles to teach programming
  • To show how to create objects in Alice
  • To show how to invoke methods on objects in Alice
  • To show how to add classes to the classpath in DrJava
  • To create objects in Java
  • To invoke methods on objects in Java

Georgia Institute of Technology

computers as simulators
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

creating a simulation
Creating a Simulation
  • Computers let us simulate things
    • We do this by creating models of the things we want to simulate
    • We need to define what types of objects we will want in our simulation and what they can do
      • Classes define the types and create objects of that type
      • Objects act in the simulation

Georgia Institute of Technology

running a simulation
Running a Simulation
  • How many objects does it take to get a person fed at a restaurant?
    • Pick people to be the customer, greeter/seater, waiter, chef, and cashier and have them role play
      • What data does each of these people need to do their job?
      • What do they need to know how to do?
      • What other objects do you talk about?
  • How about simulating going to a dentist?
    • Or to the movies?

Georgia Institute of Technology

introduction to alice
Introduction to Alice
  • Free development environment
    • From Carnegie Mellon University
    • Available from http://www.alice.org
  • Used to create
    • 3D movies
    • 3D games

Georgia Institute of Technology

getting started
Getting Started
  • Install Alice
    • Copy folder from CD
  • Start Alice
    • Double Click on Icon (Alice.exe)
  • Wait for Alice to start
    • Can take a few minutes

Georgia Institute of Technology

pick a world
Pick a World
  • Click on the Examples tab
    • And click on a world to open it
    • You might need to scroll down to see this world

Georgia Institute of Technology

playing the movie
Playing the Movie
  • To play a saved movie
    • Click the Play button
  • The movie or game will start to play in a separate window
    • Click on target to play this movie
      • Most example worlds will just start to play

Georgia Institute of Technology

create your own movie
Create Your Own Movie
  • Click on File
    • Then New World
  • Click on a template
    • Background and ground type

Georgia Institute of Technology

add objects to the world
Add Objects To the World
  • Click the Add Objects button
  • Select a category from the Local Gallery
  • Select Local Gallery to get back from a category
    • To the list of categories

Georgia Institute of Technology

add objects to the world1
Add Objects to the World
  • Click on the item to select it
    • Click on the Class description
      • Click Add Instance to world Button
    • Or drag the object from the class description and drop it into position in the world

Georgia Institute of Technology

position an object in the world
Position an Object in the World
  • Once the object is in the world
    • You can move it by
      • Dragging it with the mouse
    • Or using the mouse controls
  • Click the Undo button to undo the last action

Georgia Institute of Technology

mouse controls
Mouse Controls
  • Use mouse control buttons (from left to right)
    • To move object left, right, forward, back
    • Up and down
    • Turn left or right
    • Turn forward or backwards
    • Tumble the object (free rotation)
    • Resize object
    • Copy the object

Georgia Institute of Technology

create more objects
Create More Objects
  • You can use the copy object mouse control
  • Or add another instance to the world
    • Click on the Class
      • Click on the Add instance to world button
    • Or drag another object from the class description

Georgia Institute of Technology

objects and classes
Objects and Classes
  • You can make many objects of the same class
    • The class creates the object
  • Each object will have a unique name in the object window
  • Class names start with a capital letter
    • Object names start with a lower case letter

Georgia Institute of Technology

telling objects to do things
Telling Objects To Do Things
  • Click the Done button to stop adding objects
  • Click on a reference to an object in the object window to select it
    • Click on the Methods tab to see what it can do

Georgia Institute of Technology

messages to objects
Messages to Objects
  • In object-oriented programming we send messages to objects
    • Ask them to do a method
  • Drag a method from the left to the middle window (world - my first method)

Georgia Institute of Technology

who does the action
Who does the action?
  • We had selected bunny in the object window
    • So only bunny does the action
  • How do you think we can get bunny2 to do the same action?

Georgia Institute of Technology

moving several objects
Moving Several Objects
  • To get bunny2 to move
    • Select bunny2 in the objects window
      • Or just click on it
    • Then select the method tab
    • Drag out the same methods for bunny2
    • Click play to see what happens

Georgia Institute of Technology

deleting an object
Deleting an Object
  • Click on the object you want to delete
    • The object will be displayed with a bounding box around it
  • Click the right mouse button
    • Select delete from the pop-up menu

Georgia Institute of Technology

using turtles in java
Using Turtles in Java
  • We will work with Turtles in a World in Java
  • We have to define what we mean by a Turtle to the computer
    • We do this by writing a Turtle class definition
      • Turtle.java
    • We compile it to convert it into something the computer can understand
      • Bytes codes for a virtual machine
      • Turtle.class

Georgia Institute of Technology

history of turtles
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
Using Turtles
  • The Turtle Class was is part of several classes created at Georgia Tech
    • As part of an undergraduate class
  • Add bookClasses to your classpath to use these classes

Georgia Institute of Technology

open preferences in drjava
Open Preferences in DrJava

Georgia Institute of Technology

adding book classes to classpath
Adding Book Classes to Classpath

Add bookClasses

directory

Click on Add

Georgia Institute of Technology

creating objects
Creating Objects
  • To create objects we ask the object that defines the class to create it
    • Each object keeps a reference to the class that created it
  • The class is like a cookie cutter
    • It knows how much space each object needs (shape)
    • Many objects can be created from the class

World

Object 1

World

Object 2

World: Class

Georgia Institute of Technology

class as object factory
Class as Object Factory
  • A class is like a factory that creates objects of that class
  • We ask a class to create an object by using the keyword: newClassName
  • We can also ask the class to initialize the object
    • And pass data to help initialize it

Georgia Institute of Technology

creating objects in java
Creating Objects in Java
  • In Java to create an object of a class you use

new Class(value, value, …);

  • Our Turtle objects live in a World object
    • We must create a World object first
    • Try typing the following in the interactions pane:

new World();

Georgia Institute of Technology

creating objects1
Creating Objects
  • If you just do
    • new World();
  • You will create a new World object and it will display
    • But you will not have any way to refer to it again
    • Once you close the window the object can be garbage collected
      • The memory can be reused
  • We need a way to refer to the new object
    • to be able to work with it again

Georgia Institute of Technology

naming is important
Naming is Important
  • If you get a new pet one of the first things you do is name it
    • Gives you a way to refer to the new pet without saying
      • Please take that dog we got yesterday for a walk.
      • Please take Fifi for a walk.
  • In programming we name things we want to refer to again
    • Gives us a way to work with them
    • Like the World object
  • This is called declaring a variable

Georgia Institute of Technology

declaring a variable
Declaring a Variable
  • 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
    • Type name; OR
    • Typename = new Class(value, value, …);
  • The equal sign doesn’t mean equal
    • But assign the value of the variable on the left to the result of the stuff on the right
    • The following creates a variable named earth which refers to a World object created on the right
      • World earth = new World();

Georgia Institute of Technology

declaring variables
Declaring Variables
  • Variables are names associated with values
    • If the type of the variable is null
      • It doesn’t refer to an object yet
    • Variables can be reused
      • World earth = null;
      • earth = new World();
      • earth = new World();

null

earth

World

Object 1

earth

earth

World

Object 2

Georgia Institute of Technology

a variable associates a name with space
A Variable Associates a Name with Space
  • A variable is like a box with a label on it
    • You can put something in a box
    • You can take something out of a box
    • You can even change what is in the box
    • The size of the box restricts what you can put in it

Hat Box

Georgia Institute of Technology

limits on declaring variables
Limits on Declaring Variables
  • You can't declare two variables with the same name!

> World earth = new World();

> World earth = new World();

Error: Redefinition of 'earth'

  • You can change what an object variable refers to

> World earth = new World();

> earth = new World();

Georgia Institute of Technology

declaring variables and creating objects
Declaring Variables and Creating Objects
  • You can declare a variable and assign it to refer to a new object in one statement
    • World earth1 = new World();
    • Turtle tommy = new Turtle(earth1);

Declaration of variables

Creating the objects

Georgia Institute of Technology

turtle basics
Turtle Basics
  • The world starts off with a size of 640 by 480
    • With no turtles

World earth1 = new World();

  • The turtle starts off facing north and in the center of the 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 constructor

Turtle tommy = new Turtle(earth1);

Georgia Institute of Technology

java naming conventions
Java Naming Conventions
  • Notice that we capitalize the names of the classes, but not the variable names
    • World earth1 = new World();
    • This is different than English
      • Capitalize proper nouns (the names of things)
      • Not the type of thing
        • Earth is a world.
        • Tommy is a turtle.
  • In Java it is the class names that are the most important
    • Not the variable or method names

Georgia Institute of Technology

creating several objects
Creating Several Objects
  • You can create several World objects

World mars = new World();

  • You can create several Turtle objects

Turtle shar = new Turtle(mars);

Turtle jen = new Turtle(mars);

    • One turtle is on top of the other

Georgia Institute of Technology

moving a turtle
Moving a Turtle
  • Turtles can move forward

jen.forward();

    • The default is to move by
      • 100 steps (pixels)
  • You can also tell the turtle how far to move

shar.forward(50);

Georgia Institute of Technology

turning a turtle
Turning a Turtle
  • Turtles can turn
    • Right

jen.turnRight();

jen.forward();

    • Left

shar.turnLeft();

shar.forward(50);

Georgia Institute of Technology

turning a turtle1
Turning a Turtle
  • Turtles can turn by a specified amount
    • A positive number turns the turtle the right

jen.turn(90);

jen.forward(100);

    • A negative number turns the turtle to the left

shar.turn(-90);

shar.forward(70);

Georgia Institute of Technology

the pen
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

drawing a letter
Drawing a Letter
  • How would you use a turtle to draw a large letter T?
  • Process
    • Create a World variable and a World object and a Turtle variable and object.
    • Ask the Turtle object to go forward 100
    • Ask the Turtle object to pick up the pen
    • Ask the Turtle object to turn left
    • Ask the Turtle object to go forward 25
    • Ask the Turtle object to turn 180 degrees
    • Ask the Turtle object to put down the pen
    • Ask the Turtle object to go forward 50

Georgia Institute of Technology

drawing a t
Drawing a T

World world1 = new World();

Turtle turtle1 = new Turtle(world1);

turtle1.forward(100);

turtle1.penUp();

turtle1.turnLeft();

turtle1.forward(25);

turtle1.turn(180);

turtle1.penDown();

turtle1.forward(50);

Georgia Institute of Technology

moving to a location
Moving to a Location

639

X

  • A turtle can move to a particular location

turtle1.penUp();

turtle1.moveTo(500,20);

  • Coordinates are given as x and y values
    • X starts at 0 on the left and increases horizontally to the right
    • Y starts at 0 at the top of the window and increases to the bottom
    • A new turtle starts out at 320,240 by default

Y

479

Georgia Institute of Technology

challenge
Challenge
  • Create a World object
    • Don’t forget to declare a variable to hold a reference to it
  • Create a turtle object
    • Don’t forget to declare a variable to hold a reference to it
  • Use the turtle to draw a
    • Rectangle (but, not a square)
    • Diamond
    • Hexagon
  • Use the up arrow to reuse previous commands

Georgia Institute of Technology

setting the pen width
Setting the Pen Width
  • You can change the width of the trail the pen leaves

World world1 = new World();

Turtle turtle1 = new Turtle(world1);

turtle1.setPenWidth(5);

turtle1.forward(100);

Georgia Institute of Technology

setting the pen color
Setting the Pen Color
  • Use setPenColor to set the color of the pen

turtle1.setPenColor(java.awt.Color.RED);

  • There are several predefined colors
    • In the package java.awt
      • A package is a group of related classes
    • In the class Color
  • To use them you can use the full name
    • java.awt.Color.RED

Georgia Institute of Technology

setting colors
Setting Colors
  • You can change the pen color

turtle.setPenColor(java.awt.Color.RED);

  • You can change the turtle color

turtle1.setColor(java.awt.Color.BLUE);

  • You can change the turtle’s body color

turtle1.setBodyColor(java.awt.Color.CYAN);

  • You can change the turtle’s shell color

turtle1.setShellColor(java.awt.Color.RED);

Georgia Institute of Technology

objects can refuse
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 send Messages
  • Objects don’t “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 it’s data
      • Not let it get into an incorrect state
      • A bank account object shouldn’t let you withdraw more money that you have in the account

Georgia Institute of Technology

creating a method
Creating a Method
  • We can name a block of Java statements and then execute them again
    • By declaring a method in a class
  • The syntax for declaring a method is
    • visibilityreturnTypename(parameterList)
    • Visibility determines access
      • Usually public or private
      • The return type is the type of thing returned
      • If nothing is returned use the keyword void
    • Name the method starting with a lowercase word and uppercasing the first letter of each additional word

Georgia Institute of Technology

example method
public void drawSquare()

{

this.turnRight();

this.forward(30);

this.turnRight();

this.forward(30);

this.turnRight();

this.forward(30);

this.turnRight();

this.forward(30);

}

The visibility is public

The keyword void means this method doesn’t return a value

The method name is drawSquare

There are no parameters

Notice that the parentheses are still required

The keyword this means the object this method was invoked on

Example Method

Georgia Institute of Technology

adding a method to a class
Adding a Method to a Class

1. Open file Turtle.java

3. Compile open files

2. Type the method before the last } // end

Georgia Institute of Technology

compile errors
Compile Errors

Case matters in Java! turnright isn’t the same as turnRight

Clicking on the error takes you to the code and highlights it.

Georgia Institute of Technology

try the new method
Try the New Method
  • Compiling resets the interactions pane
    • Clearing all variables
      • But you can still use the up arrow to pull up previous statements
    • You will need to create a world and turtle again

World world1 = new World();

Turtle turtle1 = new Turtle(world1);

turtle1.forward(50);

turtle1.drawSquare();

turtle1.turn(30);

turtle1.drawSquare();

Georgia Institute of Technology

saving the interactions history in drjava
Saving the Interactions History in DrJava
  • You can save the interactions history into a script
    • And optionally edit it first before you save it
    • Click on Tools then on Save Interactions History
  • And then latter load and execute the statements in the script
    • Click on Tools and Load Interactions History as Script
      • Use the next button to see the next statement and click on the execute button to execute it

Georgia Institute of Technology

better method to draw a square
A method to draw a square

public void drawSquare()

{

int width = 30;

this.turnRight();

this.forward(width);

this.turnRight();

this.forward(width);

this.turnRight();

this.forward(width);

this.turnRight();

this.forward(width);

}

We added a local variable for the width

Only known inside the method

This makes it easier to change the width of the square

But, we still have to recompile to draw a different size square

Better Method to Draw a Square

Georgia Institute of Technology

testing the better method
Testing the Better Method
  • Type the following in the interactions pane

World world1 = new World();

Turtle turtle1 = new Turtle(world1);

turtle1.forward(50);

turtle1.drawSquare();

turtle1.turn(30);

turtle1.drawSquare();

  • Or use the saved script if you saved the last interactions history

Georgia Institute of Technology

passing a parameter
public void drawSquare(int width)

{

this.turnRight();

this.forward(width);

this.turnRight();

this.forward(width);

this.turnRight();

this.forward(width);

this.turnRight();

this.forward(width);

}

Parameter lists specify the type of thing passed and a name to use to refer to the value in the method

The type of this parameter is int

The name is width

Values are passed by making a copy of the passed value

Passing a Parameter

Georgia Institute of Technology

testing with a parameter
Testing with a Parameter
  • Type the following in the interactions pane

World world1 = new World();

Turtle turtle1 = new Turtle(world1);

turtle1.forward(50);

turtle1.drawSquare(30);

turtle1.turn(30);

turtle1.drawSquare(50);

Georgia Institute of Technology

how does that work
How Does That Work?
  • When you ask turtle1 to drawSquare(30)

turtle1.drawSquare(30);

    • It will ask the Turtle Class if it has a method drawSquare that takes an int value
      • And start executing that method
      • The parameter width will have the value of 30 during the executing of the method
      • The this keyword refers to turtle1
  • When you ask turtle1 to drawSquare(50)

turtle1.drawSquare(50);

    • The width will have a value of 50
    • The this refers to turtle1 (the object the method was invoked on)

Georgia Institute of Technology

challenges
Challenges
  • Create a method for drawing a rectangle
    • Pass the width and height
  • 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
  • Create a method for drawing a school
    • Using the other methods

Georgia Institute of Technology

summary
Summary
  • You can create objects from classes in Alice and Java
  • Each object needs a unique way to reference it
    • In Java we call this declaring a variable
  • You can create new methods
    • visibility returnTypename(Typename, Typename, …)
    • Let’s you reuse a block of statements

Georgia Institute of Technology