0% found this document useful (0 votes)
142 views119 pages

Java Training

Download as docx, pdf, or txt
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 119

SUMMER TRAINING

Program-1

Objective: WAP to create a class student that contains


attributes student, name, roll no, and marks of 3 students now
input all values from user using getstu() and method and generate
the marksheet of student using marksheet().

Source code:
import java.io.*;
class student
{
String name;
int rno,m1,m2,m3;
void getstu()throws IOException
{
BufferedReader obj=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter the name of student,roll number and
marks of three students");
name=obj.readLine();
rno=Integer.parseInt(obj.readLine());
m1=Integer.parseInt(obj.readLine());
m2=Integer.parseInt(obj.readLine());
m3=Integer.parseInt(obj.readLine());
}
void marksheet()

Akansha Dwivedi BCA 5th Sem Page 1


SUMMER TRAINING
{
int total;
float per;
String div;
total=m1+m2+m3;
per=(total/100.0f);
if(per>=60)
div="Ist Division";
if(per>=45)
div="IInd Division";
if(per>=35)
div="IIIrd Division";
else
div="Fail";
System.out.println("Roll Number=\t"+rno);
System.out.println("Student name=\t"+name);
System.out.println("Marks Obtained=\t"+total);
System.out.println("Percentage=\t"+per);
System.out.println(div);
}}
class runMarksheet
{
public static void main(String args[])throws IOException
{
student stu1=new student();
student stu2=new student();
Akansha Dwivedi BCA 5th Sem Page 2
SUMMER TRAINING
student stu3=new student();
stu1.getstu();
stu2.getstu();
stu3.getstu();
stu1.marksheet();
stu2.marksheet();
stu3.marksheet();
}}

Output:

Akansha Dwivedi BCA 5th Sem Page 3


SUMMER TRAINING

Program-2

Objective: WAP to input radius of circle and compute area,


circumference.
Source code:
import java.io.*;
class Circle
{
double r,ar,cr;
public void getradius()throws IOException
{
InputStreamReader ir= new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(ir);
System.out.println("Enter the radius");
r=Integer.parseInt(br.readLine());
}
void findCircum()
{
cr=2*Math.PI*r;
System.out.println("Circumference of a circle="+cr);
Akansha Dwivedi BCA 5th Sem Page 4
SUMMER TRAINING
}
void findArea()
{
ar=Math.PI*r*r;
System.out.println("Area of a circle="+ar);
}}
class RunCircle
{
public static void main(String args[])throws IOException
{
Circle obj=new Circle();
obj.getradius();
obj.findArea();
obj.findCircum();
}}
Output:-

Akansha Dwivedi BCA 5th Sem Page 5


SUMMER TRAINING

Program-3

Objective: WAP to find the factorial of a number.


Source code:
import java.io.*;
class factorial
{
int fact(int a)
{
int i, f = 1;
for (i = 1; i <= a; i++)
{
f = f * i;
}
return f;
}}

Akansha Dwivedi BCA 5th Sem Page 6


SUMMER TRAINING
class run
{
public static void main(String args[])throws IOException
{
int a, f;
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.print("Enter a Number :");
a=Integer.parseInt(br.readLine());
factorial ff = new factorial();
f = ff.fact(a);
System.out.println("\nThe Factorial is :" + f);
}}

Output:

Akansha Dwivedi BCA 5th Sem Page 7


SUMMER TRAINING

Program-4

Objective: WAP to input the value of a, d, n using default


constructor and print AP series.
Source code:
import java.io.*;
class apSeries
{
int a,d,n,t,i;
apSeries()throws IOException
{

Akansha Dwivedi BCA 5th Sem Page 8


SUMMER TRAINING
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter the value of a,d,n");
a=Integer.parseInt(br.readLine());
d=Integer.parseInt(br.readLine());
n=Integer.parseInt(br.readLine());
}
void printAP()
{
System.out.println("AP Series=");
for(i=0;i<=n;i++)
{
t=a+(i*d);
System.out.println(t);
}}}
class runAPSeries
{
public static void main (String args[])throws IOException
{
apSeries obj=new apSeries();
obj.printAP();
}}
Output:

Akansha Dwivedi BCA 5th Sem Page 9


SUMMER TRAINING

Program-5

Objective: WAP to compute the roots of a quadratic equation


if coefficient is initialized using parameterized call by value.

Akansha Dwivedi BCA 5th Sem Page 10


SUMMER TRAINING
Source code:
import java.io.*;
class roots
{
int a,b,c,d;
double r1,r2;
roots(int x,int y,int z)
{
a=x;
b=y;
c=z;
}
void findRoots()
{
d=(b*b)-(4*a*c);
if(d>0)
{
r1=(-b+Math.sqrt(d)/(2*a));
r2=(-b-Math.sqrt(d)/(2*a));
System.out.println("Root 1="+r1);
System.out.println("Root 2="+r2);
}
else if(d==0)
{
r1=(-b/2*a);

Akansha Dwivedi BCA 5th Sem Page 11


SUMMER TRAINING
System.out.println("Both roots are="+r1);
}
else
{
System.out.println("No Root ");
}}}
class runRoots
{
public static void main (String args[])
{
roots obj=new roots(12,44,6);
obj.findRoots();
}}
Output:

Akansha Dwivedi BCA 5th Sem Page 12


SUMMER TRAINING

Program-6

Objective: WAP to demonstrate parameterized constructor


using call by reference.
Source code:
import java.io.*;
class number
{
int a,b;
number(int x,int y)
{
a=x;
b=y;
System.out.println("class number is initialised");
}
number(number N)
{
a=N.a;
b=N.b;
System.out.println("duplicate copy is created");
}
void swap()
{
int temp;

Akansha Dwivedi BCA 5th Sem Page 13


SUMMER TRAINING
temp=a;
a=b;
b=temp;
}
void showData()
{
System.out.println("a="+a);
System.out.println("b="+b);
}}
class constructor
{
public static void main (String args[])
{
number obj=new number(50,80);
number obj2=new number(obj);
obj.swap();
System.out.println("Swapped Data");
obj.showData();
System.out.println("Actual Data");
obj2.showData();
}}
Output:

Akansha Dwivedi BCA 5th Sem Page 14


SUMMER TRAINING

Program-7

Objective: WAP to compute area of shapes if formula is given


