Java lecture 7 1
-
Upload
ramananda-sarkar -
Category
Software
-
view
70 -
download
1
Transcript of Java lecture 7 1
Objects and Classes
Lecture-7
8/30/2013 1
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 behaviour.
• The state of an object (also known as its
properties or attributes) is represented by data
fields with their current values. A circle object, for
example, has a data field radius, which is the
property that characterizes a circle.
8/30/2013 2
Objects
• The behaviour of an object (also known as its
actions) is defined by methods. To invoke a
method on an object is to ask the object to
perform an action. For example, you may define
a method named getArea() for circle objects. A
circle object may invoke getArea() to return its
area.
8/30/2013 3
Classes
• A class is a template, blueprint, or contract
that defines what an object’s data fields
and methods will be. An object is an
instance of a class. You can create many
instances of a class. Creating an instance
is referred to as instantiation.
8/30/2013 4
A class is a template for creating
objects
8/30/2013 5
A class is a construct that defines
objects of the same type.
8/30/2013 6
Constructors
• A class provides methods of a special type,
known as constructors, which are invoked to
create a new object. A constructor can perform
any action, but constructors are designed to
perform initializing actions, such as initializing
the data fields of objects.
8/30/2013 7
UML (Unified Modelling Language) class
diagram, or simply a class diagram
8/30/2013 8
Example: Defining Classes and
Creating Objects
8/30/2013 9
Example: Defining Classes and
Creating Objects
8/30/2013 10
Example: Defining Classes and
Creating Objects
• Outputs
8/30/2013 11
Strings
• Constructing a String
– String message = new String("Welcome to Java");
or
– String message = "Welcome to Java";
• You can also create a string from an array of
characters.
– char[] charArray = {'G', 'o', 'o', 'd', ' ', 'D', 'a', 'y'};
String message = new String(charArray);
8/30/2013 12
String Comparisons
• Returns true if this string is equal to string
s1.
– +equals(s1: String): boolean
• Returns true if this string is equal to string
s1 case
– insensitive.+equalsIgnoreCase(s1: String):
boolean
8/30/2013 13
String Comparisons
String s1 = new String("Welcome to Java");
String s2 = "Welcome to Java";
if (s1.equals(s2))
System.out.println("string1 and string2 have the same contents");
else
System.out.println("string1 and string2 are not equal");
8/30/2013 14
The following statements display
true and then false
String s1 = new String("Welcome to Java");
String s2 = "Welcome to Java";
String s3 = "Welcome to C++";
System.out.println(s1.equals(s2)); // true
System.out.println(s1.equals(s3)); // false
8/30/2013 15
Concatenation
• You can use the concat method to concatenate two strings. The statement shown below, for example, concatenates strings s1 and s2 into s3:
String s1 = new String("Welcome to ");
String s2 = "Java";
String s3 = s1.concat(s2); s3 = " Welcome to Java";
• You can use the plus (+) sign to concatenate two or more strings. So the abovestatement is equivalent to
String s3 = s1 + s2;
8/30/2013 16
Obtaining Substrings
8/30/2013 17
Obtaining Substrings
• For example,
• String message = "Welcome to
Java".substring(0, 11) + "HTML";
• The string message now becomes "Welcome
to HTML". 8/30/2013 18
8/30/2013 19
8/30/2013 20
replaceAll
• For example, the following statement returns a
new string that replaces $, +, or # in "a+b$#c"
with the string NNN.
String s = "a+b$#c".replaceAll("[$+#]", "NNN");
System.out.println(s);
• Here the regular expression [$+#] specifies a
pattern that matches $, +, or #. So, the output is
aNNNbNNNNNNc.
8/30/2013 21
Problem: Checking Palindromes
8/30/2013 22
8/30/2013 23
END
8/30/2013 24