Goal of lecture. Object-oriented Programming. Context of discussion. Message of lecture

Similar documents
Agenda CS121/IS223. Reminder. Object Declaration, Creation, Assignment. What is Going On? Variables in Java

Outline. Object Oriented Programming. Course goals. Staff. Course resources. Assignments. Course organization Introduction Java overview Autumn 2003

CS121/IS223. Object Reference Variables. Dr Olly Gotel

Objects and Classes. 1 Creating Classes and Objects. CSCI-UA 101 Objects and Classes

Introduction to Programming Using Java (98-388)

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

Short Notes of CS201

CS201 - Introduction to Programming Glossary By

Chapter 5: Procedural abstraction. Function procedures. Function procedures. Proper procedures and function procedures

Operating Systems CMPSCI 377, Lec 2 Intro to C/C++ Prashant Shenoy University of Massachusetts Amherst

CS558 Programming Languages Winter 2018 Lecture 4a. Andrew Tolmach Portland State University

CS558 Programming Languages

Functions in C C Programming and Software Tools

About this exam review

DOWNLOAD PDF CORE JAVA APTITUDE QUESTIONS AND ANSWERS

CONTENTS: Array Usage Multi-Dimensional Arrays Reference Types. COMP-202 Unit 6: Arrays

COMP 202. More on OO. CONTENTS: static revisited this reference class dependencies method parameters variable scope method overloading

Chapter 6 Introduction to Defining Classes

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

1 Epic Test Review 2 Epic Test Review 3 Epic Test Review 4. Epic Test Review 5 Epic Test Review 6 Epic Test Review 7 Epic Test Review 8

Programming Languages and Techniques (CIS120)

Functions in C C Programming and Software Tools. N.C. State Department of Computer Science

Object Class. EX: LightSwitch Class. Basic Class Concepts: Parts. CS257 Computer Science II Kevin Sahr, PhD. Lecture 5: Writing Object Classes

CS61C : Machine Structures

G Programming Languages - Fall 2012

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Types II. Hwansoo Han

Programming Languages and Techniques (CIS120)

Lecture 6 Introduction to Objects and Classes

CSE 307: Principles of Programming Languages

Chapter 1 Getting Started

Programming Languages and Techniques (CIS120)

Principles of Computer Science

Procedure and Object- Oriented Abstraction

Selected Java Topics

CSE 303: Concepts and Tools for Software Development

COSC 2P95. Procedural Abstraction. Week 3. Brock University. Brock University (Week 3) Procedural Abstraction 1 / 26

Run-Time Environments/Garbage Collection

Compiler construction 2009

CPSC 427: Object-Oriented Programming

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

News and information! Review: Java Programs! Feedback after Lecture 2! Dead-lines for the first two lab assignment have been posted.!

Module 10A Lecture - 20 What is a function? Why use functions Example: power (base, n)

Object Oriented Modeling

Java Fundamentals (II)

Pointers, Dynamic Data, and Reference Types