below-Area=2*((w*h)+(h*d)+(d*w)); by appropriate use of
following constructor:
1. Default.
2. Parameterized call by value.
3. Parameterized call by reference.
Source code:
class Shape
{
int w,h,d,ar;
Shape()
{
w=3;
h=5;
d=2;

Akansha Dwivedi BCA 5th Sem Page 15


SUMMER TRAINING
}
Shape(int x,int y,int z)
{
w=x;
h=y;
d=z;
}
Shape(Shape S)
{
w=S.w;
h=S.h;
d=S.d;
}
void findArea()
{
ar=2*((w*h)+(h*d)+(d*w));
System.out.println("Area of shape="+ar);
}}
class runShape
{
public static void main(String args[])
{
Shape S1=new Shape();
Shape S2=new Shape(7,9,15);
Shape S3=new Shape(S1);
S1.findArea();
Akansha Dwivedi BCA 5th Sem Page 16
SUMMER TRAINING
S2.findArea();
S3.findArea();
}}
Output:

Program-8

Objective: WAP for demonstration of Inheritance.


Source code:
class Base
{
private int a;
int b;
protected int c;
public int d;
void geta(int x)
{

Akansha Dwivedi BCA 5th Sem Page 17


SUMMER TRAINING
a=x;
}
void getbcd(int x,int y,int z)
{
b=x;
c=y;
d=z;
}
void puta()
{
System.out.println("a="+a);
}
void putbcd()
{
System.out.println("b="+b);
System.out.println("c="+c);
System.out.println("d="+d);
}
}
class Derived extends Base
{
int e,f;
void getef(int x,int y)
{
e=x;
f=y;
Akansha Dwivedi BCA 5th Sem Page 18
SUMMER TRAINING
}
void putef()
{
System.out.println("e="+e);
System.out.println("f="+f);
}
void showall()
{
//System.out.println("a="+a);
System.out.println("b="+b);
System.out.println("c="+c);
System.out.println("d="+d);
System.out.println("e="+e);
System.out.println("f="+f);
}}
class testinheritance
{
public static void main(String args[])
{
Base B=new Base();
Derived D=new Derived();
B.geta(10);
B.getbcd(20,30,40);
D.getbcd(50,60,70);
D.getef(80,90);
B.puta();
Akansha Dwivedi BCA 5th Sem Page 19
SUMMER TRAINING
B.putbcd();
D.putbcd();
D.putef();
D.showall();
}}
Output:

Program-9

Objective: WAP to create a class series that take input of a, d, n


and create two new sub classes AP and GP that print their
respective results.
Source code:
import java.io.*;
Akansha Dwivedi BCA 5th Sem Page 20
SUMMER TRAINING
class series
{
int a,d,n;
void getvalue()throws IOException
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter the value of a,d,n");
a=Integer.parseInt(br.readLine());
d=Integer.parseInt(br.readLine());
n=Integer.parseInt(br.readLine());
}}
class AP extends series
{
int t,i;
void printAP()
{
System.out.println("AP Series=");
for(i=0;i<=n;i++)
{
t=a+(i*d);
System.out.println(t);
}}}
class GP extends series
{
int t,i;
Akansha Dwivedi BCA 5th Sem Page 21
SUMMER TRAINING
void printGP()
{
System.out.println("GP Series=");
for(i=0;i<=n;i++)
{
t=(int)(a*Math.pow(d,i));
System.out.println(t);
}}}
class RunSeries
{
public static void main (String args[])throws IOException
{
AP A=new AP();
GP G=new GP();
A.getvalue();
A.printAP();
G.getvalue();
G.printGP();
}}

Output:

Akansha Dwivedi BCA 5th Sem Page 22


SUMMER TRAINING

Akansha Dwivedi BCA 5th Sem Page 23


SUMMER TRAINING
Program-10

Objective: WAP to create a class circle that contain a attribute


radius and now create tools of classes area and circumference to
compute respective ratios.

Source code:
class Circle
{
public int r;
void getvalue(int x)
{
r=x;
}
void putradius()
{
System.out.println("The radius of circle= ");
r=Integer.parseInt(br.readLine());
}
}
class Area extends Circle
{
double a;
void printArea()
{
a=Math.PI*r*r;
System.out.println("Area= "+a);
}

Akansha Dwivedi BCA 5th Sem Page 24


SUMMER TRAINING
}
class Circum extends Circle
{
double c;
void printCircum()
{
c=2*Math.PI*r;
System.out.println("Circumference of circle= "+c);
}
}
class Run_Circle
{
public static void main(String args[])
{
Area A=new Area();
Circum C=new Circum();
A.putradius();
A.getvalue();
A.printArea();
C.printCircum();
}
}
Output:

Akansha Dwivedi BCA 5th Sem Page 25


SUMMER TRAINING

Program-11

Objective: WAP to show the inheritance of constructor.


Source code:
class Number
{
int a;
Number()
{
a=50;
System.out.println("Class Number");
}}
class Value extends Number
{
int b;
Value()
{
b=70;
System.out.println("Default Constructor of class value");
}
Value(int x)

Akansha Dwivedi BCA 5th Sem Page 26


SUMMER TRAINING
{
b=x;
System.out.println("Parameterised Constructor of class value");
}
void showData()
{
System.out.println("a="+a);
System.out.println("b="+b);
}}
class ConstructorInheritance
{
public static void main(String args[])
{
Value V1=new Value();
Value V2=new Value();
V1.showData();
V2.showData();
}}
Output:

Akansha Dwivedi BCA 5th Sem Page 27


SUMMER TRAINING

Program-12

Objective: WAP to call constructor of current class using this


keyword.
Source code:
class Number
{
int a,b,c;
Number()
{
this(20);
Akansha Dwivedi BCA 5th Sem Page 28
SUMMER TRAINING
System.out.println("Constructor 1");
}
Number(int x)
{
this(x,30);
System.out.println("Constructor 2");
}
Number(int x,int y)
{
this(x,y,80);
System.out.println("Constructor 3");
}
Number(int x,int y,int z)
{
a=x;
b=y;
c=z;
System.out.println("Constructor 4");
}
void ShowData()
{
System.out.println("a= "+a);
System.out.println("b= "+b);
System.out.println("c= "+c);
}}
class This1
Akansha Dwivedi BCA 5th Sem Page 29
SUMMER TRAINING
{
public static void main(String args[])
{
Number N=new Number();
N.ShowData();
}}
Output:

Program-13
Objective: WAP to show in the base and derived class the
name of the variable can be same and also accessed by super
keyword.
Source code:
class Base
{
Akansha Dwivedi BCA 5th Sem Page 30
SUMMER TRAINING
int a=10;
float b=50.0f;
}

