Top Banner
ITEC 320 Lecture 16 Packages (1)
22

ITEC 320 Lecture 16 Packages (1). Review Questions? –HW –Exam Nested records –Benefits –Downsides.

Dec 27, 2015

Download

Documents

Judith Lee
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: ITEC 320 Lecture 16 Packages (1). Review Questions? –HW –Exam Nested records –Benefits –Downsides.

ITEC 320

Lecture 16Packages (1)

Page 2: ITEC 320 Lecture 16 Packages (1). Review Questions? –HW –Exam Nested records –Benefits –Downsides.

Packages(1)

Review

• Questions?– HW– Exam

• Nested records– Benefits– Downsides

Page 3: ITEC 320 Lecture 16 Packages (1). Review Questions? –HW –Exam Nested records –Benefits –Downsides.

Packages(1)

Objectives

• Packages– OO in Ada

Page 4: ITEC 320 Lecture 16 Packages (1). Review Questions? –HW –Exam Nested records –Benefits –Downsides.

Packages(1)

Definitions

• Client– Uses the class and its methods– Routine or package that uses the types

and routines defined in package P

• Examples–With ada.text_io; use ada.text_io;

Which one is Java?Which one is Ada?

Page 5: ITEC 320 Lecture 16 Packages (1). Review Questions? –HW –Exam Nested records –Benefits –Downsides.

Packages(1)

Ada “Classes”

• Records store information• Procedures work with records / other

data• Put the two together and you have

the Ada version of classes• Contain– Procedures– Types

Page 6: ITEC 320 Lecture 16 Packages (1). Review Questions? –HW –Exam Nested records –Benefits –Downsides.

Packages(1)

Java

class Pair {

int x, y; int distanceToOrigin() // How far to origin

{ return Math.abs(x) + Math.abs(y); } } // Client for class Pair class PairClient {

public static void main(String[] args){

Pair p; p = new Pair(); p.x = 1; p.y = 2; Sop(p.distanceToOrigin());

}}

Page 7: ITEC 320 Lecture 16 Packages (1). Review Questions? –HW –Exam Nested records –Benefits –Downsides.

Packages(1)

Ada

• 2 pieces– Record for a pair– Function to calculate the distance to the

origin

• File structure– Specification– Body

Page 8: ITEC 320 Lecture 16 Packages (1). Review Questions? –HW –Exam Nested records –Benefits –Downsides.

Packages(1)

Specification

• 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;

Page 9: ITEC 320 Lecture 16 Packages (1). Review Questions? –HW –Exam Nested records –Benefits –Downsides.

Packages(1)

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;

Page 10: ITEC 320 Lecture 16 Packages (1). Review Questions? –HW –Exam Nested records –Benefits –Downsides.

Packages(1)

Ada / Java

• Java classes (1 file)• Ada method (2 files)• What are the benefits / downsides of

each approach?• How does Java sort of provide the

same idea that Ada does?

Page 11: ITEC 320 Lecture 16 Packages (1). Review Questions? –HW –Exam Nested records –Benefits –Downsides.

Packages(1)

Rules

• The specification must be followed to the letter

• All functions / procedures must be implemented

• All return / parameter types must match

Page 12: ITEC 320 Lecture 16 Packages (1). Review Questions? –HW –Exam Nested records –Benefits –Downsides.

Packages(1)

Use

• Without the use statement, you have to fully-qualify the package name

with pairpkg; procedure pairclient is

p: pairpkg.Pair; begin

p.x := 1; p.y := 2; put(pairpkg.distanceToOrigin(p));

end pairclient;

Page 13: ITEC 320 Lecture 16 Packages (1). Review Questions? –HW –Exam Nested records –Benefits –Downsides.

Packages(1)

Specification

• 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;

Page 14: ITEC 320 Lecture 16 Packages (1). Review Questions? –HW –Exam Nested records –Benefits –Downsides.

Packages(1)

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;

Page 15: ITEC 320 Lecture 16 Packages (1). Review Questions? –HW –Exam Nested records –Benefits –Downsides.

Packages(1)

Compilation

• Rule of thumb– Keep it all in the same directory– Some way of pointing where to look for

specifications / bodies

• Compiler does the work for you– Automatically pulls in packages thanks

to the with statement

• Take a look at the files produced and think about why they are there…

Page 16: ITEC 320 Lecture 16 Packages (1). Review Questions? –HW –Exam Nested records –Benefits –Downsides.

Packages(1)

Privacy

• Java has private variables• Why ?• What do they provide?• How are they used?

Page 17: ITEC 320 Lecture 16 Packages (1). Review Questions? –HW –Exam Nested records –Benefits –Downsides.

Packages(1)

Ada

package PairPkg is -- Public section of package type Pair is private; -- Only type name is public function newPair(x, y: Integer) return Pair; function distanceToOrigin(p: Pair) return Integer; -- Private section of package private -- Code below is NOT visible to client type Pair is record -- Type implementation is private

x, y: Integer; end record

end PairPkg;

What can you do with the

Page 18: ITEC 320 Lecture 16 Packages (1). Review Questions? –HW –Exam Nested records –Benefits –Downsides.

Packages(1)

Package

package body PairPkg is function newPair(x, y: Integer) return Pair is

temp: Pair := (x, y); begin

return temp; end newPair; function distanceToOrigin(p: Pair) return Integer is begin

return p.x + p.y; end distanceToOrigin;

end PairPkg;

What does this look like?

Page 19: ITEC 320 Lecture 16 Packages (1). Review Questions? –HW –Exam Nested records –Benefits –Downsides.

Packages(1)

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;

Page 20: ITEC 320 Lecture 16 Packages (1). Review Questions? –HW –Exam Nested records –Benefits –Downsides.

Packages(1)

Specification

• 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;

Page 21: ITEC 320 Lecture 16 Packages (1). Review Questions? –HW –Exam Nested records –Benefits –Downsides.

Packages(1)

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;

Page 22: ITEC 320 Lecture 16 Packages (1). Review Questions? –HW –Exam Nested records –Benefits –Downsides.

Packages(1)

Summary

• Packages in Ada– Specifications– Body