ITEC 320 Lecture 10 Packages (2). Review Packages –What parts do they have? –Syntax?

Slides:



Advertisements
Similar presentations
Modules Program is built out of components. Each component defines a set of logically related entities (strong internal coupling) A component has a public.
Advertisements

Slide: 1 Copyright © AdaCore Packages Presented by Quentin Ochem university.adacore.com.
ITEC 320 Lecture 3 In-class coding / Arrays. Arrays Review Strings –Advantages / Disadvantages Input –What two methods are used? Conditionals Looping.
Java Review Interface, Casting, Generics, Iterator.
ITEC 320 Lecture 11 Pointers(1). Pointers Review Packages –Generic –Child Homework 3 is posted.
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
Chapter 10 Introduction to Arrays
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.
CPS 506 Comparative Programming Languages Abstract Data Type and Encapsulation.
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Wednesday, 12/11/02, Slide #1 CS 106 Intro to Comp. Sci. 1 Wednesday, 12/11/02  QUESTIONS??  Today: CLOSING CEREMONIES!  HW #5 – Back Monday (12/16)
Loops – While, Do, For Repetition Statements Introduction to Arrays
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
CHAPTER 6 Stacks Array Implementation. 2 Stacks A stack is a linear collection whose elements are added and removed from one end The last element to be.
Stacks CS 308 – Data Structures. What is a stack? It is an ordered group of homogeneous items of elements. Elements are added to and removed from the.
1 Chapter 6 Lists Plus. ADT Sorted List Operations Transformers n MakeEmpty n InsertItem n DeleteItem Observers n IsFull n LengthIs n RetrieveItem Iterators.
Lecture 9 Concepts of Programming Languages
Abstract Data Types and Encapsulation Concepts
Templates CS-240 Dick Steflik & DJ. Foreman. Reuse Templates allow us to get more mileage out of the classes we create by allowing the user to supply.
Data Structures Data structures permit the storage of related data for use in your program. –Arrays.
Multiple Choice Solutions True/False a c b e d   T F.
Chapter 11 Abstract Data Types and Encapsulation Concepts.
Templates CS212 & CS-240. Reuse Templates allow extending our classes Allows the user to supply certain attributes at compile time. Attributes specified.
1 Nell Dale Chapter 6 Lists Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
ITEC 320 Lecture 16 Packages (1). Review Questions? –HW –Exam Nested records –Benefits –Downsides.
Abstract Data Types (ADTs) and data structures: terminology and definitions A type is a collection of values. For example, the boolean type consists of.
Data Structures Lecture-12 : STL Azhar Maqsood NUST Institute of Information Technology (NIIT)
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
ITEC 320 Lecture 5 Scope Exceptions. Scope / Exceptions Review Functions Procedures.
Chap. 1 Classes, Types, and Objects. How Classes Are Declared [ ] class [extends ] [implements,, … ] { // class methods and instance variable definitions.
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 An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
Chapter 11 Abstract Data Types and Encapsulation Concepts.
ADT ITEC 320 Lecture 22. ADT Review Generic lists –Ada compilation workout Recursive algorithms –What would a recursive copy look like? Project 4 is up.
1 Recall Definition of Stack l Logical (or ADT) level: A stack is an ordered group of homogeneous items (elements), in which the removal and addition of.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Chapter 10, Slide 1 ABSTRACT DATA TYPES Based on the fundamental concept of ABSTRACTION:  process abstraction  data abstraction Both provide:  information.
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
What happens... l When a function is called that uses pass by value for a class object like our dynamically linked stack? StackType MakeEmpty Pop Push.
Chapter 6 Lists Plus. What is a Class Template? A class template allows the compiler to generate multiple versions of a class type by using type parameters.
ITEC 320 Lecture 9 Nested records / Packages. Review Project ?’s Records Exam 1 next Friday.
Access Modifiers Control which classes use a feature Only class-level variables may be controlled by access modifiers Modifiers 1. public 2. protected.
1 Chapter 11 © 1998 by Addison Wesley Longman, Inc The Concept of Abstraction - The concept of abstraction is fundamental in programming - Nearly.
1 CS Programming Languages Class 22 November 14, 2000.
(1) ICS 313: Programming Language Theory Chapter 11: Abstract Data Types (Data Abstraction)
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 10 Abstraction - The concept of abstraction is fundamental in programming - Nearly all programming.
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
Higher Computing Science 2016 Prelim Revision. Topics to revise Computational Constructs parameter passing (value and reference, formal and actual) sub-programs/routines,
LECTURE LECTURE 17 Templates 19 An abstract recipe for producing concrete code.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
ITEC 320 Lecture 6 More on arrays / Strings. Arrays Review Scope Exceptions Arrays –Dynamic arrays How? Typed and anonymous.
Click to edit Master text styles Stacks Data Structure.
© 2004 Pearson Addison-Wesley. All rights reserved October 5, 2007 Arrays ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
Sections 3.4 Formal Specification
Abstract Data Types and Encapsulation Concepts
11.1 The Concept of Abstraction
Paul Ammann & Jeff Offutt
Abstract Data Types and Encapsulation Concepts
Conditional Statements
Abstract Data Types and Encapsulation Concepts
Chap 1 Chap 2 Chap 3 Chap 5 Surprise Me
slides created by Alyssa Harding
Arrays Arrays are represented by objects but there is no class that array objects are instances of. Variables of array type are declared using bracket.
Corresponds with Chapter 5
11.1 The Concept of Abstraction
Presentation transcript:

ITEC 320 Lecture 10 Packages (2)

Review Packages –What parts do they have? –Syntax?

Packages(2) Specificatio n Tells the compiler what the package does, but not how to do it Store in a.ads file package PairPkg is type Pair is record x, y: Integer; end record function distanceToOrigin(p: Pair) return Integer; end PairPkg;

Packages(2) Body Contains the code for a certain package’s procedures / functions package body PairPkg is function distanceToOrigin(p: Pair) return Integer is begin return abs p.x + abs p.y; end distanceToOrigin; end PairPkg;

Packages(2) Client with ada.integer_text_io; use ada.integer_text_io; with pairpkg; use pairpkg; procedure pairclient is p: Pair; begin p := newPair(1, 2); put(distanceToOrigin(p)); p.x := 3; -- Syntax error end pairclient;

Packages(2) Code Input parser Reads a line Returns int / String / Float based on whitespace Specification Body Client What should be written first? Privacy?

Packages(2) Comparison Java’s Scanner –Similarities –Differences

Packages(2) Currently package IntStackPkg is type StackType is limited private; Stack_Is_Empty, Stack_Is_Full : Exception; function IsEmpty (S : StackType) return Boolean; function IsFull (S : StackType) return Boolean; procedure Push (I : Integer; S : in out StackType); procedure Pop (S : in out StackType); function Top (S : StackType) return Integer; private MaxSize : Constant Integer := 1000; type ElementArray is array(1.. MaxSize) of Integer; type StackType is record Elements : ElementArray; TheTop : Integer := 0; end record; end IntStackPkg; What happens when you want to store Strings?

Packages(2) Generic Formal parameters –Specification Actual parameters –Client Need: get away from specific types

Packages(2) Generic generic -- Generic parameters are declared here Size : Positive; -- Size of stack to create type ItemType is private; -- Type of elements package StackPkg is type Stack is limited private; Stack_Empty: exception; Stack_Full : exception; function isEmpty (s : Stack) return Boolean; function isFull (s : Stack) return Boolean; procedure push (item : ItemType; s : in out Stack); procedure pop (s : in out Stack); function top (s : Stack) return ItemType; private type StackElements is array(1.. Size) of ItemType; type Stack is record Elements : StackElements; Top : Natural := 0; end record; end StackPkg;

Packages(2) Body with StackPkg; -- Make the generic stack package available procedure avgs is -- Create the two stack packages that are needed package intstkpkg is new stackpkg(1000, integer); package fltstkpkg is new stackpkg(1000, float); -- package FltStkPkg is new StackPkg(size => 1000, ItemType => Float); -- A use statement will simplify access to the package members use IntStkPkg; use FltStkPkg; Value : Integer; Count, Sum : Integer := 0; Running_Average : Float; -- Declare the two stack variables IntStack : IntStkPkg.Stack; -- Must include package name -- here so that the FltStack : FltStkPkg.Stack; -- compiler can tell which stack -- is needed

Packages(2) Body(2) begin while not end_of_file loop get(Value); Sum := Sum + Value; Count := Count + 1; Running_Average := float(Sum) / Float(Count); IntStkPkg.push(Value, IntStack); FltStkPkg.push(Running_Average, FltStack); end loop; while not IsEmpty(IntStack) loop put(top(IntStack)); put(" "); put(top(FltStack)); new_line; pop(IntStack); pop(FltStack); end loop; end avgs; Note:

Packages(2) Procedure s generic type ItemType is private; procedure generic_swap(left, right: in out ItemType); -- Implementation of generic procedure swap procedure generic_swap(left, right : in out ItemType) is old_left : constant ItemType := left; begin left := right; right := old_left; end swap; -- Client file that instantiates generic procedure swap procedure swap_int is new generic_swap(ItemType => Integer); i,j: Integer; begin swap_int(i,j);

Packages(2) Notes Generic packages cannot be used directly Make a derived type and use it Syntax for delaying when a type is applied Consider in the declare block for packages

Packages(2) Child packages Additions to existing packages Currently –Add to specification –Add to body New –Add functions/procedures/types to existing package –What feature is this similar to?

Packages(2) Method Come up with a name for the child package New package is parent.child –ada.text_io… Specification is named –parent-child.ads Body is named –parent-child.adb

Packages(2) Rationale Soft grouping –Really, it just adds name. to a package Adds new capabilities using existing structure Logical nesting –Helps guide thinking Sharing of types between parent / children Name clash avoidance –Stay away from Ada, System, or Interface

Packages(2) Implicit /Explicit Consider Which do you consider implicit, explicit? What does implicit offer over explicit? What does explicit offer over implicit? System.out.println(p1.distanceToOrigin()); versus put(distanceToOrigin(p1));

Packages(2) Example 2 // Java Pair p = new Pair(1, 2); Pair q = new Pair(3, 4); System.out.println(p.toString()); System.out.println (q.toString()); -- Ada declare Pair q = newPair(1, 2); Pair r = newPair(3, 4); begin put(toString(q)); put(toString(r));

Packages(2) Conflation Joins together Java –Data –Methods Ada –Types –Function / Procedures Rationale for each choice? Rationale for each choice?

Packages(2) Usage scenarios What is Java best suited for? What are Java’s weaknesses? What is Ada best suited for? What are Ada’s weaknesses?

Packages(2) Implementatio n If you had to write a compiler Would you choose Java or Ada?

Packages(2) Review Packages