class derived extends Base


{
int a=70;
float b=50.5f;
void showdata()
{
System.out.println("Derived class Data");
System.out.println("a="+a);
System.out.println("b="+b);
System.out.println("Base Class Data");
System.out.println("a="+super.a);
System.out.println("b="+super.b);
}}
class Super1
{
public static void main(String args[])
{
derived D=new derived();
D.showdata();
}}

Akansha Dwivedi BCA 5th Sem Page 31


SUMMER TRAINING
Output:

Program-14

Akansha Dwivedi BCA 5th Sem Page 32


SUMMER TRAINING
Objective: WAP for demonstration of super keyword by
constructor.

Source Code:
class Company
{
String cname;
Company(String an)
{
cname=an;
System.out.println("Class Company");
}
void ShowCompany()
{
System.out.println("Company Name\t"+cname);
}}
class Department extends Company
{
String dname;
Department(String an,String dn)
{
super(an);
dname=dn;
System.out.println("Class Department");
}
void showDept()
{
this.ShowCompany();

Akansha Dwivedi BCA 5th Sem Page 33


SUMMER TRAINING
{
System.out.println("Department Name\t"+dname);
}
}}
class Employee extends Department
{
String ename;
Employee(String cname,String dname,String ename)
{
super(cname,dname);
this.ename=ename;
System.out.println("Class Employee");
}
void ShowEmp()
{
showDept();
System.out.println("Employee Name\t"+ename);
}}
class Super1
{
public static void main(String args[])
{
Employee emp=new Employee("Ananlyze","SD","William");
emp.ShowEmp();
}

Akansha Dwivedi BCA 5th Sem Page 34


SUMMER TRAINING

Output:

Akansha Dwivedi BCA 5th Sem Page 35


SUMMER TRAINING

Program-15

Objective: WAP to compute area of circle, triangle, rectangle


and square by using method overloading.
Source code:
import java.io.*;
class shapes
{
double ar;
void findArea(double r)
{
ar=Math.PI*r*r;
System.out.println("Area of circle="+ar);
}
void findArea(int a)
{
ar=a*a;
System.out.println("Area of square="+ar);
}
void findArea(int l,int b)

Akansha Dwivedi BCA 5th Sem Page 36


SUMMER TRAINING
{
ar=l*b;
System.out.println("Area of rectangle="+ar);
}
void findArea(int a,int b,int c)
{
double s=0.0;
s=(a+b+c)/2.0;
ar=Math.sqrt(s*(s-a)*(s-b)*(s-c));
System.out.println("Area of triangle="+ar);
}}
class runShapes
{
public static void main(String args[])throws IOException
{
shapes obj=new shapes();
obj.findArea(5.3f);
obj.findArea(4);
obj.findArea(2,4);
obj.findArea(3,3,3);
}}
Output:

Akansha Dwivedi BCA 5th Sem Page 37


SUMMER TRAINING

Program-16

Objective: WAP to show that using super keyword we can get


the actual definition of base class which is hidden by derived class.
Source code:
class base
{
void display()
{
System.out.println("This msg is printed from base class");
}}
class derived extends base

Akansha Dwivedi BCA 5th Sem Page 38


SUMMER TRAINING
{
void display()
{
System.out.println("This msg is printed from derived class");
}
void msg()
{
display();
super.display();
}}
class super3
{
public static void main(String args[])
{
derived D=new derived();
D.msg(); }}
Output:

Akansha Dwivedi BCA 5th Sem Page 39


SUMMER TRAINING

Program-17

Objective: WAP to show the demonstration of exception.


Source code:
class Exception1
{
public static void main(String args[])
{
int a,b,R;

Akansha Dwivedi BCA 5th Sem Page 40


SUMMER TRAINING
a=10;
b=0;
R=a+b;
System.out.println("Addition= "+R);
R=a/b;
System.out.println("Division= "+R);
R=a-b;
System.out.println("Subtraction= "+R);
R=a*b;
System.out.println("Multiplication= "+R);
}
}
Output:

Program-18

Objective: WAP to show the demonstration of try with finally.


Source code:
class Exception2
{

Akansha Dwivedi BCA 5th Sem Page 41


SUMMER TRAINING
public static void main(String args[])
{
int a,b,R;
a=10;
b=0;
R=a+b;
System.out.println("Addition= "+R);
try
{
R=a/b;
System.out.println("Division= "+R);
}
finally
{
R=a-b;
System.out.println("Subtraction= "+R);
R=a*b;
System.out.println("Multiplication= "+R);
}
System.out.println("End of program");
}
}

Output:

Akansha Dwivedi BCA 5th Sem Page 42


SUMMER TRAINING

Akansha Dwivedi BCA 5th Sem Page 43


SUMMER TRAINING
Program-19

Objective: WAP to show the demonstration of try with catch.


Source code:
class Exception3
{
public static void main(String args[])
{
int a,b,R;
a=10;
b=0;
try
{
R=a+b;
System.out.println("Addition= "+R);
R=a/b;
System.out.println("Division= "+R);
}
catch(ArithmeticException e)
{
System.out.println("MSG1: Can not divide by zero");
System.out.println("MSG2:"+ e.getMessage());
System.out.println("MSG3: "+e);
System.out.println("MSG4:");
e.printStackTrace();

Akansha Dwivedi BCA 5th Sem Page 44


SUMMER TRAINING
}
R=a-b;
System.out.println("Subtraction= "+R);
R=a*b;
System.out.println("Multiplication= "+R);
System.out.println("End of program");
}
}
Output:

Akansha Dwivedi BCA 5th Sem Page 45


SUMMER TRAINING

Program-20

Objective: WAP to show the demonstration of try, catch,


finally.
Source code:
class Exception4
{
public static void main(String args[])
{
int a,b,R;
a=20;
b=0;
R=a+b;
System.out.println("Addition= "+R);
try
{
R=a/b;
System.out.println("Division= "+R);
}
catch(ArithmeticException ae)
{

Akansha Dwivedi BCA 5th Sem Page 46


SUMMER TRAINING
System.out.println("Error: Can not be divided by zero");
System.out.println(args[4]);
}
finally
{
R=a-b;
System.out.println("Subtraction= "+R);
R=a*b;
System.out.println("Multiplication= "+R);
}
System.out.println("End of Program");
}
}
Output:

Akansha Dwivedi BCA 5th Sem Page 47


SUMMER TRAINING

Program-21

Objective: WAP to show the demonstration of throw clause.


Source code:
class testThrow
{
public static void main(String args[])
{
int a=10,b=0,r;
try
{
if(b==0)
{
ArithmeticException e=new ArithmeticException();
throw e;

Akansha Dwivedi BCA 5th Sem Page 48


SUMMER TRAINING
}
else
{
r=a/b;
System.out.println("Division="+r);
}}
catch(ArithmeticException e)
{
System.out.println("Cannot divided by zero");
}}}
Output:

Program-22

Objective: WAP to show the demonstration of throws clause.


Source code:
import java.io.*;
class testThrows
{
public static void main(String args[])throws IOException
{
int a,b,r=0;
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter first number: ");
Akansha Dwivedi BCA 5th Sem Page 49
SUMMER TRAINING
a=Integer.parseInt(br.readLine());
System.out.println("Enter second number: ");
b=Integer.parseInt(br.readLine());
r=a/b;
System.out.println("Division of two numbers is: "+r);
}
}
Output:

Program-23

Objective: WAP to show the demonstration multithreading by


extending thread class.
Source code:
class natural extends Thread
{
int i,n;
public void run()
{
n=5;
Akansha Dwivedi BCA 5th Sem Page 50
SUMMER TRAINING
for(i=0;i<=n;i++)
{
System.out.println("NATURAL\t"+i);
}
System.out.println("End of Natural Series");
}}
class even extends Thread
{
int i,n;
public void run()
{
n=10;
for(i=2;i<=n;i++)
{
if(i%2==0)
System.out.println("EVEN\t"+i);
}
System.out.println("End of Even Series");
}}
class odd extends Thread
{
int i,n;
public void run()
{
n=10;
for(i=1;i<=n;i++)
Akansha Dwivedi BCA 5th Sem Page 51
SUMMER TRAINING
{
if(i%2!=0)
System.out.println("ODD\t"+i);
}
System.out.println("End of Odd Series");
}}
class Thread1
{
public static void main(String args[])
{
natural N=new natural();
even E=new even();
odd O=new odd();
N.start();
E.start();
O.start();
}}

Output:

Akansha Dwivedi BCA 5th Sem Page 52


SUMMER TRAINING

Akansha Dwivedi BCA 5th Sem Page 53


SUMMER TRAINING
Program-24

Objective: WAP to show the demonstration of thread priority.


Source code:
class Natural extends Thread
{
int i,n;
public void run()
{
n=10;
System.out.println("Natural series");
for(i=0;i<=n;i++)
{
System.out.println("Natural\t"+i);
}
System.out.println("End of Natural series");
}}
class Even extends Thread
{
int i,n;
public void run()
{
n=10;
System.out.println("Even series");
for(i=0;i<=n;i+=2)

Akansha Dwivedi BCA 5th Sem Page 54


SUMMER TRAINING
{
System.out.println("Even\t"+i);
}
System.out.println("End of Even series");
}}
class Odd extends Thread
{
int i,n;
public void run()
{
n=10;
System.out.println("Odd series");
for(i=0;i<=n;i+=2)
{
System.out.println("Odd\t"+i);
}
System.out.println("End of Odd series");
}}
class Priority
{
public static void main(String args[])
{
Natural N=new Natural();
Even E=new Even();
Odd O=new Odd();
System.out.println("Name of 1 thread\t"+N.getName());
Akansha Dwivedi BCA 5th Sem Page 55
SUMMER TRAINING
System.out.println("Name of 2 thread\t"+E.getName());
System.out.println("Name of 3 thread\t"+O.getName());
System.out.println("Priority of thread1\t"+N.getPriority());
System.out.println("Priority of thread2\t"+E.getPriority());
System.out.println("Priority of thread3\t"+O.getPriority());

System.out.println("Natural\t"+N);
N.setName("Natural");
E.setName("Even");
O.setName("Odd");
N.setPriority(Thread.MAX_PRIORITY);
O.setPriority(2);
System.out.println("Name of 1 thread\t"+N.getName());
System.out.println("Name of 2 thread\t"+E.getName());
System.out.println("Name of 3 thread\t"+O.getName());
System.out.println("Priority of thread1\t"+N.getPriority());
System.out.println("Priority of thread2\t"+E.getPriority());
System.out.println("Priority of thread3\t"+O.getPriority());
System.out.println("Natural\t"+N);
N.start();
E.start();
O.start();
}}

Akansha Dwivedi BCA 5th Sem Page 56


SUMMER TRAINING

Output:

Akansha Dwivedi BCA 5th Sem Page 57


SUMMER TRAINING

Program-25

Objective: WAP to print 1 to n natural number and make sure


difference of one sec between printing of 2 elements.
Source code:
class NaturalThread extends Thread
{
int i,n;
public void run()
{
n=100;
for(i=1;i<=n;i++)
{
System.out.print(i);
try
{
sleep(1000);
}

Akansha Dwivedi BCA 5th Sem Page 58


SUMMER TRAINING
catch(Exception e)
{
}
System.out.print("\r");
}
}
public static void main(String args[])
{
new NaturalThread().start();
}
}

Output:

Akansha Dwivedi BCA 5th Sem Page 59


SUMMER TRAINING

Program-26

Objective: WAP to input 2 array of 10 elements and merge


these array with the help of another array of size 20.
Source code:
import java.io.*;
class merge
{
public static void main(String args[])throws IOException
{
int a[]=new int [10];
int b[]=new int [10];
int c[]=new int [20];

Akansha Dwivedi BCA 5th Sem Page 60


SUMMER TRAINING
int i,index=0;
InputStreamReader ir=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(ir);
System.out.println("Enter the elements of first array" );
for(i=0;i<10;i++)
{
a[i]=Integer.parseInt(br.readLine());
}
System.out.println("Enter the elements of second array" );
for(i=0;i<10;i++)
{
b[i]=Integer.parseInt(br.readLine());
}
System.out.println("Merged array is=");
for(i=0;i<10;i++)
{
c[index]=a[i];
index++;
}
for(i=0;i<10;i++)
{
c[index]=b[i];
index++;
}
for(i=0;i<20;i++)
{
System.out.println(c[i]);
}}}
Output:
Akansha Dwivedi BCA 5th Sem Page 61
SUMMER TRAINING

Program-27

Objective: WAP to a matrix of 4X5 and print the sum of all row.
Source code:
import java.io.*;
class RowSum
{
public static void main(String args[])throws IOException

Akansha Dwivedi BCA 5th Sem Page 62


SUMMER TRAINING
{
int a[][]=new int[4][5];
int i,j,rs=0;
InputStreamReader ir=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(ir);
System.out.println("Enter the elements of array" );
for(i=0;i<4;i++)
{
for(j=0;j<5;j++)
{
a[i][j]=Integer.parseInt(br.readLine());
}}
System.out.println(" The elements of array=" );
for(i=0;i<4;i++)
{
for(j=0;j<5;j++)
{
System.out.print(a[i][j]+"\t");
}
System.out.print("\n");
}
//sum of all rows....
for(i=0;i<4;i++)
{
for(j=0;j<5;j++)
{
Akansha Dwivedi BCA 5th Sem Page 63
SUMMER TRAINING
rs=rs+a[i][j];
}
System.out.println("Sum of row"+i"="+rs);
rs=0;
}}}
Output:

Program-28

Objective: WAP to demonstrate the abstract method of class..


Source code:
import java.io.*;
Akansha Dwivedi BCA 5th Sem Page 64
SUMMER TRAINING
abstract class base
{
int a=10;
void showBase()
{
System.out.println("This is base class" );
}
abstract void display();
abstract void msg();
}
class derived extends base
{
int b=20;
void showderived()
{
System.out.println("This is derived class" );
}
void display()
{
System.out.println("a="+a);
System.out.println("b="+b);
}
void msg()
}}}
class testabstract
{
Akansha Dwivedi BCA 5th Sem Page 65
SUMMER TRAINING
public static void main(String args[])
{
derived D=new derived();
D.showBase();
D.showderived();
D.display();
}
}
Output:

