Professional Documents
Culture Documents
SS-BE-III
Lab Teacher’s
Contents Date
No Sign
Basics of Core Java 24-01-2022
2
StringBuffer class.
5. Write a program to sort a given one-
dimensional array using Insertion sort.
Inheritance & Interfaces 07-02-2022
3
priority of threads and gets the name of threads.
5. Write a multithreaded program that displays
whether the thread is alive or not.
6. Write a multithreaded program using isAlive()
and join() methods.
7. Write a program to enter the details of Bride
and Groom. Create user defined exceptions
GroomException and BrideException to display
the appropriate messages if the age of groom
and bride does not qualify for the marriage.
8. Write a program to enter two integers from the
user and display the division of these two
numbers. Handle all the exceptions for invalid
arguments passed.
Collections in Java 21-02-2022
4
calculator. You can also use GridBag layout for
the designing purpose.
5
ASSIGNMENT-1
1. Write a program to find the factorial of a number.
Code:
public class Factorial {
int factorial(int n)
{
return (n == 0 || n == 1) ? 1 : n * factorial(n-1);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Factorial f = new Factorial();
int num = 5;
System.out.println("Factorial of "+ num + " is " +
f.factorial(num));
}
}
Output:
variable.
Code:
public class SwapNumber {
public static void main(String[] args)
{
int x = 10;
int y = 5;
System.out.println("After swaping:"
+ " x = " + x + ", y = " + y);
}
}
Output:
4.Write a program to check if a given number is even or odd.
Code:
public class EvenOdd {
// Driver code
public static void main(String[] args)
{
int n = 101;
if(isEven(n) == true)
System.out.print("Even");
else
System.out.print("Odd");
}
}
Output:
palindrome or not.
Code:
public class PalindromeNumber {
static int reverseDigits(int num)
{
int rev_num = 0;
while (num > 0) {
rev_num = rev_num * 10 + num % 10;
num = num / 10;
} return rev_num; }
if (rev_n == n)
return 1;
else
return 0;
}
n = 101;
}
Output:
6. Create an area menu to find the area of different shapes using a switch
case.
Code:
import java.util.Scanner;
public class ShapeArea {
switch(choice) {
case 'c':
System.out.print("Enter radius of circle: ");
double r = in.nextDouble();
double ca = (22 / 7.0) * r * r;
System.out.println("Area of circle = " + ca);
break;
case 's':
System.out.print("Enter side of square: ");
double side = in.nextDouble();
double sa = side * side;
System.out.println("Area of square = " + sa);
break;
case 'r':
System.out.print("Enter length of rectangle: ");
double l = in.nextDouble();
System.out.print("Enter breadth of rectangle: ");
double b = in.nextDouble();
double ra = l * b;
System.out.println("Area of rectangle = " + ra);
break;
default:
System.out.println("Wrong choice!");
}
}
}
7.Write a program to find Armstrong number between 100 and 1000.
Code:
import java.util.Scanner;
import java.lang.Math;
public class AmstrongNumber {
temp = n;
while(temp>0)
{
last = temp % 10;
sum += (Math.pow(last, digits));
temp = temp/10;
}
if(n==sum) {
return true;
}
else {
return false;
}
if(isArmstrong(i)) {
System.out.print(i+ ", ");
}
} } }
Output:
are printed with the object of the class not defining maid method.
Code:
import java.util.Scanner;
class Students {
String name,course;
int age;
Scanner scan = new Scanner(System.in);
void getDetails(){
Output:
Name: Keyur B Niama RollNo:512032
Lab Assignment-2
Arrays and Strings
1. Write a program to find the multiplication of two matrices of dimensions 3*3.
Code:
public class MatrixMultiplication {
int a[][]={{1,2,3},{4,5,7},{7,8,9}};
int b[][]={{1,2,3},{4,5,7},{7,8,9}};
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
c[i][j]=0;
for(int k=0;k<3;k++) {
c[i][j]+=a[i][k]*b[k][j];
System.out.print(c[i][j]+" ");
System.out.println();
Output:
Name: Keyur B Niama RollNo:512032
2. Write a program to input strings from the user and display the strings in alphabetic order.
Code:
import java.util.Scanner;
public class AlphabeticOrder
{
public static void main(String[] args)
{
int count;
String temp;
Scanner scan = new Scanner(System.in);
Output:
Name: Keyur B Niama RollNo:512032
3. Write a program to convert the given string to uppercase and display them in group of 5
characters.
Code:
public class StringInUppercase{
String s1="hellostring";
String s1upper=s1.toUpperCase();
char[] ch=s1upper.toCharArray();
for(int i=0;i<ch.length;i++){
System.out.print(" "+ch[i]);
}
}
}
Output:
Name: Keyur B Niama RollNo:512032
int p = s.length();
int q = s.capacity();
System.out.println("Length of string ="+ p);//Length
System.out.println("Capacity of string =" + q);//Capacity
s.append("Assignment");
System.out.println("String : "+ s);
s.append(2);
System.out.println("String : "+ s);//Append
s.reverse();
System.out.println("Reverse String : "+ s);//Reverse
}
}
Output:
int n = arr.length;
int j = i - 1;
Name: Keyur B Niama RollNo:512032
arr[j + 1] = arr[j];
j = j - 1;
arr[j + 1] = key;
int n = arr.length;
System.out.println();
ob.sort(arr);
printArray(arr);
Output:
Name: Keyur B Niama RollNo:512032
Name: Keyur B Niama Roll No:512032
Lab Assignment-3
Inheritance & Interfaces
1. Write a program to create a class Square. Derive a subclass Round Square of class
Rectangle. Derive another class Rhombus from Round-square. Create appropriate
objects for the classes with attributes and methods.
Code:
class Rectangle {
void display1() {
int borderRadius = 5;
void display2() {
void display3() {
}
Name: Keyur B Niama Roll No:512032
r.display1();
r.display2();
r.display3();
}
Output:
2. Write a program to create four classes Furniture, Chair, bed and Table. Classes
Chair, Bed and Table inherits class Furniture and overrides method use(). From main
method dynamically call the object of classes Chair, Bed and Table with the object of
Furniture class.
Code:
class Furniture{
void use() {
void use() {
Name: Keyur B Niama Roll No:512032
void use() {
void use() {
c.use();
b.use();
t.use();
}
Name: Keyur B Niama Roll No:512032
Output:
3.Write a program to create a class with two methods with the same naming add(),
one accepting two integer parameters and other accepting two double parameters.
When method is called, the appropriate method should be selected depending on the
parameter passed.
Code:
class Addition {
int sum = a + b;
return sum;
double sum = a + b ;
return sum;
{
Name: Keyur B Niama Roll No:512032
Output:
4. Write a program that creates a user interface to perform integer divisions. The user
enters two numbers in the text fields, Num1 and Num2. The division of Num1 and
Num2 is displayed in the Result field when the Divide button is clicked. If Num1 or
Num2 were not an integer, the program would throw a NumberFormatException. If
Num2 were Zero, the program would throw an Arithmetic Exception Display the
exception in a message dialog box.
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class BuildGUI extends JFrame implements ActionListener {
Name: Keyur B Niama Roll No:512032
JFrame actualWindow;
JPanel container;
JTextField txt_num1, txt_num2, txt_result;
JButton btn_div;
BuildGUI() {
actualWindow = new JFrame(" ");
container = new JPanel();
container.setLayout(new FlowLayout());
container.add(txt_num1);
container.add(txt_num2);
container.add(btn_div);
container.add(txt_result);
actualWindow.add(container);
actualWindow.setSize(300, 300);
actualWindow.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
int num1, num2;
try {
num1 = Integer.parseInt(txt_num1.getText());
Name: Keyur B Niama Roll No:512032
num2 = Integer.parseInt(txt_num2.getText());
txt_result.setText(num1/num2+"");
}
catch(NumberFormatException nfe) {
JOptionPane.showMessageDialog(actualWindow,"Please do enter
only integers");
}
catch(ArithmeticException ae) {
JOptionPane.showMessageDialog(actualWindow,"Divisor can not
be ZERO");
}
}
}
public class Assignment3_4{
public static void main(String[] args) {
new BuildGUI();
}
}
Output:
Name: Keyur B Niama Roll No:512032
Name: Keyur B Niama PRN:2018033800121251
Lab Assignment-4
Code:
System.out.println("thread is running...");
t1.start();
Output:
Code:
System.out.println("thread is running...");
t1.start();
Output:
Code:
String name;
Thread t;
name = threadname;
t.start();
try {
Thread.sleep(1000);
}catch (InterruptedException e) {
System.out.println(name + "Interrupted");
Name: Keyur B Niama PRN:2018033800121251
class Assignment4_3 {
new MultiThread("One");
new MultiThread("Two");
new MultiThread("Three");
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
Output:
Name: Keyur B Niama PRN:2018033800121251
4. Write a multithreaded program that sets the priority of threads and gets the
name of threads.
Code:
t1.setPriority(2);
t2.setPriority(5);
t3.setPriority(8);
Thread.currentThread().setPriority(10);
Output:
Name: Keyur B Niama PRN:2018033800121251
5. Write a multithreaded program that displays whether the thread is alive or not.
Code:
System.out.println("Thread1");
try {
Thread.sleep(300);
System.out.println("Thread2");
c1.start();
// c2.start();
System.out.println(c1.isAlive());
System.out.println(c2.isAlive());
Output:
Name: Keyur B Niama PRN:2018033800121251
Code:
@Override
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
t1.start();
t2.start();
// t3.start();
try {
t1.join();
t2.join();
t3.join();
} catch (InterruptedException e) {
e.printStackTrace();
System.out.println("Processing finished");
Output:
Name: Keyur B Niama PRN:2018033800121251
7. Write a program to enter the details of Bride and Groom. Create user defined
exceptions GroomException and BrideException to display the appropriate
messages if the age of groom and bride does not qualify for the marriage.
Code:
import java.util.Scanner;
BriderException(String str)
super(str);
GroomException(String str)
super(str);
bage = s.nextInt();
gage = s.nextInt();
try
if(gage < 18 )
else
catch(Exception e)
Output:
Name: Keyur B Niama PRN:2018033800121251
8. Write a program to enter two integers from the user and display the division of
these two numbers. Handle all the exceptions for invalid arguments passed.
Code:
import java.util.*;
class Assignment4_8 {
int a;
int b;
System.out.println("Enter a Number:");
a = s.nextInt();
System.out.println("Enter a Number:");
b = s.nextInt();
try {
System.out.println(a / b);
catch (ArithmeticException e) {
System.out.println(
Output:
Name: Keyur B Niama PRN:2018033800121251
Lab Assignment-5
Collection in Java
1.Write a program to create an ArrayList to add elements in the ArrayList.
Display the size and the contents of the ArrayList. Change the contents of
ArrayList by adding few elements at particular indices. Also use remove() method
to remove the elements at particular indices.
Code:
import java.util.*;
A.add("Java");
A.add("Python");
A.add("C++");
System.out.println("Size of List:"+A.size());
for(String str : A)
A.set(0, "AdvanceJava");
A.set(2, "C");
for(String str : A)
System.out.println();
A.remove(1);
A.remove("C");
Output:
1. Write a program to create a Linked list using the objects of LinkedList class
to display the first and the last element of the list.
Code:
import java.util.*;
list.add("application Layer");
Name: Keyur B Niama PRN:2018033800121251
list.add("presentation Layer");
list.add("session Layer");
list.add("Transport Layer");
list.add("Network layer");
list.add("Physical Layer");
Output:
3.Write a program to display the methods of HashSet class and TreeSet class using
switch.
Code:
import java.util.*;
System.out.println("Menu");
System.out.println("1.HashSet");
Name: Keyur B Niama PRN:2018033800121251
System.out.println("2.TreeSet");
System.out.println("3.Exit");
System.out.println("Enter Choice:");
switch(choice) {
case 1:
hs.add("computer network");
hs.add("Operating System");
hs.add("Complier Design");
System.out.println("List:" + hs);
break;
case 2:
ts.add("CN");
ts.add("OS");
ts.add("CD");
System.out.println("List:" + ts);
break;
case 3:
System.exit(0);
default :
}
Name: Keyur B Niama PRN:2018033800121251
Output:
4.Write a program that push the elements in a stack using the objects of Stack
class. Display the elements of the stack and then pop few elements. Display the
popped element out of the stack and display the entire stack after popping
elements.
Code:
import java.util.*;
stack.push(10);
Name: Keyur B Niama PRN:2018033800121251
stack.push(15);
stack.push(30);
stack.push(20);
stack.push(5);
Output:
5.Write a program that displays the contents of a hashtable using the objects of
Hash table class..
Code:
import java.util.*;
Name: Keyur B Niama PRN:2018033800121251
ht1.put(1, "one");
ht1.put(2, "two");
ht1.put(3, "three");
ht2.put(4, "four");
ht2.put(5, "five");
ht2.put(6, "six");
Output:
Name: Keyur B Niama PRN:2018033800121251
Lab Assignment- 6
Code:
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.TextEvent;
import java.awt.event.TextListener;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
JFrame frame;
String set1Icon[];
String set2Icon[];
Name: Keyur B Niama PRN:2018033800121251
// String set3Icon[];
String set4Icon[];
JPanel mainPanel;
JPanel set1Panel;
JPanel set1ChildPanel1;
JPanel set2Panel;
JPanel set3Panel;
JPanel set1ChildPanel2;
JButton set1[];
JButton set2[];
JButton set3[];
JButton set4[];
JPanel screenPanel;
JTextField screen;
CalculatorLayout(){
//Frame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainPanel.setLayout(new BoxLayout(mainPanel,BoxLayout.PAGE_AXIS));
frame.setLayout(new GridLayout());
frame.add(mainPanel);
//Main Screen
screen.addActionListener(this);
screen.setHorizontalAlignment(JTextField.RIGHT);
Name: Keyur B Niama PRN:2018033800121251
screenPanel.add(screen);
screen.setEditable(false);
screen.setHorizontalAlignment(SwingConstants.RIGHT);
mainPanel.add(screenPanel);
//
//Number Panel1
set1Panel.setLayout(new BoxLayout(set1Panel,BoxLayout.X_AXIS));
setIcon(set1,set1Icon);
addToPanel(set1,set1ChildPanel1);
set1Panel.add(set1ChildPanel1);
set2Panel.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
set2Panel.setAlignmentY(50);
Name: Keyur B Niama PRN:2018033800121251
setIcon(set2,set2Icon);
addToPanel(set2,set2Panel);
//Panel 3 Operators
//
// setIcon(set3,set3Icon);
// addToPanel(set3,set3Panel);
//Panel 4 Operators
setIcon(set4,set4Icon);
addToPanel(set4,set1ChildPanel2);
set1Panel.add(set1ChildPanel2);
//adding to frame
mainPanel.add(set2Panel);
// mainPanel.add(set3Panel);
mainPanel.add(set1Panel);
frame.setVisible(true);
frame.setSize(500, 500);
frame.setLocation(800, 100);
Name: Keyur B Niama PRN:2018033800121251
//Set Icons
for(int i = 0;i<button.length;i++) {
button[i].addActionListener(this);
//Add to Panel
//ActionEventListener
String op = obj.getText();
int num = 0;
try {
num = Integer.parseInt(op);
op = "num";
catch(Exception exc) {
double ans,num1,num2;
switch(op) {
Name: Keyur B Niama PRN:2018033800121251
s += num;
screen.setText(s);
break;
case "CE" :
screen.setText("");
break;
case "log":
ans = Math.log(val);
screen.setText("log("+val+") = "+ans);
break;
case "+":
screen.requestFocus(true);
break;
case "*":
screen.requestFocus(true);
break;
case "-":
screen.requestFocus(true);
break;
case "/":
screen.requestFocus(true);
break;
case "%":
screen.requestFocus(true);
break;
case ".":
screen.requestFocus(true);
break;
case "+/-":
if(len == 0) screen.setText("-");
str += '-';
screen.setText(str);
str += '+';
screen.setText(str);
screen.requestFocus(true);
break;
case "x^2":
num1 = Double.parseDouble(screen.getText());
screen.setText(num1+" ^ 2 = "+num2);
break;
case "x^3":
Name: Keyur B Niama PRN:2018033800121251
num1 = Double.parseDouble(screen.getText());
screen.setText(num1+" ^ 3 = "+num2);
break;
case "sqrt(x)":
num1 = Double.parseDouble(screen.getText());
num2 = Math.sqrt(num1);
screen.setText("sqrt("+num1+") = "+num2);
break;
screen.requestFocus(true);
break;
case "DEL" :
if(backStr.length() == 1) {
backStr = "";
screen.setText(backStr);
break;
case "1/x":
double sol =
Double.parseDouble(exp.evaluateExpression(screen.getText()));
sol = 1/sol;
screen.setText(sol+"");
break;
screen.setText(sol+"");
break;
//TextEventListener
//KeyEvent Listener
int flag = 0;
if(obj.equals(set1[i])) {
return set1Icon[i];
if(obj.equals(set2[i])) {
Name: Keyur B Niama PRN:2018033800121251
return set2Icon[i];
if(obj.equals(set3[i])) {
// return set3Icon[i];
if(obj.equals(set4[i])) {
return set4Icon[i];
return "";
//Evaluate expression
int i = 0;
while(i<exp.length()) {
i++;
return ans;
}
Name: Keyur B Niama PRN:2018033800121251
new CalculatorLayout();
Expression class
class ExpressionStack{
String data;
ExpressionStack next;
class StackOperation {
ExpressionStack top;
ExpressionStack base;
StackOperation(){
top = null;
base = null;
ExpressionStack newNode;
if(top == null) {
currNode.data = data;
this.top = currNode;
this.base.next = currNode;
Name: Keyur B Niama PRN:2018033800121251
else {
while(currNode != top) {
currNode = currNode.next;
newNode.data = data;
currNode.next = newNode;
currNode = newNode;
this.top = currNode;
this.top.next = null;
if(currNode == top) {
returnValue = currNode.data;
currNode = null;
this.top = null;
this.base.next = null;
else {
while(currNode.next!=top) {
currNode = currNode.next;
returnValue = currNode.next.data;
currNode.next = null;
this.top = currNode;
return returnValue;
Name: Keyur B Niama PRN:2018033800121251
return this.top.data;
while(currNode != null) {
System.out.print(currNode.data);
currNode = currNode.next;
try {
int i = 0;
while(i<expr.length()) {
num += expr.charAt(i);
i++; continue;
stackOperand.push(operand, num);
num="";
// stackOperand.display(operand);
if(!stackOperator.isEmpty(operator)) {
if(this.precedence(expr.charAt(i)+"") >
this.precedence(stackOperator.topValue(operator))) {
if(expr.charAt(i) != ')') {
stackOperator.push(operator,
expr.charAt(i)+"");
}
Name: Keyur B Niama PRN:2018033800121251
else {
this.popTillOpenBracket(stackOperator,stackOperand,operator,operand);
else {
double num2 =
Double.parseDouble(stackOperand.pop(operand));
double num1 =
Double.parseDouble(stackOperand.pop(operand));
stackOperand.push(operand, ans+"");
stackOperator.push(operator,
expr.charAt(i)+"");
else {
stackOperator.push(operator, expr.charAt(i)+"");
i++;
// stackOperand.display(operand);
// stackOperator.display(operator);
//
return stackOperand.pop(operand);
Name: Keyur B Niama PRN:2018033800121251
catch(Exception e) {
return expr;
//Is Number
return true;
return false;
//Precedence
else return 0;
//Calculate
double ans=0;
switch(operator) {
return ans;
try {
while(stackOperator.topValue(operator) != "(") {
double num2 =
Double.parseDouble( stackOperand.pop(operand));
double num1 =
Double.parseDouble(stackOperand.pop(operand));
stackOperand.push(operand,ans+"");
}catch(Exception e) {
System.out.println("Invalid brackets!!");
System.out.println(exp.evaluateExpression("3^2+1.5"));
Output:
Name: Keyur B Niama PRN:2018033800121251
Name: Keyur B Niama PRN:2018033800121251
Lab Assignment- 7
1. To Create User Interface to perform Integer Divisions. The User enters two
numbers in text fields. Num1 and Num2 The division of Num1 and Num2 is
displayed in result field when the divide button clicked. If Num1 or Num2 were not
Integer, the program would throw a NumberFormatException, If Num2 is Zero,
and the program would throw an Arithmetic Exception.Display the Exception in
Message Box.
Code:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class A7_1 extends JFrame implements ActionListener
{
TextField tf1,tf2,tf3;
Button b1;
JLabel l1,l2,l3;
A7_1()
{
setTitle("Division Operation...");
l1= new JLabel("Number 1:");
add(l1);
tf1= new TextField(15);
add(tf1);
l2= new JLabel(" Number 2:");
add(l2);
tf2= new TextField(15);
add(tf2);
l3= new JLabel("Result:");
Name: Keyur B Niama PRN:2018033800121251
add(l3);
tf3 = new TextField(15);
add(tf3);
b1=new Button("Divide");
add(b1);
b1.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e)
{
String s1="0";
String s2="0";
s1=tf1.getText();
s2=tf2.getText();
int s3 =0;
try
{
int n1=Integer.parseInt(s1);
int n2=Integer.parseInt(s2);
if(n2==0)
{
JOptionPane.showMessageDialog(null,String.valueOf("Divided By Zero"));
}
else {
s3 = n1 / n2;
String result = String.valueOf(s3);
tf3.setText(result);
}
}
Name: Keyur B Niama PRN:2018033800121251
catch(NumberFormatException e1)
{
JOptionPane.showMessageDialog(null,String.valueOf("Division
not possible"));
}
}
public static void main(String[] args)
{
A7_1 a= new A7_1();
a.setVisible(true);
a.setSize(700 , 500);
a.setLayout(new FlowLayout());
}
}
Output:
Name: Keyur B Niama PRN:2018033800121251
2. Write a java program that simulates a traffic light. The program lets the user
select one of three light Red, Yellow, Green with radio button. On Selecting a
button an appropriate message with “STOP” OR “READY” OR “GO” should appear
above the button in selected color. Initially there is no message shown..
Code:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class A7_2
{
A7_2()
{
JFrame frame = new JFrame("Traffic Light");
JLabel label = new JLabel();
label.setAlignmentX(JLabel.CENTER);
label.setSize(400,100);
label.setFont(new Font("", Font.PLAIN,25));
label.setBounds(150,10,200,100);
label.setBackground(Color.green);
box3.setBounds(210,100,65,50);
ButtonGroup bg = new ButtonGroup();
bg.add(box3);bg.add(box2);bg.add(box3);
frame.add(box1);
frame.add(box2);
frame.add(box3);
frame.add(label);
frame.setBackground(Color.white);
frame.setSize(400,400);
frame.setLayout(null);
frame.setVisible(true);
box1.addItemListener(new ItemListener()
{
public void itemStateChanged(ItemEvent e)
{
label.setText("STOP");
}
});
box2.addItemListener(new ItemListener()
{
public void itemStateChanged(ItemEvent e)
{
label.setText("READY");
}
});
box3.addItemListener(new ItemListener()
{
Name: Keyur B Niama PRN:2018033800121251
}
public static void main(String arg[])
{
new A7_2();
}
}
Output:
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
public class A7_3 extends JFrame implements KeyListener {
Name: Keyur B Niama PRN:2018033800121251
Label l;
TextArea area;
A7_3() {
JFrame frame = new JFrame();
l = new Label();
l.setBounds (20, 50, 100, 20);
area = new TextArea();
area.setBounds (20, 80, 300, 300);
area.addKeyListener(this);
frame.add(l);
frame.add(area);
frame.setSize (400, 400);
frame.setLayout (null);
frame.setVisible (true);
// frame.EXIT_ON_CLOSE()
}
public void keyPressed (KeyEvent e) {
l.setText ("Key Pressed");
}
public void keyReleased (KeyEvent e) {
l.setText ("Key Released");
}
public void keyTyped (KeyEvent e) {
l.setText ("Key Typed");
}
public static void main(String[] args) {
new A7_3();
}
}
Name: Keyur B Niama PRN:2018033800121251
Output:
4. Write a java program that handles all mouse events and shows the event names
at center of the window when a mouse event is fired. Use adapter classes.
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
public class A7_4 extends JFrame implements MouseListener{
Label l;
A7_4(){
setLayout(null);
setVisible(true);
}
public void mouseClicked(MouseEvent e) {
l.setText("Mouse Clicked");
}
public void mouseEntered(MouseEvent e) {
l.setText("Mouse Entered");
}
public void mouseExited(MouseEvent e) {
l.setText("Mouse Exited");
}
public void mousePressed(MouseEvent e) {
l.setText("Mouse Pressed");
}
public void mouseReleased(MouseEvent e) {
l.setText("Mouse Released");
}
public static void main(String[] args) {
new A7_4();
}
}
Output:
Name: Keyur B Niama PRN:2018033800121251