Вы находитесь на странице: 1из 64

JAVA

II BCA MEYYAPPAN S UCAA1303 PAGE NO. 42


EX. 1 A CLASSES AND OBJECTS

AIM:

To prepare a student mark list implementing classes and objects using java.

PROGRAM:

import java.io.*;
class student1
{
String name,regno,res,grade;
int m1,m2,m3,total;
float avg;
void getdata()throws IOException
{
System.out.println("ENTER THE NAME:");
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
name=br.readLine();
System.out.println("ENTER THE REGISTER NUMBER:");
regno=br.readLine();
System.out.println("ENTER THE MARK 1:");
m1=Integer.parseInt(br.readLine());
System.out.println("ENTER THE MARK 2:");
m2=Integer.parseInt(br.readLine());
System.out.println("ENTER THE MARK 3:");
m3=Integer.parseInt(br.readLine());
}
void calculate()
{
total=m1+m2+m3;
avg=total/(float)3;
if((m1>=40)&&(m2>=40)&&(m3>=40))
{
res="PASS";
if(avg>=80)
grade="1ST CLASS";
else if((avg<=79)&&(avg>=60))
grade="2ND CLASS";
else if((avg<=59)&&(avg>=40))
grade="3RD CLASS";
}
else
{
res="FAIL";
grade="\n------\n FAIL \n------";

II BCA MEYYAPPAN S UCAA1303 PAGE NO. 43


}
}
void display()
{
System.out.println("NAME: "+name);
System.out.println("REGISTER NUMBER: "+regno);
System.out.println("MARK DETAILS ARE");
System.out.println("MARK 1:"+m1);
System.out.println("MARK 2:"+m2);
System.out.println("MARK 3:"+m3);
System.out.println("TOTAL:"+total);
System.out.println("AVERAGE:"+avg);
System.out.println("RESULT:"+res);
System.out.println("GRADE:"+grade);
}
}
public class ClassandObject
{
public static void main(String args[])throws IOException
{
student1 s=new student1();
s.getdata();
s.calculate();
s.display();
}
}

II BCA MEYYAPPAN S UCAA1303 PAGE NO. 44


OUTPUT:

ENTER THE NAME:


Xyz
ENTER THE REGISTER NUMBER:
UCSA1234
ENTER THE MARK 1:
75
ENTER THE MARK 2:
69
ENTER THE MARK 3:
82
NAME:Xyz
REGISTER NUMBER:UCSA1234
MARK DETAILS ARE
MARK 1:75
MARK 2:69
MARK 3:82
TOTAL:226
AVERAGE:75.333336
RESULT:PASS
GRADE: 2ND CLASS

RESULT:

The preparation of student mark list using classes and objects has been executed
successfully.

II BCA MEYYAPPAN S UCAA1303 PAGE NO. 45


EX. 1 B ARRAY OF OBJECTS

AIM:

To prepare a student mark list implementing array of objects using java.

PROGRAM:

import java.io.*;
class stud
{
String name,regno,res;
int m1,m2,m3,total;
float avg;
void getdata()throws IOException
{
System.out.println("ENTER THE NAME:");
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
name=br.readLine();
System.out.println("ENTER THE REGISTER NUMBER:");
regno=br.readLine();
System.out.println("ENTER THE 1ST MARK:");
m1=Integer.parseInt(br.readLine());
System.out.println("ENTER THE 2ND MARK:");
m2=Integer.parseInt(br.readLine());
System.out.println("ENTER THE 3RD MARK:");
m3=Integer.parseInt(br.readLine());
}
void calculate()
{
total=m1+m2+m3;
avg=total/(float)3;
if((m1>=40)&&(m2>=40)&&(m3>=40))
{
res="PASS";
}
else
{
res="FAIL";
}
}
void display()
{
System.out.println(name+"\t\t\t"+regno+"\t\t"+m1+"\t\t\t"+m2+"\t\t\t"+m3
+"\t\t\t"+total+"\t\t\t"+avg+"\t\t"+res+"\t\t\t");
}

II BCA MEYYAPPAN S UCAA1303 PAGE NO. 46


void display1()throws IOException
{
getdata();
}
void display2()throws IOException
{
calculate();
display();
}
}
class ArrayofObjects
{
public static void main(String[] args)throws IOException
{
int n;
stud[] studentarray=new stud[5];
System.out.println("ENTER THE NUMBER OF STUDENTS:");
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
n=Integer.parseInt(br.readLine());
for(int i=1;i<=n;i++)
{
studentarray[i]=new stud();
System.out.println("ENTER THE MARK LIST FOR "+i+"ST STUDENT:");
studentarray[i].display1();
}
System.out.println("NAME\t\t\tREG.No\t\t\tMARK 1\t\t\tMARK 2\t\t\tMARK
3\t\t\tTOTAL\t\t\tAVERAGE\t\t\tRESULT");
for(int j=1;j<=n;j++)
{
studentarray[j].display2();
}
}
}

II BCA MEYYAPPAN S UCAA1303 PAGE NO. 47


EX. 2 CONSTRUCTOR AND ITS TYPES
OUTPUT:

ENTER THE NUMBER OF STUDENTS:


3
ENTER THE MARK LIST FOR 1ST STUDENT:
ENTER THE NAME:
Abc
ENTER THE REGISTER NUMBER:
UCSA1111
ENTER THE 1ST MARK:
90
ENTER THE 2ND MARK:
80
ENTER THE 3RD MARK:
70
ENTER THE MARK LIST FOR 2ST STUDENT:
ENTER THE NAME:
Xyz
ENTER THE REGISTER NUMBER:
UCAA2222
ENTER THE 1ST MARK:
95
ENTER THE 2ND MARK:
85
ENTER THE 3RD MARK:
75
ENTER THE MARK LIST FOR 3ST STUDENT:
ENTER THE NAME:
Pqr
ENTER THE REGISTER NUMBER:
UCAA3333
ENTER THE 1ST MARK:
87
ENTER THE 2ND MARK:
77
ENTER THE 3RD MARK:
67
NAME REG.No MARK 1 MARK 2 MARK 3 TOTAL AVERAGE RESULT
RAM UCSA1111 90 80 70 240 80.0 PASS
RAJ UCAA2222 95 85 75 255 85.0 PASS
HARI UCAA3333 87 77 67 231 77.0 PASS

RESULT:

The preparation of student mark list using array of objects has been executed
successfully.

II BCA MEYYAPPAN S UCAA1303 PAGE NO. 48


AIM:

To find area of rectangle implementing all constructors in java.

PROGRAM:

class area
{
int l,b;
area()
{
l=10;b=15;
}
area(int x,int y)
{
l=x;b=y;
}
area(area a)
{
l=a.l;
b=a.b;
}
void disp()
{
System.out.println("The area of rectangle is "+(l*b));
}
}
class DemoConstructor
{
public static void main(String args[])
{
area a1=new area();//Default Constructor
area a2=new area(1,3); //parameterized constructor
area a3= new area(a1); // Copy constructor
a1.disp();
a2.disp();
a3.disp();
}
}

II BCA MEYYAPPAN S UCAA1303 PAGE NO. 49


OUTPUT:

The area of rectangle is 150


The area of rectangle is 3
The area of rectangle is 150

RESULT:

The area of rectangle using all types of constructors has been found successfully.

II BCA MEYYAPPAN S UCAA1303 PAGE NO. 50


EX. 3 A METHOD OVERLOADING

AIM:

To find area of rectangle and square implementing method overloading in java.

PROGRAM:

class Rectangle
{
double length, breath;
double Area(double l, double b)
{
length=l;
breath=b;
return length*breath;
}
double Area(double a )
{ //overloading method
length=a;
breath=a;
return length*breath;
}
}
class Overload
{
public static void main(String args[])
{
double area;
Rectangle rec=new Rectangle();
area=rec.Area(7.5,4.4);
System.out.println("Area of rectangle is "+area);
area=rec.Area(8.5);
System.out.println("Area of square is "+area);
}
}

II BCA MEYYAPPAN S UCAA1303 PAGE NO. 51


OUTPUT:

Area of rectangle is 33.0


Area of square is 72.25

RESULT:

The area of rectangle and square using method overloading has been found
successfully.

II BCA MEYYAPPAN S UCAA1303 PAGE NO. 52


EX. 3 B METHOD OVERRIDING
AIM:

To find area, volume and surface area of a cylinder implementing method overriding
using java.

PROGRAM:

class circle
{
double radius;
final double PI=3.14;
circle(double r)
{
radius=r;
}
double Area()
{
return PI*radius*radius;
}
double Circum()
{
return 2*PI*radius;
}
}
class Cylinder extends circle
{
double height;
Cylinder(double r,double h)
{
super(r);
height=h;
}
double Area()
{
return Circum()*height;
}
double Volume()
{
return PI*radius*radius*height;
}
}

class MethodOverring

II BCA MEYYAPPAN S UCAA1303 PAGE NO. 53


{
public static void main(String args[])
{
double a,surface_area,volume;
circle cir=new circle(5.5);
Cylinder cyl=new Cylinder(3.2,12.5);
a=cir.Area();
System.out.println("Area of the circle with radius ("+cir.radius+")= " +a);
System.out.println("Calculations for the cylinder with radius ("+cyl.radius+")
and height ("+cyl.height+")");
surface_area=cyl.Area();
volume=cyl.Volume();
System.out.println("Surface area= "+surface_area);
System.out.println("Volume= "+volume);
}
}

II BCA MEYYAPPAN S UCAA1303 PAGE NO. 54


OUTPUT:

Area of the circle with radius (5.5)= 94.985


Calculations for the cylinder with radius (3.2) and height (12.5)
Surface area= 251.20000000000005
Volume= 401.9200000000001

RESULT:

The area, volume and surface area of a cylinder using method overriding has been
found successfully.

II BCA MEYYAPPAN S UCAA1303 PAGE NO. 55


EX. 4 A SINGLE INHERITANCE
AIM:

To find area and volume of a room implementing single inheritance using java.

PROGRAM:

class Room
{
int length,breadth;
Room (int x,int y)
{
length=x;
breadth=y;
}
int area()
{
return (length*breadth);
}
}
class BedRoom extends Room
{
int height;
BedRoom(int x,int y,int z)
{
super(x,y);
height=z;
}
int volume()
{
return(length*breadth*height);
}
}
class SingleInheritance
{
public static void main(String args[])
{
BedRoom room1=new BedRoom(4,5,10);
int area1=room1.area(); int volume1=room1.volume();

System.out.println("Area of the room is "+area1);


System.out.println("Volume of the room is "+volume1);
}
}

OUTPUT:

II BCA MEYYAPPAN S UCAA1303 PAGE NO. 56


Area of the room is 20
Volume of the room is 200

RESULT:

The area and volume of a room implementing single inheritance has been found
successfully.

II BCA MEYYAPPAN S UCAA1303 PAGE NO. 57


EX. 4 B MULTI-LEVEL INHERITANCE

AIM:

To create student mark list implementing multi-level inheritance using java.

PROGRAM:

import java.io.*;
class students
{
String name,rollno;
void input()throws IOException
{
System.out.println("ENTER THE NAME:");
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
name=br.readLine();
System.out.println("ENTER THE REGISTER NUMBER:");
rollno=br.readLine();
}
void output()
{
System.out.println("NAME:" +name);
System.out.println("REGISTER NUMBER:" +rollno);
}
}
class semesters extends students
{
int m1,m2,m3;
void getdata()throws IOException
{
System.out.println("ENTER THE 1ST MARK: ");
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
m1=Integer.parseInt(br.readLine());
System.out.println("ENTER THE 2ND MARK: ");
m2=Integer.parseInt(br.readLine());
System.out.println("ENTER THE 3RD MARK: ");
m3=Integer.parseInt(br.readLine());
}
void putdata()
{
System.out.println("MARK 1: " +m1);
System.out.println("MARK 2: " +m2);
System.out.println("MARK 3: " +m3);
}

II BCA MEYYAPPAN S UCAA1303 PAGE NO. 58


}
class fina extends semesters
{
int total;
float avg;
String grade,res;
void read()throws IOException
{
input();
getdata();
}
void calculate()
{
total=m1+m2+m3;
avg=total/(float)3;
}
void write()
{
output();
putdata();
System.out.println("TOTAL: " +total);
System.out.println("AVERAGE: " +avg);
}
void result()
{
if((m1>=40)&&(m2>=40)&&(m3>=40))
{
res="PASS";
if(avg>=80)
{
grade="1ST CLASS";
}
else if((avg<=79)&&(avg>=60))
{
grade="2ND CLASS";
}
else if((avg<=59)&&(avg>=40))
{
grade="3RD CLASS";
}
}
else
{
res="FAIL";
grade="\n------\n FAIL \n------";
}
System.out.println("RESULT: "+res);

II BCA MEYYAPPAN S UCAA1303 PAGE NO. 59


System.out.println("GRADE: "+grade);
}
}
class Multilevel
{
public static void main(String args[])throws IOException
{
fina f=new fina();
f.read();
f.calculate();
f.write();
f.result();
}
}

II BCA MEYYAPPAN S UCAA1303 PAGE NO. 60


OUTPUT:

ENTER THE NAME:


Abc
ENTER THE REGISTER NUMBER:
UCAA1111
ENTER THE 1ST MARK:
86
ENTER THE 2ND MARK:
76
ENTER THE 3RD MARK:
66
NAME:Abc
REGISTER NUMBER:UCAA1111
MARK 1: 86
MARK 2: 76
MARK 3: 66
TOTAL: 228
AVERAGE: 76.0
RESULT: PASS
GRADE: 2ND CLASS

RESULT:

The student mark list implementing multi-level inheritance has been created
successfully.

II BCA MEYYAPPAN S UCAA1303 PAGE NO. 61


EX. 4 C HYBRID INHERITANCE
AIM:

To find sum and factorial of given numbers implementing hybrid inheritance using
java.

PROGRAM:

class a
{
int i,j;
void show()
{
System.out.println("Value of i is "+i);
System.out.println("Value of j is "+j);
}
}
class b extends a
{
void sum()
{
int k=i+j;
System.out.println("Sum of i and j is "+k);
}
}
class c extends a
{
void fact()
{
int j=1;
for(int a=1;a<=i;a++)
{
j=j*a;
}
System.out.println("The factorial value is "+j);
}
}
class Final extends c
{
public static void main(String arg[])
{
a obj=new a();
obj.i=10;
obj.j=20;
obj.show();
b obj1=new b();
obj1.i=100;
obj1.j=200;

II BCA MEYYAPPAN S UCAA1303 PAGE NO. 62


obj1.sum();
c obj2=new c();
obj2.i=5;
obj2.fact();
}
}

OUTPUT:

II BCA MEYYAPPAN S UCAA1303 PAGE NO. 63


Value of i is 10
Value of j is 20
Sum of i and j is 300
The factorial value is 120

RESULT:

The sum and factorial of given numbers implementing hybrid inheritance has been
found successfully.

II BCA MEYYAPPAN S UCAA1303 PAGE NO. 64


EX. 4 D HIERARCHICAL INHERITANCE

AIM:

To find area of square and cube implementing hierarchical inheritance using java.

PROGRAM:

class side
{
int l;
void set_value(int x)
{
l=x;
}
}
class square extends side
{
int sq()
{
return(l*l);
}
}
class cube extends side
{
int cub()
{
return(l*l*l);
}
}
class Hierarchical
{
public static void main(String[] args)
{
square s=new square();
{
s.set_value(10);
System.out.println("Area of the square is " +s.sq());
}
cube c=new cube();
{
c.set_value(20);
System.out.println("Area of the cube is " +c.cub());
}
}
}

II BCA MEYYAPPAN S UCAA1303 PAGE NO. 65


OUTPUT:

Area of the square is 100


Area of the cube is 8000

RESULT:

The area of square and cube implementing hierarchical inheritance has been found
successfully.

II BCA MEYYAPPAN S UCAA1303 PAGE NO. 66


EX. 5 PACKAGES

AIM:

To display college class details implementing packages using java.

PROGRAM:

//Step 1: Create a package named “college” & a class named “course.java” in it.

package college;
public class course
{
String dept;
String year;
public course(String d,String y)
{
dept=d;
year=y;
}
public void display()
{
System.out.println("Department: "+dept);
System.out.println("Year: "+year);
}
}

//Step 2: Compile (F9) the above program and create another package named “dept” & a
class named “faculty.java” in it.

package dept;
public class faculty
{
String staff_name;
String subject;
public faculty(String sn,String sb)
{
staff_name=sn;
subject=sb;
}
public void display()
{
System.out.println("Name of the faculty: "+staff_name);
System.out.println("Subject handled: "+subject);
}
}

II BCA MEYYAPPAN S UCAA1303 PAGE NO. 67


//Step 3: Again compile(F9) the above program and create a class named “disp_final.java”
in a default package.

import college.*;
import dept.*;
class disp_final
{
public static void main(String args[])
{
course c=new course("Comp. Sci","II year");
faculty f=new faculty("SDR","JAVA");
c.display();
f.display();
}
}

//Step 4: Run this program.

II BCA MEYYAPPAN S UCAA1303 PAGE NO. 68


OUTPUT:

Department: Comp. Sci


Year: II year
Name of the faculty: SDR
Subject handled: JAVA

RESULT:

The college class details implementing packages has been displayed successfully.

II BCA MEYYAPPAN S UCAA1303 PAGE NO. 69


EX. 6 INTERFACES

AIM:

To find area of rectangle and circle implementing interfaces using java.

PROGRAM:

interface Area
{
final static float pi = 3.14f;
float compute (float x, float y); //Intereface implemented
}
class Rect implements Area
{
public float compute(float x, float y)
{
return (x*y);
}
}
class Circle implements Area
{
public float compute (float x, float y)
{
return(pi*x*x);
}
}
class DemoInerface
{
public static void main (String args[])
{
Rect rect = new Rect();
Circle cir = new Circle();
Area area;
area = rect;
System.out.println("Area of Rectangle = "+area.compute(10,20));
area = cir;
System.out.println("Area of Circle = " +area.compute(10,0));
}
}

II BCA MEYYAPPAN S UCAA1303 PAGE NO. 70


OUTPUT:

Area of Rectangle = 200.0


Area of Circle = 314.0

RESULT:

The area of rectangle and circle implementing interfaces has been found
successfully.

II BCA MEYYAPPAN S UCAA1303 PAGE NO. 71


EX. 7 EXCEPTION HANDLING

AIM:

To implement exception handling using java.

PROGRAM:

class ExceptionHandling
{
public static void main(String args[])
{
int vec []={3,5,4,10,2};
int nmr=24;
int quot,sum=0;
for(int i=0;i<=5;i++)
{
try
{
quot=nmr/(2-i);
try
{
sum+=vec[i];
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("\nArray index out of bounds at index
try(inner try)="+i);
}
catch(ArithmeticException e)
{
System.out.println("\nDivide by zero error at i(inner try)="+i);
}
System.out.println("\nQuotient ="+quot);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("\n Array index out of bounds at index(outer
try)"+i);
}

II BCA MEYYAPPAN S UCAA1303 PAGE NO. 72


catch(ArithmeticException e)
{
System.out.println("\nDivide by zero error at i(outer try)="+i);
}
finally
{
System.out.println("Finally Block");
}
}
System.out.println("\nSum of Numbers="+sum);
}
}

II BCA MEYYAPPAN S UCAA1303 PAGE NO. 73


OUTPUT:

Quotient =12
Finally Block

Quotient =24
Finally Block

Divide by zero error at i(outer try)=2


Finally Block

Quotient =-24
Finally Block

Quotient =-12
Finally Block

Array index out of bounds at index try(inner try)=5

Quotient =-8
Finally Block

Sum of Numbers=20

RESULT:

The concept of exception handling has been used successfully.

II BCA MEYYAPPAN S UCAA1303 PAGE NO. 74


EX. 8 A THREAD SYNCHRONIZATION

AIM:

To implement synchronized thread using java.

PROGRAM:

class Printing
{
synchronized void printnumber(int n)
{
System.out.println("Start");
for(int j=n;j>0;j--)
{
try
{
if(j==n/2)
Thread.sleep(100);
}
catch (InterruptedException e)
{
;
}
System.out.print(" "+j);
}
System.out.println("\nEnd");
}
}
class Threadserve implements Runnable
{
int n;
Printing pt;
Thread th;
Threadserve(Printing p,int x)
{
n=x;
pt=p;
th=new Thread(this);
th.start();
}
public void run()
{
pt.printnumber(n);
}
}

II BCA MEYYAPPAN S UCAA1303 PAGE NO. 75


class ThreadSynchro1
{
public static void main(String args[])
{
Printing p=new Printing();
Threadserve ts1 =new Threadserve(p,16);
Threadserve ts2=new Threadserve(p,8);
Threadserve ts3=new Threadserve(p,10);
}
}

II BCA MEYYAPPAN S UCAA1303 PAGE NO. 76


OUTPUT:

Start
16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
End
Start
10 9 8 7 6 5 4 3 2 1
End
Start
87654321
End

RESULT:

The concept of synchronized thread has been implemented successfully.

II BCA MEYYAPPAN S UCAA1303 PAGE NO. 77


EX. 8 B MULTI-THREADING

AIM:

To find factorial and sum of given numbers implementing multi-threading in java.

PROGRAM:

class Sumthread implements Runnable


{
int i,sum=0;
public void run()
{
for(i=1;i<=5;i++)
{
sum+=i;
System.out.println("Sum of Numbers from 1 up to"+i+"="+sum);
if(i==4)
Thread.yield();
}
}
}
class Factthread implements Runnable
{
int i,n,fact=1;
public void run()
{
for(i=1;i<=5;i++)
{
fact*=i;
System.out.println("Factorial of "+i+"="+fact);
}
}
}

II BCA MEYYAPPAN S UCAA1303 PAGE NO. 78


class Multithread
{
public static void main(String args[])
{
Thread ct=Thread.currentThread();
System.out.println("\nThe main thread is: "+ct.getName());
Sumthread st=new Sumthread();
Factthread ft=new Factthread();
Thread sumt =new Thread(st,"Sum thread");
Thread factt =new Thread(ft,"Factorial thread");
sumt.start();
System.out.println("\nThe thread created is: "+sumt.getName());
factt.start();
System.out.println("\nThe thread created is:"+factt.getName());
}
}

II BCA MEYYAPPAN S UCAA1303 PAGE NO. 79


OUTPUT:

The main thread is: main

The thread created is: Sum thread

The thread created is:Factorial thread


Sum of Numbers from 1 up to1=1
Sum of Numbers from 1 up to2=3
Sum of Numbers from 1 up to3=6
Sum of Numbers from 1 up to4=10
Sum of Numbers from 1 up to5=15
Factorial of 1=1
Factorial of 2=2
Factorial of 3=6
Factorial of 4=24
Factorial of 5= 120

RESULT:

The factorial and sum of given numbers implementing multi-threading has been
found successfully.

II BCA MEYYAPPAN S UCAA1303 PAGE NO. 80


EX. 9 A JAVA UTILITIES- VECTOR CLASS

AIM:

To create a name list implementing Vector class using java utilities.

PROGRAM:

import java.io.*;
import java.util.*;
class Vectorclass
{
public static void main(String agrs[])throws IOException
{
int n,i,x;
Vector v=new Vector(100);
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("\tIMPLEMENTATION OF VECTOR CLASS");
System.out.println("\t* * * * * * * * * * * * * * **");
System.out.println();
System.out.println("Input");
System.out.println("-----");
System.out.println("Enter the value of n");
n=Integer.parseInt(br.readLine());
System.out.println("Enter " + n + " names");
for(i=1;i<=n;i++)
{
String S=br.readLine();
v.addElement(S);
}
System.out.println();
System.out.println("Output");
System.out.println("------");
System.out.println("First name: "+v.firstElement());
System.out.println("Last name: "+v.lastElement());
Enumeration enum1=v.elements();
System.out.println("\nElements in the vector");
System.out.println("----------------------");
while(enum1.hasMoreElements())
{
System.out.println(enum1.nextElement());
}
System.out.println();
}
}

II BCA MEYYAPPAN S UCAA1303 PAGE NO. 81


OUTPUT:

IMPLEMENTATION OF VECTOR CLASS


* * * * * * * * * * * * * * **

Input
-----
Enter the value of n
3
Enter 3 names
ARAVIND
GIRI
THAMARAI

Output
------
First name: ARAVIND
Last name: THAMARAI

Elements in the vector


----------------------
ARAVIND
GIRI
THAMARAI

RESULT:

The name list using Vector class utilities has been created successfully.

II BCA MEYYAPPAN S UCAA1303 PAGE NO. 82


EX. 9 B JAVA UTILITIES- CALENDAR CLASS

AIM:

To print current date and time implementing Calendar class using java utilities.

PROGRAM:

import java.io.*;
import java.util.*;
class CalendarClass
{
public static void main(String args[])
{
String months[]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep",
"Oct","Nov","Dec"};
String ampm[]={"AM","PM"};
Calendar cal=Calendar.getInstance();
System.out.println();
System.out.println("\t\tCalendar Application for current Date and Time");
System.out.println("\t\t---------------------------------------------------------------");
System.out.println("The current date and time is");
System.out.print("\nDate : ");
System.out.print(cal.get(Calendar.DATE));
System.out.print("-"+(cal.get(Calendar.MONTH)+1));
System.out.print("-"+(cal.get(Calendar.YEAR)));
System.out.println();
System.out.println("Month : "+months[cal.get(Calendar.MONTH)]);
System.out.println("Year : "+cal.get(Calendar.YEAR));
System.out.print("Time : ");
System.out.print(cal.get(Calendar.HOUR));
System.out.print(":"+cal.get(Calendar.MINUTE));
System.out.print(":"+cal.get(Calendar.SECOND));
System.out.print(" "+ampm[cal.get(Calendar.AM_PM)]);
System.out.println();
cal.set(Calendar.HOUR,11);
cal.set(Calendar.MINUTE,30);
cal.set(Calendar.SECOND,28);
System.out.println();
System.out.print("Updated Time: ");
System.out.print(cal.get(Calendar.HOUR));
System.out.print(":"+cal.get(Calendar.MINUTE));
System.out.print(":"+cal.get(Calendar.SECOND));
System.out.print(" "+ampm[cal.get(Calendar.AM_PM)]);
System.out.println();
}
}

II BCA MEYYAPPAN S UCAA1303 PAGE NO. 83


OUTPUT:

Calendar Application for current Date and Time


---------------------------------------------------------------
The current date and time is

Date : 9-10-2014
Month : Oct
Year : 2014
Time : 0:54:44 AM

Updated Time: 11:30:28 AM

RESULT:

The current date and time using Calendar class utilities has been displayed
successfully.

II BCA MEYYAPPAN S UCAA1303 PAGE NO. 84


EX. 9 C JAVA UTILITIES- RANDOM CLASS

AIM:

To print random numbers implementing Random class using java utilities.

PROGRAM:

import java.util.Random;
import java.io.*;
class RandomClass
{
public static void main(String args[]) throws IOException
{
int n,x,i,j;
int a[]=new int[10];
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Input");
System.out.println("*****");
System.out.println("How many random numbers you want?");
n=Integer.parseInt(br.readLine());
Random r= new Random();
for(i=0;i<n;i++)
{
x=r.nextInt(100);
a[i]=Math.abs(x);
}
System.out.println("OUTPUT:");
System.out.println("******");
System.out.println("The Random numbers generated are");
for(i=0;i<n;i++)
{
System.out.println(a[i]);
}
//Sorting Begins
for (i=0;i<n-1;i++)
for (j=i+1;j<n;j++)
{
if (a[i]>=a[j])
{
int t=a[i];
a[i]=a[j];
a[j]=t;
}
}
System.out.println("The Random numbers in Ascending order are");

II BCA MEYYAPPAN S UCAA1303 PAGE NO. 85


System.out.println();
for(i=0;i<n;i++)
{
System.out.println(a[i]);
}
System.out.println("The Random numbers in Descending order are");
System.out.println();
for(i=n-1;i>=0;i--)
{
System.out.println(a[i]);
}
}
}

II BCA MEYYAPPAN S UCAA1303 PAGE NO. 86


OUTPUT:

Input
*****
How many random numbers you want?
5
OUTPUT:
******
The Random numbers generated are
48
7
91
39
15
The Random numbers in Ascending order are

7
15
39
48
91
The Random numbers in Descending order are

91
48
39
15
7

RESULT:

The random numbers using Random class utilities has been displayed successfully.

II BCA MEYYAPPAN S UCAA1303 PAGE NO. 87


EX. 10 APPLET LIFE-CYCLE

AIM:

To implement Applet life-cycle using java applets.

PROGRAM:

import java.applet.*;
import java.awt.*;
public class AppletleLifeCycle extends Applet
{
String mesg1,mesg2,mesg3,mesg4;
public void init()
{
mesg1="message from init method";
}
public void start()
{
mesg2="message form start method";
}
public void stop()
{
mesg3="message form stop method";
}
public void destroy()
{
mesg4="System is destroying your applet";
}
public void paint(Graphics gp)
{
gp.drawString("Demo for basic methods of applet",20,40);
if(mesg1!=null)
gp.drawString(mesg1, 20,80);
if(mesg2!=null)
gp.drawString(mesg2, 20,100);
if(mesg3!=null)
gp.drawString(mesg3, 20,120);
if(mesg4!=null)
gp.drawString(mesg4, 20,140);
}
}

II BCA MEYYAPPAN S UCAA1303 PAGE NO. 88


OUTPUT:

RESULT:

The Applet life-cycle in java applets have been used successfully.

II BCA MEYYAPPAN S UCAA1303 PAGE NO. 89


EX. 11 A MOUSE EVENTS

AIM:

To implement MouseEvents using java.

PROGRAM:

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.*;
public class Mouse1 extends Applet implements MouseListener,MouseMotionListener
{
String txt="Nothing";
public void init()
{
addMouseListener(this);
addMouseMotionListener(this);
}
public void mouseClicked(MouseEvent me)
{
txt="Mouse Clicked";
repaint();
}
public void mouseEntered(MouseEvent me)
{
txt="Mouse Entered";
repaint();
}
public void mouseExited(MouseEvent me)
{
txt="Mouse Exited";
repaint();
}
public void mousePressed(MouseEvent me)
{
txt="Mouse pressed ";
setForeground(Color.cyan);
repaint();
}
public void mouseReleased(MouseEvent me)
{
txt="Mouse Released";
setForeground(Color.magenta);
repaint();
}

II BCA MEYYAPPAN S UCAA1303 PAGE NO. 90


public void mouseDragged(MouseEvent me)
{
txt="Mouse Dragged";
setForeground(Color.red);
repaint();
}
public void mouseMoved(MouseEvent me)
{
txt="Mouse Moved";
setForeground(Color.green);
repaint();
}
public void paint(Graphics gp)
{
gp.drawString(txt,20,40);
showStatus("Mouse event handling");
}
}

II BCA MEYYAPPAN S UCAA1303 PAGE NO. 91


OUTPUT:

RESULT:

The MouseEvents in java applets have been shown successfully.

II BCA MEYYAPPAN S UCAA1303 PAGE NO. 92


EX. 11 B KEYBOARD EVENTS

AIM:

To implement KeyEvents using java.

PROGRAM:

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class DemoKeyEvents extends Applet implements KeyListener
{
String txt=" ";
String txt1=" ";
String txt2=" ";
String txt3=" ";
int kcode;
char ch;
int drawnnumber;
public void init()
{
addKeyListener(this);
requestFocus();
}
public void keyTyped(KeyEvent ke)
{
ch=ke.getKeyChar();
txt1+=ch;
if(txt1.length()>25)
txt1="";
txt ="Key typed";
repaint();
}
public void keyPressed(KeyEvent ke)
{
kcode=ke.getKeyCode();
if(kcode==ke.VK_F1)
txt2="You have typed F1 key";
if(kcode==ke.VK_SHIFT)
txt3="You have typed shift key";
txt="Key Pressed";
repaint();
}
public void keyReleased(KeyEvent ke)
{

II BCA MEYYAPPAN S UCAA1303 PAGE NO. 93


txt="Key released";
repaint();
}
public void paint(Graphics gp)
{
gp.drawString(txt,20,20);
gp.drawString(txt1,20,40);
gp.drawString(txt2,20,60);
gp.drawString(txt3,20,80);
showStatus("Key events");
}
}

II BCA MEYYAPPAN S UCAA1303 PAGE NO. 94


OUTPUT:

RESULT:

The KeyEvents in java applets have been shown successfully.

II BCA MEYYAPPAN S UCAA1303 PAGE NO. 95


EX. 12 LAYOUT MANAGER

AIM:

To implement layout manager using java.

PROGRAM:

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

/*<applet code="panelprog" height=800 width=800>


</applet> */

class myframe extends Frame


{
myframe()
{
addWindowListener(new w());
}
class w extends WindowAdapter
{
public void WindowClosing(WindowEvent we)
{
try
{
setVisible(false);
dispose();
System.exit(0);
}
catch(Exception e)
{}
}
}
}
public class panelprog extends Applet implements ActionListener,ItemListener
{
Button grid,gridbag,flow,card,border;
public void init()
{
setBackground(Color.pink);
setForeground(Color.blue);
grid=new Button("GridLayout");
gridbag=new Button("GridBagLayout");
flow=new Button("FlowLayout");
card=new Button("CardLayout");

II BCA MEYYAPPAN S UCAA1303 PAGE NO. 96


border=new Button("BorderLayout");
setFont(new Font("Sanserif",Font.BOLD,14));

add(grid);
add(gridbag);
add(flow);
add(card);
add(border);

grid.addActionListener(this);
gridbag.addActionListener(this);
flow.addActionListener(this);
card.addActionListener(this);
border.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
String str=ae.getActionCommand();
if (str.equals("GridLayout"))
g1();
else if(str.equals("GridBagLayout"))
gb1();
else if(str.equals("FlowLayout"))
f1();
else if(str.equals("CardLayout"))
c1();
else
b1();
repaint();
}
public void g1()
{
myframe mf=new myframe();
mf.setSize(300,200);
mf.setBackground(Color.cyan);
mf.setVisible(true);
mf.setTitle("GridLayout");
int n=4;
mf.setLayout(new GridLayout(n,n));
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
int k=i*n+j;
if(k>0)
mf.add(new Button(""+k));
}

II BCA MEYYAPPAN S UCAA1303 PAGE NO. 97


}
}
public void gb1()
{
myframe mf=new myframe();
mf.setSize(300,200);
mf.setBackground(Color.cyan);
mf.setVisible(true);
mf.setTitle("GridBagLayout");
GridBagConstraints gbc;
GridBagLayout g;
g=new GridBagLayout();
mf.setLayout(g);

gbc=new GridBagConstraints();
Button b1=new Button("Button 1");
Button b2=new Button("Button 2");
Button b3=new Button("Button 3");

gbc.gridwidth=GridBagConstraints.REMAINDER;
g.setConstraints(b1,gbc);
mf.add(b1);

gbc.gridwidth=GridBagConstraints.REMAINDER;
g.setConstraints(b2,gbc);
mf.add(b2);

gbc.gridwidth=GridBagConstraints.REMAINDER;
g.setConstraints(b3,gbc);
mf.add(b3);
}
public void f1()
{
myframe mf=new myframe();
mf.setSize(300,200);
mf.setBackground(Color.cyan);
mf.setVisible(true);
mf.setTitle("FlowLayout");

String s[]={"one","two","three"};
mf.setLayout(new FlowLayout());
for(int i=0;i<3;i++)
mf.add(new Button(s[i]));
}
public void c1()
{
myframe mf=new myframe();

II BCA MEYYAPPAN S UCAA1303 PAGE NO. 98


mf.setSize(300,200);
mf.setBackground(Color.cyan);
mf.setVisible(true);
mf.setTitle("CardLayout");

Button s1,s2,s3;
CardLayout c;
Panel p;
c=new CardLayout();
p=new Panel();
mf.add(p);
p.setLayout(c);
s1=new Button("Yes");
s2=new Button("No");
s3=new Button("YesNo");

s1.addActionListener(this);
s2.addActionListener(this);
s3.addActionListener(this);

p.add("yes",s1);
p.add("no",s2);
p.add("yesno",s3);

c.last(p);
}
public void b1()
{
myframe mf=new myframe();
mf.setSize(300,200);
mf.setBackground(Color.cyan);
mf.setVisible(true);
mf.setTitle("BorderLayout");

Button e,w,n,s,c;
mf.setLayout(new BorderLayout());
e=new Button("East");
w=new Button("West");
n=new Button("North");
s=new Button("South");
c=new Button("Center");

mf.add("East",e);
mf.add("West",w);
mf.add("North",n);
mf.add("South",s);
mf.add("Center",c);

II BCA MEYYAPPAN S UCAA1303 PAGE NO. 99


}
public void itemStateChanged(ItemEvent ie)
{
repaint();
}
}

