410 likes | 478 Views
CSE115 / CSE503 Introduction to Computer Science I. Dr. Carl Alphonce 343 Davis Hall alphonce@buffalo.edu. cell phones off laptops away name signs out. Announcements. Exam #1 G rades are in UBLearns (as a %) Exams will be handed back in recitation
E N D
CSE115/ CSE503Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall alphonce@buffalo.edu
cell phones off laptops away name signs out
Announcements • Exam #1 • Grades are in UBLearns (as a %) • Exams will be handed back in recitation • Q4 dropped if that helped your grade • Range: 5-100 Average (mean): 73.7 Median: 77.5 • Will go over early next week (if not before) • Lab 3 in recitation this week
Roadmap • Composition relationship • Lifetime/Scope • Unified Modeling Language (UML) • Naming conventions • Methods (without parameters) • Calling methods (local variables & instance variables) • ‘this’ (connection between stack and heap) • Association relationship • Methods (with parameters)
Relationships in model and code • relationships exist between objects in problem domains • want to capture those relationships in our models and express them in our code
Composition • A whole-part relationship (e.g. Dog-Tail) • Whole and part objects have same lifetime • when whole is created, it has its parts • when whole is destroyed, parts go away too
whole/part creation in code • Whole creates instance of part in its constructor • In Java code, involves 3 changes to whole class: • Declaration of variable of part type • Instantiation of part class in whole class constructor • Assignment of new part instance to variable
Detour • Before looking at how we express Dog-Tail relationship in code, we need to make a little detour to discuss the last two properties of variables: • scope • lifetime
Variables’ properties • name • location • type • value (contents) • scope • lifetime
Variable scope • The scope of a variable is the part of a program where a variable declaration is in effect. • Variables declared in different ways have different scope.
Scope of a local variable • A variable declared within a constructor (method) is called a local variable. • The scope of a local variable is from the point of the declaration to the end of the method body.
Scope of an instance variable • A variable declared within a class but outside of any method is called an instance variable. • The scope of an instance variable is the entire class body.
Variables’ properties • name • location • type • value (contents) • scope • lifetime
Lifetime of a variable • The period of time during execution of a program that the variable exists in memory. • This is a dynamic property (one relating to runtime)
Lifetime of a local variable • A local variable comes into existence when a method is called, and disappears when the method is completed.
Lifetime of an instance variable • Instance variables are created when a class is instantiated. • Each object has its own set of instance variables. • Instance variables persist as long as their objects persist • as far as we know right now, objects persist until the end of the runtime of the program.
Memory organization Process A Process B Process C
Memory organization Process A Process B Process C STATIC SEGMENT RUNTIME STACK FREE/AVAILABLE MEMORY HEAP dynamically allocated memory
whole/part creation in code • Whole creates instance of part in its constructor • In Java code, involves 3 changes to whole class: • Declaration of variable of part type • Instantiation of part class in whole class constructor • Assignment of new part instance to variable
whole/part creation in code(elaborated) • Whole creates instance of part in its constructor • In Java code, involves 3 changes to whole class: • Declaration of instance variable of part type • Instantiation of part class in whole class constructor • Assignment of new part instance to instance variable
Important points about composition • Whole has responsibility for creating its parts (which is why instantiation of parts happens in constructor of whole). • Whole can communicate with parts. This is why an instance variable is declared: to establish a name for the newly created object.
Class members:(instance) methods & instance variables • Any class member (method or variable declared in the class body, but not inside a method) must have an access control modifier.
Access Control Modifiers • “public” – the member can be accessed from outside the class • “private” – the member can be access only from inside the class
Class members:(instance) methods & instance variables • Our rule: • methods are public • instance variables are private. • Later in semester we will justify this rule (one we know a little more about the issues involved)
Instance variable declaration • An instance variable declaration consists of: • an access control modifier • a type • a name • a terminating ‘;’
Dog – Tail example in Java public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); } }
Dog – Tail example in Java public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); } } Class definition is shown in green:
Dog – Tail example in Java public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); } } Instance variable name is shown in green:
Dog – Tail example in Java public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); } } Instance variable declaration is shown in green:
Dog – Tail example in Java Access control modifiers are shown in green: Note that access control modifier of _tail isprivate, notpublic. public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); } }
Dog – Tail example in Java public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); } } Constructor definition is shown in green:
Dog – Tail example in Java public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); } } Header of constructor definition is shown in green:
Dog – Tail example in Java public class Dog { private Tail _tail; public Dog(){ _tail = new Tail(); } } Access control modifier in header of constructor definition is shown in green:
Dog – Tail example in Java public class Dog { private Tail _tail; public Dog(){ _tail = new Tail(); } } Name of constructor in header of constructor definition is shown in green:
Dog – Tail example in Java public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); } } Parameter list in header of constructor definition is shown in green:
Dog – Tail example in Java public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); } } Instantiation of class Tail is shown in green:
Dog – Tail example in Java public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); } } ‘new’ operator in instantiation of class Tail is shown in green:
Dog – Tail example in Java public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); } } Use of constructor in instantiation of Tail class is shown in green:
Dog – Tail example in Java public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); } } Argument list in instantiation of class Tail is shown in green:
Dog – Tail example in Java public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); } } Assignment of new Tail instance to instance variable is shown in green: