Java Lab Cycles
Java Lab Cycles
Program:
class Lab1{
System.out.println("Welcome to java");
Output:
Welcome to java
Lab 2: Write a program to display the month of a year. Months of the year
should be held in an array.
Program:
import java.util.Calendar;
class Lab2{
Calendar calendar=Calendar.getInstance();
String
mon[]={"Jan","Feb","March","April","May","June","July","Aug","Sept","Oct","Nov
","Dec"};
System.out.println(calendar.get(Calendar.MONTH));
System.out.println("Current Month="+mon[calendar.get(Calendar.MONTH)]);
Output:
5
Current Month=June
Program:
class Lab3{
try{
result = num1/num2;
catch (ArithmeticException e) {
Output:
Can't be divided by Zero: java.lang.ArithmeticException: / by zero
Lab 4: Write a program to create a user defined exception say Pay Out
of Bounds.
Program:
import java.util.*;
PayoutOfBoundsException(String msg){
int pay=sc.nextInt();
if(pay<10000 || pay>50000){
}else
Output 1:
Enter the emp salary
60000
at Lab4.main(Lab4.java:16)
Output 2:
Enter the emp salary
30000
Lab 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.
Program:
class Lab5{
return a+b;
return x+y;
return (50+50);
int int_sum=add(10,13);
float flt_sum=add(12.2f,10.0f);
int deft=add();
System.out.println("Default add:"+deft);
}
Output:
add of int is:23
Default add:100
Program:
class AddSub{
int n1,n2;
n1=x;
n2=y;
}
public int add(){
return(n1+n2);
return(n1-n2);
super(x,y);
return(n1*n2);
return(n1/n2);
}
class Lab6{
System.out.println("Sum of (20,10)is:"+md.add());
System.out.println("Substraction of (20,10)is:"+md.sub());
System.out.println("Multiplication of (20,10)is:"+md.mul());
System.out.println("Division of (20,10)is:"+md.div());
Output:
Sum of (20,10)is:30
Substraction of (20,10)is:10
Multiplication of (20,10)is:200
Division of (20,10)is:2
Lab 7: 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{
int rno;
String sname;
rno=no;
sname=nm;
void display(){
s1.display();
s2.display();
System.out.println("Static value is changed by one of the object");
s1.collegeName="KLESNC";
s1.display();
s2.display();
Output:
KLE 1 Sri
KLE 2 Ram
KLESNC 1 Sri
KLESNC 2 Ram
Program:
import java.util.*;
class Student{
String Eid;
String name;
Student(){
getInfo();
System.out.println("**Student details**");
Eid=sc.next();
System.out.println("Enter Name");
name=sc.next();
System.out.println("Enter marks");
sub1=sc.nextInt();
sub2=sc.nextInt();
sub3=sc.nextInt();
total=sub1+sub2+sub3;}
else{
total=0;
System.out.println(Eid+"\t\t"+name+"\t\t"+total);
class Lab8{
s[i]=new Student();
System.out.println("***Student Details***");
System.out.println("Eid"+"\t\t"+"name"+"\t\t"+"total");
s[i].display();
Output:
Enter Enrollment ID
s1
Enter Name
sri
Enter marks
66
67
68
**Student details**
Enter Enrollment ID
s2
Enter Name
ram
Enter marks
78
87
67
**Student details**
Enter Enrollment ID
s3
Enter Name
hari
Enter marks
98
78
65
***Student Details***
s1 sri 201
s2 ram 232
s3 hari 241
Lab 9: 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 java.util.*;
class FirstYear{
String classname;
String classteacher;
int stdcount;
public FirstYear() {
getinfo();
classteacher = sc.nextLine();
stdcount = Integer.parseInt(sc.nextLine());
for(int i=0;i<stdcount;i++)
stdnames[i]=sc.nextLine();
for(int i=0;i<stdcount;i++)
stdmarks[i]=sc.nextInt();
for(int i=0;i<stdcount;i++) {
best = stdmarks[i];
k=i;
}
fy.bestStudent();
Output:
Harshada
78
87
88
Lab 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 student with the
highest total mark. In the main method define a first-year object and
find the best student of this class
Program:
Lab 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.
Program:
import java.util.Date;
class Employee {
String name;
Date appdate;
name=nm;
appdate=apdt;
}
public void display(){
for(int i=0;i<emp.length;i++)
emp[i].display();
for(int i=0;i<emp.length;i++){
for(int j=i+1;j<emp.length;j++){
if(emp[i].appdate.after(emp[j].appdate)){
Employee t= emp[i];
emp[i]=emp[j];
emp[j]=t;
for(int i=0;i<emp.length;i++)
emp[i].display();
Output:
list of employees Before sorting
Lab 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, sex. b. Have methods for storing as well as displaying
Program:
package student.fulltime.bca;
import java.util.Scanner;
int age;
System.out.println("Student Name:");
name=sc.nextLine();
System.out.println("Student Sex:");
sex=sc.nextLine();
System.out.println("Student Age:");
age=sc.nextInt();
System.out.println("Student Name:"+name);
System.out.println("Student Sex:"+sex);
System.out.println("Student Age:"+age);
}
/* NOTE: Save this file by name BCAStudent.java
javac -d . BCAStudent.java
folder hierarchy Student\fulltime\bca will be created and BCAStudent is been placed in bca
folder
*/
//save this file as packageDemo.java outside of student folder and execute this file
import student.fulltime.bca.BCAStudent;
std.getdata();
std.display();
Output:
Student Name:
sri
Student Sex:
male
Student Age:
23
Student details are:
Student Name:sri
Student Sex:male
Student Age:23
Lab 13:
Write a small program to catch Negative Array Size Exception. This
exception is caused when the array is initialized to negative values.
Program:
import java.util.*;
class Lab13
int size=sc.nextInt();
try
catch(NegativeArraySizeException e)
{
System.out.println("Array underflow");
System.out.println(e.toString());
Output:
-2
Array underflow
java.lang.NegativeArraySizeException
Lab 14:
Program:
class Lab14{
public static void main(String args[]){
String city=null;
try{
if(city.equals("Bangalore"))
System.out.println("Equals");
else
System.out.println("not Equals");
}catch(NullPointerException e){
System.out.println("Null Pointer Exception Caught");
}
finally{
System.out.println(" this is finally block after try_catch blk");
}
}
}
Output:
Lab 15:
Program:
import java.awt.*;
public class FrameDemo{
FrameDemo(){
Frame fm = new Frame();
fm.setTitle("My First Frame");
Label lb = new Label("Welcome to GUI Programming");
fm.add(lb);
fm.setSize(300,300);
fm.setVisible(true);
}
public static void main(String args[]) {
FrameDemo ta = new FrameDemo();
}
}
Output:
Lab 16:
Program:
import java.awt.*;
Output:
Lab 17:
import java.awt.*;
import java.applet.*;
/*<applet code=Lab17 width=200 height=200>
</applet>*/
Output:
Lab Cycle: 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.*;
mb.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
n1.setText("Name: "+"Aishwarya");
a1.setText("Age: "+"42");
d1.setText("Designation: "+"Professor");
}
});
fb.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
n1.setText("Name: "+"Ram");
a1.setText("Age: "+"45");
d1.setText("Designation: "+"Manager");
}
});
f.add(mb);
f.add(fb);
f.add(l);
f.add(n1);
f.add(d1);
f.add(a1);
f.setLayout(null);
f.setSize(250,250);
f.setVisible(true);
}
}
Output:
Lab 19: Create a frame which displays your personal details with respect to a
button click .
import java.awt.*;
import java.awt.event.*;
class personalDetails
{
public static void main(String args[])
{
Frame f= new Frame("Button example");
Label lb=new Label("Welcome to my page");
lb.setFont(new Font("Calibri", Font.BOLD,14));
mb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
fnl.setText("Full Name: Aishwarya Rao");
mnl.setText("Father name:Ranjith Mother name: vijaya");
lnl.setText("Roll no: BNU85765 College name: KLE");
rl.setText("Nationality: Indian Contact no: 11111111");
al.setText("Address: Rajajinagar, Bangalore");
}
});
f.add(mb);
f.add(lb);
f.add(fnl);
f.add(mnl);
f.add(lnl);
f.add(rl);
f.add(al);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
Output:
/*Lab 20: Create a simple applet which reveals the personal information of
yours.
*/
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
repaint();
}
Output:
Lab 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
Program:
import java.awt.*;
import java.awt.event.*;
public class Lab22 extends Frame implements KeyListener{
Label lbl;
Label l;
Lab22(){
add(l);
addKeyListener(this);
requestFocus();
lbl=new Label();
lbl.setBounds(100,100,200,40);
lbl.setFont(new Font("Calibri",Font.BOLD,16));
add(lbl);
setSize(400,400);
setLayout(null);
setVisible(true);
lbl.setText("Good Morning");
else if(e.getKeyChar()== 'A' || e.getKeyChar()=='a')
lbl.setText("Good Afternoon");
lbl.setText("Good Evening");
lbl.setText("Good Night");
new Lab22();
OUTPUT:
Lab 23: Demonstrate the various mouse handling events using suitable
example.
Program:
import java.awt.*;
import java.awt.event.*;
Label l;
Lab23(){
addMouseListener(this);
l=new Label();
l.setBounds(20,50,100,20);
add(l);
setSize(300,300);
setLayout(null);
setVisible(true);
l.setText("Mouse Clicked");
l.setText("Mouse Entered");
l.setText("Mouse Exited");
l.setText("Mouse Pressed");
l.setText("Mouse Released");
new Lab23();
OUTPUT:
Lab 24: Write a program to create menu bar and pull-down menus
Program:
import java.awt.*;
class Lab24
{
Lab24(){
fileMenu.add(i1);
fileMenu.add(i2);
fileMenu.add(i3);
submenu.add(i4);
submenu.add(i5);
fileMenu.add(submenu);
mb.add(fileMenu);
f.setMenuBar(mb);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
new Lab24();
}
OUTPUT: