0% found this document useful (0 votes)
22 views43 pages

BSC 3rd Semjavaprogram

mannual

Uploaded by

Khadri S S
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
22 views43 pages

BSC 3rd Semjavaprogram

mannual

Uploaded by

Khadri S S
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 43

JAVA PROGRAMMING

PART-A

DEPARTMENT OF COMPUTER SCIENCE, SKNG GFG COLLEGE, GANGAVATHI 1


JAVA PROGRAMMING
/* 1. Program to assign two integer values to x and y. Using the 'if' statement the output of the
program should display a message whether x is greater than y.*/

class Largest
{
public static void main (String args[])
{
int x=51;
int y=5;
if(x>y)
System.out.println("Largest is x="+x);
else
System.out.println("Largest is y="+y);
}
}

OUTPUT:

DEPARTMENT OF COMPUTER SCIENCE, SKNG GFG COLLEGE, GANGAVATHI 2


JAVA PROGRAMMING
/* 2. Program to list the factorial of the numbers 1 to 10. To calculate the factorial value, use
while loop. */

public class Factorial


{
public static void main (String args[])
{
int count=1;
long fact=1;
System.out.println("Number \t\t\t Factorial");
while(count<=10)
{
fact*=count;
System.out.println(count+"\t\t\t\t"+fact);
count++;
}
}
}

OUTPUT:

DEPARTMENT OF COMPUTER SCIENCE, SKNG GFG COLLEGE, GANGAVATHI 3


JAVA PROGRAMMING
/* 3. Program to add two integers and two float numbers. When no arguments are supplied,
give a default value to calculate the sum. Use function overloading. */

class Functionoverloading
{
public void add(int x,int y)
{
int sum;
sum=x+y;
System.out.println(sum);
}
public void add(double a,double b)
{
double total;
total=a+b;
System.out.println(total);
}
}
class Mainfunover
{
public static void main(String args[])
{
Functionoverloading obj=new Functionoverloading();
obj.add(10,20);
obj.add(10.40,10.30);
}
}

OUTPUT:

DEPARTMENT OF COMPUTER SCIENCE, SKNG GFG COLLEGE, GANGAVATHI 4


JAVA PROGRAMMING
/*4. Program to perform mathematical operations. Create a class AddSub with methods to
add and substract. Create another class called MulDiv that extends from Add,Sub class to use
the member data of the super class. MulDiv should have methods to multiply and divide a
main function should access the methods and perform the mathematical operations. */

class AddSub
{
int x=20;
int y=15;
public void add( )
{
int sum;
sum=x+y;
System.out.println(sum);
}
public void sub( )
{
int minus;
minus=x-y;
System.out.println(minus);
}
}
class MulDiv extends AddSub
{
public void mul( )
{
int product;
product=x*y;
System.out.println(product);
}
public void div( )
{
int Rem;
Rem=x/y;
System.out.println(Rem);
}
}
class Operations
{
public static void main(String args[ ])
{
MulDiv obj=new MulDiv( );
obj.add( );
obj.sub( );
obj.mul( );
obj.div( );
}
}

DEPARTMENT OF COMPUTER SCIENCE, SKNG GFG COLLEGE, GANGAVATHI 5


JAVA PROGRAMMING
OUTPUT:

DEPARTMENT OF COMPUTER SCIENCE, SKNG GFG COLLEGE, GANGAVATHI 6


JAVA PROGRAMMING
/* 5. Program with class variable that is available for all instances of a class. Use static
variable declaration observe the changes that occur in the object's member variable values. */

class staticDemo
{
static int count=0;
public void increment( )
{
count++;
}
public static void main(String args[ ])
{
staticDemo obj1=new staticDemo( );
staticDemo obj2=new staticDemo( );
obj1.increment( );
obj2.increment( );
System.out.println("obj1:count is="+obj1.count);
System.out.println("obj2:count is="+obj2.count);
}
}

OUTPUT:

DEPARTMENT OF COMPUTER SCIENCE, SKNG GFG COLLEGE, GANGAVATHI 7


JAVA PROGRAMMING
/* 6. a). Program to find the area and circumference of the circle by accepting the radius from
the user. */

import java.util.Scanner;
import java.lang.Math;
public class Coc
{
public static void main(String args[])
{
double circumference,radius,area;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the radius of the circle:");
radius=sc.nextDouble();
circumference=Math.PI*2*radius;
area=Math.PI*radius*radius;
System.out.println("area of a circle is"+area);
System.out.println("the circumference of the circle is:"+circumference);
}
}

OUTPUT:

DEPARTMENT OF COMPUTER SCIENCE, SKNG GFG COLLEGE, GANGAVATHI 8


JAVA PROGRAMMING
/* 6.b). Program to accept a number and find whether the number is prime or not. */

import java.util.*;
class Prime
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the no");
int n=sc.nextInt();
int i=1,c=0;
for(i=1;i<=n;i++)
{
if(n%i==0)
{
c++;
}
}
if(c==2)
{
System.out.println(n+"is a Prime no");
}
else
{
System.out.println(n+"is not a prime no");
}
}
}

OUTPUT:

DEPARTMENT OF COMPUTER SCIENCE, SKNG GFG COLLEGE, GANGAVATHI 9


JAVA PROGRAMMING
/* 7. Program to create a student class with following attributes. Enrollment No: Name, Mark
of sub1, Mark of Sub2, Mark of sub3, Total Marks. Total of the three marks must be
calculated only when the student passes in all three subjects. The pass mark for each subject
is 50. If a candidate fails in any one of the subjects, his total mark must be declared as zero.
Using this condition write a constructor for this class. Write separate functions for accepting
and displaying student details. In the main method create an array of three student objects and
display the details. */

import java.util.Scanner;
class student
{
int rollno;
String sname;
int s1,s2,s3;
int total;
student()
{
total=0;
}
void set()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter student details: rollno, name,s1,s2,s3");
rollno=sc.nextInt();
sname=sc.next();
s1=sc.nextInt();
s2=sc.nextInt();
s3=sc.nextInt();
if(s1>50 && s2>50 && s3>50)
total=s1+s2+s3;
}
void disp()
{
System.out.println(rollno+"\t"+sname+"\t"+s1+"\t"+s2+"\t"+s3+"\t"+total);
}
public static void main(String args[])
{
student s[]=new student[3];
for(int i=0;i<3;i++)
{
s[i]=new student();
s[i].set();
}
System.out.println("rollno\t Name\t M1\t M2\t M3\t total\n");
for(int i=0;i<3;i++)
s[i].disp();
}
}

DEPARTMENT OF COMPUTER SCIENCE, SKNG GFG COLLEGE, GANGAVATHI 10


JAVA PROGRAMMING

OUTPUT:

DEPARTMENT OF COMPUTER SCIENCE, SKNG GFG COLLEGE, GANGAVATHI 11


JAVA PROGRAMMING
/*8. In a college first year class are having the following attributes Name of the class (BCA,
BCom, BSc,BBA),Name of the staff No of the students in the class, Array of students in the
class.*/

class Fclass
{
String nameofclass,nameofstaff;
int noofstds;
Fclass(String noc,String nos,int numstd)
{
nameofclass=noc;
nameofstaff=nos;
noofstds=numstd;
}
void display()
{
System.out.println(nameofclass+"\t"+nameofstaff+"\t"+noofstds);
}
}
class Firstyear
{
public static void main(String args[])
{
Fclass std[]=new Fclass[4];
std[0]=new Fclass("BCA","RAVI KUMAR",139);
std[1]=new Fclass("BBA","KHADRI S S",110);
std[2]=new Fclass("BCom ","SHIVARAJ",100);
std[3]=new Fclass("BSc","SAVITHA B",90);
System.out.println("course\t staff\t Number of students");
for(int i=0;i<4;i++)
std[i].display();
}
}
OUTPUT:

