200 likes | 271 Views
Learn the principles of object-oriented programming, data structures, and algorithm analysis. Explore object construction, design fundamentals, sorting, trees, and graphs. Understand encapsulation and abstraction within the object model and interface design. Practice with linked lists and interfaces.
E N D
CPSC 102: Computer Science IIDr. Roy P. Pargas408 Edwards Hall864-656-5855pargas@cs.clemson.eduOffice Hours10:00-11:00 am MWF2:00-3:00 pm TTh
Overview of Course • Construction and Use of Objects • Definition • Methods • Use • Data Structures • Definition and Use • Algorithm Analysis
1. The Object-Oriented Method (2) 2. Comments, Conditions, and Assertions (1) 3. Vectors (3) 4. Design Fundamentals(4) 5. Sorting (2) 6. Lists(2) Test 1 7. Linear Structures (2) 9. Ordered Structures (2) 10. Trees (3) 11. Priority Queues (2) 12. Search Trees (2) 14. Graphs (1) Test 2 Semester Outline
Objects • Classes • templates for objects • Instances or Objects • constructed from classes • Message passing • usually changes state of object
Data Abstraction and Encapsulation • Objects • sports car • string of characters • array of objects • Details of structure (implementation) unimportant • Interface (contract) important data A p p l e 0 1 2 3 4 5 6
Object Model • Data for program managed by its objects • Program manipulates data through messages or method calls to the objects
Object-Oriented Terminology • Data abstraction is accomplished through encapsulation of data in an object (an instance of a class) • Fields and methods may be declared public or protected • Fields are encapsulated by a class • Classes are encapsulated by a package • Public classes may be used by anyone who imports the package
Example: class Ratio • Interface • public Ratio(int top, int bottom) • public int getNumerator() • public int getDenominator() • public double value() • public Ratio add(Ratio other)
Example: class WordList • WordList, initially empty, will contain words • User must be able to add words to, retrieve words from, and remove words from WordList • User must know when Wordlist empty
Example: class WordList • Interface • public WordList(int size) • public boolean isEmpty() • public void add(String s) • public String selectAny() • public void remove(String word)
Example: class WordList • We do not need to know anything about application other than its interaction with Object (abstract list of words) • We really don’t know how WordList is implemented • Result is the WordListinterface
Example: class BankAccount • Read on your own • Pay attention to discussion of the method equals
Example: Linked Lists • Refer to Program #1 handout • Program #1 • due Monday, August 31, 1998 • handin.102.2 1 List.java • handin.102.2 1 ListNode.java • handin.102.2 1 LinkedList.java • handin.102.2 1 *.java
handin command handin.102.2 1 *.java Submission for 102 section 2 asg number 1: LinkedList.java List.java ListNode.java Do you wish to continue with the submission [y/n] y file LinkedList.java: 3642 bytes copied file List.java: 993 bytes copied file ListNode.java: 227 bytes copied
Example: An Association • Pig Latin translator • Example of (key-value) pair • Example of precondition • Principle 2: • Free the future:reusecode
Interfaces • Sometimes useful to describe the interface for a number of different classes without committing to an implementation • No code specified in an interface • Interfaces may be extended • Example: public interface Store public interface Collection extends Store
Example: Extending Interfaces • public interface Store • public int size() • public boolean isEmpty() • public void clear • public interface Collection extends Store • public boolean contains(Object value) • public void add(Object value) • public Object remove(Object value) • public Iterator elements()
Conclusions • Principle 3: Design and abide by interfaces as though you were the user • Principle 4: Declare data fields protected • Data abstraction is supported by separating the interface from the implementation of the data structure
Homework #1 • Problems 1.10, 1.11, 1.13 of textbook • Assigned August 26, 1998 • Due September 2, 1998 • Refer to printed handout distributed in class
Program #2 (?) • Problems: 1.6, 1.7 • Refer to Program #2 handout • Due: September 9, 1998