ITEC 320

1 / 23

# ITEC 320 - PowerPoint PPT Presentation

ITEC 320. Lecture 9 Nested records / Packages. Review. Project ?’s Records Exam 1 n ext Friday. Outline. Nested records. Design exercise. How do we represent a line mathematically? How do we represent one in Ada?. Code. type Pair is record x : Integer; y : Integer ;

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about 'ITEC 320' - fuller

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

### ITEC 320

Lecture 9

Nested records / Packages

Review
• Project ?’s
• Records
• Exam 1 next Friday
Outline
• Nested records
Design exercise
• How do we represent a line mathematically?
• How do we represent one in Ada?
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);

• Nested records
• How is it implemented in Java?
• How is it implemented in Ada?
UML
• Composition
• Is a part of
• Aggregation
• Is contained in

University

Department

Faculty

Example

Arrays
• What does an array of records like Line mean in terms of
• Memory allocation
• Syntax
Real world example
• Points
• Lines
• Line collection / displayer
• Images
• Grid
Packages
• Records are one way of grouping data
• Packages are one way of grouping functions and procedures

Which one is Java?

Definitions
• Client
• Uses the class and its methods
• Routine or package that uses the types and routines defined in package P
• Examples
• Records store information
• Procedures work with records / other data
• Put the two together and you have the Ada version of classes
• Contain
• Procedures
• Types
Java

class Pair

{

intx, y;

intdistanceToOrigin() // 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());

}

}

• 2 pieces
• Record for a pair
• Function to calculate the distance to the origin
• File structure
• Specification
• Body

Specification
• Tells the compiler what the package does, but not how to do it

package PairPkg is

type Pair is record

x, y: Integer;

end record

function distanceToOrigin(p: Pair) return Integer;

end PairPkg;

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;

• Java classes (1 file)
• What are the benefits / downsides of each approach?
• How does Java sort of provide the same idea that Ada does?
Rules
• The specification must be followed to the letter
• All functions / procedures must be implemented
• All return / parameter types must match
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;

Specification
• Tells the compiler what the package does, but not how to do it

package PairPkg is

type Pair is private;

function distanceToOrigin(p: Pair) return Integer;

private

type Pair is record

x, y: Integer;

end record;

end PairPkg;

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;

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…
Review
• Nested records
• Intro to packages