Program-29
Akansha Dwivedi BCA 5th Sem Page 66
SUMMER TRAINING
Objective: WAP to demonstrate the static variable and
method.
Source code:
class employee
{
String ename;
int ecode;
float esal;
static int count=0;
void getemp(String name,int code,float sal)
{
ename=name;
ecode=code;
esal=sal;
count++;
}
void putemp()
{
System.out.println("Name\t"+ename);
System.out.println("Code\t"+ecode);
System.out.println("Salary\t"+esal);
}
static void showcount()
{
System.out.println("total employee="+count);

Akansha Dwivedi BCA 5th Sem Page 67


SUMMER TRAINING
}}

class TestStatic
{
static int a=30;
int b=50;
public static void main(String args[])
{
int c=70;
System.out.println("a="+a);
//System.out.println("b="+b);error
System.out.println("c="+c);
System.out.println("count="+employee.count);
employee.showcount();
employee emp1=new employee();
employee emp2=new employee();
employee emp3=new employee();
emp1.getemp("Aastha",1020,55000.0f);
emp2.getemp("Aakansha",1021,75000.0f);
emp3.getemp("Shipra",1022,95000.0f);
emp1.putemp();
emp2.putemp();
emp3.putemp();
employee.showcount();
emp2.showcount();
}}
Akansha Dwivedi BCA 5th Sem Page 68
SUMMER TRAINING

