Pascal: operations on basic data structures (selection) (I) 2.3 Accessing and manipulating data

Similar documents
2. The object-oriented paradigm!

Haskell 98 in short! CPSC 449 Principles of Programming Languages

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

2.4 Structuring programs

2. The object-oriented paradigm

Short Notes of CS201

User-Defined Algebraic Data Types

CS201 - Introduction to Programming Glossary By

Introduction to Programming Using Java (98-388)

A First Look at ML. Chapter Five Modern Programming Languages, 2nd ed. 1

Data Types. Every program uses data, either explicitly or implicitly to arrive at a result.

CS558 Programming Languages

Pierce Ch. 3, 8, 11, 15. Type Systems

Final exam. Final exam will be 12 problems, drop any 2. Cumulative up to and including week 14 (emphasis on weeks 9-14: classes & pointers)

Java. Representing Data. Representing data. Primitive data types

CSE 431S Type Checking. Washington University Spring 2013

CSCI-GA Scripting Languages

SML A F unctional Functional Language Language Lecture 19

520 Principles of Programming Languages. Arithmetic. Variable Declarations. 19: Pascal

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 17: Types and Type-Checking 25 Feb 08

Final-Term Papers Solved MCQS with Reference

Classes. Classes. Classes. Class Circle with methods. Class Circle with fields. Classes and Objects in Java. Introduce to classes and objects in Java.

Types. What is a type?

S.No Question Blooms Level Course Outcome UNIT I. Programming Language Syntax and semantics

OOP: Key Concepts 09/28/2001 1

TYPES, VALUES AND DECLARATIONS

CMSC 330: Organization of Programming Languages

The results for a few specific cases below are indicated. allequal ([1,1,1,1]) should return true allequal ([1,1,2,1]) should return false

By: Abhishek Khare (SVIM - INDORE M.P)

Introduction to JavaScript p. 1 JavaScript Myths p. 2 Versions of JavaScript p. 2 Client-Side JavaScript p. 3 JavaScript in Other Contexts p.

Test 1 Summer 2014 Multiple Choice. Write your answer to the LEFT of each problem. 5 points each 1. Preprocessor macros are associated with: A. C B.

22c:111 Programming Language Concepts. Fall Types I

FP Foundations, Scheme

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

CS 320: Concepts of Programming Languages

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

Lecture Overview. [Scott, chapter 7] [Sebesta, chapter 6]

CSE 307: Principles of Programming Languages

Programming Languages Fall 2013

2. Introducing Classes

Data Types (cont.) Subset. subtype in Ada. Powerset. set of in Pascal. implementations. CSE 3302 Programming Languages 10/1/2007

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING CS6456 OBJECT ORIENTED PROGRAMMING

STRUCTURING OF PROGRAM

IFAD. VDMTools Validated. Design through Modelling. Overview of VDM -SL/++ IFAD. IFAD A/S Forskerparken 10 DK-5230 Odense M Denmark.

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

Types II. Hwansoo Han

Haskell Overview III (3A) Young Won Lim 10/4/16

Chapter 2: Using Data

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

F1 A Java program. Ch 1 in PPIJ. Introduction to the course. The computer and its workings The algorithm concept

Instantiation of Template class

Constructors and Destructors. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

CSc 520. Principles of Programming Languages 45: OO Languages Introduction

CS321 Languages and Compiler Design I Winter 2012 Lecture 13

The PCAT Programming Language Reference Manual

Object-Oriented Languages. CSc 453. Compilers and Systems Software. 23 : OO Languages. Department of Computer Science University of Arizona

Fundamentals of Programming Languages. Data Types Lecture 07 sl. dr. ing. Ciprian-Bogdan Chirila

Translator Design Lecture 22 CMSC 4173 factor Semantics (rules 63-70)

Informatica 3 Syntax and Semantics

G Programming Languages - Fall 2012

Some instance messages and methods

CMSC 330: Organization of Programming Languages. OCaml Data Types

VARIABLES AND TYPES CITS1001

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

Compiling Techniques

CS 4240: Compilers and Interpreters Project Phase 1: Scanner and Parser Due Date: October 4 th 2015 (11:59 pm) (via T-square)

OBJECT ORIENTED PROGRAMMING. Ms. Ajeta Nandal C.R.Polytechnic,Rohtak

CSC Java Programming, Fall Java Data Types and Control Constructs

CS558 Programming Languages

Object Oriented Modeling

Types. Type checking. Why Do We Need Type Systems? Types and Operations. What is a type? Consensus

CSCI312 Principles of Programming Languages!

Java Identifiers, Data Types & Variables

Keyword this. Can be used by any object to refer to itself in any class method Typically used to

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

