0% found this document useful (0 votes)
3 views27 pages

Java Lab Manual

The document contains a series of Java programming exercises, including creating simple applications, handling exceptions, using classes and objects, and implementing GUI components. Each exercise includes a description, code implementation, and expected output. Topics covered range from basic Java syntax to more advanced concepts like user-defined exceptions and GUI programming.

Uploaded by

omprakashv2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views27 pages

Java Lab Manual

The document contains a series of Java programming exercises, including creating simple applications, handling exceptions, using classes and objects, and implementing GUI components. Each exercise includes a description, code implementation, and expected output. Topics covered range from basic Java syntax to more advanced concepts like user-defined exceptions and GUI programming.

Uploaded by

omprakashv2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

CA-C9P: JAVA PROGRAMMING LAB

1. Write a simple java application, to print the message, “Welcome to java”


Sol:

class first
{
public static void main(String args[])
{
[Link]("Welcome to Java");
}
}

Output:

2. Write a program to display the month of a year. Months of the year should be held in an array.

import [Link];
class demo
{
public static void main(String[] args)
{
Calendar cal = [Link]();
String[] month = new String[] {"January", "February", "March", "April",
"May", "June",
"July", "August","September", "October", "November", "December" };
[Link]("Current Month = " + month[[Link]([Link])]);
}
}

Output:
Write a program to demonstrate a division by zero exception
Program:

class zeroexe
{
public static void main (String args[])
{
int a = 15, b = 0, result = 0;
try{
result = a/b;
[Link]("The result is" +result);
}
catch (ArithmeticException e)
{
[Link] ("Can't be divided by Zero " + e);
}
}
}

Output:

3. Write a program to create a user defined exception say Pay Out of Bounds. .
Program:
import [Link].*;
class OutOfBoundException extends Exception
{
OutOfBoundException(String msg)
{
[Link]("Out of Bounds Exception:"+msg);
}
}
class userdefinedex
{
public static void main(String args[]) throws OutOfBoundException
{
[Link]("enter age of the person");
Scanner sc = new Scanner([Link]);
int age = [Link]();
if(age<18 || age>100)
{
throw new OutOfBoundException("Person is not eligible for voting");
}
else
{
[Link]("Person is eligible for voting");
}
}
}
Output:

Output2:

4. Write a java 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.
Program:

class sum
{
int a,b;
sum()
{
a=10; b=5;
}
void add()
{
int c=a+b;
[Link]("Sum is "+c);
}

void add(int a,int b)


{
int c=a+b;
[Link]("sum is "+c);
}
void add(double a, double b)
{
double c=a+b;
[Link]("sum is "+c);
}

class calculate
{
public static void main(String args[])
{
sum S1 = new sum();
[Link]();
[Link](20,40);
[Link](6.3,7.8);
}
}

Output:

5. Write a 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.
Program:
class addsub
{
int a,b;

addsub()
{
a=50; b=10;
}
void add()
{
int c=a+b;
[Link]("addition of ”+a+ "and" +b+ "is" +c);
}
void sub()
{
int c=a-b;
[Link]("subtract of" +a+ "and" +b+ "is" +c);
}
}
class muldiv extends addsub
{
void multi()
{
int c=a*b;
[Link]("multiplication of " +a+ "and" +b+ "is" +c);
}
void div()
{
int c=a/b;
[Link]("division of"+a+"and "+b+"is"+c);
}
}

class arthmetic
{
public static void main(String args [])
{
muldiv md = new muldiv();
[Link]("ARITHMETIC OPERATIONS");
[Link]();
[Link]();
[Link]();
[Link]();
}
}

Output:

6. Write a 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.

Program:

class student
{
static String college=" bcu COllege";
int rno;
String name;
student(int rno,String name)
{
[Link]=rno;
[Link]=name;
}
void display()
{
[Link](rno+" "+name+" "+college);
}
}

public class StaticDemo


{
public static void main(String args[])
{
[Link]("objects sharing the static varible-College name\n");
student s1= new student(111,"bcu");
student s2= new student(112,"college");
[Link]();
[Link]();
[Link]("\nStatoc value changed by one of the object \n");
[Link]=" bcu COllege (Autonomous)";
[Link]();
[Link]();
}
}

7. Write a java program to create a student class with following attributes:


Enrollment_id: 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 declaredas 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.

Program:
import [Link].*;
class student
{
Scanner sc= new Scanner([Link]);
String rollno;
String name;
int m1,m2,m3,total;
student()
{
readdata();
}
public void readdata()
{
[Link]("enter student details");
[Link]("enter rollnumber");
rollno=[Link]();
[Link]("enter Name");
name=[Link]();
[Link]("enter marks in 3 subjects");
m1=[Link]();
m2=[Link]();
m3=[Link]();
if(m1>=50 && m2>= 50 && m3>= 50)
total=m1+m2+m3;
else
total=0;
}
public void display()
{
[Link](rollno+"\t\t"+name+"\t\t"+total);
}
}
public class Studentinfo
{
public static void main(String args[])
{
student s[]= new student[3];
for(int i=0;i<3;i++)
{
s[i]=new student();
}
[Link]("\t Student Details\n");
[Link]("Roll NO \tName\t Total");
for(int i=0;i<3;i++)
{
s[i].display();
}
}
}

Output:
8. In a college first year class are having the following attributesName of the class (BCA, BCom, BSc),
Name of the staff No of the students in the class, Array of students in the class
Program:

import [Link].*;
class student
{
Scanner sc= new Scanner([Link]);
String rollno;
String name;
int m1,m2,m3,total;
student()
{
readdata();
}
public void readdata()
{
[Link]("enter student details");
[Link]("enter rollnumber");
rollno=[Link]();
[Link]("enter Name");
name=[Link]();
[Link]("enter marks in 3 subjects");
m1=[Link]();
m2=[Link]();
m3=[Link]();
if(m1>=50 && m2>= 50 && m3>= 50)
total=m1+m2+m3;
else
total=0;
}
public void display()
{
[Link](rollno+"\t\t"+name+"\t\t"+total);
}
}
public class Studentinfo
{
public static void main(String args[])
{
student s[]= new student[3];
for(int i=0;i<3;i++)
{
s[i]=new student();
}
[Link]("\t Student Details\n");
[Link]("Roll NO \tName\t Total");
for(int i=0;i<3;i++)
{
s[i].display();
}
}
}

Output:
9. Define a class called first year with above attributes and define a suitable constructor. Also write a
method called best Student () which process a 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

Program:
import [Link].*;
class Firstyear
{
String classname;
String Classteacher;
int studentcount;
int marks[]=new int[50];
String stdnames[]=new String[50];
Scanner sc = new Scanner([Link]);
public Firstyear()
{
getdata();
}
public void getdata()
{
[Link]("enter class name");
classname = [Link]();
[Link]("enter the class teacher name");
classname = [Link]();
[Link]("Please enter the total number of students in the class");
studentcount = [Link]([Link]());
[Link]("Please enter the names of all students of the class:");
for(int i=0;i<studentcount;i++)
{
stdnames[i]=[Link]();
}

[Link]("Please enter the marks of all students of the class:");

for(int i=0;i<studentcount;i++)
{
marks[i]=[Link]();
}

}
public void bestStudent()
{
int best=0,k=-1;
for(int i=0;i<studentcount;i++)
{
if(marks[i]>best)
{
best =marks[i];
k=i;
}
}
[Link]("the best count is.."+stdnames[k]);
}
}
public class Student1
{
public static void main(String args[])
{
Firstyear fy= new Firstyear();
[Link]();
}
}

Ouptut:

10. Write a Java program to define a class called employee with the name and date of appointment. Create
ten employee objects as an array and sort them as per their date of appointment. ie, print them as per
their seniority.
Program:
import [Link];
class Employ
{
String name;
Date appdate;

public Employ(String nm,Date apdt)


{
name=nm;
appdate=apdt;
}
public void display()
{
[Link]("employee name:"+name+"\t appoinment date:\t"+
[Link]()+"/" +[Link]()+"/"+[Link]());
}
}
public class EmployeeDemo
{
public static void main(String args[])
{
Employ emp[]=new Employ[10];

emp[0]=new Employ("sreeni",new Date(2022,4,16));


emp[1]=new Employ("Bindu",new Date(2022,1,7));
emp[2]=new Employ("Meghana",new Date(2022,2,10));
emp[3]=new Employ("Kowshik",new Date(2022,8,1));
emp[4]=new Employ("Krishna",new Date(2022,8,2));

emp[5]=new Employ("sre ",new Date(2022,3,23));


emp[6]=new Employ("ramya",new Date(2022,2,8));
emp[7]=new Employ("rahul",new Date(2022,3,10));
emp[8]=new Employ("ravi",new Date(2022,9,1));
emp[9]=new Employ("sindhu",new Date(2022,2,3));

[Link]("List of employees");
for(int i=0;i<[Link];i++)
emp[i].display();
for(int i=0;i<[Link];i++)
{
for(int j=i+1;j<[Link];j++)
{
if(emp[i].[Link](emp[j].appdate))
{
Employ t=emp[i];
emp[i]=emp[j];
emp[j]=t;
}
}
}
[Link]("List of employees seniority wise");
for(int i=0;i<[Link];i++)
emp[i].display();
}
}

Output:

11. Create a package‘ [Link]‘ in your current working directory


a. Create a default class student in the above package with the following
attributes: Name, age, sex. b. Have methods for storing as well as
displaying

Program:
package [Link];
import [Link];
public class BCAStudent
{
String name,sex;
int age;
Scanner sc= new Scanner([Link]);
public void getdata()
{
[Link]("enter student name\n");
name=[Link]();
[Link]("enter Student Sex\n");
sex=[Link]();
[Link]("enter student age\n");
age=[Link]();
}
public void display()
{
[Link]("Student details are...\n");
[Link]("Student Name:"+name);
[Link]("Student Sex:"+sex);
[Link]("Student Age:"+age);
}
}

Program2
import [Link];
public class pacakgedemo
{
public static void main(String args[])
{
BCAStudent s1 =new BCAStudent();
[Link]();
[Link]();
}}

12. Write a small program to catch Negative Array Size Exception. This exception is caused when the
array is initialized to negative values.
Program:

class negativearray
{
public static void main(String[] args)
{
int[] array = new int[-5];
[Link]("Array length: " + [Link]);
}
}

Output:

13. Write a program to handle Null Pointer Exception and use the “finally” method to display a message to
the user.
Program:
class nullpointer
{
public static void main(String args[])
{
String name = null;
try
{
if([Link]("Sree"))
[Link]("Same");
else
[Link]("not same");
}
catch (NullPointerException e)
{
[Link]("Null Pointer Exception caught");
}
finally
{
[Link]("this is finally blcok after catching NullPointerException");
}
}
}

Output:

14. Write a program which create and displays a message on the window

Program:
import [Link].*;
public class FrameDemo
{
FrameDemo()
{
Frame fm = new Frame();
[Link]("My First Frame");
Label lb= new Label("Welcome to GUI Programming");
[Link](lb);
[Link](300,300);
[Link](true);
}
public static void main(String args[])
{
FrameDemo f1 = new FrameDemo();
}
}

Output:

15. Write a program to draw several shapes in the created window


Program:
import [Link].*;
public class Drawings extends Canvas
{
public void paint(Graphics g)
{
[Link](50,75,100,50);
[Link](200,75,100,50);
[Link](50,150,100,500,15,15);
[Link](175,275,100,50);
[Link](20,350,100,50,25,75);
[Link](175,350,100,50,25,75);
}
public static void main(String args[])
{
Drawings m = new Drawings();
Frame f = new Frame("Shapes");
[Link](m);
[Link](500,500);
[Link](true);
}
}

Output:

16. Write a program to create an applet and draw grid lines


Program:

import [Link].*;
import [Link].*;
public class Grid extends Applet
{
public void paint(Graphics G)
{
int i,j,x,y=20;
for(i=1;i<5;i++)
{
x=20;
for(j=1;j<5;j++)
{
[Link](x,y,40,40);
x=x+20;
}
y=y+20;

}
}
}
/*
* <applet code ="[Link]" height= 500 width=500></applet>
*/

Output:
[Link] a 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
similar details of mother also appear.

import [Link].*;
import [Link].*;

public class ButtonClickActionevents


{

public static void main(String[] args)


{
Frame f= new Frame ("Button Event");
label l= new Label ("DETAILS OF PARENTS");
[Link](new Font ("Calibri", [Link],16));
Label nl = new Label();
Label dl = new Label();
Label al = new Label();
[Link] (20,20,500,50);
[Link] (20,110,500,30);
[Link] (20,150,500,30);
[Link] (20,190,500,30);
Button mb = new button ("Mother");
mb . setBounds (20,70,50,30);
mb. addActionListener(New ActionListener()
{
public void actionPerformed(ActionEvent e)
{
[Link]("NAME:" + " " + "Mrs. Sreeni");
[Link]("DESIGNATION:"+" " + "Analyst");
[Link]("AGE:"+" "+"42");
}
});
Button fb= new Button ("Father");
[Link] (80, 70, 50, 30);

[Link](new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
[Link]("NAME:"+" "+"Sreeni");
[Link]("DESIGNATION:" + " "+"Professor");
[Link]("AGE: " + " "+ "45");
}
});
// adding elements to the frame
[Link](mb);
[Link](fb);
[Link](l);
[Link](nl);
[Link](dl);
[Link](al);
// setting size, layout and visibility
[Link](250, 250);
[Link](null);
[Link](true);
}
}

