Liang, Introduction to Java Programming, Sixth Edition1 Objects and Classes Gang Qian Department of Computer Science University of Central Oklahoma.

Slides:



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

1 Chapter 8 Objects and Classes Lecture 2 Prepared by Muhanad Alkhalisy.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 7 Objects and Classes.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 8 Objects and Classes.
1 Chapter 2 Objects and Classes. 2 Objectives F To understand objects and classes and use classes to model objects. F To learn how to declare a class.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 7 Objects and Classes.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 9 Objects and Classes.
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.
Chapter 9 Objects and Classes
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
Syllabus (1) WeekChapters 1Introduction to the course, basic java language programming concepts: Primitive Data Types and Operations 1, 2 2Methods, Control.
DATA STRUCTURE Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Objects and Classes.
1 Objects and Classes. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object represents an entity.
Chapter 8. About the Midterm Exam.. Exam on March 12 Monday (Tentatively) Review on March 7 Wednesday Cover from Chapter 6 Grades will be out before spring.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 8 Objects and Classes.
Objects and Classes Chapter 6 CSCI CSCI 1302 – Objects and Classes2 Outline Introduction Defining Classes for Objects Constructing Objects Accessing.
1 Introducing Methods A method is a collection of statements that are grouped together to perform an operation.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 7 Objects and Classes.
1 Chapter 2 Objects and Classes. 2 Objectives F To understand objects and classes and use classes to model objects. F To learn how to declare a class.
1.  At the end of this slide, student able to:  Object-Oriented Programming  Research on OOP features.  Do a code walkthrough to examine the implementation.
Chapter 8 Objects and Classes Object Oriented programming Instructor: Dr. Essam H. Houssein.
Objects and Classes Mostafa Abdallah
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Objects and Classes.
1 COS240 O-O Languages AUBG, COS dept Lecture 12 Title: Java Classes and Objects Reference: COS240 Syllabus.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 7 Objects and Classes.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Objects and Classes.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Objects and Classes.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Objects and Classes.
1 Java Library Lecture 9 by Dr. Norazah Yusof. 2 Java Library Java has pre-defined classes that consist of the basic language classes in Java (organized.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 7 Objects and Classes.
CS 139 Objects Based on a lecture by Dr. Farzana Rahman Assistant Professor Department of Computer Science.
Introduction To Objects Oriented Programming Instructor: Mohammed Faisal.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 8 Objects and Classes.
1 Class and Object Lecture 7. 2 Classes Classes are constructs that define objects of the same type. A Java class uses instance variables to define data.
Chapter 7 Objects and Classes. OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object represents an entity.
CS 112 Programming 2 Lecture 02 Objects and Classes (1)
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Fall 2013 Chapter 8 Objects.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 7 Objects and Classes.
Object-Oriented Programming in Java. 2 CS2336: Object-Oriented Programming in Java Buzzwords interfacejavadoc encapsulation coupling cohesion polymorphic.
1 COS240 O-O Languages AUBG, COS dept Lecture 12 Title: Java Classes and Objects Reference: COS240 Syllabus.
1 Chapter 9 Objects and Classes. 2 OO Programming in Java Other than primitive data types (byte, short, int, long, float, double, char, boolean), everything.
Lecture 9: Object and Classes Michael Hsu CSULA. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Introduction of Object Oriented Programming.
Dr. Majed Abdouli © Objects and Classes 1 Dr. Majed Abdouli © 2015, adapted from Liang, Introduction to Java Programming, Eighth Edition, (c) 2011.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 8 Objects and Classes.
Chapter 7 Objects and Classes
Lecture 3: Introduction to Object and Classes
HIGHLEVEL REVIEW Chapter 9 Objects and Classes
Chapter 8 Objects and Classes Part 1
Chapter 9 Objects and Classes
Chapter 9 Objects and Classes
Chapter 8 Objects and Classes
Chapter 9 Objects and Classes
Chapter 6 Objects and Classes
Chapter 6 Objects and Classes
Chapter 8 Objects and Classes
Chapter 9 Objects and Classes
Chapter 9 Objects and Classes Part 01
Chapter 7 Objects and Classes
OO Programming Concepts
Chapter 9 Objects and Classes
Chapter 7 Objects and Classes
Presentation transcript:

Liang, Introduction to Java Programming, Sixth Edition1 Objects and Classes Gang Qian Department of Computer Science University of Central Oklahoma

2 Objectives Concepts of Objects and Classes Creating an Object of a Class Concept of Constructors and Using Constructors to Create Objects Difference between Object Reference Variables and Primitive Data Type Variables Using Classes in the JDK Library Difference between Instance and Static Methods

3 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects An object represents an entity in the real world that can be distinctly identified  For example, a student, a desk, a circle, a button, and even a loan can all be viewed as objects An object has a unique identity, state, and behaviors  The state of an object consists of a set of data fields (also known as properties) with their current values  The behavior of an object is defined by a set of methods

4 Classes Classes are constructs that define objects of the same type A Java class uses variables to define data fields and methods to define behaviors Additionally, a class provides a special type of methods, known as constructors, which are invoked to construct objects from the class

5 Constructors Constructors are a special kind of methods that are invoked to construct objects Constructors must have the same name as the class itself Constructors do not have a return type—not even Constructors do not have a return type—not even void Constructors are invoked using the new operator when an object is created

6 Creating Objects Using Constructors Syntax: new ClassName(); Example: new Scanner(System.in);

7 Declaring Object Reference Variables To reference an object, assign the object to a reference variable To declare a reference variable, use the syntax: ClassName objectRefVar; Example: Scanner s;

8 Declaring/Creating Objects in a Single Step Syntax: ClassName objectRefVar = new ClassName(); Example: Scanner s = new Scanner(System.in); Create an object Assign object reference

9 Accessing Objects Syntax for referencing the object’s data: objectRefVar.data Syntax for invoking the object’s method: objectRefVar.methodName(arguments)  Example: s.nextInt()

10 Differences between Variables of Primitive Data Types and Object Types

11 Caution Recall that we have used: Math.methodName(arguments)  Example: Math.pow(3, 2.5) Can we invoke nextInt() using Scanner.nextInt() directly?  The answer is NO  Almost all the methods used before this lecture are static methods, which are defined using the static keyword  However, nextInt() is non-static. It must be invoked from an object/instance using the syntax: objectRefVar.methodName(arguments) Example: s.nextInt()

12 Using Classes from the JDK Library We often need to use the classes in the JDK library to develop programs We have learned several of them  JOptionPane, System, Double, etc.  Example: We have learned to obtain the current time using System.currentTimeMillis() in Example Code ShowCurrentTime.java in Lecture two

13 The Date Class Java provides a system-independent encapsulation of date and time in the java.util.Date class The Date class can be used to create an instance for the current date and time and use its toString method to return the date and time as a string

14 The Date Class Example Example: java.util.Date date = new java.util.Date(); System.out.println(date.toString());  It displays a string like Sun Mar 09 13:50:19 EST 2003

15 The Random Class We have used Math.random() to obtain a random double value between 0.0 and 1.0 (excluding 1.0 ) A more useful random number generator is provided in the java.util.Random class

16 The Random Class Example If two Random objects have the same seed, they will generate identical sequences of numbers For example, the following code creates two Random objects with the same seed 3. Random random1 = new Random(3); System.out.print("From random1: "); for (int i = 0; i < 10; i++) System.out.print(random1.nextInt(1000) + " "); Random random2 = new Random(3); System.out.print("\nFrom random2: "); for (int i = 0; i < 10; i++) System.out.print(random2.nextInt(1000) + " "); From random1: From random2:

17 Instance Methods vs. Static Methods Instance methods are invoked by an instance of the class  Example: Scanner, Date, Random, etc. Static methods are not tied to a specific object  Example: Math, JOptionPane, etc. To declare static methods, use the static modifier