1 / 87

Introduction to Object-Oriented Programming in Alice and Java

Introduction to Object-Oriented Programming in Alice and Java. Barb Ericson Georgia Institute of Technology Sept 2006. Learning Goals. Introduce computation as simulation Introduce Alice Create objects in Alice Invoke methods on objects in Alice Introduce DrJava Create objects in Java

arty
Download Presentation

Introduction to Object-Oriented Programming in Alice and Java

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Object-Oriented Programming in Alice and Java Barb Ericson Georgia Institute of Technology Sept 2006 Georgia Institute of Technology

  2. Learning Goals • Introduce computation as simulation • Introduce Alice • Create objects in Alice • Invoke methods on objects in Alice • Introduce DrJava • Create objects in Java • Introduce variables as object references • Invoke methods on objects in Java • Create a method in Java • Create a method in Alice • Introduce subclasses Georgia Institute of Technology

  3. 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

  4. 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

  5. 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 his or her job? • What does each type 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

  6. 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

  7. 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

  8. Turning On Java Syntax • Click in Edit and then Preferences • Change the display my program: from Alice Style to Java Style in Color • Quit Alice and start it up again • It will keep this setting until you change it again Georgia Institute of Technology

  9. 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

  10. Playing the Movie • To play a saved world • 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

  11. Create Your Own Movie • Click on File • Then New World • Click on a template • Background and ground type Georgia Institute of Technology

  12. 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 • Use the Search Gallery button to look for a certain type Georgia Institute of Technology

  13. 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

  14. 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

  15. 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

  16. 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

  17. 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 (a reference) • bunny, bunny2, bunny3 • Class names start with a capital letter • Object names start with a lower case letter Georgia Institute of Technology

  18. Telling Objects To Do Things • Click the Done button to stop adding objects • Click on a reference to an object in the object tree window to select it • Click on the Methods tab to see what it can do Georgia Institute of Technology

  19. Messages to Objects • In object-oriented programming we send messages to objects • Drag a method from the left to the middle window (world - my first method) • A green line will show you were it will go Georgia Institute of Technology

  20. How to Execute my_first_method • Click on the Play button • This starts the world • When the World starts it will execute the method • my_first_method Georgia Institute of Technology

  21. Method Syntax • Syntax is how statements need to be formed in a programming language • The syntax for a method call on an object is: • objRef.method(parameterList); • We call this “dot” notation Georgia Institute of Technology

  22. Who does the action? • We had selected bunny in the object tree window • Before we dragged the method • So the method is invoked on bunny • How do you think we can get bunny2 to do the same action? Georgia Institute of Technology

  23. Moving Several Objects • To get bunny2 to move • Select bunny2 in the object tree 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

  24. Sequential Execution • By default all Alice methods are executed one after the other • You can specify this explicitly using a doInOrder control structure • Drag it from here • And drag the other statements into it • What happens when you play it again? Georgia Institute of Technology

  25. Parallel Execution • What if you want two or more statements to execute at the same time? • Use a doTogether • Put the things that you want to happen at the same time in the doTogether block • We added methods to make each bunny say hello as it goes up • What happens if you put a move up and move down in a doTogether? Georgia Institute of Technology

  26. Challenge • Create a world in Alice with at least 2 objects of the same class • and at least 3 objects from different classes • Have each object do some actions • Some at the same time • Some sequential • You can use say to have objects “talk” to each other Georgia Institute of Technology

  27. 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

  28. 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

  29. 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

  30. Open Preferences in DrJava Georgia Institute of Technology

  31. Adding Book Classes to Classpath Add bookClasses directory Click on Add Georgia Institute of Technology

  32. Creating Objects • To create objects we ask the object that defines the class to create it • Each object is created in memory with space for the fields it needs • 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

  33. 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

  34. Creating Objects in Java • In Java the syntax for creating an object is: 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

  35. 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 that the object was using 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

  36. 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 • In programming this is called declaring a variable Georgia Institute of Technology

  37. 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

  38. When you declare a variable the computer assigns memory to it The amount of memory depends on the type For each variable the computer stores a map of the name to the memory location and the type When you use the name the computer looks up the memory location And uses the value at that location 1 00000000 2 00001111 3 00000000 4 00111000 5 00000000 6 00111100 7 01111000 8 00000000 9 00000000 10 00000000 11 00000000 12 00000001 Declaring Variables address memory Object of type World earth variable holds a reference To the World Object above Georgia Institute of Technology

  39. 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

  40. In your cell phone you probably have names that map to phone numbers When you pick Home it looks up the number and uses it to make the call You can’t have two names that are exactly the same The phone wouldn’t know which number you are referring to You can modify the phone number for a name Barb 555-1235 Home 555-2938 Michael 555-3214 Shannon 555-2921 Cell Phones use Variables Georgia Institute of Technology

  41. 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 • Different types have different size boxes Hat Box Georgia Institute of Technology

  42. What value(s) does the memory on the right represent? It could be 4 char values 2 bytes each (16 bits) Unicode Or 2 int values 4 bytes each (32 bits) 2’s compliment Or 1 double value 8 bytes each (64 bits) In IEEE format Or an object reference The size is up to the virtual machine 1 00001000 2 00100000 3 00000100 4 01000000 5 00000001 6 10000000 7 00111000 8 11110000 Variables Sizes Georgia Institute of Technology

  43. 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

  44. 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

  45. 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

  46. 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

  47. 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

  48. Turning a Turtle • Turtles can turn • Right jen.turnRight(); jen.forward(); • Left shar.turnLeft(); shar.forward(50); Georgia Institute of Technology

  49. 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

  50. 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

More Related