24
Lecture 2

Lecture 2

Embed Size (px)

DESCRIPTION

Lecture 2. Review. To play with Turtle, we need to download and install followings: JDK 6 Dr. Java Sample program (e.g. BookClass). JDK 6. Java Development Kit (version 6) It contains several tools, required to write and test Java applications You may download - PowerPoint PPT Presentation

Citation preview

Page 1: Lecture 2

Lecture 2

Page 2: Lecture 2

Review

• To play with Turtle, we need to download and install followings:

1. JDK 6

2. Dr. Java

3. Sample program (e.g. BookClass)

Page 3: Lecture 2

JDK 6

• Java Development Kit (version 6)– It contains several tools, required to write

and test Java applications– You may download

Windows Platform - Java(TM) SE Development Kit 6

http://java.sun.com/javase/downloads/index.jsp

Note: It is already installed in PC at computer lab

Page 4: Lecture 2

Dr. Java

• It is a software application, called IDE (Integrated Development Environment)

– It supports us to develop an application– It provides GUI builder, a text/program editor,

compiler, and debugger, etc.For example

Visual Studio, JBuilder, FrontPage and DreamWeaver

– You may download

Windows App

http://drjava.org/

Page 5: Lecture 2

Sample Program

• Sample program contains Java program, which enable us to play with Turtle– You may download

bookClasses-8-21-06.ziphttp://coweb.cc.gatech.edu/mediaComp-plan/101

– You have to extract the zip file into a directoryFor example, you can create a directory named

“BookClass”, and extract files into the directory

Page 6: Lecture 2

And then …

• After open Dr.Java, you have to tell Dr. Java where the sample program is

1

SelectEdit -> Preference

2 Click Add and specifythe directory of BookClass

Page 7: Lecture 2

• Don’t forget Homework 0

– Be prepare for doing homework and projectsat both computer lab and home !!!

Page 8: Lecture 2

Today

• Let’s talk about

– Object

– Class

– Instance

– Method

– Statement

Page 9: Lecture 2

Creating objects

World w1 = new World()

Create a new world, named w1

- This creates a window whose size is 640x480 pixels

480

640

Page 10: Lecture 2

Creating objects

Turtle t1 = new Turtle(w1)

Create a turtle, named t1, in the world w1

- as a default, a turtle is located at the center

where?

(320, 240)

Page 11: Lecture 2

Creating objects

World w1 = new World()World w2 = new World()Turtle t1 = new Turtle(w1)Turtle t2 = new Turtle(w2)Turtle t3 = new Turtle(w1)

You can create many worlds and turtles

W1 W2

T1, T3 T2

Page 12: Lecture 2

What is an object

• In real world– Dog, Book, Cell phone, etc.– Each object has a state and a behavior

For example, Dog• State – He has name, color, breed, etc.• Behavior – He is barking, wagging, etc.

– The world is viewed as a collection of objects

• Similarly, in Java programming world– Object contains data and operations

For example, Turtle t1• has name (t1) and color (green), etc.• can move, can turn, etc.

Page 13: Lecture 2

Class and Object

• For example of phone, there are many types of phones.

• To create “your phone” (which is an objects) we need a blueprint to create it.– Let’s call the blueprint PHONE

• The blueprint PHONE is called class

• In java, we say that “your phone” is an instance of class, known as PHONE

PHONE

Page 14: Lecture 2

Class and Object

TurtleObject 1

TurtleObject 2

Turtle: Class

• Class is like a cookie cutter• Many objects are created

from a class

• Using the new operator:ClassName name = new ClassName();

• For exampleTurtle t1 = new Turtle(w1)

Page 15: Lecture 2

Methods of Turtle

A Turtle has a pen

A Turtle can take actions such as move and turn

While a turtle moves, he draws a line with a pen

In Java

We call the actions as methods

e.g. move forward, and backward, turn right and left, etc.

Page 16: Lecture 2

How to call method

ObjectName.MethodName()

For example, Turtle t1 = new Turtle(w1)

t1.forward()t1.backward()t1.turnRight()t1.turnLeft()

t1.forward(200)t1.turn(45)

ObjectName.MethodName(parameter)

Page 17: Lecture 2

Statement

Each instruction is called a statement

A program is a collection of statements

Note: Please remember that semi-colon is required at the end of each statement

World w1 = new World();t1.turnLeft();

Page 18: Lecture 2

Lab

– Let’s play with Turtle

Page 19: Lecture 2

Warming up!!!

• Try to draw a triangle

Page 20: Lecture 2

Practice!!!

• Try to draw a pentagon

Page 21: Lecture 2

Practice!!!

• Try to draw a pentagon

108

72

180 x 3 / 5 =

Page 22: Lecture 2

Additional methods …

penUp()

- turtle will grab his pen up

penDown()

- turtle will put his pen down

moveTo(x,y)

- turtle will move to a position (x, y)

Page 23: Lecture 2

Additional methods … (more!!!)

hide()- turtle will hide from the world

show()- turtle will show up again

clearPath()- clear all lines in the world

note: you have to minimize the window

setColor( java.awt.Color.xxx )- turtle will change the pen color- xxx will be RED, BLUE, GREEN, etc

Page 24: Lecture 2

Challenge

• Try to draw one triangle and one squarein the world