Character Stream : It provides a convenient means for handling input and output of characters.

Topic 9: Type Checking

Topic 9: Type Checking

Programming Languages

Haskell through HUGS THE BASICS

CS 360: Programming Languages Lecture 12: More Haskell

CA341 - Comparative Programming Languages

The compilation process is driven by the syntactic structure of the program as discovered by the parser

VALLIAMMAI ENGINEERING COLLEGE

I BSc(IT) [ Batch] Semester II Core: Object Oriented Programming With C plus plus - 212A Multiple Choice Questions.

Object Oriented Programming. Solved MCQs - Part 2

Lecture 7: Type Systems and Symbol Tables. CS 540 George Mason University

G Programming Languages - Fall 2012

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

Basic Types & User Defined Types

CS162: Introduction to Computer Science II

UNIT II Structuring the Data, Computations and Program. Kainjan Sanghavi

CSc 453. OO Languages. Object-Oriented Languages... Object-Oriented Languages. Compiling OO Languages. Compilers and Systems Software

Intro to OOP Visibility/protection levels and constructors Friend, convert constructor, destructor Operator overloading a<=b a.

Type-indexed functions in Generic Haskell

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

Chapter 6 Introduction to Defining Classes

Transcription:

2.3 Accessing and manipulating data structures (selection) (I) Data without being able to generate, access and change it is useless Usually, for elemental data types imperative and object-oriented languages provide the necessary operations For complex data types, we have some operations that come with the way the data type is defined and usually the programmer will add additional operations, using already defined operations and the control structures In order to manipulate an element of a data structure, we need to generate it and keep a handle on it, which is achieved by defining a variable of the type and assign to the variable the element This is achieved by := var i: integer; a: char; i:= 5; a := 'a'; structures (selection) (II) The element assigned to a variable can be the result of evaluating a whole expression using other variables and operations on data types var i,j: integer; a: char; r:real; i:= 5*j; a := succ('a'); r:= i*j structures (selection) (III) Some build-in operations on integer: ord, abs, aqr, +, -, *, = Some build-in operations on real: trunc, abs, sin, ln, +, / Some build-in operations on boolean: not, and, or Some build-in operations on char: succ, prev There are many operations having arguments of one type and producing elements of another Java: operations on primitive data types (selection) (I) Java: operations on primitive data types (selection) (II) Java also uses the variable concept, although variables often refer to objects and parts of objects Setting a variable to an element is achieved by using the = operator There are alternatives to = that set the variable to a value based on its previous value, for example += double d; int age = 21; d = age; The element assigned to a variable can, again, be the result of evaluating a whole expression using other variables and operations on data types int i,j; char a = 'a'; float r; i = 5*j; a += 2; r = i*j; 1

Java: operations on primitive data types (selection) (III) Pascal: operations on complex data structures Some build-in operations on byte, short, int and long: ++, --, -, * Some build-in operations on float and double: /, -, * Some build-in operations on boolean: &, ^, &&,! Some build-in operations on char: <=, == Again, there are many operations having arguments of one or several types and producing elements of other types The general concepts from basic data structures regarding variables, :=, and expressions apply also to complex data types Each way of constructing a certain complex structure comes with some operations associated to it Functions and procedures can be used to perform additional data manipulations Operations on user-defined simple data types (selection) "Code types": succ, pred Subtypes of simple data types: l operations of the type from which the subtype is selected l Note: this might result is problems if the result of an operation on elements of the subtype leads out of the subtype Sets Generating sets: [], [a1..a2], [a1,a2,a3,a4] (empty set, interval, set) Operations on sets: +, -, * (union, difference, intersection) Conditions involving sets: <=, >=, =, <>, in (subset, superset, equality, inequality, membership) Arrays A variable of type array consists of so-called field variables that allow to access the fields of the array: var a: array[1..4] of integer defines variables a[1], a[2], a[3] and a[4] of type integer Access and manipulation of arrays can only be achieved by accessing and manipulating the field variables For strings we can compare them for equality Files (I) When defining a variable of a file type, automatically a so-called buffer variable (type is component type) is defined var f: file of integer defines f of type integer The buffer variable is used to access and navigate the file: l rewrite(f) opens the file associated with f for writing) l f := 5 prepares 5 to be written into f l put(f) writes 5 into f (write(f,5) achieves the same) 2

