1 Predefined Classes and Objects Chapter 3. 2 Objectives You will be able to:  Use predefined classes available in the Java System Library in your own.

Slides:



Advertisements
Similar presentations
The Random and String Classes The import statement.
Advertisements

Chapter 3 Using Classes and Objects. © 2004 Pearson Addison-Wesley. All rights reserved3-2 Using Classes and Objects We can create more interesting programs.
Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process.
Java Programming Strings Chapter 7.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
©2004 Brooks/Cole Chapter 7 Strings and Characters.
ECE122 L4: Creating Objects February 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 4 Creating and Using Objects.
Chapter Day 5. © 2007 Pearson Addison-Wesley. All rights reserved2-2 Agenda Day 5 Questions from last Class?? Problem set 1 Posted  Introduction on developing.
Chapter 3 Using Classes and Objects. Creating Objects A variable holds either a primitive type or a reference to an object A class name can be used as.
Chapter 3 Using Classes and Objects. 2 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used.
23-Jun-15 Strings, Etc. Part I: String s. 2 About Strings There is a special syntax for constructing strings: "Hello" Strings, unlike most other objects,
Fundamental Programming Structures in Java: Strings.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Copyright © 2012 Pearson Education, Inc. Chapter 3 Using Classes and Objects Java Software Solutions Foundations of Program Design Seventh Edition John.
28-Jun-15 String and StringBuilder Part I: String.
COMP 110 Introduction to Programming Mr. Joshua Stough September 10, 2007.
References, Aliases, Garbage Collection and Packages Packages and Importing Classes Reading for this Lecture: L&L, Familiarize yourself with.
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.
Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value.
Topics Chapter 2: –Data conversion Chapter 3 –Object creation and object references –The String class and its methods –The Java standard class library.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Chapter 2 Using Objects. Types A type defines a set of values and the operations that can be carried out on the values Examples: 13 has type int "Hello,
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Chapter 7: Characters, Strings, and the StringBuilder.
Chapter 3: Classes and Objects Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved Java’s String Class.
Chapter 3 Using Classes and Objects 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All.
Using Classes and Objects. We can create more interesting programs using predefined classes and related objects Chapter 3 focuses on: Object creation.
CSC 1051 M.A. Papalaskari, Villanova University Everyday objects: Strings and Wrappers CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari.
String Class. Variable Holds a primitive value or a reference to an object. Holds a primitive value or a reference to an object. A reference lets us know.
Using Classes and Objects Chapters 3 Creating Objects – Section 3.1 The String Class – Section 3.2 The Scanner Class – Section 2.6 Instructor: Scott Kristjanson.
© 2004 Pearson Addison-Wesley. All rights reserved September 7, 2007 Formatting Output & Enumerated Types & Wrapper Classes ComS 207: Programming I (in.
CSE 1201 Object Oriented Programming Using Classes and Objects.
Chapter 3A Strings. Using Predefined Classes & Methods in a Program To use a method you must know: 1.Name of class containing method (Math) 2.Name of.
SEEM Java – Basic Introduction, Classes and Objects.
Array Declarations Arrays contain a fixed number of variables of identical type Array declaration and allocation are separate operations Declaration examples:
Java String 1. String String is basically an object that represents sequence of char values. An array of characters works same as java string. For example:
© 2004 Pearson Addison-Wesley. All rights reserved3-1 Collaboration Policy Programming Tests are individual work. You may not work with others in the class.
Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Chapter 3: Using Classes and Objects Coming up: Creating Objects.
3-1 Creating Objects A variable holds either a primitive type or a reference to an object A class name can be used as a type to declare an object reference.
17-Feb-16 String and StringBuilder Part I: String.
Computer Programming 2 Lab (1) I.Fatimah Alzahrani.
CSCI 51 Introduction to Programming Dr. Joshua Stough February 24, 2009.
© 2004 Pearson Addison-Wesley. All rights reserved January 23, 2006 Creating Objects & String Class ComS 207: Programming I (in Java) Iowa State University,
Using Classes and Objects We can create more interesting programs using predefined classes and related objects Chapter 3 focuses on: object creation and.
© 2004 Pearson Addison-Wesley. All rights reserved September 5, 2007 Packages & Random and Math Classes ComS 207: Programming I (in Java) Iowa State University,
© 2004 Pearson Addison-Wesley. All rights reserved3-1 Objects Declaration: String title;  title (object variable) of type String( Class )  title is just.
Variables in Java A variable holds either
Formatting Output & Enumerated Types & Wrapper Classes
Java Primer 1: Types, Classes and Operators
Multiple variables can be created in one declaration
Programming in Java Text Books :
Creating Objects & String Class
Chapter 4: Writing Classes
Primitive Types Vs. Reference Types, Strings, Enumerations
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
Lecture 4 Using Classes Richard Gesick.
String Objects & its Methods
String and StringBuilder
Chapter 7: Strings and Characters
Chapter 3 Introduction to Classes, Objects Methods and Strings
String and StringBuilder
Creating Objects A variable holds either a primitive value or a reference to an object A class name can be used as a type to declare an object reference.
String and StringBuilder
Packages & Random and Math Classes
Outline Creating Objects The String Class The Random and Math Classes
Corresponds with Chapter 5
String Objects & its Methods
Presentation transcript:

1 Predefined Classes and Objects Chapter 3

2 Objectives You will be able to:  Use predefined classes available in the Java System Library in your own Java programs.  Create objects from predefined system classes.

Creating Objects A variable holds either a primitive type or a reference to an object. "Steve Jobs" name1 num1 38 3

Creating Objects A class name can be used as a type to declare an object reference variable String title; An object reference variable holds the address of an object. The object itself must be created separately. 4

Creating Objects We can use the new operator to create an object title = new String ("Java Software Solutions"); This calls the String constructor, which is a special method that sets up the object Creating an object is called instantiation An object is an instance of a particular class 5

References An object reference can be thought of as a pointer to the location of the object "Steve Jobs" name1 num1 38 6

Assignment Revisited The act of assignment takes a copy of a value and stores it in a variable For primitive types: num1 38 num2 96 Before: num2 = num1; num1 38 num2 38 After: 7

Reference Assignment For object references, assignment copies the address: name2 = name1; name1 name2 Before: "Steve Jobs" "Steve Wozniak" name1 name2 After: "Steve Jobs" 8

The String Class 9

Because strings are so common, we don't have to use the new operator to create a String object String title = "Java Software Solutions"; This is special syntax that works only for strings. Each string literal (enclosed in double quotes) represents a String object. Copyright © 2012 Pearson Education, Inc. 10

String Indexes It is occasionally helpful to refer to a particular character within a string. This can be done by specifying the character's numeric index. The indexes begin at zero in each string. In the string "Hello", the character 'H' is at index 0 and the 'o' is at index 4. 11

Strings in Java are immutable Once a String object has been created, neither its value nor its length can be changed. However, several methods of the String class return new String objects that are modified versions of the original. 12

Method What is a method? A group of statements that is given a name. Defined within a class. May take one or more parameters. Typically computes and returns a value. How to invoke a method? To invoke, or call, a method write the name followed by parentheses. If the method takes parameters, put the values inside the parentheses, separated by commas. If the method returns a value, the value can be assigned to a variable or used in an expression. 13

Method What happens when a method is invoked? When a method is invoked, the flow of control transfers to the first statement in that method. A method that computes a value will end with a return statement, which returns the value computed by the method to the caller. A method that does not compute a value can simply return by executing its last statement. When a method completes execution, control is returned to the place where it was called. If the method returns a value, the returned value can be used in an assignment or an expression. 14

Invoking Methods Once an object has been instantiated, we can use the dot operator to invoke its methods String title = "Data"; int count = title.length(); A method invocation can be thought of as asking an object to perform a service. The length method of the String class returns the length of the string through which it was called. 15

Invoking the length Method of a String Object class test { public static void main(String[] args) { String title = "Data"; int count = title.length(); System.out.println("count is " + count); } 16

String concat Method String concat (String str) Returns a new string consisting of this string concatenated with the string passed as the method argument. class test { public static void main(String[] args) { String title = "Data"; title = title.concat(" Structures"); System.out.println(title); } } 17

Some Methods of the String Class String toUpperCase() Returns a new string identical to this string except all lowercase letters are converted to their uppercase equivalent. Example: newTitle = title.toUpperCase(); The variable title is unchanged by this! 18

String toUpperCase Method class test { public static void main(String[] args) { String title = "Data"; title = title.concat(" Structures"); String newTitle = title.toUpperCase(); System.out.println("title is " + title); System.out.println("newTitle is " + newTitle); } 19

Some Methods of the String Class String replace (char oldChar, char newChar) Returns a new string that is identical with this string except that every occurrence of oldChar is replaced by newChar. Example: String title3 = newTitle.replace('T', 'V'); The variable newTitle is unchanged. 20

Some Methods of the String Class String substring (int beginIndex, int endIndex) Returns a new string that is a subset of this string starting at beginIndex and extending through endIndex -1. Thus the length of the substring is endIndex – beginIndex. Example: String sub = title.substring(5, 7); 21

The substring Method of class String class test { public static void main(String[] args) { String title = "Data"; title = title.concat(" Structures"); System.out.println("title is " + title); String sub = title.substring(5,7); System.out.println("sub is " + sub); } 22

Exercise What output is produced by the following code fragment? String m1, m2, m3, m4; m1 = "Programming Language"; m2 = m1.toLowerCase(); m3 = m1 + " " + "Java"; m4 = m3.replace( ' a ', ' m ' ); System.out.println(m4.substring(2, 5)); 23

24 Exercise Answer

25 Exercise Answer String m1, m2, m3, m4; m1 = "Programming Language"; m2 = m1.toLowerCase(); m3 = m1 + " " + "Java"; m4 = m3.replace( ' a ', ' m ' ); System.out.println(m4.substring(2, 5)); m1 is "Programming Language" m2 is "programming language" m3 is "programming language Java" m4 is "progrmmming lmngumge Jmvm"

Some Methods of the String Class int indexOf(String str) Returns the index within this string of the first occurrence of the specified substring. 26

String Method indexOf class test { public static void main(String[] args) { String title = "Data"; title = title.concat(" Structures"); System.out.println("title is " + title); int pos = title.indexOf("Struct"); System.out.println("Index of Struct is " + pos); } 27 End of Section