Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
215 views

NEP Java Lab Manual

Uploaded by

dhruvakm838
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
215 views

NEP Java Lab Manual

Uploaded by

dhruvakm838
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

BCA II SEM JAVA LAB MANUAL(NEP) 1

Program-1 //Write a simple java application, to print the message "Welcome to


java".

class Program1
{
public static void main(String args[])
{
System.out.println("Welcome to java");
}
}

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

import java.util.*;
class Program2
{
public static void main(String[] args)
{
Calendar c = Calendar.getInstance();
String[] month = new String[] { "January", "February", "March", "April",
"May","June", "July","August","September", "October", "November","December" };
System.out.println("Current Month = " + month[c.get(Calendar.MONTH)]);
}
}
BCA II SEM JAVA LAB MANUAL(NEP) 2

Program-3 // Write a program to demonstrate a division by Zero exception.

class Program3
{
public static void main (String args[])
{
int num1 = 15, num2 = 0, result = 0;
try
{
result = num1/num2;
System.out.println("The result is" +result);
}
catch (ArithmeticException e)
{
System.out.println ("Can't be divided by Zero " + e);
}
}
}
BCA II SEM JAVA LAB MANUAL(NEP) 3

Program-4//Write a java program to create a user defined exception say Pay out of
bounds

class Program4
{
public static void main (String args[])
{
int array[] = {20,30,40};
try
{
for(int i = 3; i >=0; i--)
{
System.out.println("The value of array is " +array[i]);
}
}
catch (ArrayIndexOutOfBoundsException e)
{
System.out.println("Error. Array is out of Bounds " +e);
}
}
}
BCA II SEM JAVA LAB MANUAL(NEP) 4

Program-5 // 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.

class Program5
{
void addition(int a, int b)
{
int sum=a+b;
System.out.println("Sum of two numbers is "+sum);
}
void addition(double a ,double b)
{
double sum=a+b;
System.out.println("Sum of two numbers is "+sum);
}
void addition()
{
int a=20,b=30;
double c=20.5,d=30.5;
int sum=a+b;
double Sum=c+d;
System.out.println("Sum of two numbers is "+sum);
System.out.println("Sum of two numbers is "+Sum);
}
BCA II SEM JAVA LAB MANUAL(NEP) 5

public static void main(String args[])


{
Program5 p5=new Program5();
p5.addition(10,20);
p5.addition(2.5,7.5);
p5.addition();
}
}

Program-6 // 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.

class addsub
{
int num1,num2;
addsub(int n1, int n2)
{
num1 = n1;
num2 = n2;
BCA II SEM JAVA LAB MANUAL(NEP) 6

}
int add()
{
return num1+num2;
}
int sub()
{
return num1-num2;
}
}

class multdiv extends addsub


{
public multdiv(int n1, int n2)
{
super(n1, n2);
}
int mul()
{
return num1*num2;
}
float div()
{
return num2/num1;
}
}

class Program6
{
public static void main(String args[])
{
addsub r1=new addsub(50,20);
int ad = r1.add();
int sb = r1.sub();
System.out.println("Addition =" +ad);
System.out.println("Subtraction =" +sb);
multdiv r2 =new multdiv(4,20);
int ml = r2.mul();
float dv =r2.div();
System.out.println("Multiply =" +ml);
BCA II SEM JAVA LAB MANUAL(NEP) 7

System.out.println("Division =" +dv);


}
}

Program-7 //Write a program with class variable that is available for all instance of a
class. use static variable declaration. observe the changes that occur in the object's
member variable values

class stat
{
static String employer="GOK";
int emp_id;
float salary;

stat(int emp_id, float salary)


{
this.emp_id=emp_id;
this.salary=salary;
}

void display()
{
System.out.println(employer + " " +emp_id+" " + salary);
}
}
BCA II SEM JAVA LAB MANUAL(NEP) 8

class Program7
{
public static void main(String args[])
{
System.out.println("Employer GOK");
stat s1=new stat(101, 100000);
stat s2=new stat(102, 200000);
s1.display();
s2.display();
System.out.println("\nEmployer GoI");
s1.employer="GoI";
s1.display();
s2.display();
}
}

Program-8 //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 declared as zero. Using this condition write a
constructor for this class. Write separate function for accepting and displaying
student details. In the main method create an array of three student objects and
display the details.
BCA II SEM JAVA LAB MANUAL(NEP) 9

