Teaching Java using Turtles part 2

Slides:



Advertisements
Similar presentations
Introduction to Object-Oriented Programming in Alice and Java
Advertisements

Computer Science 1000 LOGO I. LOGO a computer programming language, typically used for education an old language (1967) the basics are simple: move a.
Mrs. Chapman. Tabs (Block Categories) Commands Available to use Script Area where you type your code Sprite Stage All sprites in this project.
Four simple expressions in meta. Data objects Pieces of data in a computer are called objects Today, we’ll talk about four kinds of objects Numbers Pictures.
Four simple expressions in meta. Data objects Pieces of data in a computer are called objects Today, we’ll talk about four kinds of objects Numbers Pictures.
Programming in Python Turtle Graphics Dr. Kristine Nagel Genie Yang Raquel Lawrence Dr. Kristine Nagel Genie Yang Raquel Lawrence Georgia Gwinnett College.
© 2006 Pearson Education Making Objects 1 of 19 MAKING OBJECTS Views of a Class Defining Your Own Class Declaring Instance Variables Declaring Methods.
Mr. Wortzman. Tabs (Block Categories) Available Blocks Script Area Sprite Stage All sprites in this project.
01-Intro-Object-Oriented-Prog-Alice1 Barb Ericson Georgia Institute of Technology Aug 2009 Introduction to Object-Oriented Programming in Alice.
TOPIC 3 INTRODUCTION TO PROGRAMMING 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B.
04-Intro-Object-Oriented-In-Java1 Barb Ericson Georgia Institute of Technology Sept 2009 Introduction to Object-Oriented Programming in Java.
Introduction to Programming Writing Java Beginning Java Programs.
Introduction to Programming with Java. Overview What are the tools we are using – What is Java? This is the language that you use to write your program.
Georgia Institute of Technology Barb Ericson Georgia Institute of Technology May 2006 Teaching Java using Turtles part 1.
TOPIC 3 INTRODUCTION TO PROGRAMMING 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B.
Java Classes Using Java Classes Introduction to UML.
Methods in Java CSC1401. Overview In this session, we are going to see how to create class-level methods in Java.
Graphics Concepts CS 2302, Fall /3/20142 Drawing Paths.
Georgia Institute of Technology Barb Ericson Georgia Institute of Technology May 2006 Teaching Java using Turtles part 2.
Agent P, I have been hearing some rumours about a Python Turtle.
Copyright © Curt Hill Turtles The beginning of media computation.
TURTLE GRAPHICS IP MR. MELLESMOEN. LOGO IN THE 1970’S THERE WAS A SIMPLE BUT POWERFUL PROGRAMMING LANGUAGE CALLED LOGO THAT WAS USED BY A FEW.
Georgia Institute of Technology Declaring Variables – Mod 4 Barb Ericson Georgia Institute of Technology August 2005.
Java course. Assignment #2, due: Feb. 27, (20) (a) Tell the difference between the abstract class and the interface. (b) Tell the difference between.
Java Introduction part 2 CSC1401. Overview In this session, we are going to examine some of the instructions from the previous class in more detail.
Game Maker Terminology
Graphic Basics in C ATS 315. The Graphics Window Will look something like this.
Introduction to Programming Writing Java Beginning Java Programs.
Georgia Institute of Technology Declaring Variables – Mod 4 Barb Ericson Georgia Institute of Technology August 2005.
Georgia Institute of Technology Simulations Barb Ericson Jan 2005.
Variables and Functions Alice. 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.
Georgia Institute of Technology Barb Ericson Georgia Institute of Technology Aug 2006 Introduction to Object-Oriented Programming in Alice and Java.
Intro to Turtle Graphics (Part 2)
Turtle Graphics Let’s see what we can draw on Python!
Georgia Institute of Technology Barb Ericson Georgia Institute of Technology Sept 2006 Introduction to Object-Oriented Programming in Alice and Java.
Georgia Institute of Technology Dr Usman Saeed Assistant Professor Faculty of Computing and Information Technology North Jeddah Branch King Abdulaziz University.
Introducing the turtle
Georgia Institute of Technology More on Creating Classes Barb Ericson Georgia Institute of Technology June 2006.
Computer Programming.
Declaring Variables – Mod 4
Introduction to Object-Oriented Programming in Alice and Java
Introduction to Object-Oriented Programming in Java
A Tiny Look at the Graphics Window
Agent P, I have been hearing some rumours about a Python Turtle.
Learning to program with Logo
Teaching Java using Turtles part 1
Microsoft® Small Basic
Declaring Variables – Mod 4
Teaching Java using Turtles part 3
Suppressing print Newline
Declaring Variables – Mod 4
Barb Ericson Georgia Institute of Technology Oct 2005
Introduction to Object-Oriented Programming in Alice and Java
Workshop for Programming And Systems Management Teachers
Introduction to Object-Oriented Programming in Alice
Introduction to Turtle Graphics
Workshop for Programming And Systems Management Teachers
Robotics Week 4 Functions and reusable code
A Tiny Look at the Graphics Window
More on Creating Classes
Relations And Functions.
creating a ecosystems model in net logo
IAT 800 Foundations of Computational Art and Design
More on Creating Classes part 3
Creating a Simple Game in Scratch
Teaching Java using Turtles
Teaching Java using Turtles part 2
The beginning of media computation Followed by a demo
Presentation transcript:

