NEP Java Lab Manual
NEP Java Lab Manual
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
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
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 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
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;
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();
}
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();
}
}
}
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();
}
}
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;
this.name=name;
this.DOJ=DOJ;
}
}
}
class Program11
{
public static void main(String args[])
{
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
import student.fulltime.bca.StudentPackage;
public class Program12
{
public static void main(String args[])
{
StudentPackage sp=new StudentPackage();
sp.input();
sp.display();
}
}
package student.fulltime.bca;
import java.util.*;
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.*;
import java.awt.*;
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
import java.awt.*;
import java.applet.*;
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));
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);
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.*;
{
s1="Shivaswamy D S";
s2="Assistant Professor";
s3="Department of Computer Science";
s4="Seshadripuram College";
s5="Seshadripuram bangalore-20";
repaint();
}
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.*;
switch(key)
{
case KeyEvent.VK_LEFT : x1=x1-10;
x2=x2-10;
break;
}
repaint();
}
import java.awt.*;
import java.awt.event.*;
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);
}
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
repaint();
}
import java.awt.*;
class Program24
{
Program24()
{
Frame f= new Frame("Program24 Menu bar and Pull-down menus");
MenuBar m=new MenuBar();
menu.add(i1);
menu.add(i2);
menu.add(i3);
menu.add(i4);
menu.add(i5);
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);
}