DEPARTMENT OF COMPUTER SCIENCE, SKNG GFG COLLEGE, GANGAVATHI 12


JAVA PROGRAMMING
/*9. Define a class called first year with above attributes and define a suitable constructor.
Also write a method called best student() which process first-year object and return the
student with the highest total mark. In the main method define a first-year object and find the
best student of this class. */

import java.util.Scanner;
public class FirstYear
{
public String cname;
public String sname;
public int rollno;
public int m1, m2, m3, total;
public FirstYear( )
{
cname = "BCA"; m1 = m2 = m3 = total = 0;
}
public FirstYear(String cname)
{
this.cname = cname;
m1 = m2 = m3 = total = 0;
}
public void setStudent( )
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter student details: name, roll no, m1, m2, m3:");
sname = sc.next( );
rollno =sc.nextInt( );
m1 =sc.nextInt( );
m2 =sc.nextInt( );
m3 =sc.nextInt( );
total = m1 + m2 + m3;

DEPARTMENT OF COMPUTER SCIENCE, SKNG GFG COLLEGE, GANGAVATHI 13


JAVA PROGRAMMING
}
public void display( )
{
System.out.println(cname + "\t" + rollno + "\t" + sname + "\t" + m1 + "\t" + m2 + "\t"
+ m3 + "\t" + total);
}
public static void main(String args[ ])
{
FirstYear s[ ] = new FirstYear[60];
int max = 0;
String beststudent = "";
int n;
Scanner sc = new Scanner(System.in);
System.out.println("Enter no of students:");
n = sc.nextInt( );
for ( int i = 0; i < n; i++)
{
s[i] = new FirstYear( );
s[i].setStudent( );
if (s[i].total > max)
{
max = s[i].total;
beststudent =s[i].sname;
}
}
System.out.println("Course \tRollNo \tName \tM1 \tM2 \tM3 \tTotal");
for (int i=0; i < n ; i++)
{
s[i].display( );
}

DEPARTMENT OF COMPUTER SCIENCE, SKNG GFG COLLEGE, GANGAVATHI 14


JAVA PROGRAMMING
System.out.println("\nBest Student: " + beststudent);
System.out.println("Marks: " + max);
}
}

OUTPUT:

DEPARTMENT OF COMPUTER SCIENCE, SKNG GFG COLLEGE, GANGAVATHI 15


JAVA PROGRAMMING
/* 10. Program to define a class called Employee with the name and the date of appointment.
Create ten Employee objects as an array and sort them as per their date of appointment i.e,
print them as per their seniority. */

