Ques16.
Write a Java program to implement vector [use: addelement(),
elementat(), removeelement(), size().]
CODE
import [Link].*;
class vec {
public static void main(String args[]){
Vector<String> v = new Vector<>();
[Link]("Apple");
[Link]("Banana");
[Link]("Orange");
String element = [Link](1);
[Link]("Element at index 1: " + element);
[Link]("Banana");
int s = [Link]();
[Link]("Size of vector: " + s);
[Link](v);
}
}
OUTPUT
Ques17. Create a user defined exception named “nomatchexception” that is fired
when the string entered by the user is not “india”
CODE
class NomatchException extends Exception {
public NomatchException() {
super("String does not match 'india'");
}
}
public class ex {
public static void main(String[] args) {
try {
String userInput = "example";
if () {
throw new NomatchException();
}
[Link]("String matches 'india'");
} catch (NomatchException e) {
[Link]([Link]());
}
}
}
OUTPUT
Ques18. Write a Java program to show even & odd numbers by thread.
CODE
public class EvenOddThread {
public static void main(String[] args) {
Printer printer = new Printer();
Thread evenThread = new Thread(new EvenRunnable(printer, 10),
"EvenThread");
Thread oddThread = new Thread(new OddRunnable(printer, 10), "OddThread");
[Link]();
[Link]();
}
static class Printer {
private volatile boolean isEven = true;
public synchronized void print(int number) {
while ((number % 2 == 0 && !isEven) || (number % 2 != 0 && isEven)) {
try {
wait();
} catch (InterruptedException e) {
[Link]().interrupt();
}
}
[Link]([Link]().getName() + ": " + number);
isEven = !isEven;
notifyAll();
}
}
static class EvenRunnable implements Runnable {
private final Printer printer;
private final int max;
public EvenRunnable(Printer printer, int max) {
[Link] = printer;
[Link] = max;
}
public void run() {
for (int i = 2; i <= max; i += 2) {
[Link](i);
}
}
}
static class OddRunnable implements Runnable {
private final Printer printer;
private final int max;
public OddRunnable(Printer printer, int max) {
[Link] = printer;
[Link] = max;
}
public void run() {
for (int i = 1; i <= max; i += 2) {
[Link](i);
}
}
}
}
OUTPUT
Ques19. Write a Java program that draws different color shapes on an applet .set
the foreground & background color as red & blue.
CODE
OUTPUT
Ques20. Write a Java program to show moving banner by applet,
CODE
OUTPUT
Ques21. Write a Java program to demonstrate the use of equals(), trim()
,length() , substring(), compareTo() of string class.
CODE
class str {
public static void main(String args[]){
String str1="Hello";
String str2="world";
String str3="Java programming";
[Link]("string 1=" +str1 );
[Link]("string 2=" +str2 );
[Link]("using equals function " + [Link](str2));
[Link]("using trim function " + [Link]());
[Link]("using length function " + [Link]());
[Link]("using substring function " + [Link](1,3));
[Link]("using campareto function " + [Link](str2));
}
}
OUTPUT
Ques22. Write a Java program to demonstrate the use of equals() and == in Java.
CODE
class eq {
public static void main(String args[]){
String str1="Hello";
String str2="world";
String str3="Hello";
[Link]("string 1=" +str1 );
[Link]("string 2=" +str2 );
[Link]("string 3=" +str3 );
[Link]("using equals()= " + [Link](str3));
[Link]("using double" + str1==str3 );
}
}
OUTPUT
Ques23. Write a Java program to implement all mouse events and mouse motion
events.
CODE
import [Link].*;
import [Link].*;
public class MouseEventsDemo extends Frame implements MouseListener,
MouseMotionListener {
public MouseEventsDemo() {
setTitle("Mouse Events Demo");
setSize(400, 300);
addMouseListener(this);
addMouseMotionListener(this);
setVisible(true);
}
public void mouseClicked(MouseEvent e) {
[Link]("Mouse Clicked at (" + [Link]() + ", " + [Link]() + ")");
}
public void mousePressed(MouseEvent e) {
[Link]("Mouse Pressed at (" + [Link]() + ", " + [Link]() + ")");
}
public void mouseReleased(MouseEvent e) {
[Link]("Mouse Released at (" + [Link]() + ", " + [Link]() + ")");
}
public void mouseEntered(MouseEvent e) {
[Link]("Mouse Entered Frame");
}
public void mouseExited(MouseEvent e) {
[Link]("Mouse Exited Frame");
}
public void mouseDragged(MouseEvent e) {
[Link]("Mouse Dragged at (" + [Link]() + ", " + [Link]() + ")");
}
public void mouseMoved(MouseEvent e) {
[Link]("Mouse Moved at (" + [Link]() + ", " + [Link]() + ")");
}
public static void main(String[] args) {
new MouseEventsDemo();
}
}
OUTPUT
Ques24. Write a Java program to implement keyboard events.
CODE
import [Link].*;
import [Link].*;
public class Kkeyevent extends Frame implements KeyListener {
public Kkeyevent() {
setTitle("Keyboard Events Demo");
setSize(400, 300);
addKeyListener(this);
setFocusable(true);
setVisible(true);
}
public void keyPressed(KeyEvent e) {
[Link]("Key Pressed: " + [Link]([Link]()));
}
public void keyReleased(KeyEvent e) {
[Link]("Key Released: " + [Link]([Link]()));
}
public void keyTyped(KeyEvent e) {
[Link]("Key Typed: " + [Link]());
}
public static void main(String[] args) {
new Kkeyevent();
}
}
OUTPUT
Ques25. Write a Java program using AWT to create a simple calculator.
CODE