Chapter 6: From an assignment core to Java Xinming (Simon) Ou CIS 505: Programming Languages Kansas State University Fall 2010 1.

Slides:



Advertisements
Similar presentations
Optional Static Typing Guido van Rossum (with Paul Prescod, Greg Stein, and the types-SIG)
Advertisements

1 Storage Duration and Scope –Local and global variables Storage classes –automatic, static, external, register Todays Material.
C Language.
Chapter 2, Part 2: An interpreter for an object-oriented language Xinming (Simon) Ou CIS 505: Programming Languages Kansas State University Fall
Chapter 5: Abstraction, parameterization, and qualification Xinming (Simon) Ou CIS 505: Programming Languages Kansas State University Fall
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 17 Templates.
Chapter 17 Templates. Generic Algorithms Algorithms in which the actions or steps are defined, but the data types of the items being manipulated are not.
LCS Non-Dynamic Version int function lcs (x, y, i, j) begin if (i = 0) or (j = 0) return 0; else if (x[i] = y[j]) return lcs(x, y, i-1, j-1)+1; else return.
1 MODULE name (parameters) “Ontology” “Program” “Properties” The NuSMV language A module can contain modules Top level: parameters less module Lower level.
Chapter 7 Strings F To process strings using the String class, the StringBuffer class, and the StringTokenizer class. F To use the String class to process.
Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process.
Week 8 Arrays Part 2 String & Pointer
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.
Chapter 2, Part 1: An interpreter architecture for C-like languages Xinming (Simon) Ou CIS 505: Programming Languages Kansas State University Fall 2010.
Side effects in Prolog Lecturer: Xinming (Simon) Ou CIS 505: Programming Languages Fall 2010 Kansas State University 1.
Abstract Data Types Data abstraction, or abstract data types, is a programming methodology where one defines not only the data structure to be used, but.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
Lecture 3: Topics If-then-else Operator precedence While loops Static methods Recursion.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 14 – Student Grades Application: Introducing.
Principles of Object-Oriented Software Development The language Java.
Class template Describing a generic class Instantiating classes that are type- specific version of this generic class Also are called parameterized types.
CS1110: Computer Science I Chapter 5 Arrays. One-dimensional Arrays int [] data = new int [4]; data[0] = 1; data[1] = 3; data[2] = 5; data[3] = 7; Arrays.
Chapter 3 Data Structures and Abstract Data Type Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
1 Chapter 5: Names, Bindings and Scopes Lionel Williams Jr. and Victoria Yan CSci 210, Advanced Software Paradigms September 26, 2010.
High-Level Programming Languages: C++
1 Arrays Chapter 13 Especially read 13.1 – 13.2 Text introduces vectors, which we will not cover, in 13.3.
ISBN Chapter 7 Expressions and Assignment Statements.
CompSci Today’s topics Java Review Just a bunch of headings meant to trigger questions A contraction of previous notes Upcoming Midterm Exam Reading.
Problem Solving using the Java Programming Language May 2010 Mok Heng Ngee Day 5: Arrays.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays and Methods l Programming with Arrays.
Tuc Goodwin  Object and Component-Oriented Programming  Classes in C#  Scope and Accessibility  Methods and Properties  Nested.
Session 08 Module 14: Generics and Iterator Module 15: Anonymous & partial class & Nullable type.
Computing with C# and the.NET Framework Chapter 2 C# Programming Basics ©2003, 2011 Art Gittleman.
Question 1a) What is printed by the following Java program? int s; int r; int i; int [] x = {4, 8, 2, -9, 6}; s = 1; r = 0; i = x.length - 1; while (i.
Topic 3: C Basics CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering University.
(1) ICS 313: Programming Language Theory Chapter 12: Object Oriented Programming.
CIS 270—Application Development II Chapter 8—Classes and Objects: A Deeper Look.
ENEE150 – 0102 ANDREW GOFFIN Project 4 & Function Pointers.
Principles of Object-Oriented Software Development The language C++
The median again The steps of our algorithm: Read the size of the list, N. Declare and instantiate an array of integers, "list". Read the elements of list.
Programmeren 1 6 september 2010 HOORCOLLEGE 2: INTERACTIE EN CONDITIES PROGRAMMEREN 1 6 SEPTEMBER 2009 Software Systems - Programming - Week.
Type Systems CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University
Objects First With Java A Practical Introduction Using BlueJ Casting Week
1 Predefined Classes and Objects Chapter 3. 2 Objectives You will be able to:  Use predefined classes available in the Java System Library in your own.
Object Oriented Programming Session # 03.  Abstraction: Process of forming of general and relevant information from a complex scenarios.  Encapsulation:
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
CSE 130 : Winter 2009 Programming Languages Lecture 11: What’s in a Name ?
Chapter 2. Variable and Data type
Object Oriented Programming and Data Abstraction Rowan University Earl Huff.
PHP Reusing Code and Writing Functions 1. Function = a self-contained module of code that: Declares a calling interface – prototype! Performs some task.
Hindley-Milner Type Inference CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University
Print Row Function void PrintRow(float x[ ][4],int i) { int j; for(j=0;j
Collections and Iteration Week 13.  Collections  ArrayList objects  Using loops with collections Collections and Iteration CONCEPTS COVERED THIS WEEK.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 4 is due Nov. 20 (this Friday). Today: –Templates. –STL –Smart Pointers.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
Dr. Abdullah Almutairi Spring PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used,
Chapter 16 Templates Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
JAVA METHODS (FUNCTIONS). Why are they called methods? Java is a strictly object-oriented programming language Methods are functions inside of objects.
Generic Programming and Library Design Brian Bartman
PHP Tutorial. What is PHP PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.
ITM 3521 ITM 352 Functions. ITM 3522 Functions  A function is a named block of code (i.e. within {}'s) that performs a specific set of statements  It.
Blocks 1 D ::= var I = E | D1 ; D2 | class I = T | module I = D | import L | begin D1 in D2 end T ::= int | struct D end | array[E] of T | L | begin D.
Quiz 1 A sample quiz 1 is linked to the grading page on the course web site. Everything up to and including this Friday’s lecture except that conditionals.
©2004 Joel Jones 1 CS 403: Programming Languages Lecture 3 Fall 2004 Department of Computer Science University of Alabama Joel Jones.
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
Presentation transcript:

Chapter 6: From an assignment core to Java Xinming (Simon) Ou CIS 505: Programming Languages Kansas State University Fall

Core imperative language 2 P : Program D : Declaration T : TypeStructure C : Command E : Expression L : NameExpression N : Numeral I : Identifier P ::= D ; C E ::= N | L | E1 + E2 | E1 - E2 | E1 > E2 | E1 == E2 | not E | new T C ::= L = E | print L | C1 ; C2 | if E { C1 } else { C2 } | while E { C } D ::= var I = E | D1 ; D2 T ::= int | struct (I : int)+ end | array[N] of int L ::= I | L.I | L[E] N ::= 0 | 1 | 2 |... I ::= alphanumeric strings beginning with a letter, not including keywords

Storage Layout 3 var x = 2 - 1; var r = new array[3] of int; var s = new struct a:int; b:int end; var y = new int; r[0] = x; r[x] = r[0] + r[r[0] - 1]; if not(r[1] > x) { s.a = 0 } else { s.a = 1 }; s.b = r[s.a]; print s

Data-structure extensions 4 T ::= int | struct (I : int)+ end | array[N] of int T ::= int | struct D end | array[N] of T D ::= var I = E | D1 ; D2

Example 5 var database = new struct var howmany = 0; var table = new array[100] of struct var idnum = new int; var balance = 0 end end; if not(database.howmany == 100) { database.table[howmany].idnum = ; database.table[howmany].balance = 25; database.howmany = database.howmany + 1 }

Alternatively 6 T ::= int | struct D end | array[N] T ::= int | struct D end | array[N] of T

Example 7 var database = new struct var howmany = 0; var table = new array[100] end; if not(database.howmany == 100) { database.table[howmany] = new struct var idnum = ; var balance = 25 end; database.howmany = database.howmany + 1 }

Functions inside structs 8 D ::= var I = E | D1 ; D2 | proc I() { C } C ::= L = E | C1 ; C2 |... | L() T ::= int | struct D end | array[E] of T E ::= N |... | new T | L() L ::= I | L.I | L[E]

Example 9 var clock = new struct var time = 0; proc tick(){ time = time + 1 }; proc display(){ print time }; proc reset(){ time = 0 } end clock.tick(); clock.display()

Example 10 var clock = new struct var time = 0; proc tick(){ time = time + 1 }; proc display(){ print time }; proc reset(){ time = 0 } end clock.tick(); clock.display()

Class as a type-structure abstract 11 D ::= var I = E | D1 ; D2 | proc I() { C } | class I = T T ::= int | struct D end | array[E] of T | L e.g. class Entry = struct var idnum = new int; var balance = 0 end; class DB = struct var howmany = 0; var table = new array[100] of Entry end; var database = new DB

Modules 12 D ::= var I = E | D1 ; D2 |... | module I = D | import L e.g. module M = var x = new int; class C = struct var a = 0 end; var y = new array[10] of C; proc initialize(){ x = 0; for i = 0 upto 9 { y[i].a = i } } import M; initialize(); var z = new C; z.a = x + y[0].a

Avoiding namespace confusion 13 module M = newstruct var x = 7 end; module N = newstruct var x = 99 end; import M; import N; N.x = M.x + 1

Parameterization 14 Provide parameters to class. Example: class Entry = struct var idnum = new int; var balance = 0 end; class DB(size) = struct var howmany = 0; var table = new array[size] of Entry end; var database = new DB(100)

Type Templates 15 module DataBase(size, recordTemplate) = var howmany = 0; var table = new array[size] of recordTemplate; proc initialize(){ for i = 0 upto size - 1 { table[i].init() } }; proc find(index): if index >= 0 and index < size { answer = table[index].getVal() return answer } class Entry = struct var idnum = new int; var balance = new int; proc init(){ idnum = -1; balance = 0 }; fun getVal(){ return balance } end;

Specifying “type template” 16 interface RecordInterface = struct init: void -> command; getVal: void -> int end; module DataBase(size: int, recordTemplate: RecordInterface) = var howmany = 0; var table = new array[size] of recordTemplate; proc initialize(){ for i = 0 upto size - 1 { table[i].init() } }; proc find(index): if index >= 0 and index < size { answer = table[index].getVal() return answer }