Classes, Objects, and Methods



Similar documents
How to Build a Web Site using your Web Host s Web Site Builder

1. Use the class definition above to circle and identify the parts of code from the list given in parts a j.

Please Excuse My Dear Aunt Sally

Pigeonhole Principle Solutions

CS193j, Stanford Handout #10 OOP 3

MEAP Edition Manning Early Access Program Hello! ios Development version 14

Ecommerce and PayPal Shopping Cart

When To Work Scheduling Program

Scratch Primary Lesson 4

TUTORIAL: BOARDMAKER STUDIO START-UP

A Short Course in Logic Example 8

App Development Checklist

Step 1: Customize your channel

Open Broadcasting Software (OBS) Guide for Krue.tv

Video Game Design (Master) Content Skills Learning Targets Assessment Resources & Technology CEQ:

Client Questionairre. Website Design Checklist

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

A DIGITAL SOLUTIONS AGENCY Queen Anne Ave N. Ste. 337 Seattle WA,

Getting Started with the Internet Communications Engine

Presenter: Richard Turton, West Virginia University

Introduction. Inserting Hyperlinks. PowerPoint 2010 Hyperlinks and Action Buttons. About Hyperlinks. Page 1

Welcome to the HVAC Sales Academy Learning Management System (LMS) Walkthrough:

Making a Website with Hoolahoop

IBSwebpro Web Design Services. ecommerce Website Design Projects

Gmail Merge. Step 1: Writing the actual e mail

MODULE 1 // SAVE MONEY. START NOW. AMATEUR: AGES 11-14

Cooperative Learning Method Based On Game Design and Visual Object Oriented Environment to Teach Object Oriented Programming Course

CODESPARK TEACHER S GUIDE Hour of Code

Intro to Statistics 8 Curriculum

Objects and classes. Objects and classes. Jarkko Toivonen (CS Department) Programming in Python 1

Applied Internet Technology (CSCI-UA.0480) - Sample Questions

Computer Science for San Francisco Youth

The PHP 5.4 Features You Will Actually Use

Many applications consist of one or more classes, each containing one or more methods. If you become part of a development team in industry, you may

Customer Retention Strategies Quick Tips To Make A Positive Difference

Service Description and Expectations. September 2010

Google Drive: Access and organize your files

UNIVERSITY OF CALIFORNIA BERKELEY Engineering 7 Department of Civil and Environmental Engineering. Object Oriented Programming and Classes in MATLAB 1

GETTING STARTED WITH HANGOUTS... 2 START A HANGOUT... 2 JOIN A HANGOUT... 4 INVITE PEOPLE TO A HANGOUT...

Chapter 6: Project Planning & Production

CHAPTER 1 HelloPurr. The chapter covers the following topics:

Igniting young minds through computer programming

PERSONAL LEARNING PLAN- STUDENT GUIDE

Global Variables. However, when global variables are used in a function block or control modules, they must be declared as external

T R A I N I N G M A N U A L

SCRATCH Lesson Plan What is SCRATCH? Why SCRATCH?

Part 3: GridWorld Classes and Interfaces

Youtube Tips & Tricks! YouTube Tips and Tricks

GRADE 3 CURRICULUM COMPANION - ACTIVITIES PANOPLY SCHOOL DAYS ~ STUDENT ART TENT

Early Math for School Readiness Kids Play Math: An Overview Front Porch Series Broadcast Calls

Object-Oriented Programming in Java

Monarch. Teacher User Guide for v4.0

Google Sites: Creating, editing, and sharing a site

Invitation Scripts Setting an Appointment by Text Messaging (Document 8 of 11)

WebPlus X7. Quick Start Guide. Simple steps for designing your site and getting it online.

A Beginner s Guide to Website Budgets The Website You Can Get For What You Have To Spend

A: We really embarrassed ourselves last night at that business function.

Volunteer Manual CrossFit Games Regionals

Creating Mobile Apps Development and Deployment

Java 7 Recipes. Freddy Guime. vk» (,\['«** g!p#« Carl Dea. Josh Juneau. John O'Conner

Human Rights in the U.S.

Python Lists and Loops

How to Use this Workbook

Today s Topics... Intro to Computer Science (cont.) Python exercise. Reading Assignment. Ch 1: 1.5 (through 1.5.2) Lecture Notes CPSC 121 (Fall 2011)

How to use the PraxisUnico event app

Jump$tart Washington Unit Two Chapter Five: Understanding Your Paycheck

Welcome to e2020! Included in this Guide. Tips to Help You Succeed with e2020. Focus on 3 Things: Space, Time, Productivity

Getting Started with EServer Courses Using EServer.org for Open-Source, Open-Access Teaching

Customizing your Blackboard Course

McGraw-Hill The McGraw-Hill Companies, Inc.,

Teacher's notes. Embedded question drill (indirect questions)

Fish Chomp. Level. Activity Checklist Follow these INSTRUCTIONS one by one. Test Your Project Click on the green flag to TEST your code

10 TIPS TO A MORE EFFECTIVE PERFORMANCE/BEHAVIOR REVIEW PROCESS

Organizing image files in Lightroom part 2

What is Skype? Skype is a free program that uses the latest technology to bring affordable and high-quality voice communications to people.

Tagxedo. Tagxedo is based on a Microsoft application called SilverLight. If you don t have it, don t worry. Tagxedo will prompt you to download it

Trial Copy. Guidelines for the Finance Coach TRAINING 3: PLANNING. Recommended for pupils in Years 7 to 9 (German school system)

As you ask them and if not, you have things to question, we can answer, we can give advice to you by within 24 hours.

CLOCKWORK Training Manual and Reference: Inventory. TechnoPro Computer Solutions, Inc.

Enhancing Your Smart Board Lessons

Programming in HTML5 with JavaScript and CSS3

7 th Grade Math Foundations for Teaching Unit One: Numbers & Operations Module One: Rational Number s

Stocks and Mutual Funds

MODULE: RELATIONSHIPS ONLINE

Creating Calculated Fields in Access Queries and Reports

Excel & Visual Basic for Applications (VBA)

Digital Life 102. objectives. Essential Question: What is the place of digital media in our lives? Learning Overview and Objectives.

Assignment # 2: Design Patterns and GUIs

Zoo Connections Curriculum

Counselor Lesson Plan

Unit One: The Basics of Investing

Transcription:

Classes, Objects, and Methods CS 5010 Program Design Paradigms "Bootcamp" Lesson 10.1 Mitchell Wand, 2012-2014 This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License. 1

Module Introduction In this module, we will see how classes, objects, and interfaces fit into our account of information analysis and data design We'll see how the functional and the objectoriented models are related We'll learn how to apply the design recipe in an object-oriented setting. 2

Goals of this lesson Learn the basics about classes, objects, fields, and methods. Learn how these ideas are expressed in the Racket object system 3

What is an object? An object is another way of representing compound data, like a struct. Like a struct, it has fields. It has one built-in field, called this, which always refers to this object Here are pictures of two simple objects: x = 10 y = 20 r = 10 h = 30 w = 15 color = "blue" We assume that you've seen some kind of object-oriented programming before, so we're just reviewing vocabulary here. If you've really never used OOP before, go do some outside reading before continuing. 4

How do you compute with an object? Every object comes equipped with a set of procedures, called methods. Each method has a name. To invoke a method of an object, we send the object a message. For example, to invoke the areamethod of an object obj1, we write (send obj1 area) 5

Classes Every object has a class. The class specifies the fields of the object. so it's like a define-struct. The class contains the methods of that object. In a typical design, we are likely to have many objects of the same class. To create an object, we say (new C) where Cis the name of the new object's class. You also need to give new initial values for the fields, but we ll get to that later. 6

Every object knows its class (1) x = 10 y = 20 r = 10 (class* object% () (init-field x y r) (define/public (foo) (+ x y)) (define/public (bar n) (+ r n))...) x = 15 y = 35 r = 5 These objects also have a this field, but we don't show it unless we need to. Here are two objects of the same class. In the class definition, the init-field declaration specifies that each object of this class has 3 fields, named x, y, and r. The class definition also defines two methods, named fooand bar, that are applicable to any object of this class. 7

Every object knows its class (2) x = 10 y = 20 r = 10 (class* object% () (init-field x y r) (define/public (foo) (+ x y)) (define/public (bar n) (+ r n))...) obj1 obj2 x = 15 y = 35 r = 5 The variables in the method declarations refer to the fields in the object. So: (send obj1 foo) returns 30 (send obj2 foo) returns 50 8

Every object knows its class (3) x = 10 y = 20 r = 10 (class* object% () (init-field x y r) (define/public (foo) (+ x y)) (define/public (bar n) (+ r n))...) obj1 obj2 x = 15 y = 35 r = 5 Methods can also take arguments, just like functions. So (send obj1 bar 8) returns 18 (send obj2 bar 8) returns 13 9

Every object knows its class (4) obj1 x = 10 y = 20 r = 10 obj2 x = 15 y = 35 r = 5 (class* object% () (init-field x y r) (define/public (foo) (+ x y)) (define/public (bar n) (+ r n)) (define/public (baz n) (+ (send this foo) n)))...) Methods are just Racket functions, so they can do anything a Racket function can do, including send messages to objects. (send obj1 baz20) returns (+ 30 20) = 50 (send obj2 baz20) returns (+ 50 20) = 70 10

Every object knows its class (5) obj1 obj3 x = 10 y = 20 r = 10 obj2 x = 15 y = 35 r = 5 x = 15 y = 35 r = 5 (class* object% () (init-field x y r) (define/public (foo) (+ x y)) (define/public (bar n) (- r n)) (define/public (baz n) (+ (send this foo) n)))...) (class* object% () (init-field x y r) (define/public (foo) (+ x y)) (define/public (bar n) (+ r n)) (define/public (baz n) (+ (send this foo) n)))...) Here's another object, obj3, of a different class (observe that the bar method is different). If we send a message to obj3, then obj3's methods will be invoked. 11