FORM 2 (Please put your name and form # on the scantron!!!!)

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

Creating an object Instance variables

Binghamton University. CS-140 Fall Data Types in Java

Chapter 4 Defining Classes I

Stream Computing using Brook+

Storage in Programs. largest. address. address

CPSC 427: Object-Oriented Programming

COMP-202. Recursion. COMP Recursion, 2011 Jörg Kienzle and others

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University

Lecture 3: C Programm

Algorithms & Data Structures

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012

JAVA GUI PROGRAMMING REVISION TOUR III

In Java we have the keyword null, which is the value of an uninitialized reference type

[0569] p 0318 garbage

CHAPTER 7 OBJECTS AND CLASSES

Object-Oriented Programming Concepts

Programming Languages and Techniques (CIS120)

Unit 5: More on Classes/Objects Notes

BM214E Object Oriented Programming Lecture 8

CA Compiler Construction


Introduction to Programming (Java) 4/12

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming

CS 6353 Compiler Construction Project Assignments

Programming II (CS300)

CS 536 Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 11

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #44. Multidimensional Array and pointers

G Programming Languages - Fall 2012

CE221 Programming in C++ Part 1 Introduction

For example, let s say we define an array of char of size six:

Java Memory Management

Principles of Software Construction: Objects, Design, and Concurrency. Objects (continued) toad. Spring J Aldrich and W Scherlis

Implementing Subprograms

EMBEDDED SYSTEMS PROGRAMMING Language Basics

CSE 504: Compiler Design. Runtime Environments

Cpt S 122 Data Structures. Course Review Midterm Exam # 2

Chapter 5 Names, Bindings, Type Checking, and Scopes

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

CSE351 Winter 2016, Final Examination March 16, 2016

Memory Management. CS31 Pascal Van Hentenryck CS031. Lecture 19 1

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

Lecture 3. Lecture

CHAPTER 5 VARIABLES AND OTHER BASIC ELEMENTS IN JAVA PROGRAMS

UNIT V Sub u P b ro r g o r g a r m a s

Handout 7. Defining Classes part 1. Instance variables and instance methods.

Chapter 10. Class & Objects in JAVA. By: Deepak Bhinde PGT Com.Sc.


Cpt S 122 Data Structures. Course Review Midterm Exam # 1

CPSC 3740 Programming Languages University of Lethbridge. Data Types

Transcription:

Goal of lecture Object-oriented Programming Understand inadequacies of class languages like Ur- Java Extend Ur-Java so it becomes an object-oriented language Implementation in SaM heap allocation of objects references to objects Important built-in classes in Java String Array Understand concepts of modularity of code data abstraction: important design technique for making code more modular Context of discussion Programming in the large Big applications require many programmers General approach to coding: Break problem into smaller sub-problems Assign responsibility for each sub-problem to somebody Assemble everyone s contributions For this to work, each sub-problem must have a carefully defined specification Functionality: what services must code provide? Interface: how do clients obtain that functionality? Job of programmer: provide an implementation (code) that meets the specification Message of lecture Separating interface from implementation is useful for writing more modular code that is easy to maintain Implementer of class can change his code without affecting interface, so client of class does not need to change code Called data abstraction in literature Class language: may be difficult to separate interface from implementation Object-oriented language: separation of interface from implementation is possible more modular code

Running application: 8-puzzle 4 6 N S E W 4 5 6 7 8 1 3 4 2 6 4 6 4 6 Program organization Two classes : written by you Functionality: methods to» put puzzle in Sam Loyd configuration» a N/S/E/W to get new configuration» what is there in some position? Interface:?? this is what we will study Test: client class, written by someone else, which will use interface to play game Implemention of class Two sub-tasks: What is the representation of state (puzzle configuration) in your class? Ur-Java complication: only base types like int, long, float, etc. Given this representation, how do we implement operations like and? 4 6 Representation of state row (r) 0 1 2 column (c) 0 1 2 4 9 6 123496758 Model puzzle as 3x3 grid of integers Use 9 to represent empty square Three rows (0..2) and three columns (0..2) Convert grid into single integer Concatenate s in left to right, top to bottom order Converting integer s into grid Remainder when s is divided by 10: in position (2,2) Java expression: s %10 Quotient after dividing by 10 gives encoding of remaining s Java expression: s / 10 Repeat remainder/quotient operations to extract remaining s This kind of encoding may seem strange but the need for it arises in many places in computer science Storing multi-dimensional arrays in memory Godelization of logical proofs

Implementing puzzle operations : put puzzle into Sam Loyd configuration s = 123456879; (r,c): what is in position (r,c)? return (s/10 8-(3r+c) ) % 10 : left to reader Key question in Ur-Java What kind of variable should we use to store the integer that represents puzzle state? 1. method parameter/local variable stack allocation 2. class variable allocated in static area As we will see, these implementation choices affect the interface of the class. Interface L(ocal) class Test (1) state in (2) state out class State is implemented as local variable in class Test passed to/returned from methods in class Interface of class: //informal { int (); //returns encoding of initial state int (int state, int r, int c); //return in position (r,c) of grid int (int state, char d); //returns encoding of new state Ur-Java code: Interface L class test { class { int state =.(); display(state); state =.(state, N ); public static void display(int state) { for (int c = 0; c < 3; c++) System.out.print(.(state,r,c) + ); System.out.println( ); //new line public static int () { return 123456879; public static int (int state, int r, int c) { return state/((int)math.pow(10,8-(3*r+c))) %10 ; public static int (int state, char d) {

Critique of Interface L No data abstraction class implementer chose to implement state as an int. This is visible to the interface to the class, so client code is aware of it. If class implementer decided to implement state as a long, client code must change. Interface S(tatic) Static area.state class Test class State is implemented as class variable in class state does not have to be passed back and forth between two classes Interface of class: //informal { void (); //updates class variable state int (int r, int c); //return in position (r,c) of grid void (char d); //updates class variable state Ur-Java code: Interface S Critique of Interface S class test {.(); display();.( N ); public static void display() { for (int c = 0; c < 3; c++) System.out.print(.(r,c) + ); System.out.println( ); //new line class { public static int state; public static void { state = 123456879; public static int (int r, int c) { return state/((int)math.pow(10,8-(3*r+c))) %10 ; public static void (char d) { Data abstraction: yes class implementer chose to implement state as an int. This is NOT visible to the interface of class, so client code is NOT aware of it. If class implementer decides to change implementation of state to long, client code does not have to change. Modularity: change to representation of data in programmer s code does not affect client code Problem: client can have only one puzzle to play with state is a private class variable in class Mechanism we have used (class variable) gives right of puzzle creation to implementer of class rather than the client of the class

Sneaky solution to problem Static area 1.state 2.state class Test class 1 class 2 Make copies of code of class and rename classes! If client wants n puzzles, it makes n copies of code of class and renames them. Sneaky Ur-Java code: Interface S class test { 1.(); display1(); 1.( N ); 2.(); public static void display1() { for (int c = 0; c < 3; c++) System.out.print(1.(r,c) + ); System.out.println( ); //new line //similar display method for 2 class 1{ public static int state; public static void { state = 123456879; class 2{ public static int state; public static void { state = 123456879; Critique Data abstraction: yes Creation on demand: yes, but at cost of duplicating code for class and renaming classes Size of program increases Must know at compile-time how many puzzles are needed Class code duplication and renaming must be repeated whenever new release of code becomes available Some client code may need to be duplicated Look at display method(s) in previous slide High-level picture Copying code for class and renaming gives us name to refer to a particular puzzle variable (state) to store configuration methods to manipulate state: variable state in method of class n refers to class variable of n Question: can we design mechanisms in the language for doing all this?

Solution: ask Gutenberg! Algorithm for making a copy of a book in ancient times: Hire a monk Give monk paper and quill Ask monk to copy text of book Algorithm for making n copies of a book Hire a monk Give monk lots of paper and quills Ask monk to copy text of book n times Gutenberg (Strasbourg ca.1450 AD) : new algorithm for making n copies of a book First make a template of book using movable type Stamp out as many copies of book as needed Copying class code is like medieval approach to copying books! How do we exploit Gutenberg s insight in our context? What is template for puzzles? What do we stamp puzzle instances out of? How do we stamp out puzzle instances as needed from template? How do we name different puzzle instances? Object-oriented languages Class definition is template Instance variables: when puzzle is stamped out, it will have storage for these variables Instance methods: methods for manipulating instance variables Constructor: special method in class definition that is invoked to stamp out puzzles Provided automatically by system when class is defined Instances of classes are called objects Type of object: class name Objects are stamped out in area of memory called heap objects have one slot for each instance member Heap allocation class { private int state; public void { state = 123456879; public int (int r, int c) { return state/((int)math.pow(10,8-(3*r+c))) %10 ; public void (char d) { Program area. state Heap shows two instances of class. Heap Class name is used as type of object. Each object has its own instance variables. Instance variables are declared private, so not accessible to client. Instance methods are compiled into SaM code and stored in Program area. All objects of type share code for instance methods as shown in picture. state Naming instances Reference: variable that is a name for instances of some class Implementation note: reference contains either NULL or the address of some object Type of reference: specifies the type of objects that reference can name class name used as type of reference p1; // declare a reference Assignment to reference p1 = new (); //p1 is name for the new object p2 = new ();//combination Accessing instance member p1.() Implementation note: this member access is implemented by SaM that does the following:. Examine object pointed to by p1. Look inside object for starting address of method named. Invoke that method

Client code class test { Program area. puzzle1= new (); puzzle1.(); display(puzzle1); Heap puzzle2 = new (); puzzle2.(); display(puzzle2); state state public static void display( p) { for (int c = 0; c < 3; c++) System.out.print(p.(r,c) + ); System.out.println( ); //new line puzzle2 : puzzle1 : args String[]: rv. Stack frame for main Method invocation References can be passed as parameters formal parameter becomes name for object in caller object can be manipulated in callee using that name on method return, caller sees any changes made to object by callee Ex: display method (see overleaf) no need to have different code for each puzzle instance Program area class test { puzzle1= new (); puzzle1.(); display(puzzle1); Heap puzzle2 = new (); puzzle2.(); display(puzzle2); public static void display( p) { for (int c = 0; c < 3; c++) System.out.print(p.(r,c) + ); System.out.println( ); //new line c r p rv : puzzle2 : puzzle1 : args String[]: rv.. state Stack frame for display Stack frame for main state Critique Data abstraction: yes Creation on demand: yes No need to duplicate class code No need to duplicate code in client Same display method handles all puzzles

Storage reclamation Live object: object is live at a particular time t during program execution if that object can be accessed by program after time t. Finding live objects (recursive definition) If stack contains reference to an object O, O is live. If live object O1 contains reference to object O2, O2 is itself live. Dead object: object not live Periodically, system detects dead objects and reclaims their storage. Garbage collection C,C++: difficult to determine what is a reference Storage reclamation must be done explicitly by programmer (free) Error-prone Editorial remarks Tension in class language like Ur-Java: Data abstraction: client class should not know how state is implemented state cannot be implemented as local variable of client code state must be represented as class variable. Unfortunately this makes it difficult to create state dynamically. OO-languages resolve this tension. State is encapsulated in class definition, but class definition is only a template. Instances of class can be created on demand by client without breaking abstraction. Client hold holds references to objects, but implementation of state is not visible to it. User-defined types: class names are used as types of object and as types of references Aliasing Extra material Aliasing in real life: two names for same person Example: Samuel Clemens aka Mark Twain Java aliasing: two references for same object p1 = new (); p2 = new (); p2 = p1; //p2 and p1 point to same object What happens to object pointed to by p2 before the assignment p2 = p1;? Dead object: storage gets garbage collected

Java references vs C pointers C Reference to object is a pointer (address) that must be dereferenced explicitly to access object Pointer arithmetic is allowed Java Implicit dereferencing: programmer only sees references, and cannot manipulate objects directly No arithmetic is allowed on references Java approach is safer Many program bugs arise from incorrect pointer manipulation Viruses often get foothold by exploiting buffer overflows that arise in C programs that manipulate addresses in insecure ways Class variables revisited Class variables/methods can coexist with instance variables/methods All instances share class variables class Olds { private static int numwheels = 4; private static String manufacturer = GM ; private int odometer = 0;.. Olds mycar = new Olds(); Olds hercar = new Olds(); //mycar.numwheels is same location as hercar.numwheels //Each car has its own location called odometer mycar Olds hercar Olds Stack Static numwheels manufacturer String: Heap odometer odometer Olds Olds Keyword: this In instance method, keyword this is a reference to object in which the method exists (conceptually). class { public void (char d) { //invoke display method in test test.display(this); class test { puzzle1= new (); puzzle1.(); public static void display( p) { Compiling instance methods.p1.(2,3).p2.(0,1) c r this : rv. Program area How does method know which object to manipulate? Answer: low-level SaM code for takes an additional parameter: reference to object - p1.(x,y) is converted to p1.(p1,x,y) in essence Stack frame for invocation of Heap. state state

Strings String: sequence of characters String s = new String( green ); Length of string: s.length() Characters in string: s.charat(i) returns character in position i. Positions start at 0. String comparison: s1.equals(s2) returns true is s1 and s2 are same sequence of characters s1 == s2 returns true if s1 and s2 are references to same object Arrays Arrays are objects in Java. Declaring array references [] ap; //ap is reference to array of puzzles Allocating array ap = new [20]; //create an array that can hold references to 20 objects Array assignment ap[0] = new (); Array access ap[0] Array example. [] ap; ap = new [20]; ap[0] = new (); ap. []: [] 20 : :. : state stack