Output:

Akansha Dwivedi BCA 5th Sem Page 69


SUMMER TRAINING

Program-30

Objective: WAP to demonstrate the static block.


Source code:
public class staticExample
{
static int a=10;
static
{
System.out.println("This is first static block");
}
public staticExample()
{
System.out.println("This is constructor");
}
public static String staticstring="Static Variable";
static
{
System.out.println("This is second static block"+staticstring);
}
Akansha Dwivedi BCA 5th Sem Page 70
SUMMER TRAINING
static
{
staticMethod();
System.out.println("This is third static block");
}
public static void staticMethod()
{
System.out.println("This is static method");
}
public static void staticMethod2()
{
System.out.println("This is static method2");
}
public static void main(String args[])
{
System.out.println("This is main method");
System.out.println("a="+a);
staticExample se=new staticExample();
staticExample.staticMethod2();
se.staticMethod();
}
static
{
System.out.println("This is fourth static block");
}
}
Akansha Dwivedi BCA 5th Sem Page 71
SUMMER TRAINING
Output:

Program-31

Objective: WAP for demonstration of a class can extends a


class and implements an interface at a time.

Source Code:
import java.io.*;
interface College
{
String cname="SHERWOOD COLLEGE";
String code="1020";
String city="LUCKNOW";
void printstudentdetail();
void msg();
}
class Department
{

Akansha Dwivedi BCA 5th Sem Page 72


SUMMER TRAINING
String dname;
int dcode;
void getDept(String s1,int s2)
{
dname=s1;
dcode=s2;
}}
class Student extends Department implements College
{
String sname,scourse,ssem;
void InputStudentDetail()throws IOException
{
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter Student Name, Course, Semester");
sname=br.readLine();
scourse=br.readLine();
ssem=br.readLine();
}
public void printstudentdetail()
{
System.out.println("College Name\t"+cname);
System.out.println("College Code\t"+code);
System.out.println("Department Name\t"+dname);
System.out.println("Department Code\t"+dcode);
System.out.println("Student Name\t"+sname);
System.out.println("Course\t"+scourse);
System.out.println("Semester\t"+ssem);
}

Akansha Dwivedi BCA 5th Sem Page 73


SUMMER TRAINING
public void msg()
{
System.out.println("This is Lucknow");
}}
public class Interface2
{
public static void main(String args[])throws IOException
{
System.out.println(College.cname);
Student st=new Student();
st.printstudentdetail();
st.getDept("Computer Science",1002);
st.printstudentdetail();
}}

Output:

Akansha Dwivedi BCA 5th Sem Page 74


SUMMER TRAINING

Akansha Dwivedi BCA 5th Sem Page 75


SUMMER TRAINING
Program-32

Objective: WAP for demonstration of an interface that can


extend another interface.

Source code:
interface University
{
String uname="AKTU";
String city="LUCKNOW";
void putuniversity();
}
interface UGC
{
String ugc="University Grant Commission";
void show();
}
interface College extends University, UGC
{
String cname="SHERWOOD";
String city="LUCKNOW";
void putcollege();
}
class Student implements College
{
String sname,scourse;
static void display()
{

Akansha Dwivedi BCA 5th Sem Page 76


SUMMER TRAINING
System.out.println("Hello");
}
void getstudent(String str1, String str2)
{
sname=str1;
scourse=str2;
}
void putstudent()
{
Student.display();
System.out.println("Student Name\t"+sname);
System.out.println("Course\t"+scourse);
}
public void putuniversity()
{
System.out.println("Unversity Name\t"+uname);
System.out.println("City\t"+city);
}
public void putcollege()
{
System.out.println("College Name\t"+cname);
System.out.println("City\t"+city);
}
public void show()
{
}
}
class Interface3
{

Akansha Dwivedi BCA 5th Sem Page 77


SUMMER TRAINING
public static void main(String args[])
{
Student stu=new Student();
stu.getstudent("James","MCA");
stu.putuniversity();
stu.putcollege();
stu.putstudent();
}}
Output:

