Presentation is loading. Please wait.

Presentation is loading. Please wait.

ITEC 320 Lecture 12 Records. Intro to records Review Look_ahead(char,EOL); –What is this? –What does it allow us to do?

Similar presentations


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:

1 ITEC 320 Lecture 12 Records

2 Intro to records Review Look_ahead(char,EOL); –What is this? –What does it allow us to do?

3 Intro to records Outline Records –Moving beyond the basics What comes to mind when you hear the term record?

4 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?

5 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();

6 Intro to records Records & Classes Back to types (sort of) Composite –What comes to mind when you hear this? Main difference –Operations (packages) –Similarities

7 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?

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

9 Intro to records Memory Not from a musical… Java –Reference –New Ada –No references –You get what you ask for

10 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?

11 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);

12 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?

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

14 Intro to records Swapping In Java –? In Ada –?

15 Intro to records Functions / Procedures Can we send pair as a parameter? –Why? What does this mean for memory?

16 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 "+";

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

18 Intro to records Summary Records –Storing related data –Next time: Nesting Example program


Download ppt "ITEC 320 Lecture 12 Records. Intro to records Review Look_ahead(char,EOL); –What is this? –What does it allow us to do?"

Similar presentations


Ads by Google