COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.

Slides:



Advertisements
Similar presentations
Chapter 10 THINKING IN OBJECTS 1 Object Oriented programming Instructor: Dr. Essam H. Houssein.
Advertisements

Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 11 Object-Oriented.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 7 Objects and Classes.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 1 Object-Oriented.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Immutable Objects and Classes.
Liang,Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 10 Object-Oriented Modeling.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 7 Objects and Classes.
1 Object-Oriented Design. 2 Objectives F To become familiar with the process of program development. F To the relationship types: association, aggregation,
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 9 Objects and Classes.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 9 Thinking in Objects.
Chapter 9 Objects and Classes
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 12 Object-Oriented Design.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 10 Thinking in Objects.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 12 Object-Oriented.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Objects and Classes.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 10 Thinking in Objects.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Object-Oriented Modeling Chapter 10 CSCI CSCI 1302 – Object-Oriented Modeling2 Outline The Software Development Process Discovering Relationships.
Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design Guidelines.
Objects and Classes Chapter 6 CSCI CSCI 1302 – Objects and Classes2 Outline Introduction Defining Classes for Objects Constructing Objects Accessing.
Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 1 Chapter 13 Abstract Classes and Interfaces.
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
Programming Fundamentals I (COSC- 1336), Lecture 10 (prepared after Chapter 10 of Liang’s 2011 textbook) Stefan Andrei 12/18/20151 COSC-1336, Lecture 10.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Objects and Classes.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 7 Objects and Classes.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 11 Object-Oriented.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 1 Chapter 13 Abstract Classes and Interfaces.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Object-Oriented Design.
1 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 10 More on Objects and Classes.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 10 More on Objects and Classes.
Introduction To Objects Oriented Programming Instructor: Mohammed Faisal.
Chapter 7 Objects and Classes. OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object represents an entity.
CS 112 Programming 2 Lecture 02 Objects and Classes (1)
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 7 Objects and Classes.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and Polymorphism.
Object-Oriented Programming in Java. 2 CS2336: Object-Oriented Programming in Java Buzzwords interfacejavadoc encapsulation coupling cohesion polymorphic.
1 Chapter 9 Objects and Classes. 2 OO Programming in Java Other than primitive data types (byte, short, int, long, float, double, char, boolean), everything.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Fall 2013 Chapter 10 Thinking.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. P ART 3: A GGREGATION, I NHERITANCE AND P.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Introduction of Object Oriented Programming.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Chapter 13 Abstract Classes and Interfaces
Chapter 13 Abstract Classes and Interfaces
Chapter 10 Thinking in Objects
Lecture 3: Introduction to Object and Classes
OOP: Encapsulation &Abstraction
Chapter 10 Thinking in Objects
Reference: COS240 Syllabus
Chapter 10 Thinking in Objects
Chapter 9 More on Objects and Classes
Chapter 11 Object-Oriented Design
Chapter 10 Object-Oriented Modeling
Object Based Programming
Chapter 9 Thinking in Objects
Chapter 10 Thinking in Objects
Chapter 9 Objects and Classes
Chapter 9 Objects and Classes
Chapter 9 Thinking in Objects
Chapter 6 Objects and Classes
Chapter 9 Objects and Classes
CS 112 Programming 2 Lecture 02 Abstract Classes & Interfaces (2)
Chapter 7 Objects and Classes
OO Programming Concepts
Chapter 10 Thinking in Objects
Presentation transcript:

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 Announcements

 Chapter 10  Immutable objects  this keyword  Composition  Differences between procedural programming & OOP  Guidelines for OOP Objectives

More on Objects

 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

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

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; } }

Example public 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; } }

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

 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

Scope

 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

 Example Variable Scope

 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

 Example Variable Scope

this Keyword

 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

 Using this to reference hidden fields this

 Use this to call overloaded constructor this

Composition & Aggregation

 An object can contain another object.  The relationship between the two is called composition. 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. Aggregation

 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

 “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

 UML composition & aggregation notation UML

 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

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

 Aggregation may exist between objects of the same class. UML

 Aggregation may exist between objects of the same class. UML

 Chapter 10  Immutable objects  this keyword  Composition Previously

Class Abstraction

 Procedural programming  Methods  OOP  Entities grouping related methods and data OOP

 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

 Example: Loan class Class Abstraction TestLoanClass Run Loan

 Example: BMI Class Abstraction UseBMIClass Run BMI

 Example: Course Class Abstraction TestCource Course

 Example: Designing Stack Class Abstraction x Y Z Push Pop

 Example: Stack Class Abstraction Run TestStackOfIntegers

 Example: Designing Stack Class Abstraction StackOfIntegers

 Example: Guess Date Class Abstraction UseGuessDateClass RunGuessDate

Class Design Guidelines

 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

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

 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

 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

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

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

 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

 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

 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

 A property (data field) that is shared by all the instances of the class should be declared as a static property. Guideline