Akansha Dwivedi BCA 5th Sem Page 78


SUMMER TRAINING

Program-33

Objective: WAP for demonstration of a class can implement


more than one interfaces.

Source code:
interface Company
{
String cname="ANALYZE";
String city="LUCKNOW";
void putcompany();
}
interface Department
{
String dname="SD";
String dcode="ai1020";
void putdept();
}
class Employee implements Company, Department
{

Akansha Dwivedi BCA 5th Sem Page 79


SUMMER TRAINING
String ename,epost;
void getemp(String str1,String str2)
{
ename=str1;
epost=str2;
}
public void putdept()
{
System.out.println("Department Name\t"+dname);
System.out.println("Department Code\t"+dcode);
}
public void putcompany()
{
System.out.println("Company Name\t"+cname);
System.out.println("City\t"+city);
}
void putemp()
{
System.out.println("Employee Name\t"+ename);
System.out.println("Post\t"+epost);
}
}
class Interface4
{
public static void main(String args[])
{
Employee emp=new Employee();
emp.getemp("James","SE");
emp.putemp();

Akansha Dwivedi BCA 5th Sem Page 80


SUMMER TRAINING
emp.putcompany();
emp.putdept();
}
}
Output:

Program-34

Objective: WAP to create a digital clock.


Source code:
import java.util.*;
class DigitalClock extends Thread
{
int h,m,s;
String time;
String check(int n)
{
if(n>9)
return ""+n;
Akansha Dwivedi BCA 5th Sem Page 81
SUMMER TRAINING
else
return "0"+n;
}
public void run()
{
for(;;)
{
Calendar c=Calendar.getInstance();
h=c.get(Calendar.HOUR);
m=c.get(Calendar.MINUTE);
s=c.get(Calendar.SECOND);
time=check(h)+":"+check(m)+":"+check(s);
System.out.print("TIME\t"+time);
try
{
sleep(1000);
}
catch(Exception e)
{
}
System.out.print("\r");
}
}
public static void main(String args[])
{
new DigitalClock().start();
Akansha Dwivedi BCA 5th Sem Page 82
SUMMER TRAINING
}
}
Output:

Program-35

Objective: WAP to create a user defined package of AP, GP


series and Area, include it in the current program.
Source code:
//Package 1 for the AP and GP series
package userpack;
import java.util.Date;

Akansha Dwivedi BCA 5th Sem Page 83


