160 likes | 295 Views
Programming Languages: Design, Specification, and Implementation. G22.2210-001 Rob Strom October 26, 2006. Administrative. Alternative mailing address for me: robstrom@us.ibm.com Everyone should subscribe to the class mailing list: http://www.cs.nyu.edu/mailman/listinfo/g22_2110_001_fa06
E N D
Programming Languages:Design, Specification, and Implementation G22.2210-001 Rob Strom October 26, 2006
Administrative • Alternative mailing address for me: robstrom@us.ibm.com • Everyone should subscribe to the class mailing list: http://www.cs.nyu.edu/mailman/listinfo/g22_2110_001_fa06 • Reading: • Ada, C++ : generics • Java: http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf • Simple Homework (due next class) • Polymorphism Project (due 2 weeks after simple homework) BUT START NOW!!!! • Come to office hours for help!
Academic integrity Violation of any of the following rules constitutes cheating: • Students are to do all assignments individually, with no collaboration or sharing of work, unless the instructor explicitly permits collaboration. If one student shows or gives his/her work to another, then both students are considered to be cheating. Students may not use work provided by any person outside the class. Furthermore, students may not solicit other people to do assignments (in whole or in part) for them. • When an instructor permits collaboration on an assignment, then collaboration is permitted only to the degree and in the respects that he/she specifies. Each assignment that is done collaboratively must state that it was done collaboratively and must list the collaborators. • External sources, such as published materials or material on the Web, may be used in assignments only to the extent permitted by the instructor. If such a source is used, the assignment must include an attribution to the source. Ideas, algorithms, text, code, and experimental results all require proper attribution. • A student may not submit the same assignment to two different classes, whether in the same semester or in different semesters, without the explicit permission of both instructors. • During an exam, students must not communicate in any way, nor use any materials or technology not explicitly permitted by the instructor, other than pens and pencils. One student may not look at another student's test. • A student may not attempt to gain possession of or look at an exam before the start of the exam. • Students providing unauthorized information and those using it are cheating and are ALL subject to disciplinary action.Disciplinary actions can vary in severity and can result in probation or termination from the graduate program. The GSAS Policies and Procedures Manual, under "Topic 3: Discipline" includes a complete outline of sanctions and procedures.Departmental policy is to give a grade of F in the course in which the cheating occurred.By the rules of GSAS, the penalty for a second offense is termination from the graduate program.
Small homework (next week) • In C++, Java, and Ada, first define a simple type Person, either as a record or as a class, containing Name, Address, and PhoneNumber. Then implement a “container” class that uses doubly linked lists to create a list of Person, supporting operations: • Insert: add a person to the list at position (n) • Position of: return position of the person on the list (starting at 0; exception if not on the list) • Get nth: return a copy of the nth item (exception if no nth item) • Remove nth: remove the nth item (exception if no nth item) • (In later homework, we will use generics to allow a single package to work with any type, not just Person). • This homework verifies that you are familiar with defining new abstract classes that hide their implementation.
Polymorphic Programming Project: (2 weeks) • This project builds a system exploiting polymorphism. Choose one of the 3 languages C++, Ada, or Java to do it. • This system models a world composed of the following kinds of objects: • rooms: they have doors to neighboring rooms, and access to factories • factories (they create objects) • solids (coins, keys, bags, pots), some of which are containers (bags, pots, jars) • liquids (water, juice): must be in a container, can only be transferred with a funnel • doors (they transfer solid objects from one room to a neighbor) • funnels (they transfer liquids from one container to another) • Objects have attributes (weight, mass), and different kinds enforce different restrictions. • You manipulate things by repeatedly choosing a room, and giving it a “script” that can do any of these things in that room: • Ask factories in the room for things by name; e.g., you can ask for 10 keys; different factories might return different weight keys, or might return them in a bag inside a package, etc. • You can ask containers what’s inside them. • You can remove solid things from inside containers you have in the room with you. You can insert solid things from the room into containers. A container will complain if its capacity is exceeded, or if you are trying to mix solids and liquids in the same container. • You can transfer x amount of liquid from one container to another, by invoking a method on a funnel It will complain if there is too little liquid in the source container or if the receiving container doesn’t accept that kind of liquid. Usually containers will not accept liquid if they are also holding solids. • You can “ship” a container (and by implication, everything in it) from a room to a neighbor by invoking a method on one of the room’s doors. • Use polymorphic programming to: • Configure a network of at least 5 rooms with doors between them, and different kinds of factories in each • Design different kinds of solid objects, containers, and liquids, with different properties (at least 3 of each). Example: pot1 can hold solid up to 5 pounds or liquid up to 10 ounces, but not both solid and liquid. • Write a variety of scripts to obtain things from factories, extract their contents from containers, repackage them and ship them to neighboring rooms, and then go to the neighboring rooms and unpackage them again.
Programming Languages Core Exam • Syntactic issues: regular expressions, context-free grammars (CFG), BNF. • Imperative languages: program organization, control structures, exceptions • Types in imperative languages: strong typing, type equivalence, unions and discriminated types in C and Ada. • Block structure, visibility and scoping issues, parameter passing. • Systems programming and weak typing: exposing machine characteristics, type coercion, pointers & arrays in C. • Run-time organization of block-structured languages: static scoping, activation records, dynamic and static chains, displays. • Programming in the large: abstract data types, modules, packages and namespaces in Ada, Java, and C++. • Functional programming: list structures, higher order functions, lambda expressions, garbage collection, metainterpreters in Lisp and Scheme. Type inference and ML. • Object-Oriented programming: classes, inheritance, polymorphism, dynamic dispatching.Constructors, destructors and multiple inheritance in C++, interfaces in Java. • Generic programming: parametrized units and classes in C++, Ada and Java. • Concurrent programming: threads and tasks, communication, race conditions and deadlocks, protected methods and types in Ada and Java.
Object-Oriented Concepts Object-based • Encapsulation (Information Hiding) • User of an “object” need only know the interface to the object: i.e. the operations supported on it, not the implementation • The implementation can be specified separately • Polymorphism • Different instances of an object could have different implementations, so long as they all obeyed the same interface • Inheritance • Implementations can extend other implementations, specifying only what’s new or changed
Inheritance • Interface inheritance: • A subclass has all of the methods of its parent class, and possibly more • Implementation inheritance: • A subclass has all of the data components of the parent, and possibly more • A subclass may have different implementations of some of the methods of the parent class
Inheritance in Ada Private types that can be subclassed (inherited from) are called “tagged”. They are extensible records type person is tagged … type president is new person with … Derived types can either inherit operations or can over-ride (redefine) them BUT (unlike C++ virtual, Java) normally the decision about which operation to invoke is made statically, UNLESS you explicitly ask Ada to do a run-time-based dispatch.
Run-time Dispatch in Ada • A variable using dynamic dispatch is of type FOO’CLASS or access FOO’CLASS. • This means it will use the run-time type (implemented in the tag) to determine which type to use when dispatching a call. • This is only usable for methods (so-called “primitive” operations) for types. • No primitive operations can have operands of mixed types. • Dynamic dispatch variables provide true polymorphism • Correspond to: virtual member functions in C++, and to methods in Java (which are virtual by default)
Yet another parameter passing style (Ada’s three modes) • in (default): the value must exist before the call, and the formal parameter will see the same value. The formal parameter is constant, and may not be changed (same as call by value) • in out: the value must exist before the call, and the formal will see the same value. The formal may be changed; any changes to the value will be reflected back to the caller (call by “value/result”) • out: the value at the time of the call is ignored; it is undefined within the called procedure. The formal must be assigned a value, which will be reflected back to the caller. • Strangely, the manual defines certain types as reference and others as by value types. However, provided aliasing is forbidden, and constancy of in parameters is enforced, the implementation can pass by copy, reference, or value-result.
Limited – Ada • It means that there is no copying or assignment permitted • A private type may be limited, while its record representation is not • Example uses: • File descriptor – you can get a new one from the file system with OPEN, but you shouldn’t be able to just copy it around
Initialization and finalization - Ada • Ada does not have constructors. • (But of course you can call a factory method that will return either an initialized abstract object or a reference to one) • You can also build a child derived from CONTROLLED or LIMITED_CONTROLLED • It will invoke INITIALIZE each time an object is created; FINALIZE when it is destroyed; ADJUST when it is assigned • Important for: • Initialization needs to make external calls (e.g. to acquire resources like files) • Finalization needs to do more than just “throw away” the state of the object (e.g. to release resources like files) • Copying needs to create resources other than an exact copy of the original – e.g. “deep copies” that lack aliasing.
Object-orientation in C++ • Objects can be created on stack or on heap (like Ada, unlike Java). But those allocated on stack use implicit constructors. Those on heap can use explicit constructor: new Mytype(parm), which can have parameters • Heap objects must be explicitly freed with delete (unchecked: that or a copy of the reference still exists). Arrays must be freed with special array syntax. • You can separately control: the storage allocator/deallocator, the constructor/destructors • Polymorphic (dynamic) dispatch depends upon whether the member function is virtual or not (as opposed to Ada, where it depends upon what type of object was used to perform the dispatch, or Java, where it is always dynamic) • Classes are like structs, but with member functions defined within the class. Like Java, but unlike Ada, which defines them within the package. • Everything within the Ada package is a friend; C++ explicitly defines who is a friend; Java defines what has package visibility as opposed to public, private, or protected. Only tagged Ada private types support inheritance. • C++ allows multiple inheritance (Ada and Java don’t.) • Issues: overlapping operations on objects, ambiguous up-casting • All polymorphism is based upon class hierarchies (as with Ada).
Object-Orientation in Java • Objects can be created only on the heap. There can be multiple constructor forms per class. • Freeing is only done with garbage collection; destructors (called “finalize” methods are called when objects are freed. • Objects can share information in multiple new ways: • Class variables are shared by each object of the class • Instances of non-static nested classes share the instance variables of the nesting classes • All methods are virtual – that is, the code that is executed is based on the run-time class of the object on which the method is invoked, not on the compile-time type of the reference • Instance variables are: private (accessible only from within the class – but possibly from other instances of the class!!!), protected (accessible only from within the class or a subclass), package (accessible from anywhere in the package containing the class), or public (accessible from anywhere an instance is accessible) • A reference can have interface type rather than class type. Each class can explicitly support multiple interfaces totally independently of its implementation. Calls via interfaces exhibit unconstrained polymorphism.
Exceptions: • Builtin, e.g.: • Running out of memory • Division by zero • User-defined, e.g.: • NoSuchEntry • BufferFull • Raising an exception causes a block or procedure to abnormally terminate, and to signal the particular exception (possibly with a message) • A containing block or caller in the dynamic chain will catch the exception and either take alternative action or reraise it • Differences: Ada-C++ exceptions not on interfaces, Java puts them on interfaces, except RuntimeExceptions; Ada exceptions are not objects. Java try-finally block to allow contexts to do context-specific finalization • Advantages: • If a function is supposed to return something, or a procedure is supposed to initialize or modify something, and it doesn’t do it, it should not be able to return to a context where it is assumed it has done its job. • Simply returning a return code is not enough, because programmers will forget to check it.