Chapter 5 Programming with Objects and Classes OO Programming Concepts OO Programming Concepts Declaring and Creating Objects Declaring and Creating Objects.

Slides:



Advertisements
Similar presentations
1 StringBuffer & StringTokenizer Classes Chapter 5 - Other String Classes.
Advertisements

Chapter 6 Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Strings.
L3:CSC © Dr. Basheer M. Nasef Lecture #3 By Dr. Basheer M. Nasef.
Chapter 7 Strings F To process strings using the String class, the StringBuffer class, and the StringTokenizer class. F To use the String class to process.
1 Strings and Text I/O. 2 Motivations Often you encounter the problems that involve string processing and file input and output. Suppose you need to write.
Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 8 Strings and Text.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 1 Object-Oriented.
Liang,Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 10 Object-Oriented Modeling.
StringBuffer class  Alternative to String class  Can be used wherever a string is used  More flexible than String  Has three constructors and more.
Strings, Etc. Part I: Strings. About Strings There is a special syntax for constructing strings: "Hello" Strings, unlike most other objects, have a defined.
1 Object-Oriented Design. 2 Objectives F To become familiar with the process of program development. F To the relationship types: association, aggregation,
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.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 12 Object-Oriented Design.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 12 Object-Oriented.
Syllabus (1) WeekChapters 1Introduction to the course, basic java language programming concepts: Primitive Data Types and Operations 1, 2 2Methods, Control.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 7 Strings.
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.
Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design Guidelines.
Objects and Classes Chapter 6 CSCI CSCI 1302 – Objects and Classes2 Outline Introduction Defining Classes for Objects Constructing Objects Accessing.
String & Composition. Agenda This keyword. String class. String operations. Composition.
Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.
Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.
Chapter 7 Strings  Use the String class to process fixed strings.  Use the StringBuffer class to process flexible strings.  Use the StringTokenizer.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 8 Strings 1.
Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process.
Strings Chapter 7 CSCI CSCI 1302 – Strings2 String Comparisons Compare string contents with the equals(String s) method not == String s0 = “ Java”;
Strings Chapter 7 CSCI CSCI 1302 – Strings2 Outline Introduction The String class –Constructing a String –Immutable and Canonical Strings –String.
Jaeki Song JAVA Lecture 07 String. Jaeki Song JAVA Outline String class String comparisons String conversions StringBuffer class StringTokenizer class.
Strings and Text File I/O (and Exception Handling) Corresponds with Chapters 8 and 17.
1 The String Class F Constructing a String: F Obtaining String length and Retrieving Individual Characters in a string F String Concatenation (concat)
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Chapter 3 Introduction to Classes and Objects Definitions Examples.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 11 Object-Oriented.
1 Chapter 6 Programming with Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive.
Introduction To Objects Oriented Programming Instructor: Mohammed Faisal.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
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, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 10 Thinking in Objects.
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.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 7 Strings Chapter.
Objects and Classes. F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and object type.
G. Pullaiah College of Engineering and Technology
Strings.
Chapter 11 Object-Oriented Design
Chapter 10 Thinking in Objects
HIGHLEVEL REVIEW Chapter 9 Objects and Classes
Corresponds with Chapter 7
Chapter 9 Objects and Classes
Objects and Classes Creating Objects and Object Reference Variables
Lecture 07 String Jaeki Song.
Chapter 6 Objects and Classes
Chapter 6 Objects and Classes
OO Programming Concepts
Chapter 6 Objects and Classes
Chapter 7 Strings Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. Use the String class to process fixed.
Presentation transcript:

Chapter 5 Programming with Objects and Classes OO Programming Concepts OO Programming Concepts Declaring and Creating Objects Declaring and Creating Objects Constructors Constructors Modifiers (public, private and static) Modifiers (public, private and static) Instance and Class Variables and Methods Instance and Class Variables and Methods Analyzing Relationships among Classes Analyzing Relationships among Classes Case Studies (Mortgage class and Rational class) Case Studies (Mortgage class and Rational class) The Java API and Core Java classes The Java API and Core Java classes Processing Strings Processing Strings

