Three types, subtypes, and inheritance. The story up until now Everything in your computer is data Including programs Data is divided into objects Objects.

Slides:



Advertisements
Similar presentations
JavaScript I. JavaScript is an object oriented programming language used to add interactivity to web pages. Different from Java, even though bears some.
Advertisements

Two motion and change: programming with imperatives.
 Variables  What are they?  Declaring and initializing variables  Common uses for variables  Variables you get “for free” in Processing ▪ Aka: Built-in.
CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
Computer Science and Engineering College of Engineering The Ohio State University Classes and Objects: Members, Visibility The credit for these slides.
TOPIC 12 CREATING CLASSES PART 1 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson,
IAT 334 Lab 2 Computer Graphics: Rocket, PImage. June 4, 2010IAT 3342 Outline  Programming concepts –Programming Computer Graphics –Transformations –Methods.
Lecture 18 Templates, Part II. From Last Time: What is a Template? This is the “official” specification for a template. It says that to define a template.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Week 2: Primitive Data Types 1.  Programming in Java  Everything goes inside a class  The main() method is the starting point for executing instructions.
Thirteen recursion. Recursion ► [define horizontal-array [object spacing count → [if [= count 1] object [group object [translate [point spacing 0] [horizontal-array.
Six compound procedures and higher-order procedures.
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.
1 A Simple Applet. 2 Applets and applications An application is an “ordinary” program Examples: Notepad, MS Word, Firefox, Halo, etc. An applet is a Java.
Twenty high-level operations on pictures. Recap: vector graphics constructors [box width height] Creates a picture with a box [group pictures …] Makes.
Eleven colors and rasters. Color objects [color name] Returns color with the specified name [color red green blue] Returns color with the specified amounts.
Sixteen lists and compound data. Recap Names: constants and variables When evaluated, return a specific data objects Can make new names with: [define.
IAT 800 Lecture 4. Sept 18, Fall 2006IAT 8002 Outline  Programming concepts –Methods –Classes  Talk about project 1  Reading: Read Chapters 1-4 of.
Fifteen high-level operations on pictures. Recap: vector graphics constructors [box width height] Creates a picture with a box [group pictures …] Makes.
Fourteen lists and compound data. Some primitive data types Integers (whole numbers) 1, 2, 3, -1, 0, , etc. “Floating-point” numbers ,
Eight compound procedures and higher-order procedures.
Seven constructing simple procedures using abstraction.
Twelve painting with procedures. Overview Making shaded images with procedures Making a more elegant language Making textures with noise functions.
Copyright 2008 by Pearson Education Building Java Programs Graphics reading: Supplement 3G videos: Ch. 3G #1-2.
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.
Eighteen painting with procedures. Review [bitmap-from-procedure procedure width height] Returns a bitmap width pixels by height pixels Obtains colors.
Thirteen conditional expressions: letting programs make “decisions”
Unit 6 object-oriented simulation. Object-oriented simulation Used for simulation of physical systems Chemical plants Air combat Ecologies Key idea: the.
Five implementing streams using classes. Exercise 1 [repeat list count] Returns count copies of list, in order [random-pattern count elements …] Returns.
CS 106 Introduction to Computer Science I 03 / 17 / 2008 Instructor: Michael Eckmann.
Six compound procedures and higher-order procedures.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
Games and Simulations O-O Programming in Java The Walker School
IAT 355 Lecture 4 Computer Graphics: Rocket. May 9, 2014IAT 3552 Outline  Programming concepts –Programming Computer Graphics –Transformations –Methods.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
CSC 142 C 1 CSC 142 Object based programming in Java [Reading: chapter 4]
1 Interface Types & Polymorphism & introduction to graphics programming in Java.
CS2110: SW Development Methods Textbook readings: MSD, Chapter 8 (Sect. 8.1 and 8.2) But we won’t implement our own, so study the section on Java’s Map.
Week 2 - Monday.  What did we talk about last time?  Software development  Lab 1.
Image Synthesis Rabie A. Ramadan, PhD D Images.
ArrayList, Multidimensional Arrays
Introduction to Java CS1316: Representing Structure and Behavior.
PART 3. You have been using static methods in your programs before and methods are the building blocks of procedural programming. In this part of the.
1 CS1110, 8 March 2009 Two topics: elementary graphics (for A5); loops Reading: Sec and chapter 7 on loops. The lectures on the ProgramLive CD can.
Lecture 15: Intro to Graphics Yoni Fridman 7/25/01 7/25/01.
Java is an object oriented programming language In this model of programming all entities are objects that have methods and fields Methods perform tasks.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 15: More-Advanced Concepts.
1 A Simple Applet. 2 Applets and applications An application is an “ordinary” program Examples: Notepad, MS Word, Firefox, Halo, etc. An applet is a Java.
Copyright 2008 by Pearson Education Building Java Programs ArrayList Reading: 10.1.
1 Building Java Programs Chapter 7: Arrays These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold, or.
Making Python Pretty!. How to Use This Presentation… Download a copy of this presentation to your ‘Computing’ folder. Follow the code examples, and put.
Aug 9, CMSC 202 ArrayList. Aug 9, What’s an Array List ArrayList is  a class in the standard Java libraries that can hold any type of object.
Inheritance Notes Chapter 6 1. Inheritance  you know a lot about an object by knowing its class  for example what is a Komondor? 2
Three objects, classes, and hierarchies. This lecture will be long… Sorry This is going to be the big programming lecture of the quarter It’s really about.
Week 13 - Wednesday.  What did we talk about last time?  Color representation  Color class  Picture class.
Lesson 2: Reading a program. Remember: from yesterday We learned about… Precise language is needed to program Actors and Classes Methods – step by step.
CSE 1201 Object Oriented Programming ArrayList 1.
CS 106 Introduction to Computer Science I 03 / 22 / 2010 Instructor: Michael Eckmann.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
Programming in Processing Taught by Ms. Madsen Assistants: Ms. Fischer and Ms. Yen Winsor School, 2/20/08.
CompSci 4 Java 4 Apr 14, 2009 Prof. Susan Rodger.
Computer Graphics: Rocket, Java: Class
high-level operations on pictures
constructing simple procedures using abstraction
painting with procedures
LCC 6310 Computation as an Expressive Medium
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Lesson 2: Building Blocks of Programming
Introduction to Primitive Data types
Introduction to Primitive Data types
Presentation transcript:

three types, subtypes, and inheritance

The story up until now Everything in your computer is data Including programs Data is divided into objects Objects can be “inside” of other objects Boxes inside groups Colors inside bitmaps Objects have types Procedures Numbers (1, -3.5) Strings (“this is a string”, “blue”) Bitmaps Picture objects (lines, groups, boxes, etc.)

Number Value: 10 Looking inside data objects Data objects are like forms They have fields (aka members) Filled in by values The fields Have names (Width, Height) The fields are filled in by other data objects The object’s type (Box, Color) determines what fields it has Box Width: 10 Height: 10 Ellipse Width: 15 Height: 10 Procedure Name: iterated-group Arguments: proc count Body: [apply group [up-to count proc]] Color R: 240 G: 220 B: 0

Object types and type objects [type-of object] Asks an object what its type is The answer is a piece of data So it’s an object A type object [is? object type] Asks if object is of type type Answer is a Boolean (true or false) Meta has a bunch of types already named Number (any number) Integer (integers) Float (floating-point numbers) Boolean Color Bitmap List Type By convention type names are captialized

Defining new data types [class-type [name fields …] Object] Creates a new data type Called name With fields fields … We’ll explain the Object part later Returns an object representing that type So you then need to defining it to have a name Yes, I know you’ve already given it a name, but trust me [define Note [class-type [Note pitch duration volume] Object]] Says: Make a new type Note, objects of which contain within them: A pitch to play A duration for which to play it And a volume at which to play it

Creating new data objects [new type arguments …] Creates a new object of the specified type The arguments depend on the type For types created using class-type, they’ll be the values of the object’s fields, in order ► [define Note [class-type [Note pitch duration volume ] Object]] ► [define note [new Note ]] ► note.pitch 60 ► note.duration 1 ► [note.duration ← 2] ► note.duration 2

Creating new data objects [new type arguments …] Creates a new object of the specified type The arguments depend on the type For types created using class-type, they’ll be the values of the object’s fields, in order [define my-chord [list [new Note ] [new Note ] [new Note ]] Or to show off: [define my-chord [map [pitch → [new Note pitch 1 1]] [list ]]]

Types and subtypes Types often have subtypes Integers are a subtype of numbers Boxes are a subtype of Pictures All types are subtypes of the magic type Object Subtyping means objects can have many types All Integers are also Numbers All Boxes are also Pictures And all objects are Objects Object List Picture Number Integer Float Array BoxLineGroup

Defining new data types [class-type [name fields …] parent-type] The parent-type field specifies what the new type is a subtype of In the past, we’ve just make this object But you can use any of the types you define [define Picture [class-type [Picture] Object]] [define Box [class-type [Box width height] Picture]] [define Line [class-type [Line start end] Picture]] [define Group [class-type [Group objects] Picture]]

Class-based inheritance (Danger Will Robinson! This slide lies!) Types automatically “inherit” the fields of their parent types “Class” is effectively a synonym for type that means “type with inheritance” In this example Pictures have no fields Groups have one field (objects) TranslatedGroups have two (objects and point) RotatedGroup also have two (objects and angle) Etc. [define Picture [class-type [Picture] Object]] … [define Group [class-type [Group objects] Picture]] [define TranslatedGroup [class-type [TranslatedGroup point] Group]] [define RotatedGroup [class-type [RotatedGroup angle] Group]] [define InkedGroup [class-type [InkedGroup pen] Group]] [define PaintedGroup [class-type [PaintedGroup color] Group]]

Class hierarchy (partial) Object No fields Picture No fields Box width, height Group objects Line start, end TranslatedGroup objects, point PaintedGroup objects, color RotatedGroup objects, angle InkedGroup objects, pen

Some wrapper code [define box [width height → [new Box width height]]] [define line [start end → [new Line start end]]] [define group [objects … → [new Group objects]]] [define translate [point objects … → [new TranslatedGroup objects point]]] [define rotate [angle objects … → [new RotatedGroup objects angle]]] …

Reimplementing last quarter Okay, so we’ve been slowing reimplementing the graphics library from last quarter We’ve got data types (classes) to represent the different flavors of Picture We’ve got wrapper procedures to make the Picture objects for us There’s just one thing missing … Um, uh, how to we actually draw the stupid things?

From last time: Drawing in MS Windows [with-window “test” [g → [g.DrawLine [pen “black” 1] [point 0 0] [point ]] [g.DrawLine [pen “blue” 3] [point 50 0] [point ]] [g.DrawEllipse [pen “red” 40] ]]]

Drawing pictures How do we draw a picture object in a window? It depends on the type of object Boxes Call g.DrawBox Lines Call g.DrawLine Groups Draw all the objects in the group’s objects field. So we need some way of basing the action to take on the type of the argument

Generic procedures and methods [define name [generic-procedure]] [define-method [name [type arg] …] body …] A generic procedure is a procedure that bases its behavior on the type(s) of its argument(s) A method is a specification of how to perform the procedure for a given argument type

Drawing lines [define draw [generic-procedure]] [define-method [draw [Line l] g] [g.DrawLine l.start l.end] ] draw is a procedure that behaves differently depending on its argument types This method defines how draw behaves when called with a Line (named l) and some other argument named g (no special type is specified for g) In this case, the system calls the DrawLine member of g with l.start and l.end as arguments

Drawing pictures [define draw [generic-procedure]] [define-method [draw [Line l] g] [g.DrawLine l.start l.end]] [define-method [draw [Box b] g] [with half-height = [/ b.height 2] half-width = [/ b.width 2] [g.DrawBox [- half-width] [- half-height] half-width half-height]]] … «For-each is like map but it doesn’t return any results» [define-method [draw [Group x] g] [for-each [p → [draw p g]] x.objects]] «Without using for-each» [define-method [draw [Group x] g] [with position = 0 [while [< position [length x.objects]] [draw [get x.objects position] g] [position ← [+ position 1]]]]]

Okay, that doesn’t actually work … [define draw [generic-procedure]] [define-method [draw [Line l] g] [g.DrawLine pen l.start l.end]] [define-method [draw [Box b] g] [with half-height = [/ b.height 2] half-width = [/ b.width 2] [g.DrawBox pen [- half-width] [- half-height] half-width half-height]]] … «For-each is like map but it doesn’t return any results» [define-method [draw [Group x] g] [for-each [p → [draw p g]] x.objects]] «Without using for-each» [define-method [draw [Group x] g] [with position = 0 [while [< position [length x.objects]] [draw [get x.objects position] g] [position ← [+ position 1]]]]]

Fixing it … [define draw [generic-procedure]] [define-method [draw [Line l] g pen] [g.DrawLine pen l.start l.end]] [define-method [draw [Box b] g pen] [with half-height = [/ b.height 2] half-width = [/ b.width 2] [g.DrawBox pen [- half-width] [- half-height] half-width half-height]]] [define-method [draw [Group x] g pen] [for-each [p → [draw p g pen]] x.objects]] «Cool; now we know how to do inked groups» [define-method [draw [InkedGroup x] g pen] [for-each [p → [draw p g x.pen]] x.objects]] «And now…» [define really-draw-the-stupid-picture [p → [with-window [g → [draw p g [pen “black” 1]]]]]]

Uninitialized fields Most imperative languages don’t require you to specify values for all an object’s fields when you create it This is generally considered a bad idea, by the way Such fields are called “uninitialized” If you don’t give a value for a field, it’s usually set to the magic value null null isn’t a data object null has no type

How class-type really works [class-type [name initialized-fields …] parent-type other-fields …] This makes a type called name That has all the fields Of parent-type That appear in other-fields And/or that appear in initialized-fields When you call new, only the ones in initialized-fields will be initialized from the arguments

Class-based inheritance (Corrected version of the code) Types automatically “inherit” the fields of their parent types But inherited fields still need to be specified in the constructor args if you want them initialized [define Picture [class-type [Picture] Object]] … [define Group [class-type [Group objects] Picture]] [define TranslatedGroup [class-type [TranslatedGroup objects point] Group]] [define RotatedGroup [class-type [RotatedGroup objects angle] Group]] [define InkedGroup [class-type [InkedGroup objects pen] Group]] [define PaintedGroup [class-type [PaintedGroup objects color] Group]]

Generic procedures vs. member procedures You may have noticed we’ve called two different things “methods” Things you add to generic procedures using define-method Things you call by saying [x.y bla bla bla …] Why are we using both? In systems like Java and.NET (on which Meta is built) You have to specify all the x.y methods when you make the class So the code has to be inside of class-type Which makes it big and hard to read And to change something, you have to completely redo the class So this quarter, we’re making life simpler (in some ways) by using generic procedures Next quarter, we’ll focus on the Java/C++ style methods