SUMMER TRAINING
public class seriesX
{
int i,t;
public void printAP(int a,int d,int n)
{
System.out.println("AP Series=");
for(i=0;i<=n;i++)
{
t=a+(i*d);
System.out.println(t);
}
System.out.println("End of AP Series");
}
public void printGP(int a,int r,int n)
{
System.out.println("GP Series=");
for(i=0;i<=n;i++)
{
t=(int)(a*Math.pow(r,i));
System.out.println(t);
}
System.out.println("End of GP Series");
}
public void showdate()
{
Date d=new Date();
Akansha Dwivedi BCA 5th Sem Page 84
SUMMER TRAINING
System.out.println(d);
}
}
//Package 2 for the area of circle and rectangle
package userpack;
public class AreaX
{
static double ar;
public static double areaCircle(double r)
{
ar=Math.PI*r*r;
return ar;
}
public static double areaRectangle(double a,double b)
{
ar=a*b;
return ar;
}
}
//Including both packages in this program
import userpack.*;
class testPacakage1
{
public static void main(String args[])
{
seriesX S=new seriesX();
Akansha Dwivedi BCA 5th Sem Page 85
SUMMER TRAINING
S.showdate();
S.printAP(5,4,10);
S.printGP(3,2,10);
System.out.println("Area of Circle="+AreaX.areaCircle(7.7));
System.out.println("Area of
rectangle="+AreaX.areaRectangle(2.7,8.9));
}
}
Output:

Program-36

Objective: WAP to demonstrate the applet.


Akansha Dwivedi BCA 5th Sem Page 86
SUMMER TRAINING
Source code:
import java.applet.Applet;
import java.awt.Graphics;
public class applet1 extends Applet
{
public void paint(Graphics g)
{
g.drawString("Welcome in Applet Programming",20,100);
}
}
/*<applet code=applet1 width=300 height=200>
</applet>*/
Output:

Program-37
Akansha Dwivedi BCA 5th Sem Page 87
SUMMER TRAINING
Objective: WAP to demonstrate the setting of background
color, font and foreground color.
Source code:
import java.applet.*;
import java.awt.*;
public class Applet2 extends Applet
{
public void init()
{
Color c=new Color(250,100,10);
Font f=new Font("Viner Hand ITC",Font.PLAIN,30);
setBackground(Color.gray);
setForeground(c);
setFont(f);
}
public void paint(Graphics g)
{
g.drawString("Welcome to Applet Programming",20,100);
}
}
/*<Applet code=Applet2 width=400 height=200>
</Applet>*/

Akansha Dwivedi BCA 5th Sem Page 88


SUMMER TRAINING

Output:

Akansha Dwivedi BCA 5th Sem Page 89


SUMMER TRAINING

Program-38

Objective: WAP to demonstrate the graphics class and draw


the face.
Source code:
import java.awt.*;
import java.applet.*;
public class face extends Applet
{
public void paint(Graphics g)
{
g.setColor(Color.red);
g.fillOval(20,20,100,100);
g.setColor(Color.white);
//making of eyes
g.drawOval(45,40,15,15);
g.drawOval(80,40,15,15);
//filling of eyes
g.fillOval(50,45,5,5);
g.fillOval(85,45,5,5);
Akansha Dwivedi BCA 5th Sem Page 90
SUMMER TRAINING
//making of nose
g.fillOval(65,60,10,10);
//making of smile
g.fillArc(45,60,50,45,180,180);
}
}
//<applet code=face width=200 height=200></applet>

Output:

Akansha Dwivedi BCA 5th Sem Page 91


SUMMER TRAINING

Program-39

Objective: WAP to create a login page using applet.


Source code:
public class LoginApplet extends Applet
{
Label l1,l2;
TextField t1,t2;
Button b1,b2;
public void init()
{
l1=new Label("User Name");
l2=new Label("Password");
t1=new TextField(20);
t2=new TextField(20);
t2.setEchoChar('*');
b1=new Button("SUBMIT");
b2=new Button("RESET");
add(l1);
add(t1);
add(l2);
Akansha Dwivedi BCA 5th Sem Page 92
SUMMER TRAINING
add(t2);
add(b1);
add(b2);
setBackground(Color.green);
}
}
//<applet code=LoginApplet width=250 height=200></applet>
Output:

Akansha Dwivedi BCA 5th Sem Page 93


SUMMER TRAINING

Program-40

Objective: WAP to demonstrate border layout.

Source code:
import java.awt.*;
class BorderLayoutTest extends Frame
{
Button b1,b2,b3,b4,b5,b6;
BorderLayoutTest()
{
setBackground(Color.gray);
b1=new Button("Button1");
b2=new Button("Button2");
b3=new Button("Button3");
b4=new Button("Button4.....................................");
b5=new Button("Button5");
b6=new Button("Button6");
add(b1,BorderLayout.NORTH);
Akansha Dwivedi BCA 5th Sem Page 94
SUMMER TRAINING
add(b2,BorderLayout.SOUTH);
add(b3,BorderLayout.EAST);
add(b4,BorderLayout.CENTER);
add(b5,BorderLayout.WEST);
add(b6);
setSize(500,300);
setVisible(true);
}
public static void main(String args[])
{
new BorderLayoutTest();
}
}
Output:

Akansha Dwivedi BCA 5th Sem Page 95


SUMMER TRAINING

Program-41

Objective: WAP for demonstration of Action Event and listener


in Event Handling.
Source code:
import java.awt.*;
import java.awt.event.*;
class ArithGui extends Frame implements ActionListener
{
Label l1,l2,l3;
TextField t1,t2,t3;
Button b1,b2,b3,b4;
ArithGui()
{

Akansha Dwivedi BCA 5th Sem Page 96


SUMMER TRAINING
l1=new Label("Value1");
l2=new Label("Value2");
l3=new Label("Result");
t1=new TextField();
t2=new TextField();
t3=new TextField();
b1=new Button("Addittion");
b2=new Button("Substraction");
b3=new Button("Multiplication");
b4=new Button("Division");
add(l1);
add(l2);
add(l3);
add(t1);
add(t2);
add(t3);
add(b1);
add(b2);
add(b3);
add(b4);
setLayout(null);
l1.setBounds(30,30,100,30);
t1.setBounds(140,30,100,30);
l2.setBounds(30,80,100,30);
t2.setBounds(140,80,100,30);
l3.setBounds(30,130,100,30);
t3.setBounds(140,130,100,30);
b1.setBounds(30,180,100,30);
b2.setBounds(140,180,100,30);

Akansha Dwivedi BCA 5th Sem Page 97


SUMMER TRAINING
b3.setBounds(30,230,100,30);
b4.setBounds(140,230,100,30);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
setSize(700,500);
setVisible(true);
}
public void actionPerformed(ActionEvent ae)
{
double a,b,res;
a=Double.parseDouble(t1.getText());
b=Double.parseDouble(t1.getText());
if(ae.getSource()==b1)
{
res=a+b;
t3.setText(String.valueOf(res));
l3.setText("Addition");
}
else
if(ae.getSource()==b2)
{
res=a-b;
t3.setText(String.valueOf(res));
l3.setText("Subtraction");
}
else
if(ae.getSource()==b3)

Akansha Dwivedi BCA 5th Sem Page 98


SUMMER TRAINING
{
res=a*b;
t3.setText(String.valueOf(res));
l3.setText("Multiplication");
}
else
{
res=a/b;
t3.setText(String.valueOf(res));
l3.setText("Division");
}}
public static void main(String args[])
{
new ArithGui();
}}

Output:

Akansha Dwivedi BCA 5th Sem Page 99


SUMMER TRAINING

Akansha Dwivedi BCA 5th Sem Page 100


SUMMER TRAINING
Program-42
Objective: WAP to demonstrate the awt component.
Source code:
import java.awt.*;
public class awtcop extends Frame
{
public awtcop()
{
super("AWT Component Testing");
TextArea t=new TextArea();
setLayout(new FlowLayout());
setBackground(Color.cyan);
Checkbox c1=new Checkbox("MCA");
Checkbox c2=new Checkbox("BCA");
Checkbox c3=new Checkbox("B.Tech");
Checkbox c4=new Checkbox("M.Tech");
add(c1);
add(c2);
add(c3);
add(c4);
CheckboxGroup cbg1=new CheckboxGroup();
Checkbox m=new Checkbox("Male",cbg1,false);
Checkbox f=new Checkbox("Female",cbg1,false);
Choice city=new Choice();
city.add("LUCKNOW");
Akansha Dwivedi BCA 5th Sem Page 101
SUMMER TRAINING
city.add("LUCKNOW");
city.add("SITAPUR");
city.add("LUCKNOW");
city.add("LUCKNOW");
city.add("KANPUR");
city.add("LUCKNOW");
city.add("LUCKNOW");
city.add("KANPUR");
List l=new List();
for(int i=1;i<=20;i++)
l.addItem(""+i);
add(l);
add(m);
add(f);
add(city);
add(t);
setSize(1000,700);
setVisible(true);
}
public static void main(String args[])
{
new awtcop();
}
}

Akansha Dwivedi BCA 5th Sem Page 102


SUMMER TRAINING

Output:

Akansha Dwivedi BCA 5th Sem Page 103


SUMMER TRAINING

Program-43

Objective: WAP to create a notepad.


Source code:
import java.awt.*;
class Notepad1
{
MenuBar mbar;
Menu file,edit,format,help;
MenuItem fnew,open,save,saveAs,page,exit,print;
MenuItem undo,cut,copy,paste;
MenuItem wordwrap,font;
MenuItem about,topics;
TextArea ta;
Frame f;
Notepad1()
{
f=new Frame("AWT: NOTEPAD");

Akansha Dwivedi BCA 5th Sem Page 104


SUMMER TRAINING
mbar=new MenuBar();
f.setMenuBar(mbar);
ta=new TextArea();
f.add(ta);
file=new Menu("File");
edit=new Menu("Edit");
format=new Menu("Format");
help=new Menu("Help");
fnew=new MenuItem("New");
open=new MenuItem("Open");
save=new MenuItem("Save");
saveAs=new MenuItem("SaveAs");
page=new MenuItem("Page Setup...");
print=new MenuItem("Print");
exit=new MenuItem("Exit");
undo=new MenuItem("Undo");
cut=new MenuItem("Cut");
copy=new MenuItem("Copy");
paste=new MenuItem("Paste");
wordwrap=new MenuItem("Word Wrap");
font=new MenuItem("Font");
about=new MenuItem("About Notepad");
topics=new MenuItem("Help Topics");
file.add(fnew);
file.add(open);
file.add(save);
Akansha Dwivedi BCA 5th Sem Page 105
SUMMER TRAINING
file.add(saveAs);
file.add(page);
file.add(print);
file.add(exit);
edit.add(undo);
edit.add(cut);
edit.add(copy);
edit.add(paste);
format.add(wordwrap);
format.add(font);
help.add(about);
help.add(topics);
mbar.add(file);
mbar.add(edit);
mbar.add(format);
mbar.add(help);
f.setVisible(true);
f.setSize(500,500);
}
public static void main(String args[])
{
new Notepad1();
}
}
Output:

Akansha Dwivedi BCA 5th Sem Page 106


SUMMER TRAINING

Program-45

Objective: WAP to close a frame after click on close button.


Source code:
import java.awt.*;
import java.awt.event.*;
class windowevent1 extends Frame implements WindowListener
{
Dialog d;
Label l;
Button b1,b2;

Akansha Dwivedi BCA 5th Sem Page 107


SUMMER TRAINING
windowevent1()
{
setSize(1024,768);
setBackground(Color.cyan);
setVisible(true);
addWindowListener(this);
d=new Dialog(this,"Confirmation");
l=new Label("Do you want to Exit(yes/no)");
b1=new Button("yes");
b2=new Button("cancel");
d.setLayout(null);
d.add(l);
d.add(b1);
d.add(b2);
l.setBounds(20,50,150,20);
b1.setBounds(50,100,50,20);
b2.setBounds(120,100,50,20);
d.setSize(220,150);
d.setLocation(400,300);
b1.addActionListener(new buttonevent());
b2.addActionListener(new buttonevent());
}
public void windowOpened(WindowEvent we)
{
}
public void windowClosing(WindowEvent we)
Akansha Dwivedi BCA 5th Sem Page 108
SUMMER TRAINING
{
d.setVisible(true);
}
public void windowClosed(WindowEvent we)
{
}
public void windowIconified(WindowEvent we)
{
}
public void windowDeiconified(WindowEvent we)
{
}
public void windowActivated(WindowEvent we)
{
}
public void windowDeactivated(WindowEvent we)
{
}
class buttonevent implements ActionListener//inner class
{
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==b1)
{
System.exit(0);
}
Akansha Dwivedi BCA 5th Sem Page 109
SUMMER TRAINING
else
if(ae.getSource()==b2)
{
d.setVisible(false);
}
}
}
public static void main(String args[])
{
new windowevent1();
}
}

