CPSC 102: Computer Science II Dr. Roy P. Pargas 408 Edwards Hall 864-656-5855 [email protected]...

20
CPSC 102: Computer Science II Dr. Roy P. Pargas 408 Edwards Hall 864-656-5855 [email protected] Office Hours 10:00-11:00 am MWF 2:00-3:00 pm TTh

Transcript of CPSC 102: Computer Science II Dr. Roy P. Pargas 408 Edwards Hall 864-656-5855 [email protected]...

Page 1: CPSC 102: Computer Science II Dr. Roy P. Pargas 408 Edwards Hall 864-656-5855 pargas@cs.clemson.edu Office Hours 10:00-11:00 am MWF 2:00-3:00 pm TTh.

CPSC 102: Computer Science IIDr. Roy P. Pargas

408 Edwards Hall864-656-5855

[email protected]

Office Hours10:00-11:00 am MWF

2:00-3:00 pm TTh

Page 2: CPSC 102: Computer Science II Dr. Roy P. Pargas 408 Edwards Hall 864-656-5855 pargas@cs.clemson.edu Office Hours 10:00-11:00 am MWF 2:00-3:00 pm TTh.

Overview of Course

• Construction and Use of Objects– Definition– Methods– Use

• Data Structures– Definition and Use

• Algorithm Analysis

Page 3: CPSC 102: Computer Science II Dr. Roy P. Pargas 408 Edwards Hall 864-656-5855 pargas@cs.clemson.edu Office Hours 10:00-11:00 am MWF 2:00-3:00 pm TTh.

Semester Outline

• 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

Page 4: CPSC 102: Computer Science II Dr. Roy P. Pargas 408 Edwards Hall 864-656-5855 pargas@cs.clemson.edu Office Hours 10:00-11:00 am MWF 2:00-3:00 pm TTh.

Objects

• Classes– templates for objects

• Instances or Objects– constructed from

classes

• Message passing– usually changes

state of object

Page 5: CPSC 102: Computer Science II Dr. Roy P. Pargas 408 Edwards Hall 864-656-5855 pargas@cs.clemson.edu Office Hours 10:00-11:00 am MWF 2:00-3:00 pm TTh.

Data Abstraction and Encapsulation

• Objects– sports car– string of characters– array of objects

• Details of structure (implementation) unimportant

• Interface (contract) important

A p p l e

0 1 2 3 4 5 6

data

Page 6: CPSC 102: Computer Science II Dr. Roy P. Pargas 408 Edwards Hall 864-656-5855 pargas@cs.clemson.edu Office Hours 10:00-11:00 am MWF 2:00-3:00 pm TTh.

Object Model

• Data for program managed by its objects

• Program manipulates data through messages or method calls to the objects

Page 7: CPSC 102: Computer Science II Dr. Roy P. Pargas 408 Edwards Hall 864-656-5855 pargas@cs.clemson.edu Office Hours 10:00-11:00 am MWF 2:00-3:00 pm TTh.

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

Page 8: CPSC 102: Computer Science II Dr. Roy P. Pargas 408 Edwards Hall 864-656-5855 pargas@cs.clemson.edu Office Hours 10:00-11:00 am MWF 2:00-3:00 pm TTh.

Example: class Ratio

• Interface– public Ratio(int top, int bottom)– public int getNumerator()– public int getDenominator()– public double value()– public Ratio add(Ratio other)

Page 9: CPSC 102: Computer Science II Dr. Roy P. Pargas 408 Edwards Hall 864-656-5855 pargas@cs.clemson.edu Office Hours 10:00-11:00 am MWF 2:00-3:00 pm TTh.

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

Page 10: CPSC 102: Computer Science II Dr. Roy P. Pargas 408 Edwards Hall 864-656-5855 pargas@cs.clemson.edu Office Hours 10:00-11:00 am MWF 2:00-3:00 pm TTh.

Example: class WordList

• Interface– public WordList(int size)– public boolean isEmpty()– public void add(String s)– public String selectAny()– public void remove(String word)

Page 11: CPSC 102: Computer Science II Dr. Roy P. Pargas 408 Edwards Hall 864-656-5855 pargas@cs.clemson.edu Office Hours 10:00-11:00 am MWF 2:00-3:00 pm TTh.

• 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 WordList interface

Example: class WordList

Page 12: CPSC 102: Computer Science II Dr. Roy P. Pargas 408 Edwards Hall 864-656-5855 pargas@cs.clemson.edu Office Hours 10:00-11:00 am MWF 2:00-3:00 pm TTh.

Example: class BankAccount

• Read on your own

• Pay attention to discussion of the method equals

Page 13: CPSC 102: Computer Science II Dr. Roy P. Pargas 408 Edwards Hall 864-656-5855 pargas@cs.clemson.edu Office Hours 10:00-11:00 am MWF 2:00-3:00 pm TTh.

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

Page 14: CPSC 102: Computer Science II Dr. Roy P. Pargas 408 Edwards Hall 864-656-5855 pargas@cs.clemson.edu Office Hours 10:00-11:00 am MWF 2:00-3:00 pm TTh.

handin.102.2 1 *.javaSubmission for 102 section 2 asg number 1: LinkedList.java List.java ListNode.java

Do you wish to continue with the submission [y/n] yfile LinkedList.java: 3642 bytes copiedfile List.java: 993 bytes copiedfile ListNode.java: 227 bytes copied

handin command

Page 15: CPSC 102: Computer Science II Dr. Roy P. Pargas 408 Edwards Hall 864-656-5855 pargas@cs.clemson.edu Office Hours 10:00-11:00 am MWF 2:00-3:00 pm TTh.

Example: An Association

• Pig Latin translator• Example of (key-value) pair• Example of precondition

• Principle 2: – Free the future: reuse code

Page 16: CPSC 102: Computer Science II Dr. Roy P. Pargas 408 Edwards Hall 864-656-5855 pargas@cs.clemson.edu Office Hours 10:00-11:00 am MWF 2:00-3:00 pm TTh.

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

Page 17: CPSC 102: Computer Science II Dr. Roy P. Pargas 408 Edwards Hall 864-656-5855 pargas@cs.clemson.edu Office Hours 10:00-11:00 am MWF 2:00-3:00 pm TTh.

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()

Page 18: CPSC 102: Computer Science II Dr. Roy P. Pargas 408 Edwards Hall 864-656-5855 pargas@cs.clemson.edu Office Hours 10:00-11:00 am MWF 2:00-3:00 pm TTh.

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

Page 19: CPSC 102: Computer Science II Dr. Roy P. Pargas 408 Edwards Hall 864-656-5855 pargas@cs.clemson.edu Office Hours 10:00-11:00 am MWF 2:00-3:00 pm TTh.

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

Page 20: CPSC 102: Computer Science II Dr. Roy P. Pargas 408 Edwards Hall 864-656-5855 pargas@cs.clemson.edu Office Hours 10:00-11:00 am MWF 2:00-3:00 pm TTh.

Program #2 (?)

• Problems: 1.6, 1.7• Refer to Program #2 handout

• Due: September 9, 1998