Teaching Java using Turtles part 2 Georgia Institute of Technology

Georgia Institute of Technology Learning Goals Create and initialize objects Using the new operator Passing values to initialize the new object Declare variables To use objects again Send messages to objects to ask them to do something Objects can do certain behaviors You ask an object to do a behavior by sending it a message Objects can refuse to do what you ask Use turtles to draw simple shapes Georgia Institute of Technology

Georgia Institute of Technology 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 Type name = 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(); A reference to an object is a way to calculate the address of the object in memory. It is not the address of the object. References are not pointers. Georgia Institute of Technology

Georgia Institute of Technology 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(); null earth World Object 1 earth earth World Object 2 Georgia Institute of Technology

A Variable Associates a Name to 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 You can't declare two variables with the same name in a program! World earth = new World(); Error: Redefinition of 'earth' You can change what an object variable refers to earth = new World(); The first example would try to name two different boxes with the same name. But, then how would you know which one is the right one when someone refers to that name? Georgia Institute of Technology

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

Georgia Institute of Technology 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); A turtle must be created in a world or you will get an error java.lang.NoSuchMethodException: Turtle constructor. Georgia Institute of Technology

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 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 Turtles cycle through 4 different colors. The 5th turtle you create will have the same color as the first turtle. Georgia Institute of Technology

Georgia Institute of Technology 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); Passing the amount to move forward by is also called passing a parameter. Georgia Institute of Technology

Georgia Institute of Technology Turning a Turtle Turtles can turn Right jen.turnRight(); jen.forward(); Left shar.turnLeft(); shar.forward(50); Turtles turn by 90 degrees by default. Georgia Institute of Technology

Georgia Institute of Technology 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

Georgia Institute of Technology 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

Georgia Institute of Technology 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 You can do these in the interactions pane. Georgia Institute of Technology

Georgia Institute of Technology 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

Georgia Institute of Technology 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 There are 640 pixels in the width (0 to 639) and 480 in the height (0 to 479). Georgia Institute of Technology

Georgia Institute of Technology Challenge Create a World object Don’t forget to declare a variable to hold a reference to it Create a turtle object 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

Georgia Institute of Technology Summary Create and initialize objects new Class(); new Class(value1,value2); new World(); Declare variables to reuse values and objects Type name = null; Type name = new Class(); Type name = new Class(value1,value2); World earth = new World(); Turtle tommy = new Turtle(earth); Send messages to objects to ask them to do something objRef.message(); objRef.message(value1,value2); tommy.turnLeft(); tommy.forward(50); Georgia Institute of Technology