Object-Oriented Programming •Object-Oriented Programming (OOP) allows you to create your program based upon modeling objects.  Your program’s properties.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Python Objects and Classes
Department of Computer Engineering Faculty of Engineering, Prince of Songkla University 1 5 – Abstract Data Types.
Procedural programming in Java
CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
Chapter 10 Introduction to Objects and Classess 1.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. slide 1 CS 125 Introduction to Computers and Object- Oriented Programming.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. slide 1 CS 125 Introduction to Computers and Object- Oriented Programming.
8 November Forms and JavaScript. Types of Inputs Radio Buttons (select one of a list) Checkbox (select as many as wanted) Text inputs (user types text)
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 1 Introduction to Object-Oriented Programming and Software Development.
Data types and variables
Object-oriented Programming Concepts
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 1 Introduction to Object-Oriented Programming and Software Development.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 1 Introduction to Object-Oriented Programming and Software Development.
String Escape Sequences
CPT 140 Programming Constructs1 OBJECT ORIENTED TECHNOLOGY Terminology and Basic Concepts.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
CSCI 1100/1202 January 16, Why do we need variables? To store intermediate results in a long computation. To store a value that is used more than.
Input & Output: Console
Georgia Institute of Technology Introduction to Java, and DrJava Barb Ericson Georgia Institute of Technology Aug 2005.
Object Oriented Design: Identifying Objects
Copyright © 2002 W. A. Tucker1 Chapter 7 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value.
Lecture 2 Object Oriented Programming Basics of Java Language MBY.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
Lec 6 Data types. Variable: Its data object that is defined and named by the programmer explicitly in a program. Data Types: It’s a class of Dos together.
5 BASIC CONCEPTS OF ANY PROGRAMMING LANGUAGE Let’s get started …
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
BCS 2143 Object Oriented Design Using UML. Objectives Objects Interactions Finding Classes Relationship Between Classes Attribute and Operation Class.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
I Power Higher Computing Software Development High Level Language Constructs.
CONSTANTS Constants are also known as literals in C. Constants are quantities whose values do not change during program execution. There are two types.
Identifiers Identifiers in Java are composed of a series of letters and digits where the first character must be a letter. –Identifiers should help to.
Programming Paradigms Lecturer Hamza Azeem. What is PP ? Revision of Programming concepts learned in CPLB Learning how to perform “Object-Oriented Programming”
Data Representation The storage of Text Numbers Graphics.
Copyright © – Curt Hill Types What they do.
1 Overview of Object-Oriented Software Design and Java Programming Putting the Pieces Together!
Java Programming, Second Edition Chapter Two Using Data Within a Program.
Lab 4 - Variables. Information Hiding General Principle: – Restrict the access to variables and methods as much as possible Can label instance variables.
COMP Primitive and Class Types Yi Hong May 14, 2015.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Chapter 2 Data abstraction: introductory concepts.
1 Software Engineering Lecture 15 Object Oriented Software Design in Java.
Objective You will be able to define the basic concepts of object-oriented programming with emphasis on objects and classes by taking notes, seeing examples,
Types Chapter 2. C++ An Introduction to Computing, 3rd ed. 2 Objectives Observe types provided by C++ Literals of these types Explain syntax rules for.
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
Primitive Data Types. int This is the type you are familiar with and have been using Stores an integer value (whole number) between -2,147,483,648 (-2.
CSCI 51 Introduction to Programming Dr. Joshua Stough February 24, 2009.
Review A program is… a set of instructions that tell a computer what to do. Programs can also be called… software. Hardware refers to… the physical components.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Chapter 2 Variables.
JAVA By Waqas.
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Variables and Primative Types
IDENTIFIERS CSC 111.
Chapter 2 Variables.
C++ Data Types Data Type
Object-Oriented Programming
Data Types and Variables Part A – Introduction Numeric Data Types
Programs and Classes A program is made up from classes
Defining Classes and Methods
Chapter 2 Variables.
CS 4310: Software Engineering
Presentation transcript:

Object-Oriented Programming •Object-Oriented Programming (OOP) allows you to create your program based upon modeling objects.  Your program’s properties and behaviors are modeled based upon real objects like cars, books, houses, etc.

Objects •An object is a thing, either tangible or intangible. –When you write an object-oriented program it consists of many objects. –An object is composed of properties (attributes, data) and behaviors (operations).

Objects Our textbook uses a rectangle with rounded corners to represent objects. (I will use an oval).

Car Objects Clint's Porsche Jessica's BMW John's Lamborghini

Classes •Classes are the definitions (or blue prints) used to create 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.

Classes and Objects •An object is an instance of a class. –If we have a class definition called Car, then we can think of Clint's Porsche, Jessaca's BMW, and John's Lamborghini as each being an instance (object) of the class Car. •They are each a type of car.

Car Class and Objects Clint's Porsche Jessica's BMW John's Lamborghini Car

Classes and Objects •An object is an instance of exactly one class. –John's Lamborghini can not be an instance of a car class and an instance of a plane class at the same time. •It is only an instance of one class. •An instance of a class (object) belongs to that particular class. –A Lamborghini is a car. So John's Lamborghini belongs to the class Car.

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, Prosche will use an assembly line to build as many instances of the 2003 Prosche 911 as they wish.

Classes Our text represents classes using the following graphic.

Car Class and Objects Clint's Porsche Jessica's BMW John's Lamborghini Car

Defining a Class  Let’s look at our car class –Properties are variables which describe the essential characteristics of an object. For a car they are: color, model, make, how many doors, transmission type, direction of movement, etc. –Behaviors are methods that describe how the object behaves and how the behaviors may be modified. For a car they are: braking, changing gears, opening doors, painting the car, moving forwards or backwards, etc.

Defining a Class Properties or Instance Variables Behavior or Method 1 Behavior or Method 3 Behavior or Method 4 Behavior or Method 2

Parameter Values •The class definition will include parameter definitions (properties) that represent data about a particular object. – For instance the fact that Jessica's car has 4 gallons of gas in it while Clint's car has 10 gallons of gas. – The amount of gas in each car will change without affecting the amount of gas in the other cars. – These are called instance variables. – All instances (objects) of a class will have a set of instance variables that are specific to the individual object.

Parameter Instance Values Clint's Porsche Jessica's BMW John's Lamborghini Car GasLevel GasLevel = 10 GasLevel = 4 GasLevel = 9

Parameter Values •The class definitions may also include parameter definitions that represent data that is shared by all class instances (objects). –In the case of a the car class we will define a maximum gas level that represents the amount of gas the gas tanks can hold. This will be the same for each individual car. –These types of parameters are called class variables.

Class Parameter Values Clint's Porsche Jessica's BMW John's Lamborghini Car GasLevel MaxGasLevel = 18 GasLevel = 10 GasLevel = 4 GasLevel = 9

Parameter Values •Class variables may also be used to keep track of things like how many instances of a class exist. –For our car example, lets create a counter the records how many cars we have built.

Class Parameter Values Clint's Porsche Jessica's BMW John's Lamborghini Car GasLevel MaxGasLevel = 18 NumCars = 3 GasLevel = 10 GasLevel = 4 GasLevel = 9

Constant Parameters •It is possible that you will have a parameter whose value should not change while your program is running. Such a parameter is called a constant. –The MaxGasLevel parameter that we defined for the Car class is a constant. The amount of gas the gas tank can hold will not change once the car has been built.

Messages •Objects communicate much like humans do, they pass messages back and forth to one another. –Each message has three components: •The object to whom the message is addressed. •The name of the method to perform in that object. •Any parameters (variables) needed by that method.

Messages •When we "send a message" directly to another person our message will include: –An indication of who the message is intended for. –What task we want the person to do. –What the information is that they need to complete the task.

Messages •In object-oriented programming we must explicitly state to what object we are sending a message (the object name), what task (method) we want the object to complete, and any required information (parameters). – For instance, we will define a task to turn on the car's hazards in the Car class. If we want to tell Jessica to turn on her BMW's hazards we would say something like. • Jessica's BMW.turnOnHazards()

Messages Clint's Porsche Jessica's BMW John's Lamborghini Car GasLevel MaxGasLevel = 18 NumCars = 3 turnOnHazards() GasLevel = 10 turnOnHazards() GasLevel = 4 turnOnHazards() GasLevel = 9 turnOnHazards()

Messages and Methods •In order to process a message an object needs to have a method defined for the requested task. –For instance, we have to define a method to turn on the car's hazards. –A method is a small well defined piece of code that completes a specific task.

Instance Methods •Each class can have methods that are specific to each object. – This means that when the method is executed, it only affects the instance variables of the object. • If Jessica's BMW has 4 gallons of gas and she puts 6 more gallons of gas in her car, she now has 10 gallons. The amount of gas in Clint's Porsche and John's Lamborghini does not change because Jessica put gas in her BMW. – Methods that only affect the object's parameters are called instance methods.

Methods Clint's Porsche Jessica's BMW John's Lamborghini Car GasLevel MaxGasLevel = 18 NumCars = 3 turnOnHazards() addGas(amount) GasLevel = 10 turnOnHazards() addGas(amount) GasLevel = 10 turnOnHazards() addGas(amount) GasLevel = 9 turnOnHazards() addGas(amount)

Methods •It is also possible that you want information about an object, in this case you would define a method that sends (returns) a message back containing the 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 for our car.

Methods Clint's Porsche Jessica's BMW John's Lamborghini Car GasLevel MaxGasLevel = 18 NumCars = 3 turnOnHazards() addGas(amount) getGasLevel():GasLevel GasLevel = 10 turnOnHazards() addGas(amount) getGasLevel():GasLevel GasLevel = 4 turnOnHazards() addGas(amount) getGasLevel():GasLevel GasLevel = 9 turnOnHazards() addGas(amount) getGasLevel():GasLevel

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. •Every time we build a new car we need to add one more to the number of cars we have built. •Also, we may want to know how many cars total we have built.

Methods Clint's Porsche Jessica's BMW John's Lamborghini Car GasLevel MaxGasLevel = 18 NumCars = 3 turnOnHazards() addGas(amount) getGasLevel():GasLevel incrementNumCars() howManyCars() GasLevel = 10 turnOnHazards() addGas(amount) getGasLevel():GasLevel GasLevel = 4 turnOnHazards() addGas(amount) getGasLevel():GasLevel GasLevel = 9 turnOnHazards() addGas(amount) getGasLevel():GasLevel

Object-Oriented Programming •When writing object-oriented programs, first one must define the classes (like car). •Then while the program in running the instances of the classes (or objects) (like Jessica's BMW, Clint's Porsche, and John's Lamborghini) are created.

Object-Oriented Programming •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. –Examples: Word, Excel, Web pages.

Object-Oriented Programming •Benefits of Object-oriented programming: –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 the other objects can not directly access the instance variables, Information Hiding.

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 the higher level class’ instance variables and methods. They also contain their own instance variables and methods beyond the higher level class definition.

Inheritance •The higher level classes are called Superclasses and the new or lower level classes are called the subclasses. –A subclass can always become 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.

Inheritance •Our Car class is very general. •Let's define a new class called BMW that contains the parameters: model, color, engine size.

Inheritance Car GasLevel MaxGasLevel = 18 NumCars = 3 turnOnHazards() addGas(amount) getGasLevel():GasLevel incrementNumCars() howManyCars() BMW Model Color EngineSize GasLevel turnOnHazards() addGas(amount) getGasLevel():GasLevel

Inheritance •Now let's define two new classes. One for the Z3 and another for the 3 Series Sedan. –What would be some of the differences between the two classes?

Inheritance Car GasLevel MaxGasLevel = 18 NumCars = 3 turnOnHazards() addGas(amount) getGasLevel():GasLevel incrementNumCars() howManyCars() BMW Model Color EngineSize GasLevel turnOnHazards() addGas(amount) getGasLevel():GasLevel Z3 Model Color EngineSize GasLevel turnOnHazards() addGas(amount) getGasLevel():GasLevel 3 Series Sedan Model Color EngineSize NumDoors GasLevel turnOnHazards() addGas(amount) getGasLevel():GasLevel

Primitive Data Types • Primitive data types are basic data such as numbers, characters, and Boolean values.

Primitive Data Types • Java has four basic data types: – integer numbers •10, , -8 – real numbers •5.564, – boolean •true, false – characters (Unicode)  ‘a’, ‘&’, ‘z’, ‘?’

Primitive Data Types • The Java primitive data types each have a set size (bits) and value range. – This size and range are the same on every type of computer (PC, Mac, Sun workstation) that runs Java programs. •This is not true with other programming languages.

Integer Values • Integer data types: – byte uses 8 bits and represents the numbers -128 to 127 – short uses 16 bits and represents the numbers -32,768 to 32,768 – int uses 32 bits and represents the numbers -2,147,483,648 to 2,147,483,647 •usually the type you will use

Integer Values • Integer data types: – long uses 64 bits and represents the numbers -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807  When typing long integer constants into a program the number must have an ‘L’ at the end of the number. – L

Real Values • Real or floating point numbers are represented in scientific notation inside a computer. The number is divided up into two parts the mantissa and the exponent. – The mantissa contains a number between -1.0 and 1.0 – The exponent contains the power of 2 required to scale the number to its actual value. – Value = mantissa * 2 exponent

Real Values • Real numbers are characterized by their precision and range. – Precision represents the number of significant digits that is represented in the number. •Precision depends upon the number of bits in the mantissa. – Range represents the difference between the largest and smallest numbers that can be represented. •Range depends upon the number of bits in the exponent.

Real Values • Real data types: – float uses 32 bits and represents the numbers E+38 to E+38 •The mantissa is 24 bits and the exponent is 8 bits. •The float type permits 6 to 7 decimal digits of precision.

Real Values • Real data types: – double uses 64 bits and represents the numbers E+308 to E+308. •The mantissa is 53 bits and the exponent is 11 bits •The double type permits 15 to 16 decimal digits of precision. •The double data type is more precise than float.

Real Values • Round off Error – When a double is stored to a float, part of the number is lost because a double is much larger than a float. The float can not represent the same exact number. In this situation the seven most significant digits are preserved and the rest of the information is lost. •It is important to know which real data type is required for your calculation.

Character Values • Character Values: – may represent both printable and non- printable characters  some printable characters are: ‘a’, ‘%’  some non-printable characters are: newline - ‘\n’, tab - ‘\t’, carriage return - ‘\r’ – What is unique about these characters?

Character Values • Character data types:  char uses 16 bits and represents the characters ‘\u0000’ to ‘\uFFFF’ •Unicode character set which represents almost every modern language.

Boolean Values • Boolean data types – boolean uses 1 bit and represents the values true and false.