Output:

[Link] a frame which displays your personal details with respect to a button click

import [Link].*;
import [Link].*;
public class PersonalDetails
{
public static void main(String[] args)
{
Frame f = new Frame("Button Example");
Label l = new Label("WELCOME TO MY PAGE");
[Link](new Font("Calibri", [Link], 16));
Label fnl = new Label();
Label mnl = new Label();
Label lnl = new Label();
Label rl = new Label();
Label al = new Label();
[Link] (250, 30, 600, 50);
[Link](20, 120, 600, 30);
[Link] (20, 160, 600, 30);
[Link] (20, 200, 600, 30);
[Link] (20, 240, 600, 30);
[Link] (20, 280, 600, 30);
Button mb = new Button("CLICK HERE FOR MY PERSONAL DETAILS");
[Link](new Font("Calibri", [Link], 14));
[Link](210, 70, 320, 30);
[Link](new ActionListener()
{
public void actionPerformed (ActionEvent e)
{
[Link]("Full Name: Sreenivas Rao ");
[Link]("Father Name: Prabhakar Rao RL Mother Name: Kamala Age: 20");
[Link]("Roll No: BU160476 College Name: Surana College");
[Link]("Nationality : Indian Contact No : 9164591191");
[Link]("Address: 1 Main, Nagendra Block, Bangalore");
}
});
[Link](mb);
[Link](l);
[Link](fnl);
[Link](mnl);
[Link](lnl);
[Link](rl);
[Link](al);
[Link](400, 400);
[Link](null);
[Link](true);
}
}

