cop3502 programming fundamentals for cis majors 1
DESCRIPTION
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 - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: COP3502 Programming Fundamentals for CIS Majors 1](https://reader031.vdocument.in/reader031/viewer/2022020418/5681602d550346895dcf3ed9/html5/thumbnails/1.jpg)
COP3502 Programming Fundamentals for CIS Majors 1
Instructor: Parisa Rashidi
![Page 2: COP3502 Programming Fundamentals for CIS Majors 1](https://reader031.vdocument.in/reader031/viewer/2022020418/5681602d550346895dcf3ed9/html5/thumbnails/2.jpg)
Exam Next Monday, March 26th
Same time (11:45 – 12:35), same location Chapters 7, 8, 10
No HW, No PA this week
Announcements
![Page 3: COP3502 Programming Fundamentals for CIS Majors 1](https://reader031.vdocument.in/reader031/viewer/2022020418/5681602d550346895dcf3ed9/html5/thumbnails/3.jpg)
Chapter 10 Immutable objects this keyword Composition Differences between procedural
programming & OOP Guidelines for OOP
Objectives
![Page 4: COP3502 Programming Fundamentals for CIS Majors 1](https://reader031.vdocument.in/reader031/viewer/2022020418/5681602d550346895dcf3ed9/html5/thumbnails/4.jpg)
More on Objects
![Page 5: COP3502 Programming Fundamentals for CIS Majors 1](https://reader031.vdocument.in/reader031/viewer/2022020418/5681602d550346895dcf3ed9/html5/thumbnails/5.jpg)
Immutable object: If the contents of an object cannot be
changed once the object is created Its class is called an immutable class.
Immutable
Circle3
![Page 6: COP3502 Programming Fundamentals for CIS Majors 1](https://reader031.vdocument.in/reader031/viewer/2022020418/5681602d550346895dcf3ed9/html5/thumbnails/6.jpg)
A class with all private data fields and without mutators is not necessarily immutable. Example: next slide (Student class)
Immutable
![Page 7: COP3502 Programming Fundamentals for CIS Majors 1](https://reader031.vdocument.in/reader031/viewer/2022020418/5681602d550346895dcf3ed9/html5/thumbnails/7.jpg)
Example..
public class BirthDate { private int year; private int month; private int day; public BirthDate(int newYear,int newMonth,int newDay) { year = newYear; month = newMonth; day = newDay; } public void setYear(int newYear) { year = newYear; }}
![Page 8: COP3502 Programming Fundamentals for CIS Majors 1](https://reader031.vdocument.in/reader031/viewer/2022020418/5681602d550346895dcf3ed9/html5/thumbnails/8.jpg)
Examplepublic class Student { private int id; private BirthDate birthDate;
public Student(int ssn,int year, int month, int day) { id = ssn; birthDate = new BirthDate(year, month, day); }
public int getId() { return id; }
public BirthDate getBirthDate() { return birthDate; }}
![Page 9: COP3502 Programming Fundamentals for CIS Majors 1](https://reader031.vdocument.in/reader031/viewer/2022020418/5681602d550346895dcf3ed9/html5/thumbnails/9.jpg)
Example …
9
public class Test { public static void main(String[] args) { Student student = new Student(111223333,1970, 5, 3); BirthDate date = student.getBirthDate(); date.setYear(2010); // Now the student birth year is // changed! }}
![Page 10: COP3502 Programming Fundamentals for CIS Majors 1](https://reader031.vdocument.in/reader031/viewer/2022020418/5681602d550346895dcf3ed9/html5/thumbnails/10.jpg)
For a class to be immutable, it must 1. Mark all data fields private 2. Provide no mutator methods 3. Provide no accessor methods that
would return a reference to a mutable data field object.
Immutable
![Page 11: COP3502 Programming Fundamentals for CIS Majors 1](https://reader031.vdocument.in/reader031/viewer/2022020418/5681602d550346895dcf3ed9/html5/thumbnails/11.jpg)
Scope
![Page 12: COP3502 Programming Fundamentals for CIS Majors 1](https://reader031.vdocument.in/reader031/viewer/2022020418/5681602d550346895dcf3ed9/html5/thumbnails/12.jpg)
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.
Variable Scope
![Page 13: COP3502 Programming Fundamentals for CIS Majors 1](https://reader031.vdocument.in/reader031/viewer/2022020418/5681602d550346895dcf3ed9/html5/thumbnails/13.jpg)
Example
Variable Scope
![Page 14: COP3502 Programming Fundamentals for CIS Majors 1](https://reader031.vdocument.in/reader031/viewer/2022020418/5681602d550346895dcf3ed9/html5/thumbnails/14.jpg)
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.
Variable Scope
![Page 15: COP3502 Programming Fundamentals for CIS Majors 1](https://reader031.vdocument.in/reader031/viewer/2022020418/5681602d550346895dcf3ed9/html5/thumbnails/15.jpg)
Example
Variable Scope
![Page 16: COP3502 Programming Fundamentals for CIS Majors 1](https://reader031.vdocument.in/reader031/viewer/2022020418/5681602d550346895dcf3ed9/html5/thumbnails/16.jpg)
this Keyword
![Page 17: COP3502 Programming Fundamentals for CIS Majors 1](https://reader031.vdocument.in/reader031/viewer/2022020418/5681602d550346895dcf3ed9/html5/thumbnails/17.jpg)
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.
this
![Page 18: COP3502 Programming Fundamentals for CIS Majors 1](https://reader031.vdocument.in/reader031/viewer/2022020418/5681602d550346895dcf3ed9/html5/thumbnails/18.jpg)
Using this to reference hidden fields
this
![Page 19: COP3502 Programming Fundamentals for CIS Majors 1](https://reader031.vdocument.in/reader031/viewer/2022020418/5681602d550346895dcf3ed9/html5/thumbnails/19.jpg)
Use this to call overloaded constructor
this
![Page 20: COP3502 Programming Fundamentals for CIS Majors 1](https://reader031.vdocument.in/reader031/viewer/2022020418/5681602d550346895dcf3ed9/html5/thumbnails/20.jpg)
Composition & Aggregation
![Page 21: COP3502 Programming Fundamentals for CIS Majors 1](https://reader031.vdocument.in/reader031/viewer/2022020418/5681602d550346895dcf3ed9/html5/thumbnails/21.jpg)
An object can contain another object.
The relationship between the two is called composition.
Composition
![Page 22: COP3502 Programming Fundamentals for CIS Majors 1](https://reader031.vdocument.in/reader031/viewer/2022020418/5681602d550346895dcf3ed9/html5/thumbnails/22.jpg)
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.
Aggregation
![Page 23: COP3502 Programming Fundamentals for CIS Majors 1](https://reader031.vdocument.in/reader031/viewer/2022020418/5681602d550346895dcf3ed9/html5/thumbnails/23.jpg)
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”.
Composition
![Page 24: COP3502 Programming Fundamentals for CIS Majors 1](https://reader031.vdocument.in/reader031/viewer/2022020418/5681602d550346895dcf3ed9/html5/thumbnails/24.jpg)
“a student has a name” A composition relationship
“a student has an address” An aggregation relationship
An address may be shared by several students.
Example
![Page 25: COP3502 Programming Fundamentals for CIS Majors 1](https://reader031.vdocument.in/reader031/viewer/2022020418/5681602d550346895dcf3ed9/html5/thumbnails/25.jpg)
UML composition & aggregation notation
UML
![Page 26: COP3502 Programming Fundamentals for CIS Majors 1](https://reader031.vdocument.in/reader031/viewer/2022020418/5681602d550346895dcf3ed9/html5/thumbnails/26.jpg)
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.
UML
![Page 27: COP3502 Programming Fundamentals for CIS Majors 1](https://reader031.vdocument.in/reader031/viewer/2022020418/5681602d550346895dcf3ed9/html5/thumbnails/27.jpg)
An aggregation relationship is usually represented as a data field in the aggregating class.
UML
![Page 28: COP3502 Programming Fundamentals for CIS Majors 1](https://reader031.vdocument.in/reader031/viewer/2022020418/5681602d550346895dcf3ed9/html5/thumbnails/28.jpg)
Aggregation may exist between objects of the same class.
UML
![Page 29: COP3502 Programming Fundamentals for CIS Majors 1](https://reader031.vdocument.in/reader031/viewer/2022020418/5681602d550346895dcf3ed9/html5/thumbnails/29.jpg)
Aggregation may exist between objects of the same class.
UML
![Page 30: COP3502 Programming Fundamentals for CIS Majors 1](https://reader031.vdocument.in/reader031/viewer/2022020418/5681602d550346895dcf3ed9/html5/thumbnails/30.jpg)
Chapter 10 Immutable objects this keyword Composition
Previously
![Page 31: COP3502 Programming Fundamentals for CIS Majors 1](https://reader031.vdocument.in/reader031/viewer/2022020418/5681602d550346895dcf3ed9/html5/thumbnails/31.jpg)
Class Abstraction
![Page 32: COP3502 Programming Fundamentals for CIS Majors 1](https://reader031.vdocument.in/reader031/viewer/2022020418/5681602d550346895dcf3ed9/html5/thumbnails/32.jpg)
Procedural programming Methods
OOP Entities grouping related methods
and data
OOP
![Page 33: COP3502 Programming Fundamentals for CIS Majors 1](https://reader031.vdocument.in/reader031/viewer/2022020418/5681602d550346895dcf3ed9/html5/thumbnails/33.jpg)
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.
Class Abstraction
![Page 34: COP3502 Programming Fundamentals for CIS Majors 1](https://reader031.vdocument.in/reader031/viewer/2022020418/5681602d550346895dcf3ed9/html5/thumbnails/34.jpg)
Example: Loan class
Class Abstraction
Loan
-annualInterestRate: double -numberOfYears: int -loanAmount: double -loanDate: Date +Loan() +Loan(annualInterestRate: double,
numberOfYears: int, loanAmount: double)
+getAnnualInterestRate(): double +getNumberOfYears(): int +getLoanAmount(): double +getLoanDate(): Date +setAnnualInterestRate( annualInterestRate: double): void +setNumberOfYears( numberOfYears: int): void +setLoanAmount( loanAmount: double): void +getMonthlyPayment(): double +getTotalPayment(): double
The annual interest rate of the loan (default: 2.5). The number of years for the loan (default: 1) The loan amount (default: 1000). The date this loan was created. Constructs a default Loan object. Constructs a loan with specified interest rate, years, and
loan amount. Returns the annual interest rate of this loan. Returns the number of the years of this loan. Returns the amount of this loan. Returns the date of the creation of this loan. Sets a new annual interest rate to this loan.
Sets a new number of years to this loan. Sets a new amount to this loan. Returns the monthly payment of this loan. Returns the total payment of this loan.
TestLoanClass
Run
Loan
![Page 35: COP3502 Programming Fundamentals for CIS Majors 1](https://reader031.vdocument.in/reader031/viewer/2022020418/5681602d550346895dcf3ed9/html5/thumbnails/35.jpg)
Example: BMI
Class Abstraction
UseBMIClass
Run
BMI
BMI
-name: String -age: int -weight: double -height: double +BMI(name: String, age: int, weight:
double, height: double) +BMI(name: String, weight: double,
height: double) +getBMI(): double +getStatus(): String
The name of the person. The age of the person. The weight of the person in pounds. The height of the person in inches. Creates a BMI object with the specified
name, age, weight, and height. Creates a BMI object with the specified
name, weight, height, and a default age 20.
Returns the BMI Returns the BMI status (e.g., normal,
overweight, etc.)
The get methods for these data fields are provided in the class, but omitted in the UML diagram for brevity.
![Page 36: COP3502 Programming Fundamentals for CIS Majors 1](https://reader031.vdocument.in/reader031/viewer/2022020418/5681602d550346895dcf3ed9/html5/thumbnails/36.jpg)
Example: Course
Class Abstraction
Course
-name: String -students: String[] -numberOfStudents: int
+Course(name: String) +getName(): String +addStudent(student: String): void +getStudents(): String[] +getNumberOfStudents(): int
The name of the course. The students who take the course. The number of students (default: 0).
Creates a Course with the specified name. Returns the course name. Adds a new student to the course list. Returns the students for the course. Returns the number of students for the course.
TestCource
Course
![Page 37: COP3502 Programming Fundamentals for CIS Majors 1](https://reader031.vdocument.in/reader031/viewer/2022020418/5681602d550346895dcf3ed9/html5/thumbnails/37.jpg)
Example: Designing Stack
Class Abstraction
xYZ
Push Pop
![Page 38: COP3502 Programming Fundamentals for CIS Majors 1](https://reader031.vdocument.in/reader031/viewer/2022020418/5681602d550346895dcf3ed9/html5/thumbnails/38.jpg)
Example: Stack
Class Abstraction
RunTestStackOfIntegers
StackOfIntegers
-elements: int[] -size: int
+StackOfIntegers() +StackOfIntegers(capacity: int) +empty(): boolean +peek(): int
+push(value: int): int +pop(): int +getSize(): int
An array to store integers in the stack.
The number of integers in the stack.
Constructs an empty stack with a default capacity of 16. Constructs an empty stack with a specified capacity. Returns true if the stack is empty. Returns the integer at the top of the stack without
removing it from the stack. Stores an integer into the top of the stack. Removes the integer at the top of the stack and returns it. Returns the number of elements in the stack.
![Page 39: COP3502 Programming Fundamentals for CIS Majors 1](https://reader031.vdocument.in/reader031/viewer/2022020418/5681602d550346895dcf3ed9/html5/thumbnails/39.jpg)
Example: Designing Stack
Class Abstraction
StackOfIntegers
![Page 40: COP3502 Programming Fundamentals for CIS Majors 1](https://reader031.vdocument.in/reader031/viewer/2022020418/5681602d550346895dcf3ed9/html5/thumbnails/40.jpg)
Example: Guess Date
Class Abstraction
UseGuessDateClass RunGuessDate
![Page 41: COP3502 Programming Fundamentals for CIS Majors 1](https://reader031.vdocument.in/reader031/viewer/2022020418/5681602d550346895dcf3ed9/html5/thumbnails/41.jpg)
Class Design Guidelines
![Page 42: COP3502 Programming Fundamentals for CIS Majors 1](https://reader031.vdocument.in/reader031/viewer/2022020418/5681602d550346895dcf3ed9/html5/thumbnails/42.jpg)
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.
Guideline
![Page 43: COP3502 Programming Fundamentals for CIS Majors 1](https://reader031.vdocument.in/reader031/viewer/2022020418/5681602d550346895dcf3ed9/html5/thumbnails/43.jpg)
A single entity with too many responsibilities can be broken into several classes to separate responsibilities.
Guideline
![Page 44: COP3502 Programming Fundamentals for CIS Majors 1](https://reader031.vdocument.in/reader031/viewer/2022020418/5681602d550346895dcf3ed9/html5/thumbnails/44.jpg)
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.
Guideline
![Page 45: COP3502 Programming Fundamentals for CIS Majors 1](https://reader031.vdocument.in/reader031/viewer/2022020418/5681602d550346895dcf3ed9/html5/thumbnails/45.jpg)
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.
Guideline
![Page 46: COP3502 Programming Fundamentals for CIS Majors 1](https://reader031.vdocument.in/reader031/viewer/2022020418/5681602d550346895dcf3ed9/html5/thumbnails/46.jpg)
Provide a public no-arg constructor and override the equals method and the toString method defined in the Object class whenever possible.
Guideline
![Page 47: COP3502 Programming Fundamentals for CIS Majors 1](https://reader031.vdocument.in/reader031/viewer/2022020418/5681602d550346895dcf3ed9/html5/thumbnails/47.jpg)
Follow standard Java programming style and naming conventions.
Choose informative names for classes, data fields, and methods.
Guideline
![Page 48: COP3502 Programming Fundamentals for CIS Majors 1](https://reader031.vdocument.in/reader031/viewer/2022020418/5681602d550346895dcf3ed9/html5/thumbnails/48.jpg)
Always place the data declaration before the constructor, and place constructors before methods.
Always provide a constructor and initialize variables to avoid programming errors.
Guideline
![Page 49: COP3502 Programming Fundamentals for CIS Majors 1](https://reader031.vdocument.in/reader031/viewer/2022020418/5681602d550346895dcf3ed9/html5/thumbnails/49.jpg)
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).
Guideline
![Page 50: COP3502 Programming Fundamentals for CIS Majors 1](https://reader031.vdocument.in/reader031/viewer/2022020418/5681602d550346895dcf3ed9/html5/thumbnails/50.jpg)
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.
Guideline
![Page 51: COP3502 Programming Fundamentals for CIS Majors 1](https://reader031.vdocument.in/reader031/viewer/2022020418/5681602d550346895dcf3ed9/html5/thumbnails/51.jpg)
A property (data field) that is shared by all the instances of the class should be declared as a static property.
Guideline