CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
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 about regrading
Agenda Brief review Unified Modeling Language (UML) Methods in general
BRIEF REVIEW
Object class instantiation created by ‘new’ + constructor representation stored on heap
Reference an address value of ‘new’ expression
Variable name location(LHS interp) type value(RHS interp) scope lifetime local variable instance variable
Variables compared
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?)
package organizational mechanism each class belongs to a package
class “blueprint” of an object
constructor used by new to initialize a new object
scope static property of variable part of program text where a variable declaration holds
lifetime runtime property of variable period of time during which variable exists in memory
UML Unified Modeling Language –industry standard Class diagrams –used to highlight design of software Object diagrams –used to highlight runtime behavior
UML Unified Modeling Language –express design without reference to an implementation language For example
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
package cse115; public class Dog { public Dog() { } package cse115; public class Tail { public Tail() { }
package cse115; public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); } package cse115; public class Tail { public Tail() { }
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.
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() { }
METHODS
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
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.