1 INF160 IS Development Environments AUBG, COS dept Lecture 13 Title: Classes: Introduction & Review (Extract from Syllabus)

Slides:



Advertisements
Similar presentations
CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
Advertisements

George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Road Map Introduction to object oriented programming. Classes
Information Hiding Chapter 5: Classes and Objects in Depth.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Objects & Object-Oriented Programming (OOP) CSC 1401: Introduction to Programming with Java Week 15 – Lecture 1 Wanda M. Kunkle.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Introduction to methods Chapter 5: Classes and Objects in Depth.
ASP.NET Programming with C# and SQL Server First Edition
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
C++ fundamentals.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice.
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.
Classes, Objects, and Methods
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Classes and Objects. Topics The Class Definition Declaring Instance Member Variables Writing Instance Member Methods Creating Objects Sending Messages.
Programming With Java ICS201 University Of Ha’il1 Chapter 8 Polymorphism and Abstract Classes.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Classes and Objects noun A word used to denote or name a person, place, thing, quality, or act. verb That part of speech that expresses existence,
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
Classes CS 21a: Introduction to Computing I First Semester,
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
Lecture Set 11 Creating and Using Classes Part B – Class Features – Constructors, Methods, Fields, Properties, Shared Data.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Review of ICS 102. Lecture Objectives To review the major topics covered in ICS 102 course Refresh the memory and get ready for the new adventure of ICS.
Prepared by: Elsy Torres Shajida Berry Siobhan Westby.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
1 Working with Objects. 2 Defining a Class in VB.NET A class is a user-defined data type You can declare it as part of a module, but usually in a separate.
Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
1 OOP - An Introduction ISQS 6337 John R. Durrett.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
CIS 270—Application Development II Chapter 8—Classes and Objects: A Deeper Look.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
1 COS240 O-O Languages AUBG, COS dept Lecture 12 Title: Java Classes and Objects Reference: COS240 Syllabus.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Object-Oriented Programming (OOP) What we did was: (Procedural Programming) a logical procedure that takes input data, processes it, and produces output.
Classes, Interfaces and Packages
IT293 Intermediate Visual Basic Programming Unit 8: Object-oriented I Sydney Liles AIM: sydneyliles.
Object Oriented Programming Session # 03.  Abstraction: Process of forming of general and relevant information from a complex scenarios.  Encapsulation:
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
Presented by Ted Higgins, SQL Server DBA An Introduction to Object – Oriented Programming.
OOP Basics Classes & Methods (c) IDMS/SQL News
Class Fundamentals BCIS 3680 Enterprise Programming.
Object-Oriented Programming (OOP) and C++
Object-Oriented Programming: Classes and Objects Chapter 1 1.
Classes and Objects. Object vs. Class Introduction to OOPDr. S. GANNOUNI & Dr. A. TOUIRPage 2  A class could be considered as a set of objects having.
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 4th Edition.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Introduction of Object Oriented Programming.
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
Object-Oriented Programming (part 1 – Data Encapsulation)
(to be given after OOP lectures)
Creating Your Own Classes
Programming Logic and Design Seventh Edition
Object-Oriented Programming: Classes and Objects
Chapter 11 – Object-Oriented Programming
Classes, Objects, and Methods
Chapter 5: Classes and Objects in Depth
Chapter 5: Classes and Objects in Depth
Object-Oriented Programming
Chapter 5: Classes and Objects in Depth
Defining Classes and Methods
Presentation transcript:

1 INF160 IS Development Environments AUBG, COS dept Lecture 13 Title: Classes: Introduction & Review (Extract from Syllabus)

2 Lecture Contents: §OOP: Classes, Objects, Data members, Methods §Classes in VBasic §Classes in C++ §Classes in C# §Classes in Java §Applying Classes to build primitive IS

3 Demo Programs: Group name:Test160dClass §Test160dClassVB (Module1.vb) §Test160dClassCPP §Test160dClassC#(Program.cs) §Test160dClassJava

4 Formal introduction to concept of classes/objects §Transition §From Structured Programming to OOP §From Think in Functions to Think in Objects §From Algorithms + Data Structures = Programs To Classes + Objects = Programs

5 Formal introduction to concept of classes/objects §OOP (Object Oriented Programming): §Data Encapsulation & Data Hiding §Inheritance §Polymorphism

