Java File 2
Java File 2
Java File 2
class vec {
v.removeElement("Banana");
int s = v.size();
System.out.println("Size of vector: " + s);
System.out.println(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 (!userInput.equalsIgnoreCase("india")) {
throw new NomatchException();
}
System.out.println("String matches 'india'");
} catch (NomatchException e) {
System.out.println(e.getMessage());
}
}
}
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");
evenThread.start();
oddThread.start();
}
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";
System.out.println("string 1=" +str1 );
System.out.println("string 2=" +str2 );
System.out.println("string 3=" +str3 );
System.out.println("using equals()= " + str1.equals(str3));
System.out.println("using double" + str1==str3 );
}
}
OUTPUT
Ques23. Write a Java program to implement all mouse events and mouse motion
events.
CODE
import java.awt.*;
import java.awt.event.*;
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) {
System.out.println("Mouse Clicked at (" + e.getX() + ", " + e.getY() + ")");
}
public Kkeyevent() {
setTitle("Keyboard Events Demo");
setSize(400, 300);
addKeyListener(this);
setFocusable(true);
setVisible(true);
}
public void keyPressed(KeyEvent e) {
System.out.println("Key Pressed: " + KeyEvent.getKeyText(e.getKeyCode()));
}