INTRO TO OO Object lessons on Objects. Objects 2  In Java, everything has a type  Primitive types (numbers / booleans / chars)  Complex types (classes)

Slides:



Advertisements
Similar presentations
L3:CSC © Dr. Basheer M. Nasef Lecture #3 By Dr. Basheer M. Nasef.
Advertisements

C++ Classes & Data Abstraction
Chapter 2: Using Objects Part 1. To learn about variables To understand the concepts of classes and objects To be able to call methods To learn about.
Written by: Dr. JJ Shepherd
The Object of Java Spring 2003 Wayne State College CSC 340 Lect. #1.
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
1 Objects and Classes Introduction to Classes Object Variables and Object References Instantiating Objects Using Methods in Objects Reading for this Lecture:
Unified Modeling Language (UML)
Structured Programming and UML Overview Session 2 LBSC 790 / INFM 718B Building the Human-Computer Interface.
Class template Describing a generic class Instantiating classes that are type- specific version of this generic class Also are called parameterized types.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
CS 225 Java Review. Java Applications A java application consists of one or more classes –Each class is in a separate file –Use the main class to start.
UML Class Diagram: class Rectangle
Introduction to Java Programming, 4E Y. Daniel Liang.
Advanced Java and Android Day 1 Object-Oriented Programming in Java Advanced Java and Android -- Day 11.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
Classes in C++ Bryce Boe 2012/08/15 CS32, Summer 2012 B.
Dale Roberts Object Oriented Programming using Java - Class Constructors Dale Roberts, Lecturer Computer Science, IUPUI Department.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
Object-Oriented Programming - Classes, Objects Methods, Strings 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
1 Chapter 2 (Cont.) The BA’s Perspective on Object Orientation.
Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.
1 Given the Radio class  We may define other derivative types: Cassette walkman IS-A radio Alarm clock radio IS-A radio Car radio IS-A radio.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
1 Introduction Modules  Most computer programs solve much larger problem than the examples in last sessions.  The problem is more manageable and easy.
1. 2 Reference... Student stu; Reference of Student stu When the reference is created it points to a null value. Before access the reference, objects.
Objective You will be able to define and identify the basic components of a java program by taking notes, seeing examples, and completing a lab. Construction.
Objects, Classes and Syntax Dr. Andrew Wallace PhD BEng(hons) EurIng
© 2006 Pearson Addison-Wesley. All rights reserved A DrawingGizmo object can be moved around a window to draw lines. It appears like an arrow. (See.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
Chapter 8 Objects and Classes Object Oriented programming Instructor: Dr. Essam H. Houssein.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Objects and Classes.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
© 2006 Pearson Addison-Wesley. All rights reserved DrawingGizmo « constructor » DrawingGizmo() « update » void moveForward() void turnClockwise()
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Written by: Dr. JJ Shepherd
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
OOP Basics Classes & Methods (c) IDMS/SQL News
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Lecture 9: Object and Classes Michael Hsu CSULA. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object.
CSCI 51 Introduction to Programming Dr. Joshua Stough February 26, 2009.
Class Inheritance Part I
Java Programming: Guided Learning with Early Objects
OBJECT ORIENTED PROGRAMMING II LECTURE 7 GEORGE KOUTSOGIANNAKIS
The Need for Specifications
INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING (OOP) & CONCEPTS
UML Class Diagram: class Rectangle
Chapter 3 Introduction to Classes, Objects Methods and Strings
Chapter 3 Introduction to Classes, Objects Methods and Strings
Corresponds with Chapter 7
Defining Classes and Methods
Implementing Non-Static Features
Implementing Classes Chapter 3.
User-Defined Classes and ADTs
Chapter 8 Classes User-Defined Classes and ADTs
Object Oriented Programming in java
Java Programming Language
Classes, Objects and Methods
Final and Abstract Classes
Chapter 7 Objects and Classes
Presentation transcript:

INTRO TO OO Object lessons on Objects

Objects 2  In Java, everything has a type  Primitive types (numbers / booleans / chars)  Complex types (classes)  An Object is a value of complex type  has attributes (what the object is - values)  has behavior (what the object can do - operations)

Consider an AntiqueRadio Object 3  What attributes of such a radio are important?  Is it on or off?  What station is it tuned to?  What is the volume setting?  What operations can be performed on such a radio?  Change the station setting  Turn it on/off  Turn it up/down

Classes and Objects 4  Every object belongs to a group of objects of the same type. This is called the object’s class.  Similar usage to the phrase “class-action lawsuit”  Examples  The 2008 superbowl is a value of type …  Wisconsin is a value of type …  Spiderman belongs to the class of …  A class is a definition of the attributes and behavior for some group of objects  Attributes are defined via variables (called instance variable) in Java  Operations are defined via methods in Java sporting event state super hero

A Radio Class 5  A Radio class would specify that a radio has  an on/off setting  a station setting  a volume setting  an on/off control  a station select control  a volume setting control.  This is a definition of a radio.

A Radio Object 6  A radio object of the Radio class  what do we know about such an object?  how many radio objects are there?  how many radio classes are there?  More precisely we say:  An instance of the Radio class  We instantiate Radio….

Class Diagrams 7  A class is a definition of a data type  Class diagrams are “simple” ways of representing the definition without programming  UML is a formal way of drawing class diagrams (and hence defining a class)  Classes can also be defined in Java Class Name Instance Variables Methods A box represents a classThe top “slot” contains the class nameThe middle “slot” contains attributesThe bottom “slot” contains operations

Class Diagrams 8 Class Name Instance Variables Methods PortableCDPlayer boolean containsDisk int totalNumberOfTracks int trackPlaying int secondsPlayed void insertCD() void removeCD() void play() void stop() void pause() void skipToNextTrack() EXERCISE: Draw a class diagram for a Vehicle class

Our first program…. 9  Consider a DrawingTool object  The class diagram is given below: DrawingTool «constructor» DrawingTool() «update» void moveForward() void turnClockwise() void dontDraw() void draw() If you were handed a DrawingTool object, what kinds of things could you do with it?

Method Calls 10  The method call is the basic programming instruction in Java  A method call is a command to an object to “do something” Syntax of method calls: objectReference.methodName(); objectReference refers to an object methodName is the name of any void operation supported by the object Assume “myPlayer” refers to a PortableCDPlayer Object Are the following valid method calls? myPlayer.insertCD(); myPlayer.draw() myPlayer.stop(); myPlayer.accelerate(); …

Example 11  Write a sequence of statements to draw a square using a DrawingTool object referred to as “pen”: pen.draw(); pen.moveForward(); pen.turnClockwise(); pen.moveForward(); pen.turnClockwise(); pen.moveForward(); pen.turnClockwise(); pen.moveForward(); pen.draw(); pen.moveForward(); pen.turnClockwise(); pen.moveForward(); pen.turnClockwise(); pen.moveForward(); pen.turnClockwise(); pen.moveForward(); DrawingTool «constructor» DrawingTool() «update» void moveForward() void turnClockwise() void dontDraw() void draw()

Declaration and Instantiation 12  Objects don’t exist until they are created  This is known as “instantiation”  Attempting to call a method on an un-created object is an error! Syntax of object creation: objectName = new ClassName(); objectName is the name of an object ClassName is the name of the class of object that is being constructed Example: how to create a DrawingTool named pen?

Declaration and Instantiation 13  Objects can’t be created until they are declared  THE GOLDEN RULE OF PROGRAMMING  DECLARE before creating  CREATE before using  USE Syntax of instance variable declaration: modifier ClassName objectName; modifier is either private, public, or protected ClassName is the name of the class of the object to be created objectName is the name of the object to be constructed Example: how to declare, create, and use a DrawingTool named pen?

Our first ‘real’ Class InstanceVarDecls StatementSequence ImportDecls Method publicclassClassName{ publicClassName(){ } } 14

Our first ‘real’ class public class Driver { private DrawingTool pen; public Driver() { pen = new DrawingTool(); pen.draw(); pen.moveForward(); pen.turnClockwise(); pen.moveForward(); pen.turnClockwise(); pen.moveForward(); pen.turnClockWise(); pen.moveForward(); } 15