Vladimir Misic: vm(at)cs.rit.edu 1 Object-Oriented Programming Basics Reading for this week † : BG 1-4.10 UNIX 1,2,4,9 JAVA 0,1 † BG = Beginner’s Guide.

Slides:



Advertisements
Similar presentations
Numbers Treasure Hunt Following each question, click on the answer. If correct, the next page will load with a graphic first – these can be used to check.
Advertisements

Angstrom Care 培苗社 Quadratic Equation II
Process Description and Control
AP STUDY SESSION 2.
1
Chapter 7 System Models.
Chapter 7 Constructors and Other Tools. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-2 Learning Objectives Constructors Definitions.
Copyright © 2003 Pearson Education, Inc. Slide 1 Computer Systems Organization & Architecture Chapters 8-12 John D. Carpinelli.
Processes and Operating Systems
David Burdett May 11, 2004 Package Binding for WS CDL.
Local Customization Chapter 2. Local Customization 2-2 Objectives Customization Considerations Types of Data Elements Location for Locally Defined Data.
Process a Customer Chapter 2. Process a Customer 2-2 Objectives Understand what defines a Customer Learn how to check for an existing Customer Learn how.
Custom Services and Training Provider Details Chapter 4.
1 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt BlendsDigraphsShort.
1 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt Wants.
1 Chapter 12 File Management Patricia Roy Manatee Community College, Venice, FL ©2008, Prentice Hall Operating Systems: Internals and Design Principles,
Programming Language Concepts
© Vinny Cahill 1 Writing a Program in Java. © Vinny Cahill 2 The Hello World Program l Want to write a program to print a message on the screen.
1 Click here to End Presentation Software: Installation and Updates Internet Download CD release NACIS Updates.
The 5S numbers game..
1.
Week 2 The Object-Oriented Approach to Requirements
Break Time Remaining 10:00.
Turing Machines.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 5.
Table 12.1: Cash Flows to a Cash and Carry Trading Strategy.
Red Tag Date 13/12/11 5S.
PP Test Review Sections 6-1 to 6-6
2007 Pearson Education, Inc. All rights reserved C Structures, Unions, Bit Manipulations and Enumerations.
LIAL HORNSBY SCHNEIDER
By Waqas Over the many years the people have studied software-development approaches to figure out which approaches are quickest, cheapest, most.
Outline Minimum Spanning Tree Maximal Flow Algorithm LP formulation 1.
Bellwork Do the following problem on a ½ sheet of paper and turn in.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 24.1 Test-Driving the Ticket Information Application.
Exarte Bezoek aan de Mediacampus Bachelor in de grafische en digitale media April 2014.
Object-Oriented Programming Basics Prof. Ankur Teredesai, Computer Science Department, RIT.
Object-Oriented Programming. 2 An object, similar to a real-world object, is an entity with certain properties, and with the ability to react in certain.
Copyright © 2013, 2009, 2006 Pearson Education, Inc. 1 Section 5.5 Dividing Polynomials Copyright © 2013, 2009, 2006 Pearson Education, Inc. 1.
Copyright © 2012, Elsevier Inc. All rights Reserved. 1 Chapter 7 Modeling Structure with Blocks.
 Copyright I/O International, 2013 Visit us at: A Feature Within from Item Class User Friendly Maintenance  Copyright.
Chapter 1: Expressions, Equations, & Inequalities
Adding Up In Chunks.
Lecture 6: Software Design (Part I)
1 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt 10 pt 15 pt 20 pt 25 pt 5 pt Synthetic.
CSci 1130 Intro to Programming in Java
Chapter 2 Entity-Relationship Data Modeling: Tools and Techniques
1 hi at no doifpi me be go we of at be do go hi if me no of pi we Inorder Traversal Inorder traversal. n Visit the left subtree. n Visit the node. n Visit.
1 Let’s Recapitulate. 2 Regular Languages DFAs NFAs Regular Expressions Regular Grammars.
Types of selection structures
Speak Up for Safety Dr. Susan Strauss Harassment & Bullying Consultant November 9, 2012.
Essential Cell Biology
Converting a Fraction to %
Clock will move after 1 minute
PSSA Preparation.
Chapter 11 Component-Level Design
Chapter 11 Creating Framed Layouts Principles of Web Design, 4 th Edition.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 13 - Exception Handling Outline 13.1 Introduction 13.2 Exception-Handling Overview 13.3 Other.
Physics for Scientists & Engineers, 3rd Edition
Select a time to count down from the clock above
Modeling Main issues: What do we want to build How do we write this down.
User Defined Functions Lesson 1 CS1313 Fall User Defined Functions 1 Outline 1.User Defined Functions 1 Outline 2.Standard Library Not Enough #1.
Chapter 9: Using Classes and Objects. Understanding Class Concepts Types of classes – Classes that are only application programs with a Main() method.
Chapter 10 Introduction to Objects and Classess 1.
Object-Oriented Programming •Object-Oriented Programming (OOP) allows you to create your program based upon modeling objects.  Your program’s properties.
1 Overview of Object-Oriented Software Design and Java Programming Putting the Pieces Together!
1 Software Engineering Lecture 15 Object Oriented Software Design in Java.
Object-Oriented Programming Basics
CS 4310: Software Engineering
Presentation transcript:

Vladimir Misic: vm(at)cs.rit.edu 1 Object-Oriented Programming Basics Reading for this week † : BG UNIX 1,2,4,9 JAVA 0,1 † BG = Beginner’s Guide UNIX = Harley Hahn’s Student guide to Unix, Harley Hahn JAVA = An Introduction to Object Oriented Programming with Java, Thomas Wu

Vladimir Misic: vm(at)cs.rit.edu 2 How can one design a program? Top-down structured design: uses algorithmic decomposition where each module denotes a major step in some overall process Object-oriented design: divides the problem into a set of objects that interacts to solve the problem. Your program ’ s properties and behaviors are modelled based upon real objects like cars, books, houses, etc.

Vladimir Misic: vm(at)cs.rit.edu 3 Why OOD? Software is complex (too many people is doing too many things – the mess is inevitable ) One of the main goals in programming is to avoid the redundancy and objects can help to do this (inheritance) Objects can help increase modularity through data hiding (encapsulation)

Vladimir Misic: vm(at)cs.rit.edu 4 OK, but what is object ? An object is a thing, either tangible or intangible. An object-oriented program consists of many objects. An object is composed of identity, state (attributes, data, and their current values) and behavior (operations). Identity? State? Behavior? Too many new expressions – I’ll get a headache!!!

Vladimir Misic: vm(at)cs.rit.edu 5 Identity, State, Behavior Identity is the property of an object that distinguishes it from all other objects. The failure to recognize the difference between the name of the object and the object itself is the source of many errors in object-oriented (OO) programming.

Vladimir Misic: vm(at)cs.rit.edu 6 Identity, State, Behavior The state of an object encompasses all of the (static) properties of the object plus the current (dynamic) values of each of these properties A property is an inherent or distinctive characteristic, trait, quality, or feature that contribute to making an object uniquely that object We will use the word attribute, or data member, to refer to the state of an object

Vladimir Misic: vm(at)cs.rit.edu 7 Examples of State Properties –Elevators travel up or down –Vending machines accept coins –Clocks indicate the current time Values –Current floor –Number of coins deposited –The number of minutes since the last hour

Vladimir Misic: vm(at)cs.rit.edu 8 Identity, State, Behavior Behavior is how an object acts and reacts, in terms of state changes and interactions with other objects. An operation is some action that one object performs upon another in order to elicit a reaction. We will use the word method to describe object behavior in java. Invoking a method causes the behavior to take place.

Vladimir Misic: vm(at)cs.rit.edu 9 Objects and Classes – graphical representation A graphical representation of an object: A graphical representation of a class:

Vladimir Misic: vm(at)cs.rit.edu 10 Object example Audi 6BMW Z3Corvette Notice that all objects are of the same type. All objects are cars!

Vladimir Misic: vm(at)cs.rit.edu 11 Classes Classes are the definitions (or blueprints) used to create objects. I’d say: descriptions of objects. To make a car the manufacturer must first have a design from which to build the first car. Then, once all the problems are worked out, the design is used to build all the cars of that model.

Vladimir Misic: vm(at)cs.rit.edu 12 Objects An object is an instance of a class. If we have a class definition called Car, then we can think of Audi, BMW, and Corvette as each being an instance (object) of the class Car, i.e., they are each a type of car.

Vladimir Misic: vm(at)cs.rit.edu 13 Object example Audi 6BMW Z3Corvette Notice that all objects are of the same type. All objects are cars! Car

Vladimir Misic: vm(at)cs.rit.edu 14 Classes and Objects An object is an instance of exactly one class!!! –Corvette can not be an instance of a car class and an instance of a plane class at the same time. An instance of a class, an object, belongs to that particular class. –A Corvette is a car  Corvette belongs to the class Car.

Vladimir Misic: vm(at)cs.rit.edu 15 Classes Once a class is defined you can create as many instances of the class (objects from the class) as you would like. Once a blue print is completed for the 2003 Porsche 911, Porsche will use an assembly line to build as many instances of the 2003 Porsche 911 as they wish.

Vladimir Misic: vm(at)cs.rit.edu 16 Class car and objects- graphically Audi 6BMW Z3Corvette Car This line shows an instance-of relationship.

Vladimir Misic: vm(at)cs.rit.edu 17 Defining a class Properties are variables which describe the essential characteristics of an object. Properties of a car: color, model, make, how many doors, transmission type, direction of movement, etc. Behaviors are methods that describe how the object behaves and how the properties may be modified. Behavior of a car: braking, changing gears, opening doors, moving forwards or backwards, etc.

Vladimir Misic: vm(at)cs.rit.edu 18 Defining a class Behavior or Method 1 Behavior or or Method 4 Method 2 Behavior or Method 3 Properties or Instance Variables

Vladimir Misic: vm(at)cs.rit.edu 19 Instance variables The class definition will include parameter definitions (properties) that represent data about a particular object, instance variables. Example, Jessica's car may have 4 gallons of gas in it while Clint's car has 10 gallons. The amount of gas in each car may change without affecting the amount of gas in the any other cars. All instances (objects) of a class will have a set of instance variables that are specific to that individual object. The combination of the values of these instance variables is known as the object’s state.

Vladimir Misic: vm(at)cs.rit.edu 20 Instance variables Audi 6BMW Z3Corvette Car MaxSpeed = 155MaxSpeed = 165MaxSpeed = 145 MaxSpeed

Vladimir Misic: vm(at)cs.rit.edu 21 Class variables The class definitions may also include parameter definitions that represent data that is shared by all class instances (objects), called class variables. In the case of the car class, we will define a maximum allowed speed, by the law (variable LMaxSpeed). This will be the same for each individual car (that’s why your car have those annoying speed limiters ).

Vladimir Misic: vm(at)cs.rit.edu 22 Class variables Audi 6BMW Z3Corvette Car MaxSpeed = 155MaxSpeed = 165MaxSpeed = 145 MaxSpeed LMaxSpeed=155

Vladimir Misic: vm(at)cs.rit.edu 23 Class variables Class variables may also be used to keep track of things such as how many instances of a class exist. Example: let’s create a counter the records how many cars are in the garage.

Vladimir Misic: vm(at)cs.rit.edu 24 Class variables Audi 6BMW Z3Corvette Car MaxSpeed = 155MaxSpeed = 165MaxSpeed = 145 MaxSpeed LMaxSpeed=155 NumCars = 3

Vladimir Misic: vm(at)cs.rit.edu 25 Constant parameters If it was variables instead of parameters, that would be oxymoron. If there is a parameter whose value should not change while your program is running, that parameter type is called a constant. The LMaxSpeed parameter that we defined for the Car class is a constant. The maximal speed is limited by the low, and it cannot be changed once the car has been built. What about Z3 and 165 mph? Well, the owner has just made an illegal modification !!!

Vladimir Misic: vm(at)cs.rit.edu 26 Messages For Objects The object to whom the message is being sent. The name of the method (behavior) that object is to execute. Any parameters (variables) needed by that method. For Humans Who the message is for. What we want the person to do. What information is needed to do it. Audi 6 turnOnHazard()

Vladimir Misic: vm(at)cs.rit.edu 27 Messages and Methods In order to process a message, an object needs to have a method defined for the requested task. A method is a small, well-defined piece of code that completes a specific task. For our previous example, we need to define a method to turn on the car's hazard lights.

Vladimir Misic: vm(at)cs.rit.edu 28 Messages and Methods Audi 6BMW Z3Corvette Car MaxSpeed = 155 turnOnHazard() MaxSpeed = 165 turnOnHazard() MaxSpeed = 145 turnOnHazard() MaxSpeed LMaxSpeed=155 NumCars = 3 turnOnHazard()

Vladimir Misic: vm(at)cs.rit.edu 29 Instance methods Each class can have methods that are specific to each object, called instance methods. These can only affect that object's parameters, i.e., it’s instance variables. Example: If BMW has 4 gallons of gas and someone puts 6 more gallons of gas in his/her car, the car now has 10 gallons. The amount of gas in Audi and Corvette is unchanged.

Vladimir Misic: vm(at)cs.rit.edu 30 Messages and Methods Audi 6BMW Z3Corvette Car MaxSpeed = 155 turnOnHazard() addGass(amount) MaxSpeed = 165 turnOnHazard() addGass(amount) MaxSpeed = 145 turnOnHazard() addGass(amount) MaxSpeed LMaxSpeed=155 NumCars = 3 turnOnHazard() addGass(amount)

Vladimir Misic: vm(at)cs.rit.edu 31 Methods It is also possible that you want information from an object; in this case, you would define a method that sends (returns) a message back to the requester containing that information. We need to know how much gas is in our cars, so we will create a new method that returns the value of GasLevel variable for our car.

Vladimir Misic: vm(at)cs.rit.edu 32 Messages and Methods Audi 6 BMW Z3 Corvette Car MaxSpeed = 155 GasLevel = 4 turnOnHazard() addGass(amount) getGasLevel():GasLevel MaxSpeed = 165 GasLevel = 10 MaxSpeed = 145 GasLevel = 6 turnOnHazard() addGass(amount) getGasLevel():GasLevel MaxSpeed GasLevel LMaxSpeed=155 NumCars = 3 addGass(amount)getGasLevel():GasLevel turnOnHazard() addGass(amount)getGasLevel():GasLevel turnOnHazard()

Vladimir Misic: vm(at)cs.rit.edu 33 Class methods Class methods are used to get or manipulate information about all objects created from the class. Typically, class methods are changing class variables. For example: –Each time we move the car in or out of the garage, we need to add/subtract one to the number of cars: carIn( ) & carOut( ) –Also, we may want to know how many cars are actually in the garage: getNumCars( )

Vladimir Misic: vm(at)cs.rit.edu 34 Messages and Methods Audi 6BMW Z3Corvette Car MaxSpeed = 155 GasLevel = 4 turnOnHazard() addGass(amount) getGasLevel():GasLevel MaxSpeed = 165 GasLevel = 10 turnOnHazard() addGass(amount) getGasLevel():GasLevel MaxSpeed = 145 GasLevel = 6 turnOnHazard() addGass(amount) getGasLevel():GasLevel MaxSpeed GasLevel LMaxSpeed=155 NumCars = 3 addGass(amount)getGasLevel():GasLevel turnOnHazard() carIn() carOut() getNumCars():NumCars

Vladimir Misic: vm(at)cs.rit.edu 35 Types of methods There are 4 basic types of methods: Modifier (sometimes called a mutator) –Changes the value associated with an attribute of the object (or class) Accessor –Returns the value associated with an attribute of the object (or class) Constructor –Called once when the object is created (before any other method will be invoked) Destructor –Called when the object is destroyed

Vladimir Misic: vm(at)cs.rit.edu 36 OOP When writing object-oriented programs, first one must define the classes (like Car). Then, while the program is running, the instances of the classes (objects) (such as Audi, BMW, Corvette in our example) are created.

Vladimir Misic: vm(at)cs.rit.edu 37 OOP Object-oriented programming allows the programmer to hide the implementation details from the other objects and the users. In other words the implementation is transparent to the other objects or the user. Example: Although our computers all are different “under the hood”, we don’t need to know what’s there to be able to use them.

Vladimir Misic: vm(at)cs.rit.edu 38 OOP - benefits An object can be written and maintained separately from the rest of the program, modularity. An object has a “ public face ” that it uses to communicate with other objects, but other objects can not directly access its instance variables, information hiding.

Vladimir Misic: vm(at)cs.rit.edu 39 Inheritance All classes in Java are organized into a class hierarchy. The highest level classes are very general and the lower level classes are more specific. The lower level classes are based upon the higher level classes and inherit instance variables and methods from those higher level class. They also may contain their own (new) instance variables and methods beyond the higher level class definition.

Vladimir Misic: vm(at)cs.rit.edu 40 Inheritance A higher level class is called a superclass; a lower level class is called a subclass. A subclass may also be a superclass Inheritance allows you to define certain behaviors once and then to reuse those behaviors over and over again in the subclasses. This is called reusability.

Vladimir Misic: vm(at)cs.rit.edu 41 Inheritance Our Car class is very general. Let's define a new class called BMW that contains the parameters: model, color, engine size.

Vladimir Misic: vm(at)cs.rit.edu 42 Inheritance Car MaxSpeed GasLevel LMaxSpeed=155 turnOnHazard() addGass(amount) getGasLevel():GasLevel BMW Model Color EngineSize MaxSpeed GasLevel turnOnHazard() addGass(amount) getGasLevel():GasLevel

Vladimir Misic: vm(at)cs.rit.edu 43 Inheritance Now let's define two new classes. One for the Z3 and another for the 3 Series Sedan. What might be some of the differences between the two classes? Number of doors (3, 5) Roof (soft or hardtop) Therefore, we add variables NumDoors and Roof

Vladimir Misic: vm(at)cs.rit.edu 44 Inheritance Car MaxSpeed GasLevel LMaxSpeed=155 turnOnHazard() addGass(amount) getGasLevel():GasLevel BMW Model Color EngineSize MaxSpeed GasLevel turnOnHazard() addGass(amount) getGasLevel():GasLevel Z3 Model Color EngineSize Roof MaxSpeed GasLevel turnOnHazard() addGass(amount) getGasLevel():GasLevel 3 series Model Color EngineSize NumDoors MaxSpeed GasLevel turnOnHazard() addGass(amount) getGasLevel():GasLevel

Vladimir Misic: vm(at)cs.rit.edu 45 Views of the class A class can be viewed as a sort of contract that specifies what instances of the class can, and cannot do It is possible to distinguish between the outside and inside view of a class The interface of a class provides its outside view and emphasizes the abstraction The implementation of a class is its inside view

Vladimir Misic: vm(at)cs.rit.edu 46 Access Most classes provide three levels of access to their members (state and behavior): –Public The part of the class that is visible to all clients of the class –Protected The part of the class that is only visible to subclasses of the class –Private A part of the class that is not visible to any other classes

Vladimir Misic: vm(at)cs.rit.edu 47 Primitive data types What ?!!! Primitive? What is it? Certain basic data types are built into Java: –Alphabetical: char –Integral: int, byte, short, long –Real: float, double –Logical: boolean Java supports 8 primitive types (i.e. 4 basic types)

Vladimir Misic: vm(at)cs.rit.edu 48 Primitive data types TypeDescriptionBitsRange of values booleanBoolean1False, true byteintegers, very small8-128 to +127 shortintegers, small ones to intinteger32-2,147,483,648 to 2,147,483,647 longinteger, very big ones64+/- 9.2 * floatfloating point number32+/ *10 38 doublefloating point number, very accurate64+/ * charcharacter16Unicode character (eg \u12d1)

Vladimir Misic: vm(at)cs.rit.edu 49 Primitive data types When typing integer into a program, the number must have an ‘L’ at the end: L Real numbers are represented in scientific notation: –mantissa [-1,1] –exponent (powers of two)

Vladimir Misic: vm(at)cs.rit.edu 50 Real data type Characterized by precision and range –Precision: represents number of significant digits that is represented in the number. Depends upon the number of bits in the mantissa. –Range: represents the difference between the largest and smallest numbers that can be represented. Depends upon the number of bits in the exponent. float: –24b mantissa + 8b exponent –6-7 decimal digits of precision double: –53b mantissa + 11b exponent –15-16 decimal digits of precision

Vladimir Misic: vm(at)cs.rit.edu 51 Round-off error When a double is stored to a float, part of the number is lost. In this situation, the seven most significant digits are preserved and the rest of the information is lost.

Vladimir Misic: vm(at)cs.rit.edu 52 Char Character values may represent both printable and non- printable characters: –Printable: ‘a’, ‘&”, … –Non-printable: ‘\n’ new line, ‘\t’ tab, … Unicode 16b support for every character in the world: ‘\u0000’ to ‘\uFFFF’