1 / 26

CSE115: Introduction to Computer Science I

CSE115: Introduction to Computer Science I. Dr. Carl Alphonce 343 Davis Hall 645-4739 alphonce@buffalo.edu. Phones off & away Name signs out. Agenda. Announcements Cell phones / laptops off & away / Name signs out FAIR WARNING: First exam on Wednesday, Feb 15

coby
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 343 Davis Hall 645-4739 alphonce@buffalo.edu

  2. Phones off & away Name signs out

  3. Agenda • Announcements • Cell phones / laptops off & away / Name signs out • FAIR WARNING: First exam on Wednesday, Feb 15 • material up to Friday, Feb 10 / Mon Feb 13 is review • Last time • Syntax review (quick) / Demo • Relationships • in model and code • first relationship: composition • Today • Lifetime/Scope • process memory • Unified Modeling Language (UML) • composition

  4. RELATIONSHIPS

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

  6. Computing and Clifford

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

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

  9. Variables’ properties • name • location • type • value (contents) • scope • lifetime

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

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

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

  13. Variables’ properties • name • location • type • value (contents) • scope • lifetime

  14. 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)

  15. Lifetime of a local variable • A local variable comes into existence when a method is called, and disappears when the method is completed.

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

  17. Memory organization Process A Process B Process C

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

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

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

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

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

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

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

  25. Instance variable declaration • An instance variable declaration consists of: • an access control modifier • a type • a name • a terminating ‘;’

  26. Dog – Tail exampleexpressing it in Java public class Dog { private Tail _tail; public Dog() { _tail = new Tail(); } }

More Related