1 / 30

Introduction to Object Based Programming Lecture Notes 2

This lecture provides an introduction to object-based programming, covering topics such as classes, objects, encapsulation, and data hiding. Examples of elevator and passenger classes are also discussed. Includes code examples in C++ and Java.

bmoyer
Download Presentation

Introduction to Object Based Programming Lecture Notes 2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Object Based ProgrammingLecture Notes 2

  2. What is a Class ? • An abstract Data type (ADT) • A Blue Print of an object • An object template • Examples • class of elevators • class of nodes • class of Choo-Choo Trains • A Class is made up of ( Encapsulation) • State (Data) • Behavior (Functions)

  3. What is an Object ? • Object is an instance of the class template • Examples • South Elevator is an instance of elevator class. • First Node is an instance of node class. • Mickey is an instance of Choo-Choo Train class. • Each object has • Its own State (Data) • passengers, floor#, up/down • Behavior defined by class(Functions) • MoveToNextFloor, PickPassenger, DropPassenger class D C A B

  4. More about Classes • Class members can be • public (open) • Private (data hiding) • Public Members • can be accessed from anywhere • Its state can be changed • Public behavior is the interface • Private Members • Can only be accessed from its members • Concept of Data hiding

  5. An Example - Elevator Class 5 • Defining Behavior (public) • GetFloor • ShowStatus • MoveToNextFloor • PickPassenger • DropPassenger • Defining State (private) • CurrentFloor • Direction • ElevatorId • FloorWaitingList • LastServiceDate • MaxPassengers • CurrentPassengers 4

  6. An Example - Passenger Class 5 • Defining Behavior (public) • SetPassenger; • CallElevator • ShowStatus • FloorNum • Defining State (private) • PassengerId • Elevator • Origin • Destination • Download C++ Elevator Demo 4

  7. Structure of a JAVA class template Import <package-name>.*; Public class <class-name> extends <some other class> implements <an interface>{ //declaration of instance variables Private <data-type> <name> Public <class-name>() { } // constructor method Public <return-type> <method-name> ( <parameter-list>) { } }

  8. Code Explained Import <package>.* • Gives access to other classes in a package. • Package is a group of related classes • Eg: java.io • Eg: java.swing • There may be more than one package imported

  9. Code explaned • Public • Class definitions begin with the keyword public • Class is accessible to all clients • Name of a class • User defined • Rules for naming classes • Start classes with capital letters, variable and method names with lower case • Extends • Classes are organized in hierarchy • If class A is above class B, then A is called a superclass or parent and B is called subclass or child • Concept of Inheritance

  10. Code explaned • Implements • New class may optionally implement an interface • A list of methods that the implementing class must define Eg://speaker.java Public interface Speaker { public void speak(); public void announce(String str); } //philosopher.java public class Philosopher implements Speaker{ public void speak(){ ….. } } • Private • Instance variable are always declared to be private • Class clients cannot refer to private variables directly • Download Karel J. Robot Demo • Download Employee Class Demo

  11. Classes: Where are we heading? • First learn to read and use classes • Next learn to modify classes and write your own classes • If you want, take a look at the function definitions in the elevator.cpp and passenger.cpp files matching the corresponding .h files • Finally, we will learn how to use inheritance to derive new classes from old ones (a very fast and powerful way to create new classes that are variations of base classes) Together, these three activities form the basis of “object- oriented programming”

  12. An Example - Time Class • Defining Behavior (public) • SetTime • ShowCurrentTime • GetTime • SetSeconds, SetMinutes, SetHour etc.. • GetSeconds, GetMinutes, GetHour etc.. • ShowTime • Defining State (private) • Seconds • Minutes • Hour

  13. Objects vs Variables • Similarities between Objects and Variables • Both can be declared, initialized, used as an argument to an operator with the appropriate prototype (and be changed, if it is passed as a reference argument, to a state change operator) • Differences between Objects and Variables • An object is constructed from a class in a library; a variable is declared from a type built-in to JAVA • String, boolean, int, char, double • An object typically stores more complicated state (multiple values); a variable stores simple state (one value) • An object can be used in a special way to call member functions of its class; a variable cannot be used in this way • Robot.Move()

  14. Classes and Objects • Analogies • Class º Type (classes are programmer-defined types) • Object º Variable (objects are variables with lots of state) • Declarations of objects and variables are similar • Variables are declared from a type (in a declaration) • Variable declaration syntax: type variable=initial-value; • Objects are constructed from a class (in a declaration) • Object construction syntax: classobject(initial-values); • Objects can be used to call member functions (which can return values or change an object’s state) • Member function call syntax: object.function(arguments) • Example: the Account class Account MyAccount; //Construct an Account object MyAccount = new Account(12345, 45.67); MyAccount.BalanceInquiry(); //Returns the balance inquiry info

  15. More on Classes • Class declarations specify both State (data) and Behavior • Complexity is distributed among the classes that a program uses • Programmers must understand the behavior (operators and functions) declared in a class to use such constructed objects in their programs; choose meaningful names for functions • A class can be viewed as a library of functions that can be applied to objects constructed from that class

  16. Class Relationships • In OOP, the most important task is to identify the classes • The next step is to identify the relationships among those classes • The main relationships between classes/objects • Aggregation (has-a) • A computer has a CPU • A student has an ID card • Inheritance (is-a) • An apple is a fruit • A manager is an employee • Association: one-to-one, one-to-many, many-to-many

  17. The (Grady) Booch Notation • The icon to represent a class: cloud/ blob/ameba Has-a Computer CPU 1 1 ID Card Student 1 0 .. N

  18. Another Example Engine Car 1 1 4 .. N Cylinder

  19. Inheritance (is-a) Notation Employee Manager

  20. Unified Modeling Language(UML) Person Person • A class is represented by a rectangle Name Address ... Attributes Abbreviated Notation getName( ) setAddrs() Operations

  21. Elevator Object Elevator Elevator elevatorID numPassenger Attributes Abbreviated Notation moveToNextFloor() Operations

  22. Passenger Object Passenger Passenger PassengerID Origin Attributes Abbreviated Notation CallElevator() Operations

  23. Elevator Control System Design • Three Major Components • Elevator • Passenger • Control System • System Specifications • Control system is the link between elevator and passenger • Passengers send CallElevator() message to Control System • Control System allocates the resources

  24. Elevator Control System Design ctd.. • System Specifications ctd.. • Elevators will maintain its own list of passengers for internal purposes • Passenger count • Where to move next etc.. • Passengers can call a specific elevator or any elevator in a multiple elevator building. The decision is made by the control system • The control system will maintain a list of the elevators and where they are.. And also list of waiting passengers and where they are

  25. Elevator Control System Design ctd.. • System Specifications ctd.. • Once a passenger boards an elevator, it is elevators responsibility to serve the passenger. The control system will update the passenger status • Once a passenger is served, elevator notifies the control system. • The all passenger list and all elevator list is maintained by the control system • Add more specifications

  26. Elevator Class • Attributes • numPassengers 0.. maxPassengers • currentFloor .. 0..maxFloors • Direction … up..down..idle • ID .. 1..maxElevators • passengerList • Array of records • ID, STATUS, Origin, Destination • …..

  27. Elevator Class • Operations • moveToNextFloor() • pickPassenger() • dropPassenger() • showCurrentFloor() • showCurrentDirection() • …..

  28. Passenger Class • Attributes • ID • Origin • Destination • Status – waiting, onBoard, served • ……

  29. Passenger Class • Operations • callElevator() • pickFloor() • showCurrentFloor() • …… • Describe a one elevator multiple passenger scenario

  30. A Scenario • Control System creates an elevator in floor 0 • Control System creates 10 passengers on random floors • At the time passengers are created, we only know their origin and the direction they want to move • All passengers call an elevator • Control system decides the closest passenger(s) to serve and let the elevator know passenger ID’s and floor number • After initial passengers are picked, they select the destinations • …..

More Related