import java.util.*;
class Student
{
Scanner s=new Scanner(System.in);
String id, name;
int s1,s2,s3,total;

Student()
{
input();
}

public void input()


{
System.out.println("Enter Student details");
System.out.println("Enter Enrollement_id, Name and 3 Subject marks in
Order");
id=s.next();
name=s.next();
s1=s.nextInt();
s2=s.nextInt();
s3=s.nextInt();
// Condition to check the marks criteria
if(s1>=50&&s2>=50&&s3>=50)
total=s1+s2+s3;
else
total=000;
}

public void display()


{
System.out.println("\t| "+id + "\t\t |\t"+ name +"\t |\t"+total +" |");
System.out.println("\t-------------------------------------------------");
}
}
BCA II SEM JAVA LAB MANUAL(NEP) 10

class Program8
{
public static void main(String args[])
{
Student h[]= new Student[3];
for(int i=0;i<=2;i++)
{
h[i]= new Student();
}

System.out.println("\t\t\tStudent details");
System.out.println("\t============================================
=====");
System.out.println(" | Enrollment_Id |\tName \t |\ttotal |");
System.out.println("\t============================================
=====");
for(int i=0;i<=2;i++)
{
h[i].display();
}
}
}
BCA II SEM JAVA LAB MANUAL(NEP) 11

Program-9 //In a college first year class are having the following attributes Name of
the class (BCA, B.Com, BSc), Name of the staff, No of students in the class, Array of
students in the class.

import java.util.*;
class FirstYear
{
String CN; // Name of the class
String SN; // Name of the staff
int n,i; // Number of Students
String name[]=new String[4]; // Name of the Student
int marks[]=new int[4]; // Marks of the student

public FirstYear()
{
input();
}

public void input()


{
Scanner s=new Scanner(System.in);
System.out.println("Enter Name of the class");
CN=s.next();
System.out.println("Enter Name of the Staff");
SN=s.next();
System.out.println("Enter number of students");
n=s.nextInt();
System.out.println("Enter Name of all students ");
for(i=0;i<n;i++)
{
name[i]=s.next();
}
System.out.println("Enter Marks of all students");
for(i=0;i<n;i++)
{
marks[i]=s.nextInt();
}
}
BCA II SEM JAVA LAB MANUAL(NEP) 12

public void display()


{
System.out.println("Course Name \n"+CN);
System.out.println("\nStaff Name \n"+SN);
System.out.println("\nStudent_Name \t\t Marks");
for(i=0;i<n;i++)
{
System.out.println(name[i]+ "\t\t\t " + marks[i]);
}

}
}

class Program9
{
public static void main(String args[])
{
FirstYear f=new FirstYear();
f.display();
}
}
BCA II SEM JAVA LAB MANUAL(NEP) 13

Program-10 //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 studentwith the highest total mark. In the main method
define a first year object and find the best student of this class.

import java.util.*;
class FirstYear
{
String CN; // Name of the class
String SN; // Name of the staff
int n,i; // Number of Students
String name[]=new String[4]; // Name of the Student
int marks[]=new int[4]; // Marks of the student

public FirstYear()
{
input();
}

public void input()


{
Scanner s=new Scanner(System.in);
System.out.println("Enter Name of the class");
CN=s.next();
System.out.println("Enter Name of the Staff");
SN=s.next();
System.out.println("Enter number of students");
n=s.nextInt();
System.out.println("Enter Name of all students ");
for(i=0;i<n;i++)
{
name[i]=s.next();
}
System.out.println("Enter Marks of all students");
for(i=0;i<n;i++)
{
marks[i]=s.nextInt();
}
}
BCA II SEM JAVA LAB MANUAL(NEP) 14

public void display()


{
int i,max=marks[0],na=0;
for(i=1;i<marks.length;i++)
{
if(marks[i]>max)
{
max=marks[i];
na=i;
}

}
System.out.println("==========================");

System.out.println("Best Student details ");


System.out.println("==========================");

System.out.println("Name\t\t Marks");
System.out.println("==========================");

System.out.println(name[na]+"\t\t "+max);
}
}

class Program10
{
public static void main(String args[])
{
FirstYear f=new FirstYear();
f.display();
}
}
BCA II SEM JAVA LAB MANUAL(NEP) 15

Program-11 //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.

import java.util.*;
import java.util.Date;
class Employee
{
String name;
Date DOJ;

public Employee(String name, Date DOJ)


{
BCA II SEM JAVA LAB MANUAL(NEP) 16

this.name=name;
this.DOJ=DOJ;
}

public void display()


{
System.out.println(" "+name+"\t "+DOJ.getDate()+"-
"+DOJ.getMonth()+"-"+DOJ.getYear());
System.out.println("-----------------------------------");

}
}

class Program11
{
public static void main(String args[])
{

Employee e[]=new Employee[2];


e[0]=new Employee("Shiva", new Date(2023,5,8));
e[1]=new Employee("Swamy", new Date(2022,5,15));

System.out.println("Emloyee details");
System.out.println("===================================");
System.out.println("Employee Name \t Date of Joining");
System.out.println("===================================");
for(int i=0;i<e.length;i++)
e[i].display();

for(int i=0;i<e.length;i++)
{
for(int j=i+1;j<e.length;j++)
{
if(e[i].DOJ.after(e[j].DOJ))
{
Employee d;
d=e[i];
e[i]=e[j];
e[j]=d;
BCA II SEM JAVA LAB MANUAL(NEP) 17

}
}
}
System.out.println("\nEmloyee details Senioritywise");
System.out.println("===================================");
System.out.println("Employee Name \t Date of Joining");
System.out.println("===================================");
for(int i=0;i<e.length;i++)
e[i].display();
}
}
BCA II SEM JAVA LAB MANUAL(NEP) 18

Program-12 /*Create a package 'student.fulltime.BCA' in your current working


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

//Before executing program12 compile StudentPackage file everytime using follwing


command javac -d . StudentPackage.java
// Save this file as Program12.java and compile

import student.fulltime.bca.StudentPackage;
public class Program12
{
public static void main(String args[])
{
StudentPackage sp=new StudentPackage();
sp.input();
sp.display();
}
}

/*Create a package 'student.fulltime.BCA' in your current working directory


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

// Save this file as StudentPackage.java and compile using javac -d . StudentPackage.java

package student.fulltime.bca;
import java.util.*;

public class StudentPackage


{
String Name,gen;
int age;
Scanner s=new Scanner(System.in);

public void input()


{
BCA II SEM JAVA LAB MANUAL(NEP) 19

System.out.println("Enter Student name");


Name=s.next();
System.out.println("Enter Student age");
age=s.nextInt();
System.out.println("Enter Student gender");
gen=s.next();
}

public void display()


{
System.out.println("Student details");
System.out.println("=========================");
System.out.println("Name\t Age\t Gender");
System.out.println("-------------------------");
System.out.println(Name +"\t " + age +"\t " +gen);
}
}
BCA II SEM JAVA LAB MANUAL(NEP) 20

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

class Program13
{
public static void main(String args[])
{
try
{
int s[]=new int[-5];
}

catch(NegativeArraySizeException nase)
{
nase.printStackTrace();
}
System.out.println("Error : Negative Array Size Exception");
}
}
BCA II SEM JAVA LAB MANUAL(NEP) 21

Program-14 // Write a program to handle Null Pointer Exception and use the
"finally" Method to display a message to the user

class Program14
{
public static void main(String args[])
{
String name=null;
try
{

if(name.equals("Shivaswamy"))
System.out.println("Matched");
else
System.out.println("Not Matched");
}

catch(NullPointerException e)
{
System.out.println("Error: Null Pointer Exception");
}

finally
{
System.out.println("Finally Block");
}
}
}
BCA II SEM JAVA LAB MANUAL(NEP) 22

Program-15 //Write a program which create and displays a message on the window

import java.applet.*;
import java.awt.*;

public class Program15 extends Applet


{
public void paint(Graphics g)
{
g.drawString("Welcome to Seshadripuram College",50,150);
g.setFont(new Font("Times of New Roman" , Font.BOLD, 25));
}
}

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


BCA II SEM JAVA LAB MANUAL(NEP) 23

Program-16 // Write a program to draw several shapes in the created window

import java.awt.*;

class Shapes extends Canvas


{
public void paint(Graphics g)
{
//g.drawRect(50,75,100,50);
g.fillRect(250,175,100,100);
}
}

class Program16
{
public static void main(String args[])
{
Shapes s=new Shapes();
Frame f=new Frame("Drawings");
f.add(s);
f.setSize(300,450);
f.setVisible(true);
}
}
BCA II SEM JAVA LAB MANUAL(NEP) 24

Program-17 //Write a program to Create an applet and draw grid lines

import java.awt.*;
import java.applet.*;

public class Program17 extends Applet


{
public void paint(Graphics g)
{
int r,c,x,y=50;
for(r=1;r<5;r++)
{
x=20;
for(c=1;c<5;c++)
{
g.drawRect(x,y,40,40);
x=x+20;
}
y=y+20;
}
}
}

/* <applet code = "Program17.class" height=500 width=500> </applet> */


BCA II SEM JAVA LAB MANUAL(NEP) 25

Program-18 // Write 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 java.awt.*;
import java.awt.event.*;

class Program18
{
public static void main(String args[])
{
Frame f= new Frame("Buttons");
Label l = new Label("Details of Parents");
l.setFont(new Font("Times of New Roman", Font.BOLD, 14));

Label l1=new Label();


Label l2=new Label();
Label l3=new Label();
l.setBounds(20,20,500,50);
l1.setBounds(20,110,500,30);
l2.setBounds(20,150,500,40);
l3.setBounds(20,130,500,50);

Button b= new Button("Mother");


b.setBounds(20,70,50,30);
b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
l1.setText("Name:" + " " + "Saraswathamma");
l2.setText("Designation:" + " " + "House Wife");
l3.setText("Age:" + " " + "50");
}
}
);

Button b1= new Button("Father");


b1.setBounds(80,70,50,30);
b1.addActionListener(new ActionListener()
{
BCA II SEM JAVA LAB MANUAL(NEP) 26

public void actionPerformed(ActionEvent e)


{
l1.setText("Name:" + " " + "Shambhulingaiah");
l2.setText("Designation:" + " " + "Farmer");
l3.setText("Age:" + " " + "60");
}
}
);

f.add(b);
f.add(b1);
f.add(l);
f.add(l1);
f.add(l2);
f.add(l3);

f.setSize(250,250);
f.setLayout(null);
f.setVisible(true);
}
}
BCA II SEM JAVA LAB MANUAL(NEP) 27
BCA II SEM JAVA LAB MANUAL(NEP) 28

Program-19 // Create a frame which displays your personal details with respect to a
button click

import java.awt.*;
import java.awt.event.*;

class Program19
{
public static void main(String args[])
{
Frame f= new Frame("Program19");
Label l1=new Label("Personal Details");
l1.setFont(new Font("Times of New Roman", Font.BOLD, 16));
Label l2=new Label();
Label l3=new Label();
Label l4=new Label();
Label l5=new Label();
Label l6=new Label();

l1.setBounds(250,30,600,50);
l2.setBounds(30,120,600,40);
l3.setBounds(30,160,600,40);
l4.setBounds(30,200,600,40);
l5.setBounds(30,240,600,40);
l6.setBounds(30,280,600,40);

Button b = new Button("Click Here");


b.setFont(new Font("Times of New Roman", Font.BOLD, 16));
b.setBounds(210,70,320,30);

b.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
l2.setText("Name:" + " " + "Shivaswamy D S");
l3.setText("Father Name:" + " " + "Shambhulingaiah");
l4.setText("Designation:" + " " + "Assistant Professor");
l5.setText("Age:" + " " + "32");
l6.setText("Address:" + " " + "Seshadripuram College bangalore-
20");
BCA II SEM JAVA LAB MANUAL(NEP) 29

}
}
);

f.add(b);
f.add(l1);
f.add(l2);
f.add(l3);
f.add(l4);
f.add(l5);
f.add(l6);
f.setSize(500,500);
f.setLayout(null);
f.setVisible(true);
}
}
BCA II SEM JAVA LAB MANUAL(NEP) 30

Program-20 // Create a simple applet which reveals the personal information of yours

import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class Program20 extends Applet implements ActionListener


{
String s1="",s2="",s3="",s4="",s5="";

public void init()


{
setLayout(null);
setSize(600,500);
Button b= new Button("Click Here");
add(b);
b.setBounds(30,60,350,35);
b.addActionListener(this);
}

public void actionPerformed(ActionEvent e)


BCA II SEM JAVA LAB MANUAL(NEP) 31

{
s1="Shivaswamy D S";
s2="Assistant Professor";
s3="Department of Computer Science";
s4="Seshadripuram College";
s5="Seshadripuram bangalore-20";
repaint();
}

public void paint(Graphics g)


{
g.setFont(new Font("Times of New Roman", Font.BOLD, 16));
g.drawString(s1, 30 , 120);
g.drawString(s2, 30 , 140);
g.drawString(s3, 30 , 160);
g.drawString(s4, 30 , 180);
g.drawString(s5, 30 , 200);
}
}

/* <applet code="Program20.class" height=500 width=500> </applet> */


BCA II SEM JAVA LAB MANUAL(NEP) 32

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

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

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

public class Program21 extends Applet implements KeyListener


{
int x1=100, y1=50, x2=250, y2=300;

public void init()


{
addKeyListener(this);
}

public void keyPressed(KeyEvent k)


{
showStatus("KeyDown");
int key=k.getKeyCode();

switch(key)
{
case KeyEvent.VK_LEFT : x1=x1-10;
x2=x2-10;
break;

case KeyEvent.VK_RIGHT : x1=x1+10;


x2=x2+10;
break;

case KeyEvent.VK_UP : x1=x1-10;


x2=x2-10;
break;

case KeyEvent.VK_DOWN : x1=x1+10;


x2=x2+10;
break;
BCA II SEM JAVA LAB MANUAL(NEP) 33

}
repaint();
}

public void keyReleased(KeyEvent k)


{

public void keyTyped(KeyEvent k)


{
repaint();
}

public void paint(Graphics g)


{
g.drawLine(x1,y1,x2,y2);
g.drawRect(x1,y1+160,100,50);
g.drawOval(x1,y1+235,100,50);
}
}
BCA II SEM JAVA LAB MANUAL(NEP) 34

Program-22 // Write a java program to create a window when we press M or m the


window displays Good Morning, A or a the window displays Good After Noon, E or e
the window displays Good Evening, N or n the window displays Good Night.

import java.awt.*;
import java.awt.event.*;

public class Program22 extends Frame implements KeyListener


{
Label l;

Program22()
{
addKeyListener(this);
//requestFocus();
l=new Label();
l.setBounds(100,100,200,40);
l.setFont(new Font("calibri", Font.BOLD, 16));
add(l);
setSize(400,400);
BCA II SEM JAVA LAB MANUAL(NEP) 35

setLayout(null);
setVisible(true);
}

public void keyPressed(KeyEvent k)


{
if(k.getKeyChar()== 'M' || k.getKeyChar()== 'm')
l.setText("Good Morning");
else
if(k.getKeyChar()== 'A' || k.getKeyChar()== 'a')
l.setText("Good After Noon");
else
if(k.getKeyChar()== 'E' || k.getKeyChar()== 'e')
l.setText("Good Evening");
else
if(k.getKeyChar()== 'N' ||k.getKeyChar()== 'n')
l.setText("Good Night");
}

public void keyReleased(KeyEvent k)


{

public void keyTyped(KeyEvent k)


{

public static void main(String args[])


{
new Program22();
}
}
BCA II SEM JAVA LAB MANUAL(NEP) 36
BCA II SEM JAVA LAB MANUAL(NEP) 37

Program-23 // Demonstrate the various mouse handling events using suitable


example.

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

/* <applet code="Program23.class" width=300 height=200> </applet> */

public class Program23 extends Applet implements MouseListener, MouseMotionListener


{
String s = "";

public void init()


{
addMouseListener(this);
addMouseMotionListener(this);
}

public void mouseClicked(MouseEvent me)


{
s = "Mouse clicked.";
repaint();
}

public void mouseEntered(MouseEvent me)


{
s = "Mouse entered.";
repaint();
}

public void mouseExited(MouseEvent me)


{
s = "Mouse exited.";
repaint();
}

public void mousePressed(MouseEvent me)


{
s = "Mouse Pressed";
BCA II SEM JAVA LAB MANUAL(NEP) 38

repaint();
}

public void mouseReleased(MouseEvent me)


{
s = "Mouse Released";
repaint();
}

public void mouseDragged(MouseEvent me)


{
s = "Mouse Dragged";
repaint();
}

public void mouseMoved(MouseEvent me)


{
showStatus("Moving mouse at " + me.getX() + ", " + me.getY());
}

public void paint(Graphics g)


{
g.drawString(s, 20, 50);
}
}
BCA II SEM JAVA LAB MANUAL(NEP) 39

Program-24 // Write a program to create menu bar and pull-down menus.

import java.awt.*;

class Program24
{
Program24()
{
Frame f= new Frame("Program24 Menu bar and Pull-down menus");
MenuBar m=new MenuBar();

Menu menu=new Menu("Colours");


Menu submenu=new Menu("BCA");
BCA II SEM JAVA LAB MANUAL(NEP) 40

MenuItem i1=new MenuItem("Red");


MenuItem i2=new MenuItem("Green");
MenuItem i3=new MenuItem("Yellow");
MenuItem i4=new MenuItem("Blue");
MenuItem i5=new MenuItem("Pink");

menu.add(i1);
menu.add(i2);
menu.add(i3);
menu.add(i4);
menu.add(i5);

MenuItem i7=new MenuItem("A Section");


MenuItem i8=new MenuItem("B Section");
MenuItem i9=new MenuItem("c Section");

submenu.add(i7);
submenu.add(i8);
submenu.add(i9);

menu.add(submenu);
m.add(menu);
f.setMenuBar(m);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}

public static void main(String args[])


{
new Program24();
}
}
BCA II SEM JAVA LAB MANUAL(NEP) 41

You might also like