Object-Oriented Programming

Similar documents
Concepts of Programming Languages

Compiler Theory. (Semantic Analysis and Run-Time Environments)

PROGRAMMING IN VISUAL BASIC WITH MICROSOFT VISUAL STUDIO Course: 10550A; Duration: 5 Days; Instructor-led

More On inheritance. What you can do in subclass regarding methods:

Programming in Visual Basic with Microsoft Visual Studio 2010

Lecture 13: Object orientation. Object oriented programming. Introduction. Object oriented programming. OO and ADT:s. Introduction

CPS 506 Comparative Programming Languages. Programming Language

Chapter 11. Categories of languages that support OOP: 1. OOP support is added to an existing language

Inheritance and Substitution (Budd chapter 8, 10)

Introduction to Programming Using Java (98-388)

Inheritance and Interfaces

CSE 341, Autumn 2015, Ruby Introduction Summary

Syllabus & Curriculum for Certificate Course in Java. CALL: , for Queries

Absolute C++ Walter Savitch

5/23/2015. Core Java Syllabus. VikRam ShaRma

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

2.4 Structuring programs

Basic Python 3 Programming (Theory & Practical)

CSE P 501 Compilers. Static Semantics Hal Perkins Winter /22/ Hal Perkins & UW CSE I-1

Contents. Figures. Tables. Examples. Foreword. Preface. 1 Basics of Java Programming 1. xix. xxi. xxiii. xxvii. xxix

Functional Programming

Programming Languages 2nd edition Tucker and Noonan"

JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB PROGRAMMING, X452.1)

CO Java SE 8: Fundamentals

A Short Summary of Javali

This course is designed for anyone who needs to learn how to write programs in Python.

