Week 3 Object-based Programming: Classes and Objects

Slides:



Advertisements
Similar presentations
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 10: Continuing with classes Constructors, using classes.
Advertisements

 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
INSTRUCTOR: SHIH-SHINH HUANG Windows Programming Using Java Chapter3: Introduction to Classes and Objects 1.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Java: How to Program Methods Summary Yingcai Xiao.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
OBJECT ORIENTED PROGRAMMING CONCEPTS ISC 560. Object-oriented Concepts  Objects – things names with nouns  Classes – classifications (groups) of similar.
You gotta be cool. Introduction to Classes, Objects and Strings Introduction Defining a Class with a Member Function Defining a Member Function with a.
1.  A method describes the internal mechanisms that actually perform its tasks  A class is used to house (among other things) a method ◦ A class that.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
Reformatted slides from the textbook, C++ How to Program, 6/e Pearson Education, Inc. All rights reserved Chapter 3. [Lecture 02] Introduction to.
Chapter 4 Introduction to Classes, Objects, Methods and strings
IFS410 Advanced Analysis and Design Week 1 Introduction to Java: Applications
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Chapter 3 (B) 3.5 – 3.7.  Variables declared in a function definition’s body are known as local variables and can be used only from the line of their.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 9: Continuing with classes.
Object Oriented Programming
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Classes Methods and Properties. Introduction to Classes and Objects In object-oriented programming terminology, a class is defined as a kind of programmer-defined.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
 2005 Pearson Education, Inc. All rights reserved. 1 Introduction to Classes and Objects.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Object Oriented Programming Session # 03.  Abstraction: Process of forming of general and relevant information from a complex scenarios.  Encapsulation:
YG - CS Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally.
OOP Basics Classes & Methods (c) IDMS/SQL News
An Introduction to Programming with C++ Fifth Edition Chapter 14 Classes and Objects.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
Introduction to Classes and Objects CS-2303, C-Term C++ Program Structure Typical C++ Programs consist of:– main –A function main –One or more classes.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
 2007 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
3 Introduction to Classes and Objects.
Introduction to Classes and Objects
Yanal Alahmad Java Workshop Yanal Alahmad
Yanal Alahmad Java Workshop Yanal Alahmad
Microsoft Visual Basic 2005: Reloaded Second Edition
Week 4 Object-Oriented Programming (1): Inheritance
Dr Shahriar Bijani Winter 2017
Object Based Programming
Chapter 3 Introduction to Classes, Objects Methods and Strings
IFS410: Advanced Analysis and Design
MSIS 670 Object-Oriented Software Engineering
Week 6 Object-Oriented Programming (2): Polymorphism
Introduction to Classes and Objects
Object Oriented Programming in java
Week 4 Object-based Programming (2) Classes and Objects: A Deeper Look
Introduction to Classes and Objects
Classes, Objects, Methods and Strings
CIS16 Application Development and Programming using Visual Basic.net
Object Oriented Programming in java
Final and Abstract Classes
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Classes and Objects Systems Programming.
Introduction to Classes and Objects
Presentation transcript:

Week 3 Object-based Programming: Classes and Objects MSIS670: Object-Oriented Software Engineering Week 3 Object-based Programming: Classes and Objects 12/5/2018 3.1

Programming Languages Procedural Programming Language Action-oriented Functions are units of programming Object-based Programming Language Objects/classes are units Encapsulation and Interfaces Object-oriented Programming Language Other “goodies” – Polymorphism, etc. Object-Oriented Programming Languages (such as Java) encapsulates data (attributes) and methods (behavior) in objects. Objects may allow other objects to communicate their encapsulated members through well-defined interfaces. Inheritance, Polymorphism, and other features that are considered as characteristics of true OOPL, will be discussed in the Chapter 9. In this section, the main focus is creating and using the object, which are enabled by object-based programming languages. 12/5/2018

Object-based Programming How to create objects How to use objects So far, we have learned how to create a Java application. Applets are mentioned briefly. Here, we introduce a third type of a class, which defines a type of objects. A Java application will self-execute by the method “main()”. A Java applet extends JApplet and also self-execute by the methods “init(), start(), and paint()”. This third type of classes will not execute by itself. When called from other objects (methods), this class simply creates an object described in this class, initializes the instance variables by its constructor, and waits for other instructions. 12/5/2018

Declaring a Class 12/5/2018 Fig 3.1: Class Declaration with one method The GradeBook class contains a displayMessage method )lines 7-10) that displays a message on a screen. This class does not have a method “main”. A class that has a main method is a Java application. Such a class is special because the Virtual Machine can use main to begin execution. Class GradeBook is not an application because it does not have main. Therefore, if youtry to execute GradeBook, you will get an error message. 12/5/2018

Instantiating an Object of a Class Fig. 3.2: Creating an Object of class GradeBook and calling its displayMessage method Now, this class, GradeBookTest, is an application, as it has a method “main”. Within the main method, this class is declaring a variable – myGradeBook. Variable myGradeBook is initialized with the result of the class instance creation expression – new GradeBook(). Keyword new creats a new object of the class specified by the “new.” Next, an expression myGradeBook.displayMessage(); Is calling the object’s method – displayMessage – by specifying which object (myGradeBook) and with dot separator (“.”) a name of method that the object should have defined. When executing this application after compiling both files, it will display the message "Welcome to the Grade Book!" 12/5/2018

Instance Variables, set Methods and get Methods Fig. 3.7: GradeBook class that contains a courseName as instance variable This class modifies the Fig. 3.1; not only the “displayMessage()” method, it has two more methods, and a declaration of an instance variable – courseName. Note that the instance variable is outside of any methods, and declared as private. Nobody else but the methods of objects of this class can touch that. In order to assign the value to the instance variable, this class uses the method –setCourseName( String name ) – where, “name” is the value. Also it has a getCourseName() method, which returns a String value of the contents of the variable courseName. Finally, the displayMessage() method will utilize this getCOurseName() method to display the value. 12/5/2018

Constructor Class Constructor Same name as class Initializes instance variables of a class object Called when program instantiates an object of that class Class can have several constructors through overloading (next class). When you want to use an object, which is defined by the programmer, a class (object) can call a class (actually class constructor) to instantiate an object of the class. ClassName ref = new ClassName( arguments ); ClassName indicates type of object created, ref is the reference to that object (used as a variable), and new indicates that a new object is created. Arguments specifies constructor argument values. This argument should matches the constructor arguments (number of variables, and each type) – c.f. Overloaded Constructors. e.g., Time1Test line 9, Time1 time = new Time1(); time is a new object of the type “Time1”, with no argument required. Then constructor initializes the object’s instance variables according to the specification of its constructor. 12/5/2018

Lab Activities (Week 3) Coding Assignment 3 class MyRectangle Two .java files. Both should be saved on a same project. Similar to the GradeBook example. For this assignment, you have to create a class MyRectangle and another class to test this class. Place them together in a package (project) before you start coding them. Turn in the coding as usual for both files. 12/5/2018