6 The 3 main OOP characteristics §Data Encapsulation/Data Hiding l Data and its functions are said to be encapsulated into a single entity. l Data is concealed within a class, so that it cannot be accessed mistakenly by functions outside the class. §Inheritance §Polymorphism

7 The 3 main OOP characteristics §Data Encapsulation and Data Hiding §Inheritance l The process of creating new classes, called derived classes from existing classes or base classes §Polymorphism

8 The 3 main OOP characteristics §Data Encapsulation and Data Hiding §Inheritance §Polymorphism l Generally, the ability to appear in many forms l More specifically, in OOP it is the ability to redefine methods for derived classes l Ability to process objects differently depending on their data type or classdata typeclass l Giving different meanings to the same thing

9 Informal introduction to concept of classes/objects §Programs = Code + Data

10 Informal introduction to concept of classes/objects §Programs = Code + Data §The Code concept §Code includes statements organized in routines §Code (including statements and routines) reads input data, processes data, generates output data

11 Informal introduction to concept of classes/objects §Programs = Code + Data §The Data concept §Scalar data §Data collections/Data containers §Evolution of the data concept

12 Informal introduction to concept of classes/objects §Evolution of the data concept §Scalar data §Simple data type or Data type used to store a single value. §Scalar variable stores a single value.

13 Informal introduction to concept of classes/objects §Evolution of the data concept §Data collections/Data containers §Data container: composite of related data items stored in memory under the same name. l Arrays l Structures (also named Records) l Classes

14 Informal introduction to concept of classes/objects §Evolution of the data concept §Data collections/Data containers: Arrays l Array: a homogeneous collection of data items of the same type.

15 Informal introduction to concept of classes/objects §Evolution of the data concept  Data collections/Data containers : S tructures l Structure/Record/: A heterogeneous collection of simple variables l A data type for a structurerecord composed of multiple components. These components may be the same data type or may be not the same data type.

16 Informal introduction to concept of classes/objects §Evolution of the data concept §Data collections/Data containers l Classes = Structures + Routines

17 Informal introduction to concept of classes/objects §Evolution of the data concept §Data collections/Data containers l Classes = Structures + Routines l Classes = Data Items + Member Functions

18 Informal introduction to concept of classes/objects §Evolution of the data concept §Data collections/Data containers l Classes = Structures + Routines l Classes = Data Items + Member Functions l Classes = Data Fields + Methods

19 Informal introduction to concept of classes/objects §Classes = Abstract Data Types §Classes = User Defined Data Types

20 Informal introduction to concept of classes/objects §The relation basic type - variable l int age; l float score; §The relation class – object, class - instance l Person mike, marry; l SmallObj s; l SparePart pieces[10];

21 Informal introduction to concept of classes/objects §Access to data fields and methods is regulated through access modifiers: l Private l Protected l Public §Usually data fields are private §Usually methods are public

22 Informal introduction to concept of classes/objects §Specific methods: §Constructors §Destructor

23 Informal introduction to concept of classes/objects §Specific methods: §Accessor / Getter methods §Mutator / Setter methods

24 Informal introduction to concept of classes/objects §Properties: §Getter and Setter methods interpreted as data fields

25 Classes in VBasic §D§D

26 The class is essentially an extension of the structure. The class is = structure + Sub/Function procedures. Class = S tructure + procedure(s) Sub/Function procedures are member procedures. Sub/Function procedures are also called methods.

27 Example In a professor’s program to assign and display semester grades, a student object might hold a single student’s name, an ID number, midterm mark and final mark. A CalcSemGrade() method might calculate the student’s semester grade.

28 Example In a computer program, a Counter object might be used as a GP programming element to count up and count down integer values. IncCount() and DecCount() methods would tell the object to calculate for example going up and going down iterations.

29 Class Counter Private m_count As Integer ‘ data member Public Sub GetData()‘ method – member function Console.Write("Enter data:") m_count = CInt(Console.ReadLine()) End Sub Public Sub ShowData() ‘ method – member function Console.WriteLine("Counter data is = {0}", m_count) End Sub Public Sub IncCount() ‘ method – member function m_count = m_count + 1 End Sub Public Sub DecCount() ‘ method – member function m_count = m_count - 1 End Sub End Class

30 Dim a As Counter a = New Counter a.GetData() a.IncCount() : a.IncCount() : a.IncCount() a.ShowData()

