INSTRUCTOR: SHIH-SHINH HUANG Windows Programming Using Java Chapter3: Introduction to Classes and Objects 1.

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.
What have we learned so far… Preprocessor directives Introduction to C++ Variable Declaration Display Messages on Screen Get Information from User Performed.
Chapter 10 THINKING IN OBJECTS 1 Object Oriented programming Instructor: Dr. Essam H. Houssein.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Classes and Objects Systems Programming.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Java: How to Program Methods Summary Yingcai Xiao.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Object-oriented Programming Concepts
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
ASP.NET Programming with C# and SQL Server First Edition
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
Object Oriented Software Development
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
Introduction to Object-oriented programming and software development Lecture 1.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
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.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
Java How to Program, Late Objects Version, 8/e © by Pearson Education, Inc. All Rights Reserved.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Reformatted slides from the textbook, C++ How to Program, 6/e Pearson Education, Inc. All rights reserved Chapter 3. [Lecture 02] Introduction to.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Chapter 4 Introduction to Classes, Objects, Methods and strings
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo CET 3640 © Copyright by Pearson Education, Inc. All Rights Reserved.
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.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
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.
 Objects versus Class  Three main concepts of OOP ◦ Encapsulation ◦ Inheritance ◦ Polymorphism  Method ◦ Parameterized ◦ Value-Returning.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 9: Continuing with classes.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
© 2007 Lawrenceville Press Slide 1 Chapter 8 Objects  A variable of a data type that is a class. Also called an instance of a class.  Stores data  Can.
 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.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
計算機程式語言 Lecture 03-1 國立台灣大學生物機電系 林達德 3 3 Introduction to Classes and Objects.
 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.
Topics Instance variables, set and get methods Encapsulation
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Chapter 3 Introduction to Classes, Objects Methods and Strings
3 Introduction to Classes and Objects.
Introduction to Classes and Objects
Yanal Alahmad Java Workshop Yanal Alahmad
Yanal Alahmad Java Workshop Yanal Alahmad
Chapter 3: Using Methods, Classes, and Objects
Dr Shahriar Bijani Winter 2017
Chapter 3 Introduction to Classes, Objects Methods and Strings
IFS410: Advanced Analysis and Design
Week 3 Object-based Programming: Classes and Objects
Introduction to Classes and Objects
Classes, Objects, Methods and Strings
Session 2: Introduction to Object Oriented Programming
Object Oriented Programming in java
Visual Programming Lecture 4.
Classes and Objects Systems Programming.
Introduction to Classes and Objects
Presentation transcript:

INSTRUCTOR: SHIH-SHINH HUANG Windows Programming Using Java Chapter3: Introduction to Classes and Objects 1

Contents Introduction Basic Object-Oriented Concept GradeBook Example Instance Variables Constructors 2

Introduction Evolution of High-Level Language 3

Introduction Unstructured Programming  The main program directly operates on the data that are declared as global variables.  The same statement sequence must be copied if it is needed at different places. 4

Introduction Procedural Programming  Combine a sequence of statements into a procedure with calls and returns.  The main program coordinates calls to procedures and hands over data as parameters. 5

Introduction Modular Programming  Procedures of a common functionality are grouped together into separate modules.  The main program coordinates calls to procedures in separate modules. 6

Introduction Problems of Modular Programming  No matter how well structured, large programs become excessively complex.  They allow unrestricted access to global data.  Attributes and behavior are separated such that they poorly model of the real world. 7

Basic Object-Oriented Concept What are Classes ?  A class is a blueprint defining the variables and methods of a kind of category.  Variables -> States  Methods -> Behaviors. 8 States (data or attributes) Weight Gear Implementation Type ….. Behaviors (operations) Brake Change Gear Change Cadence …..

Basic Object-Oriented Concept What are Objects?  An object is an instance of a certain class  An object is a software bundle of variables and related methods. 9

