Object Oriented Programming Basics Java
Object Oriented Programming Basics Java
• modifiable string
• defined in java.lang
• declared final classes
• implement the CharSequence interface
• provides much of the functionality of strings
➢ StringBuffer Constructors
• general forms:
int length( )
int capacity( )
➢ ensureCapacity( )
• Preallocate
• general form:
void ensureCapacity(int capacity)
✓ Here, capacity specifies the size of the buffer.
➢ setLength( )
• set the length of the buffer within a StringBuffer object
• void setLength(int len)
✓ len specifies the length of the buffer. This value must be nonnegative.
• Increase in the size of the buffer adds null characters to the end
• setLength( ) with a value less than the current value then
then the characters stored beyond the new length will be lost.
✓append( )
• The append( ) method concatenates.
• It has several overloaded versions. Here are a few of its forms:
StringBuffer append(String str)
StringBuffer append(int num)
StringBuffer append(Object obj)
class appendDemo {
public static void main(String args[]) {
String s;
int a = 42;
StringBuffer sb = new StringBuffer(40);
s = sb.append("a = ").append(a).append("!").toString();
System.out.println(s);
}
}
a = 42!
✓ insert( )
• The insert( ) method inserts one string into another.
• It is overloaded to accept values of all the simple types, plus Strings, Objects, and
CharSequences.
• These are a few of its forms:
StringBuffer insert(int index, String str)
StringBuffer insert(int index, char ch)
StringBuffer insert(int index, Object obj)
Here, index specifies the index at which point the string will be inserted .
// Demonstrate insert().
class insertDemo {
public static void main(String args[]) {
StringBuffer sb = new StringBuffer("I Java!");
sb.insert(2, "like ");
System.out.println(sb);
}
}
The output of this example is shown here:
I like Java!
// Demonstrate replace()
class replaceDemo {
public static void main(String args[]) {
StringBuffer sb = new StringBuffer("This is a test.");
sb.replace(5, 7, "was");
System.out.println("After replace: " + sb);
}
}
Here is the output:
After replace: This was a test.
Saturday, August 3, 2019
✓ substring( )
• obtain a portion of a StringBuffer.
String substring(int startIndex)
String substring(int startIndex, int endIndex)
ftp://172.16.3.238/Books/Computers/Java/
• Software component
• Perform a simple or complex function
• Visible or invisible to a user
• Autonomous or distributed
• Reusable in different environments
• No restriction on its capability
• Java beans are classes that encapsulate many objects into a single object (the bean).
• Main parts of JB are properties, events, and methods.
• They are serializable, have a zero-argument constructor.
• Allow properties using getter and setter methods.
• A reusable software component in Java that can be manipulated visually in an
application builder tool.
• What is Introspection?
• Design Patterns for Properties
o Simple Properties
o Indexed Properties
• Design Patterns for Events
• Methods and Design Patterns
o Using the BeanInfo Interface
• Two ways…
1. Simple naming conventions
- Introspection mechanisms to infer information about a Bean.
2. Additional class that extends the BeanInfo interface
- Supplies this information.
Simple Properties
• A simple property has a single value.
• It can be identified by the following design patterns :
public T getN( )
public void setN(T arg)
where N is the name of the property and T is its type
• A read/write property has both of these methods to access its values.
• A read-only property has only a get method.
• A write-only property has only a set method.
They return arrays of objects provide information about the properties, events,
and methods of a Bean.
The classes PropertyDescriptor, EventSetDescriptor, and MethodDescriptor
are defined within the java.beans package.
• When creating a class that implements BeanInfo, you must call that class
bnameBeanInfo, where bname is the name of the Bean.
• For example, if the Bean is called MyBean, then the information class must
be called MyBeanBeanInfo.
// A simple Bean.
import java.awt.*;
import java.awt.event.*;
import java.io.Serializable;
public Colors() {
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent me) {
change();
}
});
design-pattern introspection is used, and all events are found, including those in Colors’
superclass, Canvas.
• Remember, if you don’t override one of the “get” methods defined by SimpleBeanInfo,
• To observe the difference that ColorsBeanInfo makes, erase its class file and then run
• The List interface extends Collection and declares the behavior of a collection.
• Stores a sequence of elements.
• Elements can be inserted or accessed by their position in the list using a zero-based
index.
• List is a generic interface that has this declaration:
interface List<E>
Here, E specifies the type of objects that the list will hold.
• In addition to the methods defined by Collection, List defines some of its own,
which are summarized in below.
• interface Set<E>
• Here, E specifies the type of objects that the set will hold.
• The SortedSet interface extends Set and declares the behavior of a set sorted in
ascending order.
• SortedSet is a generic interface that has this declaration:
interface SortedSet<E>
Here, E specifies the type of objects that the set will hold.
• Addition to those methods defined by Set, the SortedSet interface declares the
methods summarized as follows.
• The Queue interface extends Collection and declares the behavior of a queue, which is often
a first-in, first-out list.
• However, there are types of queues in which the ordering is based upon other criteria.
• Queue is a generic interface that has this declaration:
interface Queue<E>
✓ void trimToSize( )
– reduce the size to the elements in ArrayList.
The LinkedList class extends AbstractSequentialList and implements the List, Deque,
and Queue interfaces.
It provides a linked-list data structure.
LinkedList is a generic class that has this declaration:
class LinkedList<E>
It has two constructors:
LinkedList( )
LinkedList(Collection<? extends E> c)
• The first constructor builds an empty linked list.
• The second constructor builds a linked list that is initialized with the elements of the
collection c.
The second form initializes the hash set by using the elements of c.
The third form initializes the capacity of the hash set to capacity. (The default capacity is 16.)
The fourth form initializes both the capacity and the fill ratio (also called load
The fill ratio must be between 0.0 and 1.0, and it determines how full the hash set can be before
it is resized upward.
// Demonstrate HashSet.
import java.util.*;
class HashSetDemo {
public static void main(String args[]) {
// Create a hash set.
HashSet<String> hs = new HashSet<String>();
// Add elements to the hash set.
hs.add("B");
hs.add("A");
hs.add("D");
hs.add("E");
hs.add("C");
hs.add("F");
System.out.println(hs);
}
}
The following is the output from this program:
[D, A, F, C, B, E]
✓You might want to experiment with the other methods defined by NavigableSet.
Using an Iterator
Before you can access a collection through an iterator, you must obtain one.
Collection classes provides an iterator( ) method that returns an iterator to the
start of the collection.
By using this iterator object, you can access each element in the collection one
element at a time.