31 Example In a computer program, a Distance object might be used as a GP programming element to explore English measure system based on feet and inches. AddDist1 and AddDist2 methods would tell the object to add for example two English style distances.

32 Class Distance Private m_feet As Integer, m_inches As Single‘ data members Public Sub GetData() ‘ method – member function Console.Write("Enter feet:") m_feet = CInt(Console.ReadLine()) Console.Write("Enter inches:") m_inches = CSng(Console.ReadLine()) End Sub Public Sub ShowData() ‘ method – member function Console.WriteLine("Distance data is feet={0} inches={1}", m_feet, m_inches) End Sub Public Sub AddDist1(ByVal d1 As Distance, ByVal d2 As Distance) m_feet = d1.m_feet + d2.m_feet m_inches = d1.m_inches + d2.m_inches End Sub Public Function AddDist2(ByVal d As Distance) As Distance Dim temp As Distance temp = New Distance temp.m_feet = m_feet + d.m_feet temp.m_inches = m_inches + d.m_inches Return temp End Function End Class

33 Dim aa, bb, cc As Distance aa = New Distance(3, 3.0) bb = New Distance(6, 6.0) cc = New Distance aa.ShowData() : bb.ShowData() : cc.ShowData() cc.AddDist1(aa, bb) cc.ShowData() cc = aa.AddDist2(bb) cc.ShowData()

34 General Template for a Class Class className Private member variable declarations Public Property X... End Property Sub A()... End Sub End Class Properties Methods Interface

35 Private Data The following declaration may be inside a class: Private m_name As type The word Private is used to ensure that the variable cannot be accessed directly from outside the class – data hiding. The variable is an internal variable, local to the class. m_name is called an instance variable or member variable. It is used to hold the value of a property.

36 Example Class Student Private m_name As String ‘Name Private m_id As String ‘ID Private m_midterm As Double ‘Midterm mark Private m_final As Double ‘Final mark... End Class Dim Pupil As Student‘pupil is of type Student Pupil = New Student()‘create instance of pupil ‘OR Dim Pupil As New Student()

37 Property Block Member variables are only accessed indirectly via a Property block. Property values are set (i.e. written) by the Set procedure Property values are got (i.e. read) by the Get procedure Attention! The Property concept comes to systematically/formally replace/substitute the pair of methods introduced as GetData() & ShowData() as in the Counter/Distance examples of class templates You are recommended to use Properties instead of GetData()/ShowData()-like class methods

38 Get and Set Public Property Name() As String Get Return m_name ‘ read value of property End Get Set(ByVal Value As String) m_name = Value ‘ write value of property End Set End Property Property block Example: External identifier of property – may be accessed

39 Example class Counter with property block Class Counter Private m_count As Integer... Public Property Count() As Integer Get Return m_count ' read value of property End Get Set(ByVal param As Integer) m_count = param ' write value of property End Set End Property... End Class

40 Example class Counter with property block Dim d As New Counter d.Count = 880 Console.WriteLine _ ("Counter data displayed through property = {0}", d.Count) Dim var As Integer var = d.Count Console.WriteLine _ ("Counter data displayed through assignment = {0}", var)

41 Object Constructors Each class may have one or more special method(s) called constructor(s) that is/are always invoked when the object is instantiated. The constructor(s) may take or may not take parameters. It is used to perform tasks to initialize the object. The first line of no-argument constructor has the form: Public Sub New() The first line of constructor with argument(s) has the form: Public Sub New(ByVal par1 As dataType,...)

42 ‘ Constructors with direct access to data member ‘ m_count Class Counter Private m_count As Integer... Public Sub New() m_count = 0 End Sub Public Sub New(ByVal val As Integer) m_count = val End Sub... End Class

43 ‘ Constructors with indirect access to data member ‘ m_count using Property block Class Counter Private m_count As Integer Public Property Count() As Integer Get Return m_count ' read value of property End Get Set(ByVal param As Integer) m_count = param ' write value of property End Set End Property Public Sub New() Count = 0 End Sub Public Sub New(ByVal val As Integer) Count = val End Sub End Class

44 Dim b, c As Counter b = New Counter b.ShowData() c = New Counter(20) c.ShowData()

45 Classes in VB §Open the source text Module1.vb §Compile and run the VB program §Expand the functionality of the Book class with data fields and methods for the book author, for the book publisher, for the book number of pages and for the price of the book.

