Nested Records and Nested Composite Types
Nested Composite Type
- Primitive type:
- Has no internal structure
- A variable holds a single value
- Examples: Integer, float, character
- Composite Type:
- Has internal structure
- Examples: arrays and records (and classes)
- Nested Composite Type:
- Some or all fields are other composite types
Example: Class Line
- A Line is defined by two points
- A type for a line contains a field for each point
- In Java, we implement a line with two pair fields
- In Ada, we implement a line with nested records
Example: Class Line
- Let's implement type Line in Java
- A line segment is modeled using a two Pairs
- Each line has has a start Pair and an end Pair
class Pair{
int x;
int y;
Pair(int x, int y){
this.x = x; this.y = y;
}
...
}
class Line{
Pair start;
Pair end;
...
}
...
// CLIENT
Line myLine;
myLine = new Line();
myLine.start = new Pair(1, 2):
myLine.end = new Pair(3, 4):
S.o.p(myLine.start.x);
S.o.p(myLine.end.x);
Type Line in Ada
- Let's implement type Line with nested records
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);
Composite Objects: Reference and Value Semantics
-
In a language that has reference semantics (eg in java)
the fields of a composite object are actually references
to other objects.
- Notice the difference between what is in a Line object in Java
and Ada
- Java: Two pointers
- Ada: Four integers
- Notation for access is identical, but memory allocation and
actions required for data access are significantly different
- In java, composite objects always have reference
semantics (ie have fields that are pointers).
- In Ada and C++, composite objects can have either
reference or value semantics (ie the fields can be either
values or pointers).
Composition vs Aggregation
- In Object Modeling (eg UML) we have two ways of making one type
part of another
- Composition: the nested object cannot be separated
- Aggregation: the nested object has a separate existance
- Example:
- University
- Department
- Faculty
- If the university no longer exists, then neither do the departments
- University is a composition of department
- Even if the university is destroyed, the faculty still exist
- Department is a aggregation of faculty
- Composition can be naturally implemented with nested records
- Composition can be naturally implemented with references
Example: An Array of Lines
- What does an array of lines look like:
- Java:
// Declaration and allocation of the array
Line[] myArray;
myArray = new Line[3];
// Initialize
...
- Ada:
-- Declaration of the array type
type LineArray is array(1..3) of Line;
-- Declaration and allocation of the array
myArray: LineArray;
-- Initialize
...
- Consider both syntax and memory allocation of myArray
- Consider both syntax and actions of accessing myArray
Example: A List of Lines
- What would a data structure look like for a list of lines
- The list would contain the following:
- Array of lines
- Count of the number of valid lines in the array
- An almost complete solution: linelist.adb
(and prettified)
- Contains routines for reading a list of lines from input and for
creating a list of random lines.
Class Project: A List of Unique Words and their Frequencies
- Input: list of words, one word per line, each word is shorter than 80 characters
- Output: Print list of unique words and their frequecies
- Constraint: main is two lines:
l: WhatTypeIsThis;
begin
getWords(l);
putWordsAndFreqs(l);
end words;
What structure would prove useful?
Partial Roadmap:
- Design types (draw pictures)
- Read and write all words, ignoring test for uniqueness
- Enter all words into array and print the array
A Type for Words
- Define a new type Word: to be a String and a length
- A Word has a String and a length
- How would we define the data type
- What operations for words might we want?
- e...
- p...
- pl...
- ne...
- to...
- le...
- trysimpleword.adb (prettified)
- Last question: Wouldn't we want to encapsulate the word type?
- Answer: Yes, package are our next topic