Output:
20. Create a simple applet which reveals the personal information of yours.

Program:
/*Create a simple applet which reveals the personal information-of yours.*/
import [Link].*;
import [Link].*;
import [Link].*;
public class PersonalDetailsApplet extends Applet implements ActionListener
{
String s1 = "", s2 = "", s3 = "", s4 = " ", s5 = " ";
public void init()
{
setLayout(null);
setSize(400, 300);
Button btn= new Button("CLICK HERE FOR MY PERSONAL DETAILS");
add(btn);
[Link](20, 50, 300, 30);
[Link](this);
}
public void actionPerformed (ActionEvent e)
{
s1 = "Full Name: Sreenivas Rao";
s2 = "Father Name: Prabhakar Rao RL Mother Name: Kamalamma Age: 25";
s3 = "Roll No : Bu16041976 College Name: Surana College";
s4= "Nationality: Indian Contact No: 9164591191";
s5 = "Address: I main , Nagendra Block, Bangalore-50";
repaint();
}
public void paint (Graphics g)
{
[Link](new Font("TimesRoman", [Link], 14));
[Link](s1, 20, 110);
[Link](s2, 20, 140);
[Link](s3, 20, 180);
[Link](s4, 20, 220);
[Link](s5, 20, 260);
}
}
/*
*<applet code="[Link]" height=400 width=400> </applet>
*/
Output:

21. Write a program to move different shapes according to the arrow key pressed.

import [Link].*;
import [Link].*;
public class
extends Frame implements KeyListener
{

Label lbl;
KeysDemo()
{
addkeyListener(this);
requestFocus();
lbl = new Label();
[Link] (100, 100, 200, 40);
[Link](new Font("Calibri", [Link], 16));
add(lbl);
setSize(400, 400);
setLayout(null);
setVisible(true);
}
public void keyPressed(KeyEvent e)
{
if ([Link]() == 'M' || [Link]() == 'm')
[Link]("GOOD MORNING");
else if ([Link]() == 'A' ||[Link]() == 'a')
[Link]("GOOD AFTERNOON");
else if ([Link]() 'E' || [Link]() == 'e')
[Link]("GOOD EVENING");
else if ([Link]() == 'N' || [Link]() == 'n')
[Link]("GOOD NIGHT");
}
public void keyReleased (KeyEvent e)
{
}
public void keyTyped(KeyEvent e)
{
}
public static void main(String[] args)
{
new KeysDemo();
}
}

Output:
22. Demonstrate the various mouse handling events using suitable example.

import [Link].*;
import [Link];
import [Link];

public class MouseListenerExample implements MouseListener


{
// create two labels lbl1 and 1b12
Label lbl1, 1b12;
// create a frame frame
Frame fr;
// create a sing s
String s;
MouseListenerExample()
{
fr = new Frame("Java Mouse Listener Example");
lbl1 = new Label("Demo for the Mouse Event", [Link]);
lbl2= new Label();
// set the layout of frame as Flowlayout
[Link](new FlowLayout());
// add label 1 to frame
[Link](lbl1);
// add label 2 to frame
[Link](lbl2);
// Register the created class MouseListenerExample with MouseListener
[Link](this);
// set the size of frame where width is 250 and height is 250
[Link](250, 250);
// set the visibility of frame as true
[Link](true);
}
// implementation of mouseClicked method
public void mouseClicked (MouseEvent ev)
{
[Link]("Mouse Button Clicked");
[Link](true);
}
// implementation of mouseEntered method
public void mouseEntered (MouseEvent ev)
{
[Link]("Mouse has entered the area of window");
[Link](true);
}
// implementation of mouseExited method
public void mouseExited (MouseEvent ev)
{
[Link]("Mouse has left the area of window");
[Link](true);
}
// implementation of mousePressed method
public void mousePressed (MouseEvent ev)
{
[Link]("Mouse button is being pressed");
[Link](true);
}
// implementation of mouseReleased method
public void mouseReleased (MouseEvent ev)
{
[Link](" Mouse Released");
[Link](true);
}
// main method

public static void main(String args[])


{
new MouseListenerExample();

Output:
24 Write a program to create menu bar and pull-down menus
import [Link].*;
public class MenuDemo
{
MenuDemo()
{
Frame fr = new Frame("Menu Demo");
MenuBar mb = new MenuBar();
Menu fileMenu = new Menu("File");
Menu editMenu = new Menu("Edit");
Menu viewMenu = new Menu("View");
[Link](fileMenu);
[Link](editMenu);
[Link](viewMenu);
MenuItem a1 = new MenuItem("New");
MenuItem a2 = new MenuItem("Open");
MenuItem a3 = new MenuItem("Save");
MenuItem b1 = new MenuItem("Copy");
MenuItem b2 = new MenuItem("Find");
MenuItem c1 = new MenuItem("Show");
[Link](a1);
[Link](a2);
[Link](a3);
[Link](b1);
[Link](b2);
[Link](c1);
[Link] (mb);
[Link](300, 300);
[Link](null);
[Link](true);
}
public static void main(String args[])
{ new MenuDemo(); } }

Output:

You might also like