Every object knows its class (6) obj1 obj3 x = 10 y = 20 r = 10 obj2 x = 15 y = 35 r = 5 x = 15 y = 35 r = 5 (class* object% () (init-field x y r) (define/public (foo) (+ x y)) (define/public (bar n) (- r n)) (define/public (baz n) (+ (send this foo) n)))...) (class* object% () (init-field x y r) (define/public (foo) (+ x y)) (define/public (bar n) (+ r n)) (define/public (baz n) (+ (send this foo) n)))...) So (send obj2 bar 8) = (+ 5 8) = 13 (send obj3 bar 8) = (-5 8) = -3 12

Using The Racket Class System We will use full Racket (yay!) Write #lang racket at the beginning of each file And set the Language level to "Determine Language from Source" 13

First demonstration system: spaceinvaders.rkt A simple animated system using the universe module and the Racket object system Specifications: We have classes for worlds bombs helicopters 14

Game Description When the system starts, the world contains just a helicopter the helicopter rises at a constant rate Press space to drop a new bomb Bombs fall at a constant rate Bombs are draggable 15

Goal We'll walk through the code of this system to illustrate the Racket object system. 16

Demonstration: space-invaders.rkt Demonstration and code walkthrough 10-1- space-invaders.rkt Demonstration: http://youtu.be/hbcpu5b8q40(0:48) Walkthrough: Part 1: http://youtu.be/pbc0ruzb33u(7:09) Part 2: http://youtu.be/hszgsj04ll0(7:03) As with other videos, these videos were recorded earlier, and may not represent our best current practice. In particular, they use Number instead of Integer. 17

Lesson Summary We ve learned the basics about classes, objects, fields, and methods. We ve seen how these ideas are expressed in the Racket object system 18