Files (II) l reset(f) opens the file associated with f (for reading) l get(f) assigns to f the actual component of f (which is the next component in the file) l read(f,i) assigns the actual component of f to the variable i A buffer variable is very similar to a pointer variable The end of a file is marked with eof and the condition eof(f) is true, if the end of f is reached Records (I) When defining a variable of a record type, component variables for the components are automatically defined (with the appropriate types): type date = record day: 1..31; month: 1..12; year: 1000..3000 end; var today:date; automatically defines today.day, today.month and today.year Records (II) Access and manipulation of variables of type record can only be achieved by accessing and manipulating the component variables (even when using with) Pointers A variable of a pointer type has to be initialized: var point: integer; new(point); assigns to point a certain address in memory that is intended to store an integer value By assigning to a pointer variable an integer value this value is stored at the address By assigning to a pointer variable another pointer variable, the two variables point to the same address Conditions on pointer variables are =, <> and nil for an undefined value at the address dispose(point) allows to destroy pointer variables Functions and procedures (I) Functions and procedures (II) Functions produce values of a type and can be defined by the programmer using parameter declarations Parameters of functions (and procedures) can be given to it l by value (any expression that occurs in the function call at the parameter position is evaluated and the result is assigned to the parameter) l by reference (a pointer to the result of the expression is assigned to the parameter; if the expression is a variable, then sideeffects are possible) Functions can be used in larger expressions to produce values that can be assigned to variables Procedures do not produce a value of a data structure, instead they can be seen as subprograms and values that the main program wants to get from procedures are transferred by assigning them to parameters (of the right type) that are defined as callby-reference Calling a procedure is just a command It is possible to define functions and procedures within functions and procedures, which brings up the problem of the scope of names (see 2.4) 3

Functions and procedures (III) Example: function call by value variable function rfakul (n: integer) : integer; result type begin if n = 0 then rfakul := 1 set result value else rfakul := rfakul(n-1)*n end recursive call of function rfakul(5); call of function in main program Functions and procedures (IV) Example: procedure call by reference variables procedure mydiv (div1, div2: integer; var fact, res: integer); var help: integer; begin local variable help:= div1; fact:= 0; while help > div2 do begin fact:= fact+1; help:= help-div2; end res:= help; end call of procedure in main program; i,j integer variables mydiv(20,3,i,j); Java: operations on complex data structures The general concepts from basic data structures regarding variables, =, and expressions apply also to complex data types But: Java variables only reference objects, they do not contain them! F treat them as pointer variables! Each way of constructing a certain complex structure comes with some operations (methods) associated to it Additionally, there are user-defined methods Arrays Similar to Pascal, we refer to an element in a array by adding the index to the variable representing the array: Button[] buttons = new Button[10]; produces buttons[0], buttons[1],,buttons[9] An array is created and assigned to a variable by new int[] abc; abc = new int[500] buttons.length returns the array's length Classes (I) Classes (II) For a class, Java automatically creates several methods that allow to create objects of the class and to access components (fields) of objects l new is used to create an object of the argument class (using the class constructor): Circle mycircle = new Circle(3,4,7.5); l Components are accessed by using the object name followed by a dot and the field name double myradius; myradius = mycircle.r; mycircle.x = 3.5; l There is no way to tell Java that an object should be deleted that guarantees that this is done immediately. BUT: a programmer can write a finalizer method that is invoked when the system (i.e. the garbage collector) removes an object. Another BUT: the finalizer might never be executed. 4

Classes (III) Usually, additional operations involving an object (and possibly other objects) are needed public class Circle { public double x,y; public double r; public double circumference() {return 2*3.1416*r; public double area() { return 3.1416*r*r; The user-defined methods are accessed by using the object name followed by a dot and the method name mycircle.circumference Classes (IV) Due to inhertitance, objects of a class can have more methods than the ones explicitely defined when defining the class. This can make it necessary to override methods! Methods can also be overloaded, i.e. we use the same name for several methods that have different argument types (within a class). Often the constructor method for a class is overloaded: Classes (V) public class Circle { public double x, y, r; public Circle(double x, double y, double r){ this.x = x; this.y = y; this.r = r; this refers to the object itself. Classes (VI) Methods for interfaces and abstract classes: l an abstract class can have nonabstract methods, i.e. methods that are fully defined and implemented (obviously, these methods do not need to know how the abstract parts will be implemented later, resp. they just call them) l An interface cannot define any nonabstract methods Operations on data in imperative programming Operations on data in object-oriented programming The definition of data types and the definition of the operations on them are usually separated. Usually we cannot define parameterized data types and consequently we cannot overload operations (except for build-in data structures) and overriding operations cannot be reflected in a structure on data structures (only via the program structure) Making use of information where an element is stored is possible and often such information is used to interpret data in different ways (C is a bad example for this) Data structure and the operations on the defined data are defined together in classes. What operations on a certain piece of data are available is also determined by the class hierarchy Overloading and overriding of operations is possible and rather convenient, since operations doing the same thing (only on different data structures) can be named the same (this is also called polymorphism due to dynamic binding) 5