Output:

Akansha Dwivedi BCA 5th Sem Page 110


SUMMER TRAINING

Program-46

Akansha Dwivedi BCA 5th Sem Page 111


SUMMER TRAINING
Objective: WAP to demonstrate the mouse event to change
the color of screen by clicking on screen.
Source code:
import java.awt.*;
import java.awt.event.*;
class MouseEventDemo extends Frame implements
MouseListener
{
MouseEventDemo()
{
setTitle("Mouse Event Example");
setBackground(Color.yellow);
setSize(700,500);
setVisible(true);
addMouseListener(this);
}
public void mouseEntered(MouseEvent me)
{
setBackground(Color.blue);
}
public void mouseExited(MouseEvent me)
{
setBackground(Color.red);
}
public void mouseClicked(MouseEvent me)

Akansha Dwivedi BCA 5th Sem Page 112


SUMMER TRAINING
{
setBackground(Color.green);
}
public void mousePressed(MouseEvent me)
{
setBackground(Color.black);
}
public void mouseReleased(MouseEvent me)
{
setBackground(Color.white);
}
public static void main(String args[])
{
new MouseEventDemo();
}
}
Output:-

Akansha Dwivedi BCA 5th Sem Page 113


SUMMER TRAINING

Akansha Dwivedi BCA 5th Sem Page 114


SUMMER TRAINING

Program-47

Objective: WAP to set the RGB on the screen and by scrolling


their range change the color of screen.
Source code:
import java.awt.*;
import java.awt.event.*;
public class scroll extends Frame implements AdjustmentListener
{

Akansha Dwivedi BCA 5th Sem Page 115


SUMMER TRAINING
Scrollbar s1,s2,s3;
Label l1,l2,l3;
public scroll()
{
super("Scroll Bar");
s1=new Scrollbar(Scrollbar.HORIZONTAL,0,1,0,255);
s2=new Scrollbar(Scrollbar.HORIZONTAL,10,2,0,255);
s3=new Scrollbar(Scrollbar.HORIZONTAL,5,1,0,255);
setLayout(null);
l1=new Label("RED");
l2=new Label("GREEN");
l3=new Label("BLUE");
add(l1);
add(s1);
add(l2);
add(s2);
add(l3);
add(s3);
l1.setBounds(20,50,50,30);
s1.setBounds(70,50,250,30);
l2.setBounds(10,100,50,30);
s2.setBounds(70,100,250,30);
l3.setBounds(10,150,50,30);
s3.setBounds(70,150,250,30);
s1.addAdjustmentListener(this);
s2.addAdjustmentListener(this);
Akansha Dwivedi BCA 5th Sem Page 116
SUMMER TRAINING
s3.addAdjustmentListener(this);
setSize(500,500);
setVisible(true);
}
public void adjustmentValueChanged(AdjustmentEvent ae)
{
int r,g,b;
r=s1.getValue();
g=s2.getValue();
b=s3.getValue();
Color c=new Color(r,g,b);
String s="red:"+r+"Green:"+g+"Blue:"+b;
setTitle(s);
setBackground(c);
}
public static void main(String ar[])
{
new scroll();
}
}

Output:

Akansha Dwivedi BCA 5th Sem Page 117


SUMMER TRAINING

Akansha Dwivedi BCA 5th Sem Page 118


SUMMER TRAINING

Akansha Dwivedi BCA 5th Sem Page 119

You might also like