1 / 33

CSE115: Introduction to Computer Science I

CSE115: Introduction to Computer Science I. Dr. Carl Alphonce 219 Bell Hall 645-4739 alphonce@buffalo.edu. Phones off Signs out Picnic on Friday . Agenda. Relationships in model and code first relationship: composition Lifetime/Scope process memory Unified Modeling Language (UML)

jolene
Download Presentation

CSE115: Introduction to Computer Science I

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Phones off Signs out Picnic on Friday 

  3. Agenda • Relationships • in model and code • first relationship: composition • Lifetime/Scope • process memory • Unified Modeling Language (UML) • Composition

  4. Relationships in model and code • relationships exist between objects in problem domains • must express these relationships in our code

  5. 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 (we’ll talk about object destruction later)

  6. Lifetime of a variable • Time during execution of a program that the variable exists. • A local variable comes into existence when a method is called, and disappears when the method is completed. • Instance variables are created when a class is instantiated. Instance variables persist as long as their objects persist.

  7. Memory organization Process A Process B Process C

  8. Memory organization Process A Process B Process C STATIC SEGMENT RUNTIME STACK FREE/AVAILABLE MEMORY HEAP dynamically allocated memory

  9. Local variables • Declared inside a method • Scope is: • from point of declaration • to end of method body • Lifetime is: • from method invocation • to method return

  10. Instance variables • A variable declared as a class member (i.e. within the class body but not within any method) is called an instance variable. • The scope of an instance variable is the entire class body. • Each instance of a class has its own set of instance variables.

  11. 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.

  12. 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. • 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)

  13. Instance variable declaration • An instance variable declaration consists of an access control modifier in addition to a type and a name. • A rule in CSE115 is that all instance variables must be declared using the “private” access control modifier

  14. Access Control Modifiers • “public” – the member can be accessed from outside the class body • “private” – the member can be access only from inside the class body

  15. Dog – Tail example in Java public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); } }

  16. Dog – Tail example in Java public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); } } Class definition is shown in green:

  17. Dog – Tail example in Java public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); } } Instance variable name is shown in green:

  18. Dog – Tail example in Java public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); } } Instance variable declaration is shown in green:

  19. 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(); } }

  20. Dog – Tail example in Java public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); } } Constructor definition is shown in green:

  21. Dog – Tail example in Java public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); } } Header of constructor definition is shown in green:

  22. 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:

  23. 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:

  24. 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:

  25. Dog – Tail example in Java public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); } } Instantiation of class Tail is shown in green:

  26. 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:

  27. 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:

  28. 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:

  29. 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:

  30. UML Unified Modeling Language

  31. UML • Unified Modeling Language • express design without reference to an implementation language • For example

  32. Composition in UML

More Related