1 / 51

COP3502 Programming Fundamentals for CIS Majors 1

COP3502 Programming Fundamentals for CIS Majors 1. Instructor: Parisa Rashidi. Exam Next Monday, March 26 th Same time (11:45 – 12:35), same location Chapters 7, 8, 10 No HW, No PA this week. Chapter 10 Immutable objects t his keyword Composition

lilia
Download Presentation

COP3502 Programming Fundamentals for CIS Majors 1

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. COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi

  2. Exam • Next Monday, March 26th • Same time (11:45 – 12:35), same location • Chapters 7, 8, 10 • No HW, No PA this week

  3. Chapter 10 • Immutable objects • this keyword • Composition • Differences between procedural programming & OOP • Guidelines for OOP

  4. More on Objects

  5. Immutable object: • If the contents of an object cannot be changed once the object is created • Its class is called an immutable class. Circle3

  6. A class with all private data fields and without mutators is not necessarily immutable. • Example: next slide (Student class)

  7. public class BirthDate {private intyear;private intmonth;private intday;public BirthDate(intnewYear,intnewMonth,intnewDay) { year = newYear; month = newMonth; day = newDay; }public void setYear(intnewYear) { year = newYear; }}

  8. public class Student {private intid;privateBirthDatebirthDate;publicStudent(intssn,intyear, int month, int day) { id = ssn;birthDate = newBirthDate(year, month, day); }public intgetId() {return id; }publicBirthDategetBirthDate() {returnbirthDate; }}

  9. public class Test {publicstatic void main(String[] args) { Student student = newStudent(111223333,1970, 5, 3);BirthDate date = student.getBirthDate();date.setYear(2010); // Now the student birth year is // changed! }} 9

  10. For a class to be immutable, it must • Mark all data fields private • Provide no mutator methods • Provide no accessor methods that would return a reference to a mutable data field object.

  11. Scope

  12. The scope of instance and static data fields is the entire class. • They can be declared anywhere inside a class. • The scope of a local variable starts from its declaration and continues to the end of the block that contains the variable. • A local variable must be initialized explicitly before it can be used.

  13. Example

  14. If a local variable has the same name as a class’s variable, the local variable takes precedence and the class’s variable with the same name is hidden.

  15. Example

  16. this Keyword

  17. The this keyword is the name of a reference that refers to an object itself. • One common use of the this keyword is reference a class’s hidden data fields. • Another common use of the this keyword to enable a constructor to invoke another constructor of the same class.

  18. Using thisto reference hidden fields

  19. Use thisto call overloaded constructor

  20. Composition & Aggregation

  21. An object can contain another object. • The relationship between the two is called composition.

  22. Composition is a special case of the “aggregation” relationship. • Aggregation models “has-a” relationships. • The owner object is called an aggregating object. • The subject object is called an aggregated object.

  23. An object may be owned by several other aggregating objects. • If an object is exclusively owned by an aggregating object, the relationship between them is referred to as “composition”.

  24. “a student has a name” • A composition relationship • “a student has an address” • An aggregation relationship • An address may be shared by several students.

  25. UML composition & aggregation notation

  26. Each class involved in a relationship may specify a multiplicity. • A multiplicity could be a number or an interval that specifies how many objects of the class are involved in the relationship. • The character * means an unlimited number of objects • The interval m..n means that the number of objects should be between m and n, inclusive.

  27. An aggregation relationship is usually represented as a data field in the aggregating class.

  28. Aggregation may exist between objects of the same class.

  29. Aggregation may exist between objects of the same class.

  30. Chapter 10 • Immutable objects • this keyword • Composition

  31. Class Abstraction

  32. Procedural programming • Methods • OOP • Entities grouping related methods and data

  33. Class abstraction means to separate class implementation from the use of the class. • The user of the class does not need to know how the class is implemented.

  34. Loan • Example: Loan class TestLoanClass Run

  35. BMI UseBMIClass • Example: BMI Run

  36. Course TestCource • Example: Course

  37. Example: Designing Stack Pop Push Z Y x

  38. Example: Stack Run TestStackOfIntegers

  39. Example: Designing Stack StackOfIntegers

  40. Example: Guess Date GuessDate UseGuessDateClass Run

  41. Class Design Guidelines

  42. Coherence • A class should describe a single entity • All the class operations should support a coherent purpose. • You can use a class for students, for example, but you should not combine students and staff in the same class, because students and staff have different entities.

  43. A single entity with too many responsibilities can be broken into several classes to separate responsibilities.

  44. Classes are designed for reuse. Users can incorporate classes in many different combinations, orders, and environments. • Therefore, you should design a class that imposes no restrictions on what or when the user can do with it.

  45. Design the properties to ensure that the user can set properties in any order, with any combination of values. • Design methods to function independently of their order of occurrence.

  46. Provide a public no-arg constructor and override the equals method and the toString method defined in the Object class whenever possible.

  47. Follow standard Java programming style and naming conventions. • Choose informative names for classes, data fields, and methods.

  48. Always place the data declaration before the constructor, and place constructors before methods. • Always provide a constructor and initialize variables to avoid programming errors.

  49. Make the fields private and accessor methods public if they are intended for the users of the class. • Make the fields or method protected if they are intended for extenders of the class (more on extension and inheritance later).

  50. You can use get methods and set methods to provide users with access to the private data, but only to private data you want the user to see or to modify. • A class should also hide methods not intended for client use.

More Related