Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall 645-4739 1.

Similar presentations


Presentation on theme: "CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall 645-4739 1."— Presentation transcript:

1 CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall 645-4739 alphonce@buffalo.edu 1

2

3

4 Announcements Advice –take notes in lecture –don’t rely only on slides –REVIEW notes regularly –bring notes to recitation –ask questions whenever you have them! Web-CAT –don’t worry if you got an e-mail about regrading

5 Agenda Brief review Unified Modeling Language (UML) Methods in general

6 BRIEF REVIEW

7 Object class instantiation created by ‘new’ + constructor representation stored on heap

8 Reference an address value of ‘new’ expression

9 Variable name location(LHS interp) type value(RHS interp) scope lifetime local variable instance variable

10 Variables compared

11 Expression examples of expressions –‘new’ expression –variable expression (on RHS of assignment) example1.Terrarium t1; example1.Terrarium t2; t1 = new example1.Terrarium(); t2 = t1;(Q: What does this do?)

12 package organizational mechanism each class belongs to a package

13 class “blueprint” of an object

14 constructor used by new to initialize a new object

15 scope static property of variable part of program text where a variable declaration holds

16 lifetime runtime property of variable period of time during which variable exists in memory

17 UML Unified Modeling Language –industry standard Class diagrams –used to highlight design of software Object diagrams –used to highlight runtime behavior

18 UML Unified Modeling Language –express design without reference to an implementation language For example

19 Binary Class Relationships: directional binary  two classes are involved – source class has code modification – target class does not composition –source: WHOLE –target: PART in diagram: –line decoration is on source/WHOLE –show only detail that’s needed/desired

20 package cse115; public class Dog { public Dog() { } package cse115; public class Tail { public Tail() { }

21 package cse115; public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); } package cse115; public class Tail { public Tail() { }

22 Questions from lecture Can you define more than one instance variable in a class definition? Can Dog have a composition relationship with other classes too? ANSWER: Yes! See example on next slide (which shows Dog – Nose composition). Also talked about Dog-Paw composition of cardinality 4.

23 package cse115; public class Dog { private Tail _tail; private Nose _nose; public Dog() { _tail = new Tail(); _nose = new Nose(); } package cse115; public class Tail { public Tail() { }

24 METHODS

25 package lab2; public class EcoSystem { private example1.Terrarium _t; public EcoSystem() { _t = new example1.Terrarium(); } public void addTwoCaterpillars() { example1.Caterpillar c1 = new example1.Caterpillar(); example1.Caterpillar c2 = new example1.Caterpillar(); _t.add(c1); _t.add(c2); c1.start(); c2.start(); } Constructor definition Method definition

26 package lab2; public class EcoSystem { private example1.Terrarium _t; public EcoSystem() { _t = new example1.Terrarium(); } public void addTwoCaterpillars() { example1.Caterpillar c1 = new example1.Caterpillar(); example1.Caterpillar c2 = new example1.Caterpillar(); _t.add(c1); _t.add(c2); c1.start(); c2.start(); } The scope of an instance variable declaration is the entire class body. The variable can (and should) be assigned an initial value (initialized) in… The variable can be used in any method of the class. Here’s a method definition. Both the constructor and this method are in the scope of the instance variable declaration. The instance variable can (and should) be assigned an initial value (initialized) in… the constructor. It can be used in any method in class body.


Download ppt "CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall 645-4739 1."

Similar presentations


Ads by Google