Compsci201, L3: Object- Oriented Programming (OOP)

1y ago
13 Views
1 Downloads
2.95 MB
39 Pages
Last View : 1d ago
Last Download : 3m ago
Upload by : Eli Jorgenson
Transcription

Compsci 201, L3: ObjectOriented Programming (OOP) 9/5/22 Compsci 201, Fall 2022, OOP 1

Logistics, Coming up Wednesday, 9/7 Interfaces vs. Implementations, ArrayList First APT Exercises due Complete at least 4 for full credit Friday, 9/9 Discussion 2: APTs, Sets, Strings, Git Monday 9/12 Prjoect 0: Person201 due (warmup project) 9/5/22 Compsci 201, Fall 2022, OOP 2

Course Policy Reminders Collaboration reminder: Can discuss projects and APTs conceptually, code must be your own. If you can’t write the code yourself, you’re not going to be ready for whatever you want to do next. Getting Help reminder: We want to help! 9/5/22 Course website getting help page Su-Th every evening, Use OhHai to queue Some daytime hours, plus Ed discussion Expect help about your process and how to make progress – not “solutions” or for TAs to debug your code for you. Compsci 201, Fall 2022, OOP 3

Recapping some Java Themes 9/5/22 Compsci 201, Fall 2022, OOP 4

Comments on Java Style Code blocks: Opening { ends first line of if, for, while, or method Indent every line inside the block Closing } on a separate line, last of block, not indented Variable & method names: One-word names: lowercase Multi-word names: camelCase Should be informative 9/5/22 Compsci 201, Fall 2022, OOP 5

More comments on Java style Class names: Capitalized & CamelCase MUST match name of .java file! Comments: // for one line /* */ for multiple lines 9/5/22 Compsci 201, Fall 2022, OOP 6

Javadoc use 9/5/22 Compsci 201, Fall 2022, OOP 7

Writing Javadoc Common annotations for methods include: @param, @returns, @throws 9/5/22 Compsci 201, Fall 2022, OOP 8

Java API ArrayList Reminder Import statement: Basic usage of ArrayList: List of values of same type in order, can grow Uses add(), get(), size(), contains() .add() Appends to end of list .get(i) returns i’th index element 9/5/22 Compsci 201, Fall 2022, OOP .contains(x) returns true if x 9in list

Java API HashSet Reminder An import statement: Basic usage of HashSet: Unordered collection, does not store duplicates Uses add(), size(), contains() Prints 2, no duplicates 9/5/22 Compsci 201, Fall 2022, OOP 10

Java API Collections and Primitive vs. object types Why ArrayList Integer . instead of ArrayList int ? Java API Collections (ArrayList, HashSet, ) only store reference types, not primitive types. Integer is an int object, can convert back and forth “automatically.” Same principle for other primitive types, e.g., double vs. Double 9/5/22 Compsci 201, Fall 2022, OOP 11

ArrayList - Array Conversion, Primitive Types 1/24/22 Compsci 201, Spring 2022, Sets Maps 12

Object-Oriented Programming 9/5/22 Compsci 201, Fall 2022, OOP 13

Java is object-oriented A language is object-oriented if programs in that language are organized by the specification and use of objects. “An object consists of some internal data items plus operations that can be performed on that data.”–ZyBook We call these methods 9/5/22 Scanner is a Class, s is an object. Keeps track of where it is in the file and can get the next word. Compsci 201, Fall 2022, OOP 14

Aside: Python uses objects too Split is a method we are calling on this String object, not a regular function! Same syntax in Python and Java for method calls: object . method ( method arguments ) 9/5/22 Compsci 201, Fall 2022, OOP 15

Object Concept Consider points in two-dimensions. All different objects, but each of the same class Class is a blueprint for these objects Data (instance variables) x-coordinate (x) y-coordinate (y) Operations (methods) Create a point Print a point Change coordinates Get distance to another point Each point object has its own x and y value. 9/5/22 Compsci 201, Fall 2022, OOP Methods should be able to operate on a particular point 16

Language History: A story of increasing abstraction and organization Imperative Programming (Fortran I, etc.) Code organized into a linear sequence of operations. Procedural Programming (C, etc.) Object-Oriented Procedures or Programming (Java,etc.) functions, that can be All data accessible as called by a main variables in the same program. global scope. Local versus global variables. 9/5/22 Define more complex variable types using classes, use to create objects. Dynamic methods to go along with specific classes/types. Compsci 201, Fall 2022, OOP 17

Classes and objects Class specifies the data and operations for a type of object. They are a template or a blueprint for objects. Alternately, objects are instances of a class. Instance variables. Each Point object has its own x and y value. A constructor method specifies how to create a new Point object. Same name as class. this keyword refers to object on which method is called. 9/5/22 . operator accesses instance variable or method of this object Compsci 201, Fall 2022, OOP 18

Creating objects, calling methods Method defined inside the point class new Point allocates memory and calls the constructor to set the instance variables (-2.0, 2.0) (1.0, 1.0) Note how the printPoint() method “knows” the correct value for x and y – they are stored with the objects on which we call the method as instance variables. 9/5/22 Compsci 201, Fall 2022, OOP 19

Two reasons to call a method For the side effect, what it did to the object For the return value, no change to object 9/5/22 Compsci 201, Fall 2022, OOP 20

WOTO Go to duke.is/5vtee Not graded for correctness, just participation. Try to answer without looking back at slides and notes. But do talk to your neighbors! 9/5/22 Compsci 201, Fall 2022, OOP 21

or .equals()? For primitive types: checks for equal values. For objects, generally does not. Need to use .equals() method for objects. true false 9/5/22 Correct way to compare String objects. Must be implemented for the given Class! Compsci 201, Fall 2022, OOP 22

Default Object .equals Prints false, is just checking memory locations 9/5/22 Compsci 201, Fall 2022, OOP 23

Overriding default Object .equals Prints true, is using the method we wrote to check values 9/5/22 Compsci 201, Fall 2022, OOP 24

Object vs. object, Inheritance? Object: ancestor of all classes Default behavior that's not too useful, @Override for .equals object – synonym for instance of a class What you get when you call new Inheritance is a major topic in object-oriented programming to which we will return! 9/5/22 Compsci 201, Fall 2022, OOP 25

How do I know what .equals does for Java API classes? Read at the Java API documentation!!! docs.oracle.com/en/java/javase/17/docs/api 9/5/22 Compsci 201, Fall 2022, OOP 26

When do I need new? Every time I create an object, not automatic! We created the array, but did not call new for the individual Point objects. 9/5/22 Compsci 201, Fall 2022, OOP 27

When do I need new again? For every object you want to create! An even stranger error creating one object but multiple references to it. p myPoints Prints 9/5/22 Compsci 201, Fall 2022, OOP 28

Creating a List of points; contains uses equals Good, we called new for every Point object we want to create. Prints false. ArrayList .contains loops over list checking .equals(), but only default implementation here! 9/5/22 Compsci 201, Fall 2022, OOP 29

WOTO Go to duke.is/n5r6b Not graded for correctness, just participation. Try to answer without looking back at slides and notes. But do talk to your neighbors! 9/5/22 Compsci 201, Fall 2022, OOP 30

Public vs. Private Public – Can be accessed by code outside of the class. Private – Can only be accessed by code inside of the class. Can access this public instance variable Cannot access this private instance variable 9/5/22 Compsci 201, Fall 2022, OOP 31

The value of privacy Suppose your entire system crashes terribly if some code is called on a negative uniqueID. uniqueID is private, so other code cannot directly change it Can check for correctness in only code allowed to change uniqueID 9/5/22 Compsci 201, Fall 2022, OOP 32

(Im)mutability An object is immutable if you cannot change it after creation. Methods that change objects are called mutators. Java Strings are immutable, even though you can “append” to them. Creates a new String and assigns it every time! More like (and then get rid of sOld) 9/5/22 Compsci 201, Fall 2022, OOP 33

Static belongs to the class Regular instance variables and methods are called on an object. Static methods are called on the class, do not use any instance variables. Often utility “functions” Note that split is called on a String object Whereas sqrt is called on the Math class 9/5/22 Compsci 201, Fall 2022, OOP 34