Basic Object-Oriented Concept Ideas of Object-Oriented Programming  Object-Orientation is a modeling technique that tries to imitate the way we think.  Software objects combing both data and functions model the real-world objects.  The data is hidden (Data Encapsulation).  The member function is the way to interact with the data. 10

Object-Oriented Concepts Object-Oriented Programming (OOP)  A program is considered as a set of interacting objects.  Objects interact to perform the task by sending messages to each other. 11

Basic Object-Oriented Concept Benefits of OOP  Reusability: Programmers can reuse the code in the superclass many times.  Software Prototyping: The developed class can be considered as the software IC which makes the system development more easier and efficient. 12

Basic Object-Oriented Concept Examples 13 AddressBo ok Member Socket Edit Dialog Chat Dialog Chat Log …………

Basic Object-Oriented Concept Examples 14 AddressBook Member Socket Edit Dialog Chat Dialog Chat Log …………

Class and Instance Class Declaration [modifier] class ClassName { //fields [modifiers] type FieldName [= initial value]; //constructors [modifiers] ClassName([arguments]){ //code } // methods [modifiers] type MethodName([argument]){ //code } }; 15

GradeBook Example Design of GradeBook  CourseName: attribute  Record the name of the course.  DisplayMessage(): method  Display the message to the users.  Perform the task without any arguments  Complete the task without result returned. String CourseName; void DisplayMessage(){ }/* End of DisplayMessage */ 16

GradeBook Example Class Declaration  Encapsulate all the related attributes and operations into a class.  Each class declaration contains keyword “class” followed by its name.  Every class’s body is enclosed in a pair of left and right braces “{“, “}”. public class GradeBook{ String CourseName; Void DisplayMessage(){ }/* End of DisplayMessage */ }/* End of class GradeBook */ 17

GradeBook Example Class Implementation  Write the statements to perform the desired operations. public class GradeBook{ String CourseName; Void DisplayMessage(){ System.out.println(“Welcome to Java Course!”); }/* End of DisplayMessage */ }/* End of class GradeBook */ 18

GradeBook Example GradeBook Tester  A class contains main() method that is used to control the application’s execution.  It creates a GradeBook object and uses it. public class GradeBookTest { public static void main(String args[]){ ……… }/* End of main */ }/* End of GradeBookTest */ GradeBookTest 19

GradeBook Example GradeBook Tester public class GradeBookTest { public static void main(String args[]){ GradeBook javaGradeBook = new GradeBook(); javaGradeBook.DisplayMessage(); }/* End of main */ }/* End of GradeBookTest */ blueprint: class new javaGradeBook:object 20

GradeBook Example public class GradeBook{ String CourseName; Void DisplayMessage(){ System.out.println(“Welcome to Java Course!”); }/* End of DisplayMessage */ }/* End of class GradeBook */ public class GradeBookTest { public static void main(String args[]){ GradeBook javaGradeBook = new GradeBook(); javaGradeBook.DisplayMessage(); }/* End of main */ }/* End of GradeBookTest */ 21

GradeBook Example Implementation without Object 22 public class GradeBookTest { public static void main(String args[]){ String CourseName; DisplayMessage(); }/* End of main */ public void DisplayMessage(){ System.out.println(“Welcome to Java Course!”); }/* End of DisplayMessage */ }/* End of GradeBookTest */

Instance Variables Description  Variables declared in the body of a particular method are known as local variables.  Variables declared inside a class but outside the bodies of class’s method are known as fields.  The fields in the created object represents the instance variables.  Each object (instance) of the class has a separate instance variable in memory. 23

Instance Variables public class GradeBook{ String CourseName; Void DisplayMessage(){ System.out.println(“Welcome to Java Course!”); }/* End of DisplayMessage */ }/* End of class GradeBook */ new javaGradeBook:object c++GradeBook:object 24

Instance Variables Access Modifiers  The access modifier is to identify whether the variables/methods are accessible outside.  private : accessible only to the methods of the class  public: accessible to the methods outside the class  Declaring instance variables wit the private modifier is known as data hiding.  The default modifier of Java is private. 25

Instance Variables public class GradeBook{ private String CourseName; Void DisplayMessage(){ …… }/* End of DisplayMessage */ }/* End of class GradeBook */ public class GradeBook{ public String CourseName; Void DisplayMessage(){ …… }/* End of DisplayMessage */ }/* End of class GradeBook */ 26

Instance Variables Set()/Get() Method  The fields in Java are generally designed as hiding for the purpose of data encapsulation.  The ways to access the hiding data are through the methods. 27

Instance Variables public class GradeBook{ String CourseName; public void SetCourseName( String name ){ courseName = name; // store the course name } // end method setCourseName public String GetCourseName(){ return courseName; } // end method getCourseName public void DisplayMessage(){ System.out.printf( "Welcome to the grade book for %s!\n", GetCourseName() ); }/* End of DisplayMessage */ }/* End of GradeBook */ 28

Instance Variables public class GradeBookTest { public static void main(String args[]){ GradeBook javaGradeBook = new GradeBook(); javaGradeBook.SetCourseName("Java"); javaGradeBook.DisplayMessage(); GradeBook cppGradeBook = new GradeBook(); cppGradeBook.SetCourseName("C++"); cppGradeBook.DisplayMessage(); }/* End of main */ }/* End of GradeBookTest */ Welcome to the grade book for Java! Welcome to the grade book for C++! 29

Instance Variables Implementation without Object 30 public class GradeBookTest { public static void main(String args[]){ ………… }/* End of main */ public void DisplayMessage(String str){ System.out.println(“Welcome to the grade book for %s”, str); }/* End of DisplayMessage */ public void SetCourseName(String& str1, String str2){ str1 = str2; }/* End of SetCourseName */ }/* End of GradeBookTest */

Instance Variables 31 Implementation without Object public class GradeBookTest { public static void main(String args[]){ String CourseName1; String CourseName2; SetCourseName1(CourseName1, “Java”); DisplayMessage(CourseName1); SetCourseName1(CourseName2, “C++); DisplayMessage(CourseName2); }/* End of main */ public void DisplayMessage(String str){……} public void SetCourseName(String str1, String str2){……} }/* End of GradeBookTest */

Constructor Description  The constructor is used to initialize the an object of a class when the object is created.  The keyword new calls the class’s constructor to perform the initialization.  By default, the complier provides a default constructor with no parameters.  In default constructor, all variables are set to their default values. 32

Constructor Declaration  A constructor must have the same name as its class.  Constructor cannot return values, even void.  Normally, constructors are declared as public. public class GradeBook{ String CourseName; /* default constructor provided by compilier */ public GradeBook() { courseName = NULL; } // end method setCourseName }/* End of GradeBook */ 33

Constructor Declaration  If you design your own constructor, the Java compiler will not create a default constructor. public class GradeBook{ String CourseName; /* default constructor provided by compilier */ public GradeBook() { courseName = NULL; } // end method setCourseName public GradeBook(String name) { courseName = name; } // end method setCourseName */ }/* End of GradeBook */ no default constructor 34

Constructor public class GradeBookTest { public static void main(String args[]){ GradeBook javaGradeBook = new GradeBook(); javaGradeBook.SetCourseName("Java"); javaGradeBook.DisplayMessage(); GradeBook cppGradeBook = new GradeBook(); cppGradeBook.SetCourseName("C++"); cppGradeBook.DisplayMessage(); }/* End of main */ }/* End of GradeBookTest */ Exception in thread "main" java.lang.Error: Unresolved compilation problems: The constructor GradeBook() is undefined 35

Constructor public class GradeBookTest { public static void main(String args[]){ GradeBook javaGradeBook = new GradeBook(“Java”); javaGradeBook.DisplayMessage(); GradeBook cppGradeBook = new GradeBook(“C++”); cppGradeBook.DisplayMessage(); }/* End of main */ }/* End of GradeBookTest */ Welcome to the grade book for Java! Welcome to the grade book for C++! 36

37