JAVA PROGRAMMING LAB
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 a[])
{
int x=10,y=20;
if ( x > y )
{
System.out.println("x is greater y");
}
else
{
System.out.println("x is not greater than y");
}
}
}
******************************OUT-PUT**********************************
F:\nepjavalab\java lab>javac largest.java
F:\nepjavalab\java lab>java largest
x is not greater than y
VSM’S BBA AND BCA COLLGE Page 1
JAVA PROGRAMMING LAB
2. Program to list the factorial of the numbers 1 to 10. To calculate the factorial value,
use while loop. (Hint: Fact of 4 = 4*3*2*1)
class factorial
{
public static void main(String a[])
{
int i=1,n=10,fact=1;
while ( i<=n )
{
fact=fact*i;
System.out.println("factorial of "+ i +"="+fact);
i++;
}
}
}
******************************OUT-PUT**********************************
F:\nepjavalab\java lab>javac factorial.java
F:\nepjavalab\java lab>java factorial
factorial of 1=1
factorial of 2=2
factorial of 3=6
factorial of 4=24
factorial of 5=120
factorial of 6=720
factorial of 7=5040
factorial of 8=40320
factorial of 9=362880
factorial of 10=3628800
VSM’S BBA AND BCA COLLGE Page 2
JAVA PROGRAMMING LAB
3. Program to find the area and circumference of the circle by accepting the radius from
the user.
import java.io.*;
class circle
{
public static void main(String a[])
{
float pi=3.142f;
DataInputStream d=new DataInputStream (System.in);
System.out.println("Enter radius of a circle:");
try
{
int r=Integer.parseInt(d.readLine());
float area = pi*r*r;
float cir = 2*pi*r;
System.out.println("Area of circle="+area);
System.out.println("Circumference of a circle="+cir);
}
catch(Exception e)
{
}
}
}
******************************OUT-PUT**********************************
F:\nepjavalab\java lab>javac circle.java
F:\nepjavalab\java lab>java circle
Enter radius of a circle:
Area of circle=78.55
Circumference of a circle=31.42
VSM’S BBA AND BCA COLLGE Page 3
JAVA PROGRAMMING LAB
4. 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.
import java.io.*;
class add
{
void sum(int a,int b)
{
int res=a+b;
System.out.println("The Addition of two no is="+res);
}
void sum(float f1,float f2)
{
float res=f1+f2;
System.out.println("The Addition of two no is="+res);
}
class mathover
{
public static void main(String args[])
{
add a1 =new add();
a1.sum(10,20);
a1.sum(10.50f,50.25f);
}
}
******************************OUT-PUT**********************************
F:\nepjavalab\java lab>javac mathover.java
F:\nepjavalab\java lab>java mathover
The Addition of two no is=30
The Addition of two no is=60.75
VSM’S BBA AND BCA COLLGE Page 4
JAVA PROGRAMMING LAB
5. Program to perform mathematical operations. Create a class called AddSub with
methods to add and subtract. Create another class called MulDiv that extends from
AddSub 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=6,y=2;
int sum()
{
return(x+y);
}
int sub()
{
return (x-y);
}
}
class MulDiv extends AddSub
{
int mul()
{
return (x*y);
}
int div()
{
return (x/y);
}
}
class mathop
{
public static void main(String a[])
{
MulDiv obj=new MulDiv();
System.out.println("Sum="+obj.sum());
System.out.println("Subtraction="+obj.sub());
System.out.println("Multiplication="+obj.mul());
System.out.println("Division="+obj.div());
}
}
VSM’S BBA AND BCA COLLGE Page 5
JAVA PROGRAMMING LAB
******************************OUT-PUT**********************************
F:\nepjavalab\java lab>javac mathop.java
F:\nepjavalab\java lab>java mathop
Sum=8
Subtraction=4
Multiplication=12
Division=3
VSM’S BBA AND BCA COLLGE Page 6
JAVA PROGRAMMING LAB
6. 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 staticmain
{
int x;
static int y,z;
static void mul()
{
y=5;
z=15;
int m=y*z;
System.out.println("Multiplication of y & z="+m);
}
public static void main(String ar[])
{
staticmain s1=new staticmain();
staticmain s2=new staticmain();
s1.x=10;
s2.x=20;
s1.y=100;
s2.y=200;
System.out.println("s1's x="+s1.x);
System.out.println("s2's x="+s2.x);
System.out.println("s1's y="+s1.y);
System.out.println("s2's y="+s2.y);
staticmain.mul();
}
}
******************************OUT-PUT**********************************
F:\nepjavalab\java lab>javac staticmain.java
F:\nepjavalab\java lab>java staticmain
s1's x=10
s2's x=20
s1's y=200
s2's y=200
Multiplication of y & z=75
VSM’S BBA AND BCA COLLGE Page 7
JAVA PROGRAMMING LAB
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 passing 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.*;
public class student
{
static int eno;
static String name;
static float m1,m2,m3,tot;
static void input()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter student enrollment no:");
eno=sc.nextInt();
System.out.println("Enter student name:");
name=sc.next();
System.out.println("Enter subject1 marks:");
m1=sc.nextFloat();
System.out.println("Enter subject2 marks:");
m2=sc.nextFloat();
System.out.println("Enter subject3 marks:");
m3=sc.nextFloat();
}
student(float x,float y,float z)
{
if(x>=50 && y>=50 && z>=50)
{
tot=x+y+z;
}
else
{
tot=0;
}
}
VSM’S BBA AND BCA COLLGE Page 8
JAVA PROGRAMMING LAB
void display()
{
System.out.println("enrollment no is:"+eno);
System.out.println("Name is:"+name);
System.out.println("Marks1="+m1);
System.out.println("Marks2="+m2);
System.out.println("Marks3="+m3);
System.out.println("Total="+tot);
}
public static void main(String a[])
{
student[] obj=new student[3];
for(int i=0;i<3;i++)
{
student.input();
obj[i]=new student(m1,m2,m3);
obj[i].display();
}
}
}
******************************OUT-PUT**********************************
F:\nepjavalab\java lab>javac student.java
F:\nepjavalab\java lab>java student
Enter student enrollment no:
101
Enter student name:
bahubali
Enter subject1 marks:
98
Enter subject2 marks:
65
Enter subject3 marks:
87
enrollment no is:101
Name is:bahubali
Marks1=98.0
Marks2=65.0
Marks3=87.0
Total=250.0
VSM’S BBA AND BCA COLLGE Page 9
JAVA PROGRAMMING LAB
Enter student enrollment no:
102
Enter student name:
parv
Enter subject1 marks:
87
Enter subject2 marks:
97
Enter subject3 marks:
77
enrollment no is:102
Name is:parv
Marks1=87.0
Marks2=97.0
Marks3=77.0
Total=261.0
Enter student enrollment no:
103
Enter student name:
soukhya
Enter subject1 marks:
88
Enter subject2 marks:
99
Enter subject3 marks:
77
enrollment no is:103
Name is:soukhya
Marks1=88.0
Marks2=99.0
Marks3=77.0
Total=264.0
VSM’S BBA AND BCA COLLGE Page 10
JAVA PROGRAMMING LAB
8. Write a program to demonstrate multiple inheritance and use of Implementing
Interfaces
import java.io.*;
interface circle
{
float pi= 3.142f ;
float compute ( float r );
}
class rect
{
float compute ( float l, float b )
{
return ( l * b ) ;
}
}
class area extends rect implements circle
{
public float compute ( float r )
{
return ( pi * r * r );
}
}
class multiple
{
public static void main(String args[])
{
area a = new area ();
System.out.println ( "Area of circle=" + a.compute ( 1f ) );
System.out.println ( "Area of rectangle=" + a.compute ( 5f, 10f ) );
}
}
******************************OUT-PUT**********************************
F:\nepjavalab\java lab>javac multiple.java
F:\nepjavalab\java lab>java multiple
Area of circle=3.142
Area of rectangle=50.0
VSM’S BBA AND BCA COLLGE Page 11
JAVA PROGRAMMING LAB
9. Illustrate creation of thread by
a) Extending Thread class.
class A extends Thread
{
public void run()
{
for(int i=1;i<=5;i++)
{
System.out.println("From ThreadA i value is="+i);
}
System.out.println("Exit from ThreadA ");
}
}
class B extends Thread
{
public void run()
{
for(int j=1;j<=5;j++)
{
System.out.println("From ThreadB j value is="+j);
}
System.out.println("Exit from ThreadB ");
}
}
class C extends Thread
{
public void run()
{
for(int k=1;k<=5;k++)
{
System.out.println("From ThreadC k value is="+k);
}
System.out.println("Exit from ThreadC ");
}
}
VSM’S BBA AND BCA COLLGE Page 12
JAVA PROGRAMMING LAB
class threadmain
{
public static void main(String args[])
{
A t1=new A();
B t2=new B();
C t3=new C();
t1.start();
t2.start();
t3.start();
}
}
*****************************OUT-PUT*******************************
F:\nepjavalab\java lab>javac threadmain.java
F:\nepjavalab\java lab>java threadmain
From ThreadA i value is=1
From ThreadC k value is=1
From ThreadA i value is=2
From ThreadB j value is=1
From ThreadC k value is=2
From ThreadA i value is=3
From ThreadB j value is=2
From ThreadC k value is=3
From ThreadA i value is=4
From ThreadB j value is=3
From ThreadC k value is=4
From ThreadA i value is=5
From ThreadB j value is=4
From ThreadC k value is=5
Exit from ThreadA
From ThreadB j value is=5
Exit from ThreadC
Exit from ThreadB
VSM’S BBA AND BCA COLLGE Page 13
JAVA PROGRAMMING LAB
b) Implementing Runnable Interfaces
class A implements Runnable
{
public void run()
{
for(int i=1;i<=5;i++)
{
System.out.println("From ThreadA i value is="+i);
}
System.out.println("Exit from ThreadA ");
}
}
class B implements Runnable
{
public void run()
{
for(int j=1;j<=5;j++)
{
System.out.println("From ThreadB j value is="+j);
}
System.out.println("Exit from ThreadB ");
}
}
class C implements Runnable
{
public void run()
{
for(int k=1;k<=5;k++)
{
System.out.println("From ThreadC k value is="+k);
}
System.out.println("Exit from ThreadC ");
}
}
VSM’S BBA AND BCA COLLGE Page 14
JAVA PROGRAMMING LAB
class runnablemain
{
public static void main(String args[])
{
A t1=new A();
Thread ta=new Thread(t1);
B t2=new B();
Thread tb=new Thread(t2);
C t3=new C();
Thread tc=new Thread(t3);
ta.start();
tb.start();
tc.start();
}
}
VSM’S BBA AND BCA COLLGE Page 15
JAVA PROGRAMMING LAB
*****************************OUT-PUT*******************************
F:\nepjavalab\java lab>javac runnablemain.java
F:\nepjavalab\java lab> java runnablemain
From ThreadC k value is=1
From ThreadB j value is=1
From ThreadC k value is=2
From ThreadA i value is=1
From ThreadB j value is=2
From ThreadC k value is=3
From ThreadA i value is=2
From ThreadB j value is=3
From ThreadC k value is=4
From ThreadA i value is=3
From ThreadB j value is=4
From ThreadC k value is=5
From ThreadA i value is=4
From ThreadB j value is=5
Exit from ThreadC
From ThreadA i value is=5
Exit from ThreadB
Exit from ThreadA
VSM’S BBA AND BCA COLLGE Page 16
JAVA PROGRAMMING LAB
10. Create a package „BCA‟ in your current working directory.
a. Create a class student in the above package with the following attributes: Name, age,
gender. Include appropriate constructor and a method for displaying the details.
package BCA;
public class student1
{
public String name,gender;
public int age;
public student1(String n,int a,String g)
{
name=n;
age=a;
gender=g;
}
public void display()
{
System.out.println("Name is:"+name);
System.out.println("Age is:"+age);
System.out.println("Gender is:"+gender);
}
}
b. Import above package and access the member variables and function contained in a
package.
import BCA.*;
public class mainpack
{
public static void main(String args[])
{
student1 s1= new student1("abc",20,"f");
s1.display();
}
}
VSM’S BBA AND BCA COLLGE Page 17
JAVA PROGRAMMING LAB
*****************************OUT-PUT*******************************
F:\nepjavalab\java lab>cd BCA
F:\nepjavalab\java lab\BCA>javac student.java
F:\nepjavalab\java lab\BCA>cd..
F:\nepjavalab\java lab>javac mainpack.java
F:\nepjavalab\java lab>java mainpack
Name is:abc
Age is:20
Gender is:f
VSM’S BBA AND BCA COLLGE Page 18
JAVA PROGRAMMING LAB
PART-B
1. Program to catch Negative Array Size Exception. This exception is caused when the
array size is initialized to negative values.
class arrayexcept
{
public static void main(String ar[])
{
try
{
int a[]=new int[-1];
System.out.println("Exception not caught because array size is not negative");
}
catch(NegativeArraySizeException e)
{
System.out.println("Array size is initialized to Negative value");
}
}
}
******************************OUT-PUT**********************************
F:\nepjavalab\java lab>javac arrayexcept .java
F:\nepjavalab\java lab>java arrayexcept
Array size is initialized to Negative value
VSM’S BBA AND BCA COLLGE Page 19
JAVA PROGRAMMING LAB
2. Program to demonstrate exception handling with try, catch and finally.
import java.io.*;
class trycatch
{
public static void main(String arg[])
{
int a=0,b=0,c=0;
try
{
DataInputStream in =new DataInputStream(System.in);
System.out.println("Enter the value of a and b");
a=Integer.parseInt(in.readLine());
b=Integer.parseInt(in.readLine());
c=a/b;
System.out.println("value after division a by b is = "+c);
}
catch(Exception e)
{
System.out.println("Exception caught in try block");
System.out.println("Exception is=" + e);
System.out.println("The value of b should not be zero it creates a exception");
}
finally
{
System.out.println("I am always execute at the last ");
}
}
}
****************************OUT-PUT************************************
F:\nepjavalab\java lab>javac trycatch.java
F:\nepjavalab\java lab>java trycatch
Enter the value of a and b
10
0
Exception caught in try block
Exception is=java.lang.ArithmeticException: / by zero
The value of b should not be zero it creates a exception
I am always execute at the last
VSM’S BBA AND BCA COLLGE Page 20
JAVA PROGRAMMING LAB
3. Program which create and displays a message on the window
import java.awt.*;
import java.awt.event.*;
public class message extends Frame
{
Label l1=new Label("hiiiii hello welcome ");
message()
{
add(l1);
setSize(300,300);
setVisible(true);
public static void main(String ar[])
{
new message();
}
}
****************************OUT-PUT************************************
VSM’S BBA AND BCA COLLGE Page 21
JAVA PROGRAMMING LAB
4. Program to draw several shapes in the created window
import java.applet.*;
import java.awt.*;
public class shapes extends Applet
{
public void paint(Graphics g)
{
g.setColor(Color.red);
g.drawLine(10,10,40,40);
g.drawRect(50,50,50,100);
g.fillRect(120,120,100,100);
g.drawOval(220,220,80,50);
g.fillOval(300,300,50,50);
g.drawArc(400,400,80,50,100,200);
}
}
/*<applet
code=shapes.class
width=400
height=600>
</applet>*/
VSM’S BBA AND BCA COLLGE Page 22
JAVA PROGRAMMING LAB
****************************OUT-PUT************************************
VSM’S BBA AND BCA COLLGE Page 23
JAVA PROGRAMMING LAB
5. Programto create a 4×4 grid and fills it in with 15 buttons, each
1. labeled with its index.
import java.awt.*;
import java.awt.event.*;
public class buttongrid
{
Frame f;
buttongrid ()
{
f=new Frame("Grid with Buttons"); // creates frame with title mentioned.
// f=new JFrame(); <-- it creates frame without title
Button b1=new Button("1");
Button b2=new Button("2");
Button b3=new Button("3");
Button b4=new Button("4");
Button b5=new Button("5");
Button b6=new Button("6");
Button b7=new Button("7");
Button b8=new Button("8");
Button b9=new Button("9");
Button b10=new Button("10");
Button b11=new Button("11");
Button b12=new Button("12");
Button b13=new Button("13");
Button b14=new Button("14");
Button b15=new Button("15");
f.add(b1);
f.add(b2);
f.add(b3);
f.add(b4);
f.add(b5);
f.add(b6);
f.add(b7);
f.add(b8);
f.add(b9);
f.add(b10);
f.add(b11);
f.add(b12);
f.add(b13);
f.add(b14);
f.add(b15);
f.setLayout(new GridLayout(4,4));
f.setSize(400,300); // void setSize(width, height)
f.setVisible(true);
}
VSM’S BBA AND BCA COLLGE Page 24
JAVA PROGRAMMING LAB
public void windowClosing(WindowEvent we)
{
f.setVisible(false);
}
public static void main(String ar[])
{
new buttongrid ();
}
}
****************************OUT-PUT************************************
F:\nepjavalab\java lab>javac buttongrid.java
F:\nepjavalab\java lab>java buttongrid
VSM’S BBA AND BCA COLLGE Page 25
JAVA PROGRAMMING LAB
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. When we
click mother button similar details of mother also appear.
import java.awt.*;
import java.awt.event.*;
public class buttonclick extends Frame implements ActionListener
{
static Panel p=new Panel();
Button b1 = new Button("Father");
Button b2 = new Button("Mother");
TextField t;
buttonclick()
{
b1=new Button("Father");
b2=new Button("Mother");
t=new TextField(150);
b1.addActionListener(this);
b2.addActionListener(this);
p.add(t);
p.add(b1);
p.add(b2);
add(p);
setSize(1100,500);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1)
{
t.setText("Father Name is: ABC Father age is: 50"+ "\n" + "Father Designation is:
BusinessMan");
}
else if(e.getSource()==b2)
{
t.setText("Mother Name is: XYZ Mother age is: 45 Mother Designation is: Bank
Employee");
}
}
VSM’S BBA AND BCA COLLGE Page 26
JAVA PROGRAMMING LAB
public static void main(String ar[])
{
buttonclick f = new buttonclick();
}
}
****************************OUT-PUT************************************
F:\nepjavalab\java lab>javac buttonclick.java
F:\nepjavalab\java lab>java buttonclick
VSM’S BBA AND BCA COLLGE Page 27
JAVA PROGRAMMING LAB
VSM’S BBA AND BCA COLLGE Page 28
JAVA PROGRAMMING LAB
7. Create a frame which displays your personal details with respect to a button click
import java.awt.*;
import java.awt.event.*;
public class person extends Frame implements ActionListener
{
Button b=new Button("Click");
TextArea t=new TextArea(8,20);
Panel p =new Panel();
person()
{
p.add(t);
p.add(b);
add(p);
setSize(300,500);
setVisible(true);
b.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
t.setText("Name: BAHUBALI\n");
t.append(" Age=39 \n Gender=MALE \n MOB=9632587147");
}
public static void main(String ar[])
{
new person();
}
}
VSM’S BBA AND BCA COLLGE Page 29
JAVA PROGRAMMING LAB
****************************OUT-PUT************************************
VSM’S BBA AND BCA COLLGE Page 30
JAVA PROGRAMMING LAB
8. Program to create a window with TextFields and Buttons. The "ADD" button adds
the two integers and display the result. The "CLEAR" button shall clear all the text
fields
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class addnum extends Frame implements ActionListener
{
TextField t1=new TextField(10);
TextField t2=new TextField(10);
TextField t3=new TextField(10);
Label l1=new Label("A");
Label l2=new Label("B");
Label l3=new Label("SUM");
Button b1=new Button("ADD");
Button b2=new Button("CLEAR");
Panel p=new Panel();
addnum()
{
p.add(l1);
p.add(t1);
p.add(l2);
p.add(t2);
p.add(l3);
p.add(t3);
p.add(b1);
p.add(b2);
add(p);
b1.addActionListener(this);
b2.addActionListener(this);
setSize(300,400);
setVisible(true);
}
VSM’S BBA AND BCA COLLGE Page 31
JAVA PROGRAMMING LAB
public void actionPerformed(ActionEvent e)
{
int a,b,sum;
if(e.getSource()==b1)
{
a=Integer.parseInt(t1.getText());
b=Integer.parseInt(t2.getText());
sum = a+b;
t3.setText(Integer.toString(sum));
}
else if(e.getSource()==b2)
{
t1.setText(" ");
t2.setText(" ");
t3.setText(" ");
}
}
public static void main(String agrs[])
{
addnum d=new addnum();
}
****************************OUT-PUT************************************
VSM’S BBA AND BCA COLLGE Page 32
JAVA PROGRAMMING LAB
9. Program to create a window, when we press M or m, the window displays “good
morning”, A or a, the window display‟s Good Afternoon” , E or e, the window displays
“good morning”, N or n, the window displays “good morning
import java.awt.*;
import java.awt.event.*;
public class keymessage extends Frame implements KeyListener
{
TextField t=new TextField();
TextField t2=new TextField();
keymessage()
{
t.setBounds(20,100,150,30);
t2.setBounds(20,150,150,30);
add(t);
add(t2);
t.addKeyListener(this);
t2.addKeyListener(this);
setSize(300,300);
setLayout(null);
setVisible(true);
public void keyTyped(KeyEvent ke)
{
if(ke.getKeyChar()=='M' || ke.getKeyChar()=='m')
{
t2.setText("Good Morning");
}
else if(ke.getKeyChar()=='A' || ke.getKeyChar()=='a')
{
t2.setText("Good AfterNoon");
}
else if(ke.getKeyChar()=='E' || ke.getKeyChar()=='e')
{
t2.setText("Good Evening");
}
else if(ke.getKeyChar()=='N' || ke.getKeyChar()=='n')
{
t2.setText("Good Night");
}
}
VSM’S BBA AND BCA COLLGE Page 33
JAVA PROGRAMMING LAB
public void keyReleased(KeyEvent ke)
{
}
public void keyPressed(KeyEvent ke)
{
}
public static void main(String args[])
{
keymessage k=new keymessage();
}
****************************OUT-PUT************************************
F:\nepjavalab\java lab>javac keymessage.java
F:\nepjavalab\java lab>java keymessage
VSM’S BBA AND BCA COLLGE Page 34
JAVA PROGRAMMING LAB
VSM’S BBA AND BCA COLLGE Page 35
JAVA PROGRAMMING LAB
10. Demonstrate the various mouse handling events using suitable example.
import java.awt.*;
import java.awt.event.*;
public class mouseevent extends Frame implements MouseListener
{
Label l;
mouseevent()
{
addMouseListener(this);
l=new Label();
l.setBounds(20,50,100,20);
add(l);
setSize(300,300);
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 mouseevent();
}
}
VSM’S BBA AND BCA COLLGE Page 36
JAVA PROGRAMMING LAB
****************************OUT-PUT************************************
F:\nepjavalab\java lab>javac mouseevent.java
F:\nepjavalab\java lab>java mouseevent
VSM’S BBA AND BCA COLLGE Page 37
JAVA PROGRAMMING LAB
VSM’S BBA AND BCA COLLGE Page 38
JAVA PROGRAMMING LAB
11. 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("Degree Courses");
Menu submenu=new Menu("Science");
MenuItem i1=new MenuItem("BCA");
MenuItem i2=new MenuItem("BBA");
MenuItem i3=new MenuItem("BCom");
MenuItem i4=new MenuItem("BSc");
MenuItem i5=new MenuItem("Msc");
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();
}
}
VSM’S BBA AND BCA COLLGE Page 39
JAVA PROGRAMMING LAB
****************************OUT-PUT************************************
F:\nepjavalab\java lab>javac menuexample.java
F:\nepjavalab\java lab>java menuexample
VSM’S BBA AND BCA COLLGE Page 40