Programming Language Concepts: Lecture 1

Similar documents
Programming Language Concepts: Lecture 2

Programming Language Concepts: Lecture 2

CS 11 java track: lecture 1

Getting started with Java

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University

Classes, subclasses, subtyping

Goals. Java - An Introduction. Java is Compiled and Interpreted. Architecture Neutral & Portable. Compiled Languages. Introduction to Java

Getting Started with Java. Atul Prakash

INDEX. A SIMPLE JAVA PROGRAM Class Declaration The Main Line. The Line Contains Three Keywords The Output Line

Classes and Objects 3/28/2017. How can multiple methods within a Java class read and write the same variable?

CS111: PROGRAMMING LANGUAGE II. Lecture 1: Introduction to classes

Compiling Techniques

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

Lecture 1: Overview of Java

An overview of Java, Data types and variables

CS 231 Data Structures and Algorithms, Fall 2016

Branching and Boolean Expressions

Branching and Boolean Expressions

JAVA: A Primer. By: Amrita Rajagopal

Functional Programming in Haskell Part I : Basics

Java Bytecode (binary file)

1. Download the JDK 6, from

Introduction Basic elements of Java

Lecture 02, Fall 2018 Friday September 7

Lesson 04: Our First Java Program (W01D4

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

Kickstart Intro to Java Part I

Crash Course Review Only. Please use online Jasmit Singh 2

Data Structures G5029

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

CS 177 Recitation. Week 1 Intro to Java

Data Abstraction and Specification of ADTs

Introduction to Programming Using Java (98-388)

CS110D: PROGRAMMING LANGUAGE I

Lecture 1: Object Oriented Programming. Muhammad Hafeez Javed

Parsing Scheme (+ (* 2 3) 1) * 1

INFO Object-Oriented Programming

Object-Oriented Programming (OOP) Basics. CSCI 161 Introduction to Programming I

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

CS-140 Fall Binghamton University. Methods. Sect. 3.3, 8.2. There s a method in my madness.

Lecture 22: Java. Overall Structure. Classes & Objects. Every statement must end with ';' Carl Kingsford, , Fall 2015

BASICS.

CMSC131. Introduction to your Introduction to Java. Why Java?

1.1 Your First Program

Computer Science 1 Ah

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

1.1 Your First Program

Programming Languages and Techniques (CIS120)

Atelier Java - J1. Marwan Burelle. EPITA Première Année Cycle Ingénieur.

COE318 Lecture Notes Week 3 (Week of Sept 17, 2012)

The Java programming environment. The Java programming environment. Java: A tiny intro. Java features

LECTURE 2 (Gaya College of Engineering)

G52PGP. Lecture oo3 Java (A real object oriented language)

Programming Language Concepts: Lecture 11

C02: Overview of Software Development and Java

Space Exploration EECS /25

Methods and Data (Savitch, Chapter 5)

Lecture 2, September 4

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

Introduction to Java

Programming Language Concepts: Lecture 10

Lecture 1 - Introduction (Class Notes)

! Widely available. ! Widely used. ! Variety of automatic checks for mistakes in programs. ! Embraces full set of modern abstractions. Caveat.

CS-140 Fall Binghamton University. Methods. Sect. 3.3, 8.2. There s a method in my madness.

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

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013

Chapter 11. Abstract Data Types and Encapsulation Concepts

Administration. Classes. Objects Part II. Agenda. Review: Object References. Object Aliases. CS 99 Summer 2000 Michael Clarkson Lecture 7

COMP 2355 Introduction to Systems Programming

CS 61C: Great Ideas in Computer Architecture Introduction to C

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. Stacks and Queues. Lecture 11.

Index. Course Outline. Grading Policy. Lab Time Distribution. Important Instructions

CS 101 : Introduction to Programming. Lecture 1 22/January/2018 Ashish Sureka

Programming Language Concepts: Lecture 9

Simple Java Programs. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Lec 3. Compilers, Debugging, Hello World, and Variables

CSCE 314 Programming Languages. Type System

CS-140 Fall 2017 Test 1 Version Practice Practie for Sept. 27, Name:

Introduction to Java

A PROGRAM IS A SEQUENCE of instructions that a computer can execute to

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

Program Fundamentals

Associate Professor Dr. Raed Ibraheem Hamed

CMSC 4023 Chapter 11

Classes. Classes as Code Libraries. Classes as Data Structures. Classes/Objects/Interfaces (Savitch, Various Chapters)

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

Abstract Data Types. Stack. January 26, 2018 Cinda Heeren / Geoffrey Tien 1

Inf1-OOP. Textbooks. Who and What. Organizational Issues. Why Java? Course Overview. Hello, World! in Java. Ewan Klein, Perdita Stevens

Fundamentals of Programming. Lecture 19 Hamed Rasifard

Interfaces & Generics

CompSci 125 Lecture 02

Lecture #6-7 Methods

Crash Course in Java. Why Java? Java notes for C++ programmers. Network Programming in Java is very different than in C/C++

Certified Core Java Developer VS-1036

Inf1-OOP. Textbooks. Who and What. Organisational issues. Why Java? Course Overview. Hello, World! in Java

CmSc 150 Fundamentals of Computing I. Lesson 28: Introduction to Classes and Objects in Java. 1. Classes and Objects

1.1 Your First Program

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

Transcription:

Programming Language Concepts: Lecture 1 Madhavan Mukund Chennai Mathematical Institute madhavan@cmi.ac.in http://www.cmi.ac.in/~madhavan/courses/pl2009 PLC 2009, Lecture 1, 12 January 2009

Data and datatypes Programs manipulate data Basic built in data types Int, Float, Char,... Built in collective datatypes Arrays, lists,... Choice depends on underlying architecture Random access arrays for traditional von Neumann machines Lists for functional programming Many useful data structures Stacks, queues, trees,... Programming language cannot anticipate all requirements

User defined datatypes Stack in C int s[100]; int tos = 0; /* points to top of stack */ Should not be able to access s[5] if tos == 7 Abstract datatype Data organization in terms of how the data in the data structure can be manipulated Implementation should not allow user to circumvent this

User defined datatypes Stack in C int s[100]; int tos = 0; /* points to top of stack */ Should not be able to access s[5] if tos == 7 Abstract datatype Data organization in terms of how the data in the data structure can be manipulated Implementation should not allow user to circumvent this Can we enforce this rather than depend on programmer discipline?

Class Classes, [Simula, 1967] The word class is not very significant

Class Classes, [Simula, 1967] The word class is not very significant Class definition has two parts How the data is stored in this type. What functions are available to manipulate this data.

Stack as a class class stack { int values[100]; /* values stored in an array */ int tos = 0; /* top of stack, initially 0 */ push (int i,...){ /* push i onto stack */ values[tos] = i; tos = tos+1; /* Should check tos < 100!! */ int pop (...){ /* pop and return top of stack */ tos = tos - 1; /* Should check tos > 0!! */ return values[tos]; bool is_empty (...){ /* is the stack empty? */ return (tos == 0); /* yes iff tos is 0 */

Classes Traditionally, we pass data to functions push(s,i) /* stack s, data i */

Classes Traditionally, we pass data to functions push(s,i) /* stack s, data i */ Instead, instantiate classes as objects, each with a private copy of functions stack s,t; /* References to stack */ s = new stack; /* Create one stack... */ t = new stack; /*... and another */ s.push(7);

Classes Traditionally, we pass data to functions push(s,i) /* stack s, data i */ Instead, instantiate classes as objects, each with a private copy of functions stack s,t; /* References to stack */ s = new stack; /* Create one stack... */ t = new stack; /*... and another */ s.push(7); This creates only one object with two names s = new stack; /* Create one stack... */ t = s; /*... assign another name */

Classes... In our class definition, the data to be passed to a function is implicit Each function is implicitly attached to an object, and works on that object i = s.pop(); if (t.is_empty()) {...

No... in arguments to functions class stack { int values[100]; /* values stored in an array */ int tos = 0; /* top of stack, initially 0 */ push(int i){ /* push i onto stack */ values[tos] = i; tos = tos+1; /* Should check tos < 100!! */ int pop(){ /* pop and return top of stack */ tos = tos - 1; /* Should check tos > 0!! */ return values[tos]; bool is_empty(){ /* is the stack empty? */ return (tos == 0); /* yes iff tos is 0 */

Classes and objects An object is an instance of a class Traditionally, functions are more fundamental than data Here, functionality is implicitly tied to data representation

Classes and objects An object is an instance of a class Traditionally, functions are more fundamental than data Here, functionality is implicitly tied to data representation OO terminology Internal variables instance variables, fields Functions methods

Public vs private Implementation details should be private

Public vs private Implementation details should be private class date { int day, month, year; How do we read and set values for date objects? Functions getdate and setdate Accessor and mutator methods

Public vs private Implementation details should be private class date { int day, month, year; How do we read and set values for date objects? Functions getdate and setdate Accessor and mutator methods Programmers are lazy! Allow access to internal variables of an object if (s.tos == 0){...

Public vs private To restore data integrity, classify internals as public or private class stack{ private int values[100]; private int tos = 0;...

Public vs private To restore data integrity, classify internals as public or private class stack{ private int values[100]; private int tos = 0;... Should private variables be visible to other objects of the same class?

Public vs private To restore data integrity, classify internals as public or private class stack{ private int values[100]; private int tos = 0;... Should private variables be visible to other objects of the same class? Does it make sense to have private methods?

Private methods? class stack {... push (int i){ /* push i onto stack */ if (stack_full){ extend_stack();... /* Code to add i to stack * / extend_stack(){... /* Code to get additional space for stack data *...

Static components All functions defined in classes Classes have to be instantiated Where does computation begin?

Static components All functions defined in classes Classes have to be instantiated Where does computation begin? Need functions that exist without instantiating a class static functions Also useful for library functions IO.read(), IO.write(...)

Static components All functions defined in classes Classes have to be instantiated Where does computation begin? Need functions that exist without instantiating a class static functions Also useful for library functions IO.read(), IO.write(...) Also static fields class Math { public static double PI = 3.1415927; public static double E = 2.7182818; public static double sin(double x) {......

Private static? Does a combination of private and static make sense? class interest-rate { private static double base_rate = 7.32; private double deposit-amount; public double sixmonth-yield(){... /* uses base-rate and deposit-amount */ public double oneyear-yield(){... /* uses base-rate and deposit-amount */...

Static fields and methods Static entities exist before any objects are created Static fields are shared across objects

Static fields and methods Static entities exist before any objects are created Static fields are shared across objects class stack {... private static int num_push = 0; /* number of pushes across all stacks */ push (int i,...){... num_push++; /* update static variable */......

Static fields and methods Static entities exist before any objects are created Static fields are shared across objects class stack {... private static int num_push = 0; /* number of pushes across all stacks */ push (int i,...){... num_push++; /* update static variable */...... Static methods should not refer to non-static fields

Constants class Math { public static double PI = 3.1415927;... User can modify PI!

Constants class Math { public static double PI = 3.1415927;... User can modify PI! Declare PI to be final class Math { public static final double PI = 3.1415927;... What could it mean for a function to be final?

Java basics Java program : collection of classes Each class xyz in a separate file xyz.java

Java basics Java program : collection of classes Each class xyz in a separate file xyz.java To start the computation: one class must have a static method public static void main(string[] args) void is the return type String[] args refers to command line arguments

Java basics Java program : collection of classes Each class xyz in a separate file xyz.java To start the computation: one class must have a static method public static void main(string[] args) void is the return type String[] args refers to command line arguments Java programs are usually interpreted on Java Virtual Machine

Java basics Java program : collection of classes Each class xyz in a separate file xyz.java To start the computation: one class must have a static method public static void main(string[] args) void is the return type String[] args refers to command line arguments Java programs are usually interpreted on Java Virtual Machine javac compiles Java into bytecode for JVM javac xyz.java creates class file xyz.class

Java basics Java program : collection of classes Each class xyz in a separate file xyz.java To start the computation: one class must have a static method public static void main(string[] args) void is the return type String[] args refers to command line arguments Java programs are usually interpreted on Java Virtual Machine javac compiles Java into bytecode for JVM javac xyz.java creates class file xyz.class java xyz interprets and runs bytecode in class file

Hello world class helloworld{ public static void main(string[] args){ System.out.println("Hello world!");

Hello world class helloworld{ public static void main(string[] args){ System.out.println("Hello world!"); Store in helloworld.java

Hello world class helloworld{ public static void main(string[] args){ System.out.println("Hello world!"); Store in helloworld.java javac helloworld.java to compile to bytecode Creates helloworld.class

Hello world class helloworld{ public static void main(string[] args){ System.out.println("Hello world!"); Store in helloworld.java javac helloworld.java to compile to bytecode Creates helloworld.class java helloworld to execute

Hello world class helloworld{ public static void main(string[] args){ System.out.println("Hello world!"); Store in helloworld.java javac helloworld.java to compile to bytecode Creates helloworld.class java helloworld to execute Note: javac requires extension.java java should not be provided.class javac automatically follows dependencies and compiles all classes required