II BCA MEYYAPPAN S UCAA1303 PAGE NO. 100


OUTPUT:

II BCA MEYYAPPAN S UCAA1303 PAGE NO. 101


RESULT:

The layout manager in java applets have been displayed successfully.

II BCA MEYYAPPAN S UCAA1303 PAGE NO. 102


EX. 13 MENUS

AIM:

To implement simple menus using java.

PROGRAM:

import java.awt.*;
import java.awt.event.*;
public class MenuTest extends Frame implements ActionListener, ItemListener
{
MenuBar mb;
Menu f,s,c;
MenuItem n,o,b,g;
CheckboxMenuItem r;
MenuTest()
{
mb=new MenuBar();

f=new Menu("File");
s=new Menu("Settings");
c=new Menu("Color");

n=new MenuItem("New",new MenuShortcut(KeyEvent.VK_N));


o=new MenuItem("Open");

r=new CheckboxMenuItem("Red");
b=new MenuItem("Blue");
g=new MenuItem("Green");

f.add(n);
f.addSeparator();
f.add(o);

c.add(r);
c.add(b);
c.add(g);

s.add(c);
mb.add(f);
mb.add(s);

r.addItemListener(this);
b.addActionListener(this);

II BCA MEYYAPPAN S UCAA1303 PAGE NO. 103


g.addActionListener(this);

setMenuBar(mb);
setSize(400,400);
setVisible(true);
}
public static void main(String s[])
{
new MenuTest();
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource().equals(g))
setBackground(Color.green);
else
setBackground(Color.blue);
}
public void itemStateChanged(ItemEvent ie)
{
if(ie.getSource().equals(r))
{
if(r.getState())
setBackground(Color.red);
else
setBackground(Color.pink);
}

}
}

II BCA MEYYAPPAN S UCAA1303 PAGE NO. 104


OUTPUT:

RESULT:

The simple menus in java applets have been shown successfully.

II BCA MEYYAPPAN S UCAA1303 PAGE NO. 105

Вам также может понравиться