1 / 41

CSE115 / CSE503 Introduction to Computer Science I

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

vanya
Download Presentation

CSE115 / CSE503 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/ CSE503Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall alphonce@buffalo.edu

  2. cell phones off laptops away name signs out

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

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

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

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

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

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

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

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

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

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

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

  18. Memory organization Process A Process B Process C

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

More Related