OO Programming Concepts

Class and Objects

Class Declaration class Circle { // states double radius = 1.0; double radius = 1.0; // behaviors double findArea() { double findArea() { return radius*radius* ; return radius*radius* ; }}

Declaring Objects ClassName objectName; Example: Circle myCircle;

Creating Objects objectName = new ClassName(); Example: myCircle = new Circle();

Declaring/Creating Objects in a Single Step ClassName objectName = new ClassName(); Example: Circle myCircle = new Circle();

Differences between variables of primitive types and object types

Copying Variables of Primitive Types and Object Types

Accessing Objects Referencing the object’s data: Referencing the object’s data: objectName.data objectName.data myCircle.radius myCircle.radius Referencing the object’s method: Referencing the object’s method: objectName.method objectName.method myCircle.findArea() myCircle.findArea()

Example 5.1 Using Objects Objective: Demonstrate creating objects, accessing data, and using methods. Objective: Demonstrate creating objects, accessing data, and using methods. TestCircle Run

Constructors Circle(double r) { radius = r; radius = r;}Circle(){ radius = 1.0; radius = 1.0;} myCircle = new Circle(5.0);

Example 5.2 Using Constructors Objective: Discuss the role of constructors and use them to create objects. Objective: Discuss the role of constructors and use them to create objects. TestCircleWithConstructors Run

Passing Objects to Methods Passing by reference Passing by reference Passing by value Passing by value Example 5.3 Passing Objects as Arguments TestPassingObject Run

Visibility Modifiers and Accessor Methods By default, the class, variable, or data can be accessed by any class in the same package. public The class, data, or method is visible to any class in any package. private The data or methods can be accessed only by the declaring class. The getter and setter accessor methods are used to read and modify private properties.

Example 5.4 Using the private Modifier and Accessor Methods TestCircleWithPrivateModifierRun In this example, private data are used for the radius and the accessor methods getRadius and setRadius are provided for the clients to retrieve and modify the radius.

Instance Variables, and Methods Instance variables belong to a specific instance. Instance methods are invoked by an instance of the class.

Class Variables, Constants, and Methods Class variables are shared by all the instances of the class. Class methods are not tied to a specific object. Class constants are final variables shared by all the instances of the class. To declare class variables, constants, and methods, use the static modifier.

Class Variables, Constants, and Methods, cont. UML notation (Unified Modeling language)

Example 5.5 Using Instance and Class Variables and Method Objective: Demonstrate the roles of instance and class variables and their uses. This example adds a class variable numOfObjects to track the number of Circle objects created. Objective: Demonstrate the roles of instance and class variables and their uses. This example adds a class variable numOfObjects to track the number of Circle objects created. TestInstanceAndClassVariableRun

Analyzing Relationships among Classes Association Association Aggregation Aggregation Inheritance Inheritance

Association Association represents a general binary relationship that describes an activity between two classes. Association represents a general binary relationship that describes an activity between two classes.

Aggregation Aggregation is a special form of association, which represents an ownership relationship between two classes. Aggregation is a special form of association, which represents an ownership relationship between two classes. Aggregation models the relationship like has-a, part-of, owns, and employed-by. Aggregation models the relationship like has-a, part-of, owns, and employed-by.

Inheritance Inheritance models the is-a relationship between two classes. Inheritance models the is-a relationship between two classes.

Class Abstraction Class abstraction means to separate class implementation from the use of the class. Class abstraction means to separate class implementation from the use of the class. The creator of the class provides a description of the class and let the user know how the class can be used. The creator of the class provides a description of the class and let the user know how the class can be used. The user of the class does not need to know how the class is implemented. The user of the class does not need to know how the class is implemented. The detail of implementation is encapsulated and hidden from the user. The detail of implementation is encapsulated and hidden from the user.

Class Design 1. Identify classes for the system. 1. Identify classes for the system. 2. Describe attributes and methods in each class. 3. Establish relationships among classes. 4. Create classes.

Example 5.6 Borrowing Mortgages Name Borrower Address Mortgage

Example 5.6 Borrowing Mortgages, cont. The following is a test program that uses the classes Name, Address, Borrower, and Mortgage. The following is a test program that uses the classes Name, Address, Borrower, and Mortgage. BorrowMortgage Run

Example 5.7 Using the Rational Class Rational Run Objective: Define a class for rational numbers that provides constructors and addition, subtraction, multiplication, and division methods. TestRationalClass

Java API and Core Java classes java.lang java.lang Contains core Java classes, such as numeric classes, strings, and objects. This package is implicitly imported to every Java program. java.awt java.awt Contains classes for graphics. java.applet java.applet Contains classes for supporting applets.

java.io java.io Contains classes for input and output streams and files. java.util java.util Contains many utilities, such as date. java.net java.net Contains classes for supporting network communications. Java API and Core Java classes, cont.

java.awt.image java.awt.image Contains classes for managing bitmap images. java.awt.peer java.awt.peer Platform-specific GUI implementation. Others: Others:java.sqljava.rmi Java API and Core Java classes, cont.

The String Class Declaring a String: Declaring a String:  String message = "Welcome to Java!"  String message = new String("Welcome to Java!“);  String s = new String(); String Comparisons String Comparisons String Concatenation String Concatenation Substrings Substrings String Length String Length Retrieving Individual Characters in a String Retrieving Individual Characters in a String

String Comparisons equals equals String s1 = "Welcome"; String s2 = "welcome"; if (s1.equals(s2)) // if the contents of s1 and s2 are if (s1.equals(s2)) // if the contents of s1 and s2 are {... } // the same {... } // the same if (s1 == s2) // if s1 and s2 refer to same object if (s1 == s2) // if s1 and s2 refer to same object { … }

Substrings String is an immutable class; its values cannot be changed individually. String s1 = "Welcome to Java"; String s2 = s1.substring(0,10) + "HTML";

Finding String Length Finding string length using the length() method: message = "Welcome"; message.length() (returns 7 )

Retrieving Individual Characters in a String Do not use message[0] Do not use message[0] Use message.charAt(index) Use message.charAt(index) Index starts from 0 Index starts from 0

Example 5.8 Finding Palindromes Objective: C hecking whether a string is a palindrome: a string that reads the same forward and backward. Objective: C hecking whether a string is a palindrome: a string that reads the same forward and backward. FindPalindrome Run

The StringBuffer Class The StringBuffer class is an alternative to the String class. In general, a string buffer can be used wherever a string is used. StringBuffer is more flexible than String. You can add, insert, or append new contents into a string buffer. However, the value of a string is fixed once the string is created.

StringBuffer Constructors public StringBuffer() public StringBuffer() No characters, initial capacity 16 characters. public StringBuffer(int length) public StringBuffer(int length) No characters, initial capacity specified by the length argument. public StringBuffer(String str) public StringBuffer(String str) Represents the same sequence of characters as the string argument. Initial capacity 16 plus the length of the string argument.

Appending New Contents into a String Buffer StringBuffer strBuf = new StringBuffer(); strBuf.append("Welcome"); strBuf.append(' '); strBuf.append("to"); strBuf.append("Java");

The StringTokenizer Class Constructors StringTokenizer(String s, String delim, boolean returnTokens) StringTokenizer(String s, String delim, boolean returnTokens) StringTokenizer(String s, String delim) StringTokenizer(String s, String delim) StringTokenizer(String s) StringTokenizer(String s)

The StringTokenizer Class Methods boolean hasMoreTokens() boolean hasMoreTokens() String nextToken() String nextToken() String nextToken(String delim) String nextToken(String delim)

Example 5.10 Testing StringTokenizer Objective: Using a string tokenizer, retrieve words from a string and display them on the console. Objective: Using a string tokenizer, retrieve words from a string and display them on the console. TestStringTokenizerRun