PSVM: Public Static Void Main Method that is: public – can call outside of class static – belongs to class, not an object void – no return value main – starting point for a program to run args allows for commandline arguments 9/5/22 Compsci 201, Fall 2022, OOP 35

APT and OOP, making a PSVM method Suppose you’re working on the SandwichBar APT. Remember what you know about Java OOP: whichOrder is a regular method, need to call on an object of the SandwichBar class. whichOrder has parameters, need to supply those. All java programs must begin in a PSVM method. 9/5/22 Compsci 201, Fall 2022, OOP 36

APT and OOP, making a PSVM method PSVM method can be in the same class or in a separate “driver” class in the same directory. Creating test parameters, using example from APT site. Make a SandwichBar object Call the method 9/5/22 Compsci 201, Fall 2022, OOP 37

Why use Classes/objects? Because you must in Java Formal specification for complex data structures Convenience and ease of correct programming Composition, Interfaces, & Implementations, Extending & Inheritance – More later! It’s ok to not be fully “convinced” yet. But OOP has proven itself to be a powerful paradigm for designing complex scalable software. 9/5/22 Compsci 201, Fall 2022, OOP 38

Fred Brooks, Why is programming fun? Duke ‘53 Founded Compsci @ UNC Turing award winner, design 1. Sheer joy of making things 2. Pleasure of making things that are useful 3. Fascination of fashioning complex puzzle-like objects 4. Delight in working in such a tractable medium [like a poet] 9/5/22 Compsci 201, Fall 2022, OOP 39

9/5/22 Compsci 201, Fall 2022, OOP 3. Recapping some Java Themes 9/5/22 Compsci 201, Fall 2022, OOP 4. Comments on Java Style Code blocks: Opening {ends first line of if, for, while, or method . Aside: Python uses objects too 9/5/22 Compsci 201, Fall 2022, OOP 15 Split is a methodwe are calling on this String object, not a

Related Documents:

method dispatch in different object-oriented programming languages. We also include an appendix on object-oriented programming languages, in which we consider the distinction between object-based and object-oriented programming languages and the evolution and notation and process of object-oriented analysis and design, start with Chapters 5 and 6;

object-oriented programming language is based on a kind of old object-oriented programming language. For example, though C language is an object-oriented programming language, it still retains the pointer which is complex but has strong function. But C# improved this problem. C# is a kind of pure object-oriented language.

It stands for Object Oriented Programming. Object‐Oriented Programming ﴾223﴿ uses a different set of programming languages than old procedural programming languages ﴾& 3DVFDO, etc.﴿. Everything in 223 is grouped as self sustainable "REMHFWV". Hence, you gain reusability by means of four main object‐oriented programming concepts.

The principle of object oriented programming is to combine both data and the associated functions into a single unit called a class. An object in programming means data. Data is therefore predominant in object oriented programming. The concept will become clear with examples. However, in the object oriented paradigm, accessibility of

Object Oriented Programming 7 Purpose of the CoursePurpose of the Course To introduce several programming paradigms including Object-Oriented Programming, Generic Programming, Design Patterns To show how to use these programming schemes with the C programming language to build “good” programs.

Object Class: Independent Protection Layer Object: Safety Instrumented Function SIF-101 Compressor S/D Object: SIF-129 Tower feed S/D Event Data Diagnostics Bypasses Failures Incidences Activations Object Oriented - Functional Safety Object: PSV-134 Tower Object: LT-101 Object Class: Device Object: XS-145 Object: XV-137 Object: PSV-134 Object .

1. From structured programming to object-oriented programming 1 2. Towards Object-oriented Programming 7 3. Phenomena and Concepts 13 4. Towards Object-oriented Programs 19 5. The C# Language and System 23 6. C# in relation to C 25 7. C# in relation to Java 53 8. C# in relation to Visual Basic 57 9. C# Tools and IDEs 59 10.

Abstract—OOP (Object Oriented Programming) is an object-oriented programming method. The purpose of OOP is to facilitate the development of the program by following the models that have existed in everyday life. Object-oriented programming techniques are becoming very popular today in the process of creating multi-operating system applications.