Presentation on theme: "ITEC 320 Lecture 12 Records. Intro to records Review Look_ahead(char,EOL); –What is this? –What does it allow us to do?"— Presentation transcript:
ITEC 320 Lecture 12 Records
Intro to records Review Look_ahead(char,EOL); –What is this? –What does it allow us to do?
Intro to records Outline Records –Moving beyond the basics What comes to mind when you hear the term record?
Intro to records Visually How would you draw a representation of your ITEC 120, 220 programs? How would you draw a representation of P1? What are the differences? Is it just functions?
Intro to records Java’s NameValuePai r Contains a String named id Contains an Any named pair; What can this be used for? –How do you create one? –What about the memory for it? –What about = and == Have you ever written a class like this? What are the benefits / downsides of this class? Side note: value.extract_Object(); value.extract_double();
Intro to records Records & Classes Back to types (sort of) Composite –What comes to mind when you hear this? Main difference –Operations (packages) –Similarities
Intro to records Pair What would the Java code for a class that encapsulates a pair of numbers look like? How would you use such a class?
Intro to records Ada Records –Advantages –Disadvantages –Comparison to Java procedure PairDemo is type Pair is record x: Integer; y: Integer; end record; p1: Pair; begin p1.x := 3; p1.y := 4; put(p1.x); put(p1.y); end PairDemo;
Intro to records Memory Not from a musical… Java –Reference –New Ada –No references –You get what you ask for
Intro to records Initial values Consider the following Pair p1; p1 = new Pair(); System.out.println(p1.x); System.out.println(p1.y); p1: Pair; begin put(p1.x); put(p1.y); And How do you fix this?
Intro to records Assignme nt Multiple ways to assign values p1: Pair := (5, 6); begin put(p1.x); p1 := (7, 8); put(p1.y); p1 := (y => 10, x =>9); p1 := (others => 0);
Intro to records Assignment / Equality Java Pair p1, p2; p1 = new Pair(); p1.x = 3; p1.y = 4; p2 = p1; if (p1 == p2)... // True or false? p1.x = 5; System.out.println(p2.x); if (p1 == p2)... // True or false?
Intro to records Assignment / Equality Ada procedure example is type Pair is record x: Integer; y: Integer; end record; p1, p2: Pair; begin p1 := (3, 4); p2 := p1; if p1 = p2 then-- True or false? p1.x := 5; end if; put(p2.x); if p1 = p2 then -- True or false? put(“Equal”); end if; end example;
Intro to records Swapping In Java –? In Ada –?
Intro to records Functions / Procedures Can we send pair as a parameter? –Why? What does this mean for memory?
Intro to records Operators Buildup with Ada.Integer_Text_IO; use Ada.Integer_Text_IO; with Ada.Text_IO; use Ada.Text_IO; procedure tryRecords2 is type Pair is record x: Integer; y: Integer; end record; procedure putPair(p: Pair) is begin put(p.x); put(p.y); new_line; end putPutPair function "+"(a, b: Pair) return Pair is answer: Pair; begin answer.x := a.x + b.x; answer.y := a.y + b.y; return answer; end "+";
Intro to records Operators Usage p1: Pair := (1, 2); p2: Pair := (3, 4); p3: Pair; begin p3 := "+"(p1, p2); putPair(p3); p1 := p1 + p1; putPair(p1); if p1 = p2 then put_line("Same"); else put_line("Different"); end if; end tryRecords2;
Intro to records Summary Records –Storing related data –Next time: Nesting Example program