import java.util.*;
class Employee
{
String name;
Date appdate;
public Employee(String nm,Date apdt)
{
name=nm;
appdate=apdt;
}
public void display()
{
System.out.println("EmployeeName:"+name+"Appointmentdappdate.getDate()+"/"+a
ppdate.getMonth()+"/"+appdate.getYear();
}
}
class Empsort
{
public static void main (String args[])
{
Employee emp[]=new Employee[10];
emp[0]=new Employee("Shaha PD",new Date(1999,05,22));
emp[1]=new Employee("Patil AS",new Date(2000,01,12));
emp[2]=new Employee("Phadake PV",new Date(2009,04,25));
emp[3]=new Employee("Shide SS",new Date(2005,02,19));
emp[4]=new Employee("Shrivatsav RT",new Date(2010,02,01));
emp[5]=new Employee("Ramanjineya",new Date(2013,03,01));
emp[6]=new Employee("Jennifar",new Date(2014,04,01));
emp[7]=new Employee("Anjum",new Date(2022,05,01));
emp[8]=new Employee("Samant",new Date(2012,06,01));
emp[9]=new Employee("Channa",new Date(2011,07,01));
System.out.println("\nList of employees\n");
for(int i=0;i<emp.length;i++)
emp[i].display();
for(int i=0;i<emp.length;i++)
{
for(int j=0;j<emp.length;j++)
{
if(emp[i].appdate.before(emp[j].appdate))
{
Employee t=emp[i];
emp[i]=emp[j];
emp[j]=t;
}
}

DEPARTMENT OF COMPUTER SCIENCE, SKNG GFG COLLEGE, GANGAVATHI 16


JAVA PROGRAMMING
}
System.out.println("\n list of Employees Seniority wise\n");
for(int i=0;i<emp.length;i++)
emp[i].display();
}
}

OUTPUT:

DEPARTMENT OF COMPUTER SCIENCE, SKNG GFG COLLEGE, GANGAVATHI 17


JAVA PROGRAMMING
/*11. Create a package student.Full time B.Sc in your current working directory.
a) Create a default class student in the above package with the following attributes:
Name,age,sex./*

package Student.fulltime.B.Sc;
import java.util.Scanner;
public class Studentpkg
{
public String name,gender;
public int age;
public void accept()
{
System.out.println("Enter the name");
Scanner scan=new Scanner(System.in);
name=scan.nextLine();
System.out.println("Enter the gender");
gender=scan.nextLine();
System.out.println("Enter the age");
Scanner scan1=new Scanner(System.in);
age=scan1.nextInt();
}
public void display()
{
System.out.println("\n student Information\n");

System.out.println("Name:"+name+"\n"+"Gender"+gender+"\n"+"Age"+age+"\n");
}
}

DEPARTMENT OF COMPUTER SCIENCE, SKNG GFG COLLEGE, GANGAVATHI 18


JAVA PROGRAMMING
/*b) Have methods for storing as well as displaying.

package Student.fulltime.BCA;
import Student.fulltime.BCA.Studentpkg;
public class StudentP
{
public static void main(String args[])
{
Studentpkg S1=new Studentpkg();
S1.accept();
S1.display();
}
}

OUTPUT:
Enter the name:Ram
Enter the Gender:Male
Enter the Age:19

Student Information:

Name:Ram
Gender:Male
Age:19

DEPARTMENT OF COMPUTER SCIENCE, SKNG GFG COLLEGE, GANGAVATHI 19


JAVA PROGRAMMING

PART-B

DEPARTMENT OF COMPUTER SCIENCE, SKNG GFG COLLEGE, GANGAVATHI 20


JAVA PROGRAMMING

/* 1. Program to catch Negative Array Size Exception. This exception is caused when the
array is initialized to negative values.*/

public class NegativeArraySizeExceptionExample


{
public static void main(String args[])
{
try
{
int array[]=new int[-5];
}
catch(NegativeArraySizeException nase)
{
nase.printStackTrace();
}
System.out.println("continuing execution...");
}
}

OUTPUT:

DEPARTMENT OF COMPUTER SCIENCE, SKNG GFG COLLEGE, GANGAVATHI 21


JAVA PROGRAMMING
/* 2. Program to handle NullPointerException and use the "Finally" method to display a
message to the user. */

import java.io.*;
class Exceptionfinally
{
public static void main(String args[])
{
String ptr=null;
try
{
if(ptr.equals("gfg"))
System.out.println("same");
else
System.out.println("not same");
}
catch(NullPointerException e)
{
System.out.println("Null Pointer Exception Caught");
}
finally
{
System.out.println("finally block is always executed");
}
}
}

OUTPUT:

DEPARTMENT OF COMPUTER SCIENCE, SKNG GFG COLLEGE, GANGAVATHI 22


JAVA PROGRAMMING
/* 3. Program which create and displays a message on the window.*/

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
class Message implements ActionListener
{
public static void main(String args[])
{
JFrame frame=new JFrame("Original Frame");
frame.setSize(300,300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Message obj=new Message();
JButton b1=new JButton("view Message");
frame.add(b1);
b1.addActionListener(obj);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
JFrame sub_frame=new JFrame("Sub Frame");
sub_frame.setSize(200,200);
JLabel lbl=new JLabel("!!! Hello !!!");
sub_frame.add(lbl);
sub_frame.setVisible(true);
}
}

DEPARTMENT OF COMPUTER SCIENCE, SKNG GFG COLLEGE, GANGAVATHI 23


JAVA PROGRAMMING
OUTPUT:

DEPARTMENT OF COMPUTER SCIENCE, SKNG GFG COLLEGE, GANGAVATHI 24


JAVA PROGRAMMING
/* 4. Program to draw several shapes in the created window. */
import java.awt.*;
import java.applet.*;
public class Shapes extends Applet
{
public void paint(Graphics g)
{
g.setFont(new Font("Cambria",Font.BOLD,15));
g.drawString("Different Shapes",15,15);
g.drawLine(10,20,50,60);
g.drawRect(10,70,40,40);
g.setColor(Color.RED);
g.fillOval(60,20,30,90);
g.fillArc(60,135,80,40,180,180);
g.fillRoundRect(20,120,60,30,5,5);
}
}

/* <applet code="Shapes" width=200 height=200> </applet> */

OUTPUT:

DEPARTMENT OF COMPUTER SCIENCE, SKNG GFG COLLEGE, GANGAVATHI 25


JAVA PROGRAMMING
/* 5. Program to create an applet and draw gridlines. */

import java.awt.*;
import java.applet.*;
public class chessboard extends Applet
{
public void paint(Graphics g)
{
int row,column,x,y;
for(row=0;row<8;row++)
{
for(column=0;column<8;column++)
{
x=column*20;
y=row*20;
if((row%2)==(column%2))
g.setColor(Color.white);
else
g.setColor(Color.black);
g.fillRect(x,y,20,20);
}
}
}
}

/* <applet code="chessboard" width=200 height=200> </applet> */

DEPARTMENT OF COMPUTER SCIENCE, SKNG GFG COLLEGE, GANGAVATHI 26


JAVA PROGRAMMING

OUTPUT:

DEPARTMENT OF COMPUTER SCIENCE, SKNG GFG COLLEGE, GANGAVATHI 27


JAVA PROGRAMMING
/* 6. Program which creates a frame with two buttons father and mother. When we click the
father button the name of the father his age and designation must appear. */

import java.awt.*;
import java.awt.event.*;
public class FatherMother extends Frame implements ActionListener
{
Button b1,b2;
Label lbl;
FatherMother()
{
b1=new Button("Father");
b1.setBounds(50,40,170,40);
add(b1);
b1.addActionListener(this);
b2=new Button("Mother");
b2.setBounds(200,40,170,40);
add(b2);
lbl=new Label();
lbl.setBounds(50,80,400,200);
add(lbl);
b2.addActionListener(this);
setSize(300,300);
setLayout(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1)
lbl.setText("Name:Chiranjeevi Age:66 Designation:Manager");

DEPARTMENT OF COMPUTER SCIENCE, SKNG GFG COLLEGE, GANGAVATHI 28


JAVA PROGRAMMING
else
lbl.setText("Name:Kousalya Age:56 Dessignation:Asst.Manager");
}
public static void main(String args[])
{
FatherMother a=new FatherMother();
}
}

OUTPUT:

DEPARTMENT OF COMPUTER SCIENCE, SKNG GFG COLLEGE, GANGAVATHI 29


JAVA PROGRAMMING
/* 7. Create a frame which displays your personal details with respect to a button click. */
import java.awt.*;
import java.awt.event.*;
class Personal extends Frame implements ActionListener
{
Button b1;
Label lbl;
Personal()
{
b1=new Button("Display");
lbl=new Label();
b1.setBounds(30,90,90,30);
lbl.setBounds(250,250,250,120);
lbl.setText(" ");
setSize(400,350);
setVisible(true);
add(b1);
b1.addActionListener(this);
add(lbl);
}
public void actionPerformed(ActionEvent e)
{
lbl.setText("Name:Raja,Age:20,Course:BSC 2nd sem");
}
public static void main(String args[])
{
Personal p=new Personal();
}
}
/* <applet code="Personal.class" width="200" height="200"> </applet> */

DEPARTMENT OF COMPUTER SCIENCE, SKNG GFG COLLEGE, GANGAVATHI 30


JAVA PROGRAMMING
OUTPUT:

DEPARTMENT OF COMPUTER SCIENCE, SKNG GFG COLLEGE, GANGAVATHI 31


JAVA PROGRAMMING
/* 8. Create a simple applet which revels the personal information of yours. */
import java.applet.Applet;
import java.awt.Graphics;
public class First extends Applet
{
public void paint(Graphics g)
{
g.drawString("Name:Vindhya Rani",150,150);
g.drawString("Age:20",200,200);
g.drawString("Course:Bsc",250,250);
g.drawString("Percentage:85",300,300);
}
}

/* <applet code="First.class" width="400" height="400"></applet> */

OUTPUT:

DEPARTMENT OF COMPUTER SCIENCE, SKNG GFG COLLEGE, GANGAVATHI 32


JAVA PROGRAMMING
/* 9. Program to move different shapes according to the arrow key pressed. */
import java.applet.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class MoveShapes extends Applet implements KeyListener
{
private int x=20,y=20;
private int Width=100,Height=100;
private int inc=5;
private Color myColor=Color.red;
public void init()
{
addKeyListener(this);
}
public void keyPressed(KeyEvent e)
{
int code=e.getKeyCode();
if(code==KeyEvent.VK_LEFT)
{
x-=inc;
if(x<getWidth())
x=20;
repaint();
}
else if(code==KeyEvent.VK_RIGHT)
{
x+=inc;
if(x>getWidth())
x=getWidth()-100;

DEPARTMENT OF COMPUTER SCIENCE, SKNG GFG COLLEGE, GANGAVATHI 33


JAVA PROGRAMMING
repaint();
}
else if(code==KeyEvent.VK_DOWN)
{
y+=inc;
if(y>getHeight())
y=getHeight()-100;
repaint();
}
else if(code==KeyEvent.VK_UP)
{
y-=inc;
if(y<getHeight())
y=20;
repaint();
}
}
public void paint(Graphics g)
{
g.drawString("Click to activate",7,20);
g.setColor(Color.RED);
g.fillRect(x,y,Width,Height);
g.setColor(Color.yellow);
g.fillOval(x+100,y+100,Width,Height);
}
public void keyTyped(KeyEvent e){}
public void keyReleased(KeyEvent e){}
}
/*<applet code="MoveShapes.class" width="400" height="400"> </applet> */

DEPARTMENT OF COMPUTER SCIENCE, SKNG GFG COLLEGE, GANGAVATHI 34


JAVA PROGRAMMING

OUTPUT:

DEPARTMENT OF COMPUTER SCIENCE, SKNG GFG COLLEGE, GANGAVATHI 35


JAVA PROGRAMMING
/* 10. Program to create a window when we press M or m the window displays Good
Morning, A or a the window displays Good Afternoon, E or e the window displays Good
Evening, N or n the window displays Good Night. */

import java.applet.Applet;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class Key extends Applet implements KeyListener
{
static String
str="Hello";
public void init()
{
setBackground(Color.white);
Font f=new Font("arial",Font.BOLD,20);
setFont(f);
addKeyListener(this);
}
public void paint(Graphics g)
{
g.drawString("Click here to activate",100,100);
g.drawString("Type M/m--->To Good Morning",100,200);
g.drawString("Type A/a--->To Good Afternoon",100,300);
g.drawString("Type E/e--->To Good Evening",100,400);
g.drawString("Type N/n--->To Good Night",100,500);
g.setColor(Color.blue);
g.drawString(str,600,200);
}

DEPARTMENT OF COMPUTER SCIENCE, SKNG GFG COLLEGE, GANGAVATHI 36


JAVA PROGRAMMING
public void keyTyped(KeyEvent e)
{
char key=e.getKeyChar();
if(key=='M'||key=='m')
{
str="GOOD MORNING";
repaint();
}
else if(key=='A'||key=='a')
{
str="GOOD AFTERNOON";
repaint();
}
if(key=='E'||key=='e')
{
str="GOOD EVENING";
repaint();
}
if(key=='N'||key=='n')
{
str="GOOD NIGHT";
repaint();
}
switch(key)
{
case'M':
case'm':str="GOOD MORNING";repaint();break;
case'A':
case'a':str="GOOD AFTERNOON";repaint();break;
case'E':

DEPARTMENT OF COMPUTER SCIENCE, SKNG GFG COLLEGE, GANGAVATHI 37


JAVA PROGRAMMING
case'e':str="GOOD EVENING";repaint();break;
case'N':
case'n':str="GOOD NIGHT";repaint();break;
}
}
public void keyPressed(KeyEvent e){}
public void keyReleased(KeyEvent e){}
}
/*<applet code="Key.class" width="300" height="300"> </applet> */

OUTPUT:

DEPARTMENT OF COMPUTER SCIENCE, SKNG GFG COLLEGE, GANGAVATHI 38


JAVA PROGRAMMING

DEPARTMENT OF COMPUTER SCIENCE, SKNG GFG COLLEGE, GANGAVATHI 39


JAVA PROGRAMMING
/* 11. Demonstrate the various mouse handling events using suitable example. */
import java.awt.*;
import java.awt.event.*;
public class MainM extends Frame implements MouseListener
{
Label lbl;
MainM()
{
super("AWT Frame");
lbl=new Label();
lbl.setBounds(25,60,250,30);
lbl.setAlignment(Label.CENTER);
add(lbl);
setSize(300,300);
setLayout(null);
setVisible(true);
this.addMouseListener(this);
}
public void windowClosing(WindowEvent e)
{
dispose();
}
public static void main(String args[])
{
new MainM();
}
public void mouseClicked(MouseEvent e)
{
lbl.setText("Mouse Clicked");
}

DEPARTMENT OF COMPUTER SCIENCE, SKNG GFG COLLEGE, GANGAVATHI 40


JAVA PROGRAMMING
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e)
{
lbl.setText("Mouse Entered");
}
public void mouseExited(MouseEvent e)
{
lbl.setText("Mouse Exited");
}
}

OUTPUT:

DEPARTMENT OF COMPUTER SCIENCE, SKNG GFG COLLEGE, GANGAVATHI 41


JAVA PROGRAMMING
/*12. Program to create menu bar and pull down menus. */
import java.awt.*;
class MenuExample
{
MenuExample()
{
Frame f=new Frame("Menu and MenuItem Example");
MenuBar mb=new MenuBar();
Menu menu=new Menu("Menu");
Menu SubMenu=new Menu("Sub Menu");
MenuItem i1=new MenuItem("Item 1");
MenuItem i2=new MenuItem("Item 2");
MenuItem i3=new MenuItem("Item 3");
MenuItem i4=new MenuItem("Item 4");
MenuItem i5=new MenuItem("Item 5");
menu.add(i1);
menu.add(i2);
menu.add(i3);
SubMenu.add(i4);
SubMenu.add(i5);
menu.add(SubMenu);
mb.add(menu);
f.setMenuBar(mb);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String args[])
{
new MenuExample();

DEPARTMENT OF COMPUTER SCIENCE, SKNG GFG COLLEGE, GANGAVATHI 42


JAVA PROGRAMMING
}
}

OUTPUT:

DEPARTMENT OF COMPUTER SCIENCE, SKNG GFG COLLEGE, GANGAVATHI 43

You might also like