final java program
final java program
Aim:
To write a java program for swapping two numbers.
Coding:
class swap
{
public static void main(String args[])
{
float a=10.5f;
float b=20.6f;
float t;
System.out.println("Before swap");
System.out.println(a);
System.out.println(b);
t=a;
a=b;
b=t;
System.out.println("After swap");
System.out.println(a);
System.out.println(b);
}
}
Output:
D:\IIcs>java swap
Before swap
10.5
20.6
After swap
20.6
10.5
Result:
Thus the program was executed and run successfully.
Ex no:2
Temperature conversion
Date:12.12.24
Aim:
To write a java program for temperature conversion
Coding:
class temp
{
public static void main(String args[])
{
int c;
int f=23;
c=(f-32)*5/9;
System.out.println("Celcius:"+c);
f=(c+32)*9/5;
System.out.println("Fahrenheat:"+f);
}
}
Output:
D:\IIcs>java temp
Celcius:-5
Fahrenheat:48
Result:
Thus the program was executed and run successfully.
Ex no:3
Student details using class and objects
Date:16.12.24
Aim:
To write a java program for student details using class and
objects
Coding:
class student
{
String name;
int rno;
public void display()
{
System.out.println("Name:"+name);
System.out.println("Rollno:"+rno);
}
public static void main(String args[])
{
student s=new student();
s.name="AAA";
s.rno=10;
s.display();
}
}
Output:
D:\IIcs>java student
Name:AAA
Rollno:10
Result:
Thus the program was executed and run successfully.
Ex No:4
Invoice bill using Class and Object
Date:16.12.24
Aim:
To write a java program for incoice bill using Class and
Object
Coding:
import java.io.*;
import java.lang.*;
class invoice
{
int pid,qty;
String pname;
Double price,bill;
DataInputStream ob=new DataInputStream(System.in);
void get()throws IOException
{
System.out.println("Enter the product id:");
pid=Integer.parseInt(ob.readLine());
System.out.println("Enter the product name:");
pname=ob.readLine();
System.out.println("Enter the quality:");
qty=Integer.parseInt(ob.readLine());
System.out.println("Enter the price:");
price=Double.parseDouble(ob.readLine());
}
void calc()
{
bill=qty*price;
}
void put()
{
System.out.println("\n\nBill amount is"+bill);
}
}
class invoicebill
{
public static void main(String args[])throws IOException
{
System.out.println("********************");
System.out.println("INVOICEBILL USING CLASS AND OBJECT");
System.out.println("****************************************
*");
invoice inv=new invoice();
inv.get();
inv.calc();
inv.put();
}
}
Output
D:\IIcs>java invoicebill
********************
INVOICEBILL USING CLASS AND OBJECT
*****************************************
Enter the product id:
1234
Enter the product name:
apple
Enter the quality:
5
Enter the price:
20
Bill amount is100.0
Result:
Thus the program was executed and run successfully.
Ex.no:5
Check two string are anagram or not
Date:19.12.24
Aim:
Two write a java program for check two string are anagram or
not.
Coding:
import java.io.*;
import java.util.*;
class anagram
{
public static void main(String args[])throws IOException
{
String s1,s2;
DataInputStream ob= new DataInputStream(System.in);
System.out.println("*****");
System.out.println("CHECK TWO STRING ARE ANAGRAM OR NOT");
System.out.println("enter 1st string:");
s1=ob.readLine();
System.out.print("enter 2nd string:");
s2=ob.readLine();
if(s1.length()!=s2.length())
{
System.out.println("\n given string are not anagram");
System.exit(0);
}
char c1[]= s1.toLowerCase().toCharArray();
char c2[]=s2.toLowerCase().toCharArray();
Arrays.sort(c1);
Arrays.sort(c2);
if(Arrays.equals(c1,c2))
System.out.println("\n given string are anagram");
else
System.out.println("\n given string are not anagram");
}
}
Output:
D:\iics>javac anagram.java
D:\iics>java anagram
*****
CHECK TWO STRING ARE ANAGRAM OR NOT
*****
enter 1st string:karthi
enter 2nd string:ihtrak
given string are anagram
Result:
Thus the program was executed and run successfully.
Ex No:6
Executing various Window Applications
Date:26.12.24
Aim:
To write a java program for Executing various Window
Applications
Coding:
import java.io.*;
import java.lang.*;
class window
{
public static void main(String args[])throws IOException
{
int ch;
DataInputStream ob=new DataInputStream(System.in);
do
{
System.out.println("******");
System.out.println("Executing various windows application");
System.out.println("********");
System.out.println("\n 1-Notepad\n2-Paint\n3-Calculator\n4-
Explorer\n5-Exit\n");
Runtime r=Runtime.getRuntime();
Process p=null;
System.out.print("Enter your choice");
ch=Integer.parseInt(ob.readLine());
switch(ch)
{
case 1:
try
{
p=r.exec("Notepad.exe");
System.out.println("Notepad is opened");
}
catch(Exception e)
{
System.out.println("\nError in notepad");
}
break;
case 2:
try
{
p=r.exec("mspaint.exe");
System.out.println("Paint is opened");
}
catch(Exception e)
{
System.out.println("Error in paint");
}
break;
case 3:
try
{
p=r.exec("calc.exe");
System.out.println("Calculator is opened");
}
catch(Exception e)
{
System.out.println("Error in calculator");
}
break;
case 4:
try
{
p=r.exec("Explorer.exe");
System.out.println("\n Explorer is opened");
}
catch(Exception e)
{
System.out.println("\n Errore in exception");
}
break;
case 5:
System.out.println("Exit the program");
System.exit(0);
default:
System.out.println("Invalid choice");
break;
}
}
while(ch!=5);
}
}
Output
D:\IIcs>java window
********************************
Executing various windows application
********************************
1-Notepad
2-Paint
3-Calculator
4-Explorer
5-Exit
Notepad is opened
*******************************
Executing various windows application
********************************
1-Notepad
2-Paint
3-Calculator
4-Explorer
5-Exit
1-Notepad
2-Paint
3-Calculator
4-Explorer
5-Exit
1-Notepad
2-Paint
3-Calculator
4-Explorer
5-Exit
1-Notepad
2-Paint
3-Calculator
4-Explorer
5-Exit
Result:
Thus the program was executed and run successfully.
Ex no:7
Factorial using function
Date:26.12.24
Aim:
To write a java program for factorial using function.
Coding:
class factorial
{
factorial(int n)
{
int fact=1;
for(int i=1;i<=n;i++)
{
fact=fact*i;
}
System.out.println("Factorial value is:"+fact);
}
}
class fac
{
public static void main(String args[])
{
factorial f=new factorial(5);
}
}
Output:
D:\IIcs>java fac
Factorial value is:120
Result:
Thus the program was executed and run successfully.
Ex:no:8
Prime Number Using Scanner
Date:2.1.25
Aim:
To write a java program for prime number using scanner
Coding:
import java.util.*;
import java.io.*;
class prime
{
public static void main(String args[])
{
int num,n,i,j,count;
Scanner s=new Scanner(System.in);
System.out.println("Enter the number for prime number
generation:");
num=s.nextInt();
System.out.println("1 is neither prime nor composite");
System.out.println("2 is even prime number");
for(j=3;j<=num;j++)
{
i=1;
n=j;
count=0;
while(i<=n)
{
if((n%i==0))
{
count=count+1;
}
i=i+1;
}
if(count==2)
{
System.out.println(n);
}
}
}
}
Output:
D:\IIcs>java prime
Enter the number for prime number generation:
10
1 is neither prime nor composite
2 is even prime number
3
5
7
Result:
Thus the program was executed and run successfully.
Ex no:9
String handling function
Date:2.1.25
Aim:
To write a java program for String handling function
Coding:
public class strmanip
{
public static void main(String args[])
{
String s1="Welcome";
String s2=" Java ";
String s3="computer";
String s4="science";
String s5="";
String s6="OPERATING SYSTEM";
String s7="SCIENCE";
char ch=s1.charAt(3);
System.out.println("Output");
System.out.println("******");
System.out.println("Character At the 3rd place is:"+ch);
int findex=s1.indexOf("l");
int Iindex=s1.lastIndexOf("l");
System.out.println("First index of 1:"+findex);
System.out.println("Last index of 1:"+Iindex);
System.out.println("Length of the string java is:"+s2.length());
System.out.println("Length of the string comparision of computer
and science:"+s3.compareTo(s4));
System.out.println("String comparition of science and
SCIENCE :"+s4.compareTo(s7));
System.out.println("Stromg
concatenation:"+s3.concat("Department"));
System.out.println("Is string empty:"+s5.isEmpty());
System.out.println("Before trim,string length is:"+s2.length());
String ss=s2.trim();
System.out.println("After trim,string length is:"+ss.length());
System.out.println("Convert into lowercase:"+s4.toLowerCase());
System.out.println("Convert into uppercase:"+s7.toUpperCase());
String str=s7.replace("SCIENCE","Arts");
System.out.println("String replcement:"+str);
System.out.println("String Ignore case of same
string :"+s4.equalsIgnoreCase(s7));
System.out.println("String Ignore case of Different
string :"+s4.equalsIgnoreCase(s6));
}
}
Output:
D:\IIcs>javac strmanip.java
D:\IIcs>java strmanip
Output
******
Character At the 3rd place is:c
First index of 1:2
Last index of 1:2
Length of the string java is:6
Length of the string comparision of computer and science:-16
String comparition of science and SCIENCE :32
Stromg concatenation:computerDepartment
Is string empty:true
Before trim,string length is:6
After trim,string length is:4
Convert into lowercase:science
Convert into uppercase:SCIENCE
String replcement:Arts
String Ignore case of same string :true
String Ignore case of Different string :false
Result:
Thus the program was executed and run successfully.
Ex no:10
Matrix Multiplication
Date:9.1.25
Aim:
To write a java program for matrix multiplication
Coding:
import java.io.*;
import java.lang.*;
class matmul
{
public static void main(String args[])throws IOException
{
int m,n,p,q,sum=0,i,j,k;
int a[][],b[][],c[][];
DataInputStream ob=new DataInputStream(System.in);
System.out.println("*********************");
System.out.println("MATRIX MULTIPLICATION");
System.out.println("**********************");
System.out.println("Enter the number of rows and columns of first
matrix");
m=Integer.parseInt(ob.readLine());
n=Integer.parseInt(ob.readLine());
System.out.println("Enter thenumber of rows and columns of second
matrix");
p=Integer.parseInt(ob.readLine());
q=Integer.parseInt(ob.readLine());
if(n!=p)
{
System.out.println("\nMATRIX MULTIPLICATION CANNOT
PERFORMED");
System.exit(0);
}
else
{
a=new int [m][n];
b=new int [p][q];
c=new int [m][q];
System.out.println("Enter the element of first matrix:");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
a[i][j]=Integer.parseInt(ob.readLine());
System.out.println("Enter the element of second matrix:");
for(i=0;i<p;i++)
for(j=0;j<p;j++)
b[i][j]=Integer.parseInt(ob.readLine());
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
for(k=0;k<p;k++)
{
sum=sum+a[i][k]*b[k][j];
}
c[i][j]=sum;
sum=0;
}
}
System.out.println("first matrix is:");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
System.out.println(a[i][j]+"\t");
}
System.out.println();
}
System.out.println("second matrix is:");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
System.out.println(b[i][j]+"\t");
}
System.out.println();
}
System.out.println("Matrix multiplication is:");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
System.out.println(c[i][j]+" ");
}
System.out.println();
}
}
}
}
Output:
D:\IIcs>java matmul
*********************
MATRIX MULTIPLICATION
*********************
Enter The Number Of Rows and columns of first matrix
2
2
Enter The Number Of Rows And Columns Of Second Matrix
2
2
Enter The Element Of First Matrix:
3
3
3
3
Enter The Element Of Second Matrix:
3
3
3
3
First Matrix is:
3 3
3 3
Second Matrix is:
3 3
3 3
Matrix Multiplication is:
18 18
18 18
Ex.no:11
Method overloading using constructor
Date:9.1.25
Aim:
Coding:
class area
{
int x,y,a;
area(int l,int b)
{
x=l;
y=b;
a=x*y;
System.out.println("area of rectangle:"+a);
}
area(float s)
{
float z,a1;
z=s;
a1=z*z;
System.out.println("area of square:"+a1);
}
}
class method
{
public static void main(String args[])
{
area ar=new area(10,5);
area as=new area(8.7f);
}
}
Output:
area of rectangle:50
area of square:75.689995
Result:
Thus the program was executed and run successfully.
Ex.no:12
static member function
Date:9.1.25
Aim:
Coding:
import java.io.*;
class arioperation
{
static int add(int x,int y)
{
return(x+y);
}
static int sub(int x,int y)
{
return(x-y);
}
static int mul(int x,int y)
{
return(x*y);
}
static int div(int x,int y)
{
return(x/y);
}
}
class app
{
public static void main(String args[])
{
int ad,sb,ml,dv;
ad=arioperation.add(100,200);
sb=arioperation.sub(ad,150);
ml=arioperation.mul(ad,sb);
dv=arioperation.div(ml,ad);
System.out.println("add value is:"+ad);
System.out.println("sub value is:"+sb);
System.out.println("mul value is:"+ml);
System.out.println("div value is:"+dv);
}
}
Output:
add value is:300
sub value is:150
mul value is:45000
div value is:150
Result:
Thus the program was executed and run successfully.
Ex.no:13
Single Inheritance
Date:23.1.25
Aim:
Coding:
class area
{
int a;
area(int n)
{
a=n;
}
int calc()
{
return(a*a);
}
}
class rectangle extends area
{
int b;
rectangle(int m,int n)
{
super(n);
b=m;
}
int calc1()
{
return(a*b);
}
}
class singleinh
{
public static void main(String args[])
{
rectangle rec=new rectangle(10,20);
System.out.println("output");
System.out.println("area of square:"+rec.calc());
System.out.println("area of rectangle:"+rec.calc1());
}
}
Output:
area of square:400
area of rectangle:200
Result:
Thus the program was executed and run successfully.
Ex.no:14
Multilevel Inheritance
Date:23.1.25
Aim:
Coding:
class empty
{
String empname="karthi";
int eid=101;
int salary=50000;
void display()
{
System.out.println("employee name:"+empname);
System.out.println("employee id:"+eid);
System.out.println("salary:"+salary);
}
}
class allowance extends empty
{
int bp=30000;
double hra,ta,pf;
void calc()
{
hra=bp*0.12;
ta=bp*0.1;
pf=bp*0.1;
System.out.println("home rent allowance:"+hra);
System.out.println("traveeling allowance:"+ta);
System.out.println("provident fund:"+pf);
}
}
class sal extends allowance
{
double gp,np;
void calc1()
{
gp=bp+hra+ta;
np=gp-pf;
System.out.println("gross pay:"+gp);
System.out.println("net pay:"+np);
}
}
class empsal
{
public static void main(String args[])
{
sal s=new sal();
s.display();
s.calc();
s.calc1();
}
}
Output:
employee name:karthi
employee id:101
salary:50000
home rent allowance:3600.0
travelling allowance:3000.0
provident fund:3000.0
gross pay:36600.0
net pay:33600.0
Result:
Thus the program was executed and run successfully.
Ex no:15
Multiplication table using multithreading
Date:27.1.25
Aim:
To write a java program for Multiplication table using
multithreading
Coding:
import java.io.*;
class child1 implements Runnable
{
String msg;
Thread t;
child1(String msg)
{
this.msg=msg;
t=new Thread(this,msg);
t.start();
}
public void run()
{
try
{
System.out.println("Multiplication table 5 starts..");
for(int i=1;i<=10;i++)
{
System.out.println(i+"*5="+i*5);
t.sleep(500);
}
System.out.println("\n multiplication table 5 ends..");
}
catch(InterruptedException e)
{
System.out.println("\nError"+e);
}
}
}
class child2 extends Thread
{
String msg;
child2(String msg)
{
this.msg=msg;
start();
}
public void run()
{
try
{
System.out.println("\n Multiplication table 10 Statrs..");
for(int i=1;i<=10;i++)
{
System.out.println(i+"*10="+i*10);
sleep(1000);
}
System.out.println("Multipliaction table 10 ends..");
}
catch(InterruptedException e)
{
System.out.println("\n Error"+e);
}
}
}
class multithread
{
public static void main(String args[])
{
child1 c1=new child1("multiplication table-5");
child2 c2=new child2("multiplication table-10");
}
}
Output:
D:\IIcs>java multithread
Multiplication table 5 starts..
1*5=5
Result:
Thus the program was executed and run successfully.
Ex.no:16
String null pointer exception
Date:27.1.25
Aim:
Coding:
class nullexcep
{
public static void main(String args[])
{
try
{
String str=null;
System.out.println(str.length());
}
catch(NullPointerException e)
{
System.out.println("there is null value");
}
}
}
Output:
there is null value
Result:
Thus the program was executed and run successfully.
Ex.no:17
string array handling exception
Date:27.1.25
Aim:
Coding:
public static void main(String args[])
{
try
{
String str="java";
System.out.println(str.length());
char c=str.charAt(0);
c=str.charAt(35);
System.out.println(c);
}
catch(StringIndexOutOfBoundsException e)
{
System.out.println("StringIndexOutOfBoundsException!");
}
}
}
Output:
4
StringIndexOutOfBoundsException!
Result:
Thus the program was executed and run successfully.
Ex.no:18
Arithmetic exception
Date:27.1.25
Aim:
Coding:
class arithexcep
{
public static void main(String args[])
{
try
{
int x=10,y=0;
int result=x/y;
System.out.println("result:"+result);
}
catch(ArithmeticException e)
{
System.out.println("number cannot be divided");
}
}
}
Output:
number cannot be divided
Result:
Thus the program was executed and run successfully.
Ex.no:19
File
Date:30.1.25
Aim:
To write a java program for file
Coding:
import java.io.*;
public class streamread
{
public static void main(String args[])throws IOException
{
InputStreamReader cin=null;
try
{
cin=new InputStreamReader(System.in);
System.out.print("Enter character'e' to exit");
char c;
do
{
c=(char)cin.read();
System.out.println(c);
}
while(c!='e');
}
finally
{
if(cin!=null)
{
cin.close();
}
}
}
}
Output:
D:\IIcs>java streamread
Enter character'e' to exit
e
e
Result:
Thus the program was executed and run successfully.
Ex.no:20
file copy using stream
Date:30.1.25
Aim:
Coding:
import java.io.*;
class filecopy
{
public static void main(String args[])throws IOException
{
int i;
String src,des;
DataInputStream ob=new DataInputStream(System.in);
System.out.println("enter the source file name:");
src=ob.readLine();
System.out.println("enter the destination file name:");
des=ob.readLine();
try
{
FileInputStream fin=new FileInputStream(src);
FileOutputStream fos=new FileOutputStream(des);
do
{
i=fin.read();
if(i!=-1)
{
System.out.print((char)i);
fos.write(i);
}
}
while(i!=-1);
System.out.print("\n source file:"+src+"is successfully copied to
destination."+des);
}
catch(IOException e)
{
System.out.println("file error"+e);
}
}
}
Output:
enter the source file name:fin.txt
enter the destination file name:fos.txt
nishandhini
karthigaiselvi
selvalakshmi
jansirani
source file:fin.txtis successfully copied to destination.fos.txt
Result:
Thus the program was executed and run successfully.
Ex.no:21
Reterive host name and address
Date:3.2.25
Aim:
Coding:
import java.net.*;
class host
{
public static void main(String args[])throws UnknownHostException
{
System.out.println("retrive host name and address:");
InetAddress addr=InetAddress.getLocalHost();
String name=addr.getHostName();
System.out.println("\n name of the localhost is:"+name);
String ip=addr.getHostAddress();
System.out.println("\n address of the localhost is:"+ip);
}
}
Output:
retrive host name and address:
Result:
Thus the program was executed and run successfully.
Ex.no:22
This point
Date:6.2.25
Aim:
Coding:
class point
{
int x,y;
point(int x,int y)
{
this.x=x;
this.y=y;
}
void display()
{
System.out.println("point:("+x+","+y+")");
}
}
class pointd extends point
{
pointd(int x,int y)
{
super(x,y);
}
void display()
{
System.out.println("pointd:("+x+","+y+")");
}
}
class mainp
{
public static void main(String args[])
{
point point=new point(3,5);
pointd pointd=new pointd(7,9);
point.display();
pointd.display();
}
}
Output:
point:(3,5)
pointd:(7,9)
Result:
Thus the program was executed and run successfully.
Ex.no:23
Interface
Date:13.2.25
Aim:
Coding:
import java.util.*;
import java.io.*;
interface stu
{
void info();
}
interface mark
{
void details();
}
class multi implements stu,mark
{
String stuname="karthi";
int regno=12343;
Scanner s=new Scanner(System.in);
int java=s.nextInt();
int syssoft=s.nextInt();
public void info()
{
System.out.println("student name:"+stuname);
System.out.println("register number:"+regno);
System.out.println("java:"+java);
System.out.println("system software:"+syssoft);
}
public void details()
{
int total;
double avg;
total=java+syssoft;
System.out.println("total mark:"+total);
avg=total/2;
System.out.println("average mark is:"+avg);
}
}
class std
{
public static void main(String args[])
{
System.out.println("enter two mark");
multi m=new multi();
m.info();
m.details();
}
}
Output:
enter two mark
67
89
student name:karthi
register number:12343
java:67
system software:89
total mark:156
average mark is:78.0
Result:
Thus the program was executed and run successfully.
Ex no:24
Digital clock using applet and thread
Date:6.3.25
Aim:
To write a java program for digital clock using applet and
thread
Coding:
import java.applet.*;
import java.awt.*;
import java.util.*;
/*<applet code="Digital" width="200" height="200"></applet>*/
public class Digital extends Applet implements Runnable
{
Thread t;
public void start()
{
t=new Thread(this);
t.start();
}
public void run()
{
try
{
for(;;)
{
repaint();
Thread.sleep(1000);
}
}
catch(InterruptedException e)
{
System.out.print("Error");
}
}
public void paint(Graphics g)
{
showStatus("WELCOME TO DIGITAL CLOCK");
setBackground(Color.pink);
setForeground(Color.red);
Date d=new Date();
g.setFont(new Font("Times New Roman",Font.BOLD,30));
g.drawString(d.getHours()+":"+d.getMinutes()
+":"+d.getSeconds(),600,300);
}
}
Output:
D:\IIcs>appletviewer Digital.java
Result:
Thus the program was executed and run successfully.
Ex.no:25
Scrolling text using applet and thread
Date:12.3.25
Aim:
To write a java program for scrolling text using
Coding:
import java.applet.*;
import java.awt.*;
/*<applet code="scroll" width="200" height="200"></applet>*/
public class scroll extends Applet implements Runnable
{
Thread t;
char c;
String s;
public void start()
{
s="SCROLLING TEXT USING APPLET AND THREAD";
t=new Thread(this);
t.start();
}
public void run()
{
try
{
for(;;)
{
repaint();
Thread.sleep(1000);
c=s.charAt(0);
s=s.substring(1)+c;
}
}
catch(InterruptedException e)
{
System.out.print("Error");
}
}
public void paint(Graphics g)
{
showStatus("SCROLLING TEXT USING APPLET AND THREAD");
setBackground(Color.pink);
setForeground(Color.red);
g.setFont(new Font("Monotype corsiva ",Font.BOLD,30));
g.drawString(s,400,400);
}
}
Output:
D:\IIcs>appletviewer scroll.java
Result:
Thus the program was executed and run successfully.
Ex.no:26
Drawing human face using graphic primitives
Date:17.3.25
Aim:
To write a java program for Drawing human face using graphic
primitives
Coding:
import java.awt.*;
import java.applet.*;
/*<applet code="Face" width="400" height="400"></applet>*/
public class Face extends Applet
{
public void paint(Graphics g)
{
showStatus("Drawing Human Face");
setBackground(Color.pink);
setForeground(Color.blue);
g.drawString("Drawing Human Face",120,20);
g.drawOval(40,40,120,150);
g.drawOval(57,75,30,20);
g.drawOval(110,75,30,20);
g.fillOval(68,81,10,10);
g.fillOval(121,81,10,10);
g.drawOval(85,100,30,30);
g.fillArc(60,125,80,40,180,180);
g.drawOval(25,92,15,30);
g.drawOval(160,92,15,30);
}
}
Output:
D:\IIcs>appletviewer face.java
Result:
Thus the program was executed and run successfully.
Ex.no:27
Changing background color of rectangle using applet
Date:20.3.25
Aim:
To writer a java program for Changing background color of
rectangle using applet
Coding:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/*<applet code="SB" width="400" height="300"></applet>*/
public class SB extends Applet implements AdjustmentListener
{
Scrollbar r,g,b;
TextField ta;
public void init()
{
setLayout(null);
r=new Scrollbar(0,0,1,0,256);
r.setBounds(100,120,200,30);
g=new Scrollbar(0,0,1,0,256);
g.setBounds(100,150,200,30);
b=new Scrollbar(0,0,1,0,256);
b.setBounds(100,180,200,30);
ta=new TextField(50);
ta.setBounds(100,10,200,100);
add(r);
add(g);
add(b);
add(ta);
r.addAdjustmentListener(this);
g.addAdjustmentListener(this);
b.addAdjustmentListener(this);
}
public void adjustmentValueChanged(AdjustmentEvent ae)
{
int cr,cg,cb;
cr=r.getValue();
cg=g.getValue();
cb=b.getValue();
showStatus("Color is-->r="+cr+",g="+cg+",b="+cb);
ta.setBackground(new Color(cr,cg,cb));
}
}
Output:
D:\IIcs>appletviewer SB.java
Result:
Thus the program was executed and run successfully.
Ex.no:28
Simple key Event
Date:21.3.25
Aim:
To write a java program for simple key event.
Coding:
import javax . swing.*;
import java.awt.*;
import java.awt.event.*;
public class SimpleKeyEvent extends JFrame implements KeyListener
{
private JLabel label;
public SimpleKeyEvent ()
{
setTitle("KeyEvent example");
setSize(400,300);
setDefaultCloseOperation(JFrame . EXIT_ON_CLOSE);
setLayout(new FlowLayout());
label=new JLabel("press any key...");
label.setFont(new Font ("arial",Font.BOLD,20));
add(label);
addKeyListener(this);
setFocusable(true);
setVisible(true);
}
Result:
Thus the program was executed and run successfully.