Computer Components. Software{ User Programs. Operating System. Hardware

22c:111 Programming Language Concepts. Fall Syntax III

Collections. Collections Collection types Collection wrappers Composite classes revisited Collection classes Hashtables Enumerations

Preface to the Second Edition Preface to the First Edition Brief Contents Introduction to C++ p. 1 A Review of Structures p.

Data Abstraction. Hwansoo Han

Lexical and Syntactic Analysis

The Java language has a wide variety of modifiers, including the following:

And Parallelism. Parallelism in Prolog. OR Parallelism

CEU s (Continuing Education Units) 12 Hours (i.e. Mon Thurs 5 9PM or Sat Sun 8AM 5PM)

CS 231 Data Structures and Algorithms, Fall 2016

Project 6 Due 11:59:59pm Thu, Dec 10, 2015

NOTES ON OBJECT-ORIENTED MODELING AND DESIGN

Chapter 13 Object Oriented Programming. Copyright 2006 The McGraw-Hill Companies, Inc.

Lecture Notes on Programming Languages

AP Computer Science 4325

Object-oriented Compiler Construction

RAJIV GANDHI COLLEGE OF ENGINEERING AND TECHNOLOGY DEPARTMENT OF INFORMATION TECHNOLOGY OBJECT ORIENTED PROGRAMMING QUESTION BANK UNIT I 2 MARKS

Compilers. Prerequisites

Collections Questions

Object-oriented Programming. Object-oriented Programming

Chapter 11 Object and Object- Relational Databases

Chapter 6 Introduction to Defining Classes

11-1. Collections. CSE 143 Java. Java 2 Collection Interfaces. Goals for Next Several Lectures

Objects and Iterators

School of Computing and Information Sciences. Course Title: Data Structures Date: 3/30/2010 Course Number: COP 3530 Number of Credits: 3

Abstract Data Types and Encapsulation Concepts

Late-bound Pragmatical Class Methods

Inheritance and Substitution גרא וייס המחלקה למדעי המחשב אוניברסיטת בן-גוריון

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

Murach s Beginning Java with Eclipse

CSCI312 Principles of Programming Languages!

What is Inheritance?

Topics. Modularity and Object-Oriented Programming. Dijkstra s Example (1969) Stepwise Refinement. Modular program development

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

OOP Design Conclusions and Variations

Assignment 5. Introduction

Rules and syntax for inheritance. The boring stuff

Fast Track to Core Java 8 Programming for OO Developers (TT2101-J8) Day(s): 3. Course Code: GK1965. Overview

Collections. Collections. Collections - Arrays. Collections - Arrays

Programming Exercise 14: Inheritance and Polymorphism

MaanavaN.Com CS1203 OBJECT ORIENTED PROGRAMMING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Object-Oriented Languages and Object-Oriented Design. Ghezzi&Jazayeri: OO Languages 1

(800) Toll Free (804) Fax Introduction to Java and Enterprise Java using Eclipse IDE Duration: 5 days

4 CoffeeStrainer Virtues and Limitations

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

Java 8 Programming for OO Experienced Developers

Implements vs. Extends When Defining a Class

INSTITUTE OF AERONAUTICAL ENGINEERING

Java Classes. Produced by. Introduction to the Java Programming Language. Eamonn de Leastar

SPARK-PL: Introduction

Definition of DJ (Diminished Java)

Introduction to Visual Basic and Visual C++ Introduction to Java. JDK Editions. Overview. Lesson 13. Overview

Static Semantics. Winter /3/ Hal Perkins & UW CSE I-1

Part I Basic Concepts 1

Core Java - SCJP. Q2Technologies, Rajajinagar. Course content

«Computer Science» Requirements for applicants by Innopolis University

SRM ARTS AND SCIENCE COLLEGE SRM NAGAR, KATTANKULATHUR

Computer Science II (20073) Week 1: Review and Inheritance

ECE 3574: Dynamic Polymorphism using Inheritance

The class Object. Lecture CS1122 Summer 2008

WA1278 Introduction to Java Using Eclipse

Type Hierarchy. Lecture 6: OOP, autumn 2003

Beyond CSE143. What s Left To Do? Templates. Using Templates. A Template Class. A Problem with Reusing Code CSE 143

Introduction to Programming System Design CSCI 455x (4 Units)

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

Comp 311 Principles of Programming Languages Lecture 21 Semantics of OO Languages. Corky Cartwright Mathias Ricken October 20, 2010

Software Design Heuristics

AP Computer Science A Course Syllabus

Core Java SYLLABUS COVERAGE SYLLABUS IN DETAILS

CS 430 Spring Mike Lam, Professor. Data Types and Type Checking

INHERITANCE & POLYMORPHISM. INTRODUCTION IB DP Computer science Standard Level ICS3U. INTRODUCTION IB DP Computer science Standard Level ICS3U

Standard. Number of Correlations

Programming Languages 2nd edition Tucker and Noonan"

Transcription:

13 Object-Oriented Programming Exercises 13.1 Using Java as an example: 13.2 Code reuse: inhertiance, interfaces. In the case of an interface, any class implementing the Comparable interface can be sorted or stored in an ordered colection. Type safety: in downcasting using an explicit cast, Java always checks the class (type) of an object. Similarly, the instanceof operator allows one to check the run time type of an object. Abstraction: abstract classes and interfaces allow one to deal with an abstraction. Consider the Expression class in the abstract syntax of Clite; it allows one to deal with the concept of an expression without worrying about the kind of expression an object may be. Encapsulation: both classes and packages provide encapsulation mechanisms. The visiblity attributes (protected, private) allow the programmer to hide the representation of an object from the client. 13.3 When you want to say about classes: an x is an a and a b and a c, you are defining one class by of combining features from multiple classes. In C++ this act is called multiple inheritance; it carries some sticky baggage because each class can have an implementation of the same method. In Java, you can perform the same act, but since x can only extend one of the three classes a, b, or c, only one implementation can be provided. The other two must be interface classes, which are not allowed to provide implementations of methods. So the C++ problems in combining multiple 57

58 13. OBJECT-ORIENTED PROGRAMMING classes using multiple inheritance do not occur in Java using multiple interfaces. 13.4 (a) One obvious problem is that arrays are pseudo objects and distinct from other Collection classes such as ArrayList. Hashtable extends Dictionary, but no other class does; a Vector and a Hashtable are both automatically synchronized giving a performance penalty. A Stack extends a Vector and so as all of a Vectors methods, as well as being synchronized. (b) C++ comes with the Standard Temple Library, which contains collections comparable to those of Java. Until Java 5, the major difference was that the C++ collections supported generics or templates and Java did not. This distinction disappeared with the advent of Java 5. The other major difference is that one can ignore templates in Java since the Java class hierarchy is a tree rooted in the Object class. THe C++ class hierarchy is not a tree. Hence, one cannot ignore templates when declaring a collection. (c) Although there are many excellent collections for Ada (see, for example, the Public Ada Library), no standard library of collections is provided with Ada. 13.5 Ada 95, as an extension of Ada 83, is not a true object-oriented language; you can write entirely procedural code using the Ada 83 subset. A class in Ada corresponds to a package containing a main type; Ada is far less object-oriented than Java. 13.6 13.7 13.8 Similarly, Perl is a procedural language to which classes were added. Most of Perl, including the builtin libraries, constitute an imperative scripting language. Objects can be ignored. Ruby is truly an object-oriented language. Like Smalltalk, everything in Ruby is an object, including control structures. 13.9 See the Clite sources available at the Instructor web site. 13.10 Students should start with the complete Clite sources available at the Instructor web site. Also, see the file AbstractSyntax.java in the ch12code directory. This file contains the typeof, V, and M functions for Expressions, which can serve as a model for reimplementing the rest of Clite in a more object-oriented style.

59 13.11 The Clite test program is provided in the software distribution as the text file ootest.cpp in the ch13code directory. It computes the real roots of a quadratic equation using Newton s method. Compare your results with those of the Clite interpreter. 13.12 Unary addition can be discarded by the parser. Unary minus can be converted to a binary subtraction from zero. The not operator and cast conversions are much more difficult to deal with. There seems to be no real advantage into converting these to binary operators. 13.13 See the files Concordance.java, MakeConcordance.java, Report.java, Document.java in the ch13code directory. The requested modification is trivial; just report the number of entries in the list for each word as part of the report. 13.14 This is a simple test of Concordance.java. 13.15 These are simple experiments with Concordance.java. 13.16 See file Knight.java in the ch13code directory; students also need the file Queens.java in the student source code directory for this chapter. 13.17 See [Wirth, 1976, p. 146] for the required attempt method: public void attempt(int level) { Enumeration e = b.moves(level); while (e.hasmoreelements()) { Object move = e.nextelement( ); if (b.valid(level, move)) { b.record(leve, move); if (b.done(level)) b.display( ); else attempt(level+1); b.undo(level, move); } // if valid } // while } // attempt Additionally, a display method was added to the Backtracker interface. Also note that this version of attempt does not return a boolean. A simple exercise is to convert the enumeration to an iterator. Another is to use generics and a for each loop. 13.18 Using classes that define the abstract syntax of Clite: (a) Expression is a client of Conditional. (b) Value is a subclass of Expression.

60 13. OBJECT-ORIENTED PROGRAMMING 13.19 See distributed software for the Clite interpreter. 13.20 The addition of generics complicates the syntax and semantics of the language, but simplifies writing programs in the language. This tradeoff of power vs. convenience is an ongoing one in the design and implementation of programming languages. For example, to write a loop only a while is needed, but most languages also provide a for and a do while. Templates in Java simplify programming by eliminating the need for most downcasts (since collections can store any form of object) and allow the use of the for all loop introduced in Java 5. Both of these are conveniences but logically unnecessary. 13.21 This is a fairly large undertaking. Essentially, each class in the abstract syntax would need to have both a V (validity) method and an M (meaning) method. The class Expression and its subclasses must also implement a typeof method. The code is simpler because you no longer need a dispatching function for abstract syntax classes and, for example, the meaning functions do not have to be passed as an argument the abstract syntax they are dealing with. By limiting the scope of the abstract syntax actually used, this can be made into a non-team Project. (a) Consider the Assignment class, for example: class Assignment extends Statement {... public void V(TypeMap tm) {... } public State M(State sigma) {... } } Of course, the meaning function of an Expression and its subclasses would return a Value. (b) Note that the abstract syntax being evaluated is not being passed as an argument. (c) A subset of Clite including only assignments, values, variables, and binary expressions can be done by an individual. 13.22 This exercise should be ignored (it properly belongs in Chapter 18 on Correctness). 13.23 This exercise should be ignored (it properly belongs in Chapter 18 on Correctness). 13.24 See the file Polynomial.py in the ch13code directory. 13.25 See the file Polynomial.py in the ch13code directory.

61 13.26 See the file Fraction.py in the ch13code directory. 13.27 a. A Set in theory is equivalent to a Map, since a map k v can always be converted to a tuple < k, v > and vice veras. In Java, a HashSet lacks mnay of the methods of a HashMap, so the latter is preferred for this application. b. hash multimap, hash map, hash multiset, hash set. c. None.

62 13. OBJECT-ORIENTED PROGRAMMING