23
ITEC 320 Lecture 9 Nested records / Packages

ITEC 320 Lecture 9 Nested records / Packages. Review Project ?’s Records Exam 1 next Friday

Embed Size (px)

Citation preview

Page 1: ITEC 320 Lecture 9 Nested records / Packages. Review Project ?’s Records Exam 1 next Friday

ITEC 320

Lecture 9Nested records / Packages

Page 2: ITEC 320 Lecture 9 Nested records / Packages. Review Project ?’s Records Exam 1 next Friday

Nested records / Packages

Review

• Project ?’s• Records• Exam 1 next Friday

Page 3: ITEC 320 Lecture 9 Nested records / Packages. Review Project ?’s Records Exam 1 next Friday

Nested records / Packages

Outline

• Nested records

Page 4: ITEC 320 Lecture 9 Nested records / Packages. Review Project ?’s Records Exam 1 next Friday

Nested records / Packages

Design exercise

• How do we represent a line mathematically?

• How do we represent one in Ada?

Page 5: ITEC 320 Lecture 9 Nested records / Packages. Review Project ?’s Records Exam 1 next Friday

Nested records / Packages

Code

type Pair is record x: Integer; y: Integer;

end record; type Line is record

start: Pair; end: Pair;

end record; myLine: Line; begin

myLine.start.x := 1; myLine.start.y := 2; myLine.end := (3, 4); put(myLine.start.x); put(myLine.end.x);

Page 6: ITEC 320 Lecture 9 Nested records / Packages. Review Project ?’s Records Exam 1 next Friday

Nested records / Packages

Java/Ada

• Nested records– How is it implemented in Java?– How is it implemented in Ada?

Page 7: ITEC 320 Lecture 9 Nested records / Packages. Review Project ?’s Records Exam 1 next Friday

Nested records / Packages

UML

• Composition– Is a part of

• Aggregation– Is contained in

University DepartmentFaculty Example

Page 8: ITEC 320 Lecture 9 Nested records / Packages. Review Project ?’s Records Exam 1 next Friday

Nested records / Packages

Arrays

• What does an array of records like Line mean in terms of –Memory allocation– Syntax

Page 9: ITEC 320 Lecture 9 Nested records / Packages. Review Project ?’s Records Exam 1 next Friday

Nested records / Packages

Real world example

• Points• Lines• Line collection / displayer• Images• Grid• World loader

Page 10: ITEC 320 Lecture 9 Nested records / Packages. Review Project ?’s Records Exam 1 next Friday

Nested records / Packages

Packages

• Records are one way of grouping data

• Packages are one way of grouping functions and procedures

Page 11: ITEC 320 Lecture 9 Nested records / Packages. Review Project ?’s Records Exam 1 next Friday

Nested records / Packages

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 12: ITEC 320 Lecture 9 Nested records / Packages. Review Project ?’s Records Exam 1 next Friday

Nested records / Packages

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 13: ITEC 320 Lecture 9 Nested records / Packages. Review Project ?’s Records Exam 1 next Friday

Nested records / Packages

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 14: ITEC 320 Lecture 9 Nested records / Packages. Review Project ?’s Records Exam 1 next Friday

Nested records / Packages

Ada

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

origin

• File structure– Specification– Body

Page 15: ITEC 320 Lecture 9 Nested records / Packages. Review Project ?’s Records Exam 1 next Friday

Nested records / Packages

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 16: ITEC 320 Lecture 9 Nested records / Packages. Review Project ?’s Records Exam 1 next Friday

Nested records / Packages

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 17: ITEC 320 Lecture 9 Nested records / Packages. Review Project ?’s Records Exam 1 next Friday

Nested records / Packages

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 18: ITEC 320 Lecture 9 Nested records / Packages. Review Project ?’s Records Exam 1 next Friday

Nested records / Packages

Rules

• The specification must be followed to the letter

• All functions / procedures must be implemented

• All return / parameter types must match

Page 19: ITEC 320 Lecture 9 Nested records / Packages. Review Project ?’s Records Exam 1 next Friday

Nested records / Packages

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 20: ITEC 320 Lecture 9 Nested records / Packages. Review Project ?’s Records Exam 1 next Friday

Nested records / Packages

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 private; function distanceToOrigin(p: Pair) return Integer;

privatetype Pair is record

x, y: Integer; end record;

end PairPkg;

Page 21: ITEC 320 Lecture 9 Nested records / Packages. Review Project ?’s Records Exam 1 next Friday

Nested records / Packages

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 9 Nested records / Packages. Review Project ?’s Records Exam 1 next Friday

Nested records / Packages

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 23: ITEC 320 Lecture 9 Nested records / Packages. Review Project ?’s Records Exam 1 next Friday

Nested records / Packages

Review

• Nested records• Intro to packages