46 Classes in C++ §D§D

47 Classes in C++ §Open the source text TEst160dClassesCPP.cpp §Compile and run the C++ program §Expand the functionality of the Book class with data fields and methods for the book author, for the book publisher, for the book number of pages and for the price of the book.

48 Classes in C++ class Book { private: string m_Title; public: Book() { m_Title = " ";} Book(string pb) { m_Title = pb; } // method accessor string getTitle() { return m_Title; } // methods mutators void setTitle(string pb) { m_Title = pb; } void printBook() { cout << endl; cout << m_Title; cout << endl; } };

49 Classes in C++ int main() { Book myBook; myBook.setTitle("Formal Languages and Language Prcessors"); cout << endl; cout << myBook.getTitle(); cout << endl; Book linBook("Operating Systems"); cout << endl; cout << linBook.getTitle(); cout << endl; myBook.printBook(); linBook.printBook(); return 0; }

50 Classes in C# §D§D

51 Classes in C# §Open the source text Program.cs §Compile and run the C# program §Expand the functionality of the Book class with data fields and methods for the book author, for the book publisher, for the book number of pages and for the price of the book.

52 Classes in C# class Book { private string m_Title; public Book() { m_Title = " ";} public Book(string pb) { m_Title = pb; } // method accessor public string getTitle() { return m_Title; } // methods mutators public void setTitle(string pb) { m_Title = pb; } // property Title for data member m_Title public string Title // property { get { return m_Title; } set { m_Title = value; } } // end of property public void printBook() { Console.WriteLine(); Console.WriteLine(" {0}",m_Title); Console.WriteLine(); } } // end of class Book

53 Classes in C# class Program { static void Main(string[] args) { Book myBook = new Book(); myBook.Title = "Formal Languages & Language Processors"; // property // or myBook.setTitle("Formal Languages & Language Processors"); // setter/mutator method //myBook.Publisher() = "TU SOfia"; //myBook.NumPages() = 190; //myBook.Price() = 5.0; Book linBook = new Book("Operating Systems"); //' printing Book structure Console.WriteLine(); Console.WriteLine("{0}", myBook.Title); // property // or Console.WriteLine("{0}", myBook.getTitle() ); // getter method Console.WriteLine(); myBook.printBook(); linBook.printBook(); }// end of Main }

54 Classes in Java §D§D

55 Classes in Java §Open source text Test160dClassesJava.java §Compile and run the Java program §Expand the functionality of the Book class with data fields and methods for the book author, for the book publisher, for the book number of pages and for the price of the book.

56 Classes in Java class Book { private String m_Title; public Book() { m_Title = " ";} public Book(String pb) { m_Title = pb; } // method accessor public String getTitle() { return m_Title; } // methods mutators public void setTitle(String pb) { m_Title = pb; } // property Title for data member m_Title // NO PROPERTIES in JAVA public void printBook() { System.out.println(); System.out.println(" "+ m_Title); System.out.println(); } } // end of class Book

57 Classes in Java public static void main(String[] args) { Book myBook = new Book(); //myBook.Author = "lin & Bon"; myBook.setTitle("Formal Languages & Language Processors"); // set method //myBook.Publisher() = "TU SOfia"; //myBook.NumPages() = 190; //myBook.Price() = 5.0; Book linBook = new Book("Operating Systems"); //' printing Book structure System.out.println(); System.out.println(" " + myBook.getTitle() ); // getter method System.out.println(); myBook.printBook(); linBook.printBook(); }

58 Applying Classes to build primitive IS §Examples of elementary IS: l Books l Journals l Family members l Students in INF160 class §Approaches to develop simple IS: l Using many scalar variables l Using parallel arrays l Using Class template and array of Classes

59 Using many scalar variables §E§E

60 Using parallel arrays §E§E

61 Using Class template and array of Classes §E§E

62 §To introduce UML before BlueJ

63 Class SmallObj class SmallObj { private: int somedata; public: void SetData(int d) { somedata = d; } void ShowData() { cout << "\nData is =" << somedata; } };

64 SmallObj – UML class diagram SmallObj somedata -somedata SetData(int) +SetData(int) ShowData() +ShowData()

65 SmallObj – UML class diagram after G.Booch cloud form SmallObj SmallObj somedata -somedata SetData(int) +SetData(int) ShowData() +ShowData()

66 Thank You For Your Attention!