510 likes | 670 Views
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
E N D
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi
Exam • Next Monday, March 26th • Same time (11:45 – 12:35), same location • Chapters 7, 8, 10 • No HW, No PA this week
Chapter 10 • Immutable objects • this keyword • Composition • Differences between procedural programming & OOP • Guidelines for OOP
Immutable object: • If the contents of an object cannot be changed once the object is created • Its class is called an immutable class. Circle3
A class with all private data fields and without mutators is not necessarily immutable. • Example: next slide (Student class)
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; }}
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; }}
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
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.
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.
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.
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.
An object can contain another object. • The relationship between the two is called composition.
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.
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”.
“a student has a name” • A composition relationship • “a student has an address” • An aggregation relationship • An address may be shared by several students.
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.
An aggregation relationship is usually represented as a data field in the aggregating class.
Chapter 10 • Immutable objects • this keyword • Composition
Procedural programming • Methods • OOP • Entities grouping related methods and data
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.
Loan • Example: Loan class TestLoanClass Run
BMI UseBMIClass • Example: BMI Run
Course TestCource • Example: Course
Example: Designing Stack Pop Push Z Y x
Example: Stack Run TestStackOfIntegers
Example: Designing Stack StackOfIntegers
Example: Guess Date GuessDate UseGuessDateClass Run
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.
A single entity with too many responsibilities can be broken into several classes to separate responsibilities.
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.
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.
Provide a public no-arg constructor and override the equals method and the toString method defined in the Object class whenever possible.
Follow standard Java programming style and naming conventions. • Choose informative names for classes, data fields, and methods.
Always place the data declaration before the constructor, and place constructors before methods. • Always provide a constructor and initialize variables to avoid programming errors.
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).
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.