Programming Languages and Techniques (CIS120)

Similar documents
Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

CIS 120 Midterm II November 16, Name (printed): Pennkey (login id):

CIS 120 Midterm II November 16, 2012 SOLUTIONS

Programming Languages and Techniques (CIS120)

CIS 120 Midterm II March 29, 2013 SOLUTIONS

CIS 120 Midterm II November 8, Name (printed): Pennkey (login id):

Programming Languages and Techniques (CIS120)

CIS 120 Midterm II November 8, 2013 SOLUTIONS

Name: Pennkey: CIS 120 Midterm II November 18, 2011

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Name: Pennkey: CIS 120 Final Exam December 21, Do not begin the exam until you are told to do so. You have 120 minutes to complete the exam.

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Array Based Lists. Collections

Introduction to Object-Oriented Programming

Programming Languages and Techniques (CIS120)

CIS 120 Midterm II November 7, Name (printed): Pennkey (login id):

Programming Languages and Techniques (CIS120e)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Announcements. Lecture 04b Header Classes. Review (again) Comments on PA1 & PA2. Warning about Arrays. Arrays 9/15/17

Programming Languages and Techniques (CIS120)

Lab Assignment Three

Lecture 17. For Array Class Shenanigans

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

CIS 120 Final Exam May 7, Name (printed): Pennkey (login id):

Array. Prepared By - Rifat Shahriyar

Programming Languages and Techniques (CIS120)

Computer Science II Data Structures

COE318 Lecture Notes Week 4 (Sept 26, 2011)

P2: Advanced Java & Exam Preparation

Object Oriented Programming. Java-Lecture 6 - Arrays

Creating and Using Objects

Programming Languages and Techniques (CIS120)

Homework 2: Imperative Due: 5:00 PM, Feb 15, 2019

Computer Science 62. Midterm Examination

CIS 120 Final Exam 15 December 2017

CSCI-1200 Data Structures Spring 2016 Lecture 6 Pointers & Dynamic Memory

CS 231 Data Structures and Algorithms, Fall 2016

No Aids Allowed. Do not turn this page until you have received the signal to start. Read this entire page or you ll miss the bonus question.

Computer Science 62. Bruce/Mawhorter Fall 16. Midterm Examination. October 5, Question Points Score TOTAL 52 SOLUTIONS. Your name (Please print)

Lecture 3: Queues, Testing, and Working with Code

Name: Pennkey (eg, sweirich): CIS 120 Final Exam May 8, 2012

CSE 143. More ArrayIntList; pre/post; exceptions; debugging. Computer Programming II. CSE 143: Computer Programming II

Adam Blank Lecture 3 Autumn 2016 CSE 143. Computer Programming II

Programming Languages and Techniques (CIS120)

CS159. Nathan Sprague. September 30, 2015

Inf1-OP. Inf1-OP Exam Review. Timothy Hospedales, adapting earlier version by Perdita Stevens and Ewan Klein. March 20, School of Informatics

Agenda CS121/IS223. Reminder. Object Declaration, Creation, Assignment. What is Going On? Variables in Java

Programming Languages and Techniques (CIS120e)

Announcements. Lecture 05a Header Classes. Midterm Format. Midterm Questions. More Midterm Stuff 9/19/17. Memory Management Strategy #0 (Review)

CS1083 Week 2: Arrays, ArrayList

DO NOT. UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N.

Exceptions vs. Errors Exceptions vs. RuntimeExceptions try...catch...finally throw and throws

Inf1-OOP. OOP Exam Review. Perdita Stevens, adapting earlier version by Ewan Klein. March 16, School of Informatics

CS121/IS223. Object Reference Variables. Dr Olly Gotel

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

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Classes Classes 2 / 36

Programming Languages and Techniques (CIS120)

Introduction Data Structures Nick Smallbone

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

CS : Data Structures Michael Schatz. Sept 5, 2018 Lecture 3: Introduction to Interfaces

Practice exam for CMSC131-04, Fall 2017

Arrays. Friday, November 9, Department of Computer Science Wellesley College

CS61BL. Lecture 1: Welcome to CS61BL! Intro to Java and OOP Testing Error-handling

Anatomy of a Method. HW3 is due Today. September 15, Midterm 1. Quick review of last lecture. Encapsulation. Encapsulation

Birkbeck (University of London) Software and Programming 1 In-class Test Mar 2018

Data abstractions: ADTs Invariants, Abstraction function. Lecture 4: OOP, autumn 2003

Building Java Programs

+ Today. Lecture 26: Concurrency 3/31/14. n Reading. n Objectives. n Announcements. n P&C Section 7. n Race conditions.

AP CS Unit 7: Arrays Exercises

Java Programming: from the Beginning

Encapsulation. Administrative Stuff. September 12, Writing Classes. Quick review of last lecture. Classes. Classes and Objects

Lecture 6: ArrayList Implementation

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

COE318 Lecture Notes Week 10 (Nov 7, 2011)

CS 307 Midterm 2 Spring 2011

More on Arrays CS 16: Solving Problems with Computers I Lecture #13

1B1b Classes in Java Part I

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

PRACTICE MIDTERM EXAM #1

Declaring and ini,alizing 2D arrays

Lecture 07, Fall 2018 Wednesday September 19

Programming Languages and Techniques (CIS120)

Assignment 1 is due tonight at 11:59pm. Assignment 2 is due next Thursday at 11:59pm

Advanced Computer Programming

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

3. Convert 2E from hexadecimal to decimal. 4. Convert from binary to hexadecimal

Transcription:

Programming Languages and Techniques (CIS120) Lecture 23 March 16, 2018 Arrays, Java ASM

What is the value of ans at the end of this program? int[] a = {1, 2, 3, 4; int[] b = a; b[0] = 0; int ans = a[0]; 1. 1 2. 2 3. 3 4. 0 5. NullPointerException 6. ArrayIndexOutOfBoundsException Answer: 0

What is the value of ans at the end of this program? int[] a = null; int ans = a.length; 1. 1 2. 2 3. 3 4. 0 5. NullPointerException 6. ArrayIndexOutOfBoundsException Answer: NullPointerException

What is the value of ans at the end of this program? int[] a = {; int ans = a.length; 1. 1 2. 2 3. 3 4. 0 5. NullPointerException 6. ArrayIndexOutOfBoundsException Answer: 0

Announcements HW6: Java Programming (Pennstagram) Due: next Tuesday at 11:59pm Note: Run JUnit tests (under Tools menu) Reminder: please complete mid-semester survey See post on Piazza Upcoming: Midterm 2 Friday, March 23rd in class Coverage: mutable state, queues, deques, GUI, Java CIS120 5

The Java Abstract Stack Machine Objects, Arrays, and Static Methods

Java Abstract Stack Machine Similar to OCaml Abstract Stack Machine Workspace Stack Heap Contains the currently executing code Remembers the values of local variables and "what to do next" after function/method calls Stores reference types: objects and arrays Key differences: Everything, including stack slots, is mutable by default Objects store what class was used to create them Arrays store type information and length New component: Class table (coming soon)

Heap Reference Values Arrays Type of values that it stores Length Values for all of the array elements int [] a = { 0, 0, 7, 0 ; int[] length 4 0 0 7 0 length never mutable; elements always mutable Objects Name of the class that constructed it Values for all of the fields class { private int elt; private next; elt 1 next null fields may or may not be mutable public/private not tracked by ASM

Multidimensional Arrays

Multi-Dimensional Arrays A 2-d array is just an array of arrays... String[][] names = {{"Mr. ", "Mrs. ", "Ms. ", {"Smith", "Jones"; System.out.println(names[0][0] + names[1][0]); // --> Mr. Smith System.out.println(names[0][2] + names[1][1]); // --> Ms. Jones names[1][1] just means (names[1])[1] More brackets more dimensions

Multi-Dimensional Arrays int[][] products = new int[5][]; for(int col = 0; col < 5; col++) { products[col] = new int[col+1]; for(int row = 0; row <= col; row++) { products[col][row] = col * row; What would a Java ASM stack and heap look like after running this program?

Multi-Dimensional Arrays int[][] products = new int[5][]; for(int col = 0; col < 5; col++) { products[col] = new int[col+1]; for(int row = 0; row <= col; row++) { products[col][row] = col * row; Stack Heap products 0 0 0 0 0 1 2 3 4 4 6 8 9 12 16 Note: This heap picture is simplified it omits the class identifiers and length fields for all 6 of the arrays depicted. (Contrast with the array Shown last time.) Note also that orientation doesn t matter on the heap.

More Array Examples See ArrayExamples.java

Objects on the ASM

or 0 for "NullPointerException" public class { public int elt; public next; public (int e0, n0) { elt = e0; next = n0; public class Test { public static void main(string[] args) { n1 = new (1,null); n2 = new (2,n1); n3 = n2; n3.next.next = n2; n4 = new (4,n1.next); n2.next.elt = 9; System.out.println(n1.elt); Answer: 9 What does the following program print? 1 9

Workspace Stack Heap n1 = new (1,null); n2 = new (2,n1); n3 = n2; n3.next.next = n2; n4 = new (4,n1.next); n2.next.elt = 9;

Workspace Stack Heap n1 = ; n2 = new (2,n1); n3 = n2; n3.next.next = n2; n4 = new (4,n1.next); n2.next.elt = 9; elt 1 next null Note: we re skipping details here about how the constructor works. We ll fill them in next week. For now, assume the constructor allocates and initializes the object in one step.

Workspace Stack Heap n2 = new (2,n1); n3 = n2; n3.next.next = n2; n4 = new (4,n1.next); n2.next.elt = 9; n1 elt 1 next null

Workspace Stack Heap n2 = ; n3 = n2; n3.next.next = n2; n4 = new (4,n1.next); n2.next.elt = 9; n1 elt 1 next null elt 2 next

Workspace Stack Heap n3 = n2; n3.next.next = n2; n4 = new (4,n1.next); n2.next.elt = 9; n1 n2 elt 1 next null elt 2 next

Workspace Stack Heap n3.next.next = n2; n4 = new (4,n1.next); n2.next.elt = 9; n1 n2 n3 elt 1 next null elt 2 next

Workspace Stack Heap n3.next.next = n2; n4 = new (4,n1.next); n2.next.elt = 9; n1 n2 n3 elt 1 next null elt 2 next

Workspace Stack Heap n3.next.next = n2; n4 = new (4,n1.next); n2.next.elt = 9; n1 n2 n3 elt 1 next elt 2 next

Workspace Stack Heap n4 = new (4,n1.next); n2.next.elt = 9; n1 n2 n3 elt 1 next elt 2 next

Workspace Stack Heap n4 = ; n2.next.elt = 9; n1 n2 n3 elt 1 next elt 2 next elt 4 next

Workspace Stack Heap n2.next.elt = 9; n1 n2 n3 elt 1 next n4 elt 2 next elt 4 next

Workspace Stack Heap n2.next.elt = 9; n1 n2 n3 elt 1 next n4 elt 2 next elt 4 next

Workspace Stack Heap n2.next.elt = 9; n1 n2 n3 elt 9 next n4 elt 2 next elt 4 next

What is the value of ans at the end of this program? Counter[] a = { new Counter(), new Counter() ; Counter[] b = a; a[0].inc(); b[0].inc(); int ans = a[0].inc(); public class Counter { 1. 1 2. 2 3. 3 4. 0 5. NullPointerException 6. ArrayIndexOutOfBoundsException private int r; public Counter () { r = 0; public int inc () { r = r + 1; return r;

What is the value of ans at the end of this program? Counter[] a = { new Counter(), new Counter() ; Counter[] b = { a[0], a[1] ;; a[0].inc(); b[0].inc(); int ans = a[0].inc(); public class Counter { 1. 1 2. 2 3. 3 4. 0 5. NullPointerException 6. ArrayIndexOutOfBoundsException private int r; public Counter () { r = 0; public int inc () { r = r + 1; return r;

What does the ASM look like at the end of this program? Counter[] a = { new Counter(), new Counter() ; Counter[] b = { a[0], a[1] ; a[0].inc(); public class Counter { b[0].inc(); int ans = a[0].inc(); private int r; public Counter () { r = 0; public int inc () { r = r + 1; return r;

What does the ASM look like at the end of this program? Counter[] a = { new Counter(), new Counter() ; Counter[] b = { a[0], a[1] ; a[0].inc(); public class Counter { b[0].inc(); int ans = a[0].inc(); private int r; Stack a Heap Counter[] length 2 public Counter () { r = 0; public int inc () { r = r + 1; return r; b Counter[] length 2 Counter r 3 Counter r 0

Design Exercise: Resizable Arrays Arrays that grow without bound.

Resizable Arrays public class ResArray { /** Constructor, takes no arguments. */ public ResArray() { /** Access the array at position i. If position i has not yet * been initialized, return 0. */ public int get(int i) { /** Modify the array at position i to contain the value v. */ public void set(int i, int v) { /** Return the extent of the array. */ public int getextent() { Object Invariant: extent is always 1 past the last nonzero value in data (or 0 if the array is all zeros)

Adding extent private int extent = 0; /* INVARIANT: extent = 1+index of last nonzero * element, or 0 if all elements are 0. */ /** Modify the array at position i to contain the value v. */ public void set(int idx, int val) { if (idx < 0) { throw new IllegalArgumentException(); grow(idx); data[idx] = val; if (val!= 0 && idx+1 > extent) { extent = idx+1; if (val == 0 && idx+1 == extent) { while (extent > 0 && data[extent-1] == 0) { extent--; /** Return the extent of the array. */ public int getextent() { return extent;

ResArray ASM Workspace ResArray x = new ResArray(); x.set(3,2); x.set(4,1); x.set(4,0); Stack Heap

ResArray ASM Workspace ResArray x = new ResArray(); x.set(3,2); x.set(4,1); x.set(4,0); x Stack Heap ResArray data extent 040 int[] length 0

ResArray ASM Workspace ResArray x = new ResArray(); x.set(3,2); x.set(4,1); x.set(4,0); x Stack Heap ResArray data extent 04 int[] length 0 int[] length 4 0 0 0 2

ResArray ASM Workspace ResArray x = new ResArray(); x.set(3,2); x.set(4,1); x.set(4,0); x Stack Heap ResArray data extent 04 int[] length 4 0 0 0 2

ResArray ASM Workspace ResArray x = new ResArray(); x.set(3,2); x.set(4,1); x.set(4,0); Stack Heap int[] x ResArray data length 8 0 0 0 2 1 0 0 0 extent 045 int[] length 4 0 0 0 2

ResArray ASM Workspace ResArray x = new ResArray(); x.set(3,2); x.set(4,1); x.set(4,0); Stack Heap int[] x ResArray data length 8 0 0 0 2 1 0 0 0 extent 045

ResArray ASM Workspace ResArray x = new ResArray(); x.set(3,2); x.set(4,1); x.set(4,0); Stack Heap int[] x ResArray data length 8 0 0 0 2 0 0 0 0 extent 04

Resizable Arrays public class ResArray { /** Constructor, takes no arguments. */ public ResArray() { Object Invariant: extent is always 1 past the last nonzero value in data (or 0 if the array is all zeros) /** Access the array at position i. If position i has not yet * been initialized, return 0. */ public int get(int i) { /** Modify the array at position i to contain the value v. */ public void set(int i, int v) { /** Return the extent of the array. */ public int getextent() { /** The smallest prefix of the ResArray * that contains all of the nonzero values, as a normal array. */ public int[] values() {

Values Method public int[] values() { int[] values = new int[extent]; for (int i=0; i<extent; i++) { values[i] = data[i]; return values; Or maybe we can do it more straightforwardly? public int[] values() { return data;

ResArray ASM Workspace ResArray x = new ResArray(); x.set(3,2); int[] y = x.values(); y[3] = 0; x Stack Heap ResArray data extent 04 int[] length 4 0 0 0 2

ResArray ASM Workspace ResArray x = new ResArray(); x.set(3,2); int[] y = x.values(); y[3] = 0; x Stack Heap ResArray data extent 04 int[] length 4 0 0 0 2 y

ResArray ASM Workspace ResArray x = new ResArray(); x.set(3,2); int[] y = x.values(); y[3] = 0; x y Stack Heap ResArray data extent 04 int[] length 4 0 0 0 0 invariant violation!

Object encapsulation All modification to the state of the object must be done using the object's own methods. Use encapsulation to preserve invariants about the state of the object. Enforce encapsulation by not returning aliases from methods.