Java Programs (5,6,7)
Java Programs (5,6,7)
Implement a java program to illustrate the use of different types of character extraction, string
comparison, string search and string modification methods.
System.out.println("Example of substring():");
do {
System.out.println(str);
index = str.indexOf(search);
if (index != -1) {
result = str.substring(0, index);
result = result + replace;
result = result + str.substring(index + search.length());
str = result;
}
} while (index != -1);
System.out.println();
}
Output:
d) Example of equals() vs ==
Hello equals Hello -> true
Hello == Hello -> false
e) Example of compareTo()
Sorted array:
Now aid all come country for good is men of the the their time to to
a) Example of concat()
Concatenated string: Hello World
b) Example of replace()
Replaced string: Hewwo
c) Example of trim()
Trimmed string: "Hello World"
6. Implement a java program to illustrate the use of different types of StringBuffer methods
public class StrinBufferDemo {
// B. ensureCapacity()
sb.ensureCapacity(30);
System.out.println("B. ensureCapacity()");
System.out.println("Updated capacity after ensureCapacity(30): " +
sb.capacity());
System.out.println();
// C. setLength()
sb.setLength(2);
System.out.println("C. setLength()");
System.out.println("Buffer after setLength(2): " + sb);
System.out.println();
// E. getChars()
char[] target = new char[5];
sb.getChars(0, 2, target, 0);
System.out.println("E. getChars()");
System.out.print("Characters copied into target array: ");
System.out.println(target);
System.out.println();
// F. append()
sb.append(" World!");
System.out.println("F. append()");
System.out.println("Buffer after append: " + sb);
System.out.println();
// G. insert()
sb.insert(6, "Beautiful ");
System.out.println("G. insert()");
System.out.println("Buffer after insert: " + sb);
System.out.println();
// H. reverse()
sb.reverse();
System.out.println("H. reverse()");
System.out.println("Reversed buffer: " + sb);
System.out.println();
// J. replace()
sb.replace(5, 9, "was");
System.out.println("J. replace()");
System.out.println("Buffer after replace(5, 9, \"was\"): " + sb);
System.out.println();
// K. substring()
String substring = sb.substring(5);
System.out.println("K. substring()");
System.out.println("Substring from index 5: " + substring);
}
}
Output:
Length: 5
Capacity: 21
B. ensureCapacity()
C. setLength()
Character at index 1: e
E. getChars()
F. append()
G. insert()
H. reverse()
J. replace()
K. substring()
7 Demonstrate a swing event handling application that creates 2 buttons Alpha and Beta and
displays the text “Alpha pressed” when alpha button is clicked and “Beta pressed” when beta
button is clicked.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
JLabel jlab;
public EventDemo() {
jfrm.setLayout(new FlowLayout());
jfrm.setSize(220, 90);
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Make two buttons.
jbtnAlpha.addActionListener(new ActionListener() {
});
jbtnBeta.addActionListener(new ActionListener() {
});
jfrm.add(jbtnAlpha);
jfrm.add(jbtnBeta);
jfrm.add(jlab);
jfrm.setVisible(true);
}
SwingUtilities.invokeLater(new Runnable() {
new EventDemo();
});
Output:
8. A program to display greeting message on the browser “Hello UserName”, “How Are You?”,
accept username from the client using servlet.