[Go to site: main page, start]

0% found this document useful (0 votes)
5 views83 pages

Java Programs for Basic Concepts

The document contains a series of Java programming exercises, each with a specific aim, coding examples, outputs, and results indicating successful execution. Topics include swapping numbers, temperature conversion, student details using classes, invoice billing, anagram checking, executing window applications, factorial calculation, prime number generation, string handling, matrix multiplication, method overloading, static member functions, and single inheritance. Each exercise is dated and structured with clear coding and output sections.

Uploaded by

sri.srikarthi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views83 pages

Java Programs for Basic Concepts

The document contains a series of Java programming exercises, each with a specific aim, coding examples, outputs, and results indicating successful execution. Topics include swapping numbers, temperature conversion, student details using classes, invoice billing, anagram checking, executing window applications, factorial calculation, prime number generation, string handling, matrix multiplication, method overloading, static member functions, and single inheritance. Each exercise is dated and structured with clear coding and output sections.

Uploaded by

sri.srikarthi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Ex no:1

Swapping two numbers


Date:12.12.24

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;
[Link]("Before swap");
[Link](a);
[Link](b);
t=a;
a=b;
b=t;
[Link]("After swap");
[Link](a);
[Link](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;
[Link]("Celcius:"+c);
f=(c+32)*9/5;
[Link]("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()
{
[Link]("Name:"+name);
[Link]("Rollno:"+rno);
}
public static void main(String args[])
{
student s=new student();
[Link]="AAA";
[Link]=10;
[Link]();
}
}

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 [Link].*;
import [Link].*;
class invoice
{
int pid,qty;
String pname;
Double price,bill;
DataInputStream ob=new DataInputStream([Link]);
void get()throws IOException
{
[Link]("Enter the product id:");
pid=[Link]([Link]());
[Link]("Enter the product name:");
pname=[Link]();
[Link]("Enter the quality:");
qty=[Link]([Link]());
[Link]("Enter the price:");
price=[Link]([Link]());
}
void calc()
{
bill=qty*price;
}
void put()
{
[Link]("\n\nBill amount is"+bill);
}
}
class invoicebill
{
public static void main(String args[])throws IOException
{
[Link]("********************");
[Link]("INVOICEBILL USING CLASS AND OBJECT");
[Link]("****************************************
*");
invoice inv=new invoice();
[Link]();
[Link]();
[Link]();
}
}

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.
[Link]
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 [Link].*;
import [Link].*;
class anagram
{
public static void main(String args[])throws IOException
{
String s1,s2;
DataInputStream ob= new DataInputStream([Link]);
[Link]("*****");
[Link]("CHECK TWO STRING ARE ANAGRAM OR NOT");
[Link]("enter 1st string:");
s1=[Link]();
[Link]("enter 2nd string:");
s2=[Link]();
if([Link]()!=[Link]())
{
[Link]("\n given string are not anagram");
[Link](0);
}
char c1[]= [Link]().toCharArray();
char c2[]=[Link]().toCharArray();
[Link](c1);
[Link](c2);
if([Link](c1,c2))
[Link]("\n given string are anagram");
else
[Link]("\n given string are not anagram");
}
}

Output:
D:\iics>javac [Link]

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 [Link].*;
import [Link].*;
class window
{
public static void main(String args[])throws IOException
{
int ch;
DataInputStream ob=new DataInputStream([Link]);
do
{
[Link]("******");
[Link]("Executing various windows application");
[Link]("********");
[Link]("\n 1-Notepad\n2-Paint\n3-Calculator\n4-
Explorer\n5-Exit\n");
Runtime r=[Link]();
Process p=null;
[Link]("Enter your choice");
ch=[Link]([Link]());
switch(ch)
{
case 1:
try
{
p=[Link]("[Link]");
[Link]("Notepad is opened");
}
catch(Exception e)
{
[Link]("\nError in notepad");
}
break;
case 2:
try
{
p=[Link]("[Link]");
[Link]("Paint is opened");
}
catch(Exception e)
{
[Link]("Error in paint");
}
break;
case 3:
try
{
p=[Link]("[Link]");
[Link]("Calculator is opened");
}
catch(Exception e)
{
[Link]("Error in calculator");
}
break;
case 4:
try
{
p=[Link]("[Link]");
[Link]("\n Explorer is opened");
}
catch(Exception e)
{
[Link]("\n Errore in exception");
}
break;
case 5:
[Link]("Exit the program");
[Link](0);
default:
[Link]("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

Enter your choice1

Notepad is opened
*******************************
Executing various windows application
********************************

1-Notepad
2-Paint
3-Calculator
4-Explorer
5-Exit

Enter your choice 2


Paint is opened
*******************************
Executing various windows application
*******************************

1-Notepad
2-Paint
3-Calculator
4-Explorer
5-Exit

Enter your choice 3


Calculator is opened
********************************
Executing various windows application
********************************

1-Notepad
2-Paint
3-Calculator
4-Explorer
5-Exit

Enter your choice4


Explorer is opened
********************************
Executing various windows application
********************************

1-Notepad
2-Paint
3-Calculator
4-Explorer
5-Exit

Enter your choice5

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;
}
[Link]("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 [Link].*;
import [Link].*;
class prime
{
public static void main(String args[])
{
int num,n,i,j,count;
Scanner s=new Scanner([Link]);
[Link]("Enter the number for prime number
generation:");
num=[Link]();
[Link]("1 is neither prime nor composite");
[Link]("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)
{
[Link](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=[Link](3);
[Link]("Output");
[Link]("******");
[Link]("Character At the 3rd place is:"+ch);
int findex=[Link]("l");
int Iindex=[Link]("l");
[Link]("First index of 1:"+findex);
[Link]("Last index of 1:"+Iindex);
[Link]("Length of the string java is:"+[Link]());
[Link]("Length of the string comparision of computer
and science:"+[Link](s4));
[Link]("String comparition of science and
SCIENCE :"+[Link](s7));
[Link]("Stromg
concatenation:"+[Link]("Department"));
[Link]("Is string empty:"+[Link]());
[Link]("Before trim,string length is:"+[Link]());
String ss=[Link]();
[Link]("After trim,string length is:"+[Link]());
[Link]("Convert into lowercase:"+[Link]());
[Link]("Convert into uppercase:"+[Link]());
String str=[Link]("SCIENCE","Arts");
[Link]("String replcement:"+str);
[Link]("String Ignore case of same
string :"+[Link](s7));
[Link]("String Ignore case of Different
string :"+[Link](s6));
}
}
Output:
D:\IIcs>javac [Link]
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 [Link].*;
import [Link].*;
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([Link]);
[Link]("*********************");
[Link]("MATRIX MULTIPLICATION");
[Link]("**********************");
[Link]("Enter the number of rows and columns of first
matrix");
m=[Link]([Link]());
n=[Link]([Link]());
[Link]("Enter thenumber of rows and columns of second
matrix");
p=[Link]([Link]());
q=[Link]([Link]());
if(n!=p)
{
[Link]("\nMATRIX MULTIPLICATION CANNOT
PERFORMED");
[Link](0);
}
else
{
a=new int [m][n];
b=new int [p][q];
c=new int [m][q];
[Link]("Enter the element of first matrix:");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
a[i][j]=[Link]([Link]());
[Link]("Enter the element of second matrix:");
for(i=0;i<p;i++)
for(j=0;j<p;j++)
b[i][j]=[Link]([Link]());
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;
}
}
[Link]("first matrix is:");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
[Link](a[i][j]+"\t");
}
[Link]();
}
[Link]("second matrix is:");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
[Link](b[i][j]+"\t");
}
[Link]();
}
[Link]("Matrix multiplication is:");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
[Link](c[i][j]+" ");
}
[Link]();
}
}
}
}

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
[Link]
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;
[Link]("area of rectangle:"+a);
}
area(float s)
{
float z,a1;
z=s;
a1=z*z;
[Link]("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.
[Link]
static member function
Date:9.1.25

Aim:

Coding:
import [Link].*;
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=[Link](100,200);
sb=[Link](ad,150);
ml=[Link](ad,sb);
dv=[Link](ml,ad);
[Link]("add value is:"+ad);
[Link]("sub value is:"+sb);
[Link]("mul value is:"+ml);
[Link]("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.
[Link]
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);
[Link]("output");
[Link]("area of square:"+[Link]());
[Link]("area of rectangle:"+rec.calc1());
}
}

Output:
area of square:400
area of rectangle:200
Result:
Thus the program was executed and run successfully.
[Link]
Multilevel Inheritance
Date:23.1.25

Aim:

Coding:
class empty
{
String empname="karthi";
int eid=101;
int salary=50000;
void display()
{
[Link]("employee name:"+empname);
[Link]("employee id:"+eid);
[Link]("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;
[Link]("home rent allowance:"+hra);
[Link]("traveeling allowance:"+ta);
[Link]("provident fund:"+pf);
}
}
class sal extends allowance
{
double gp,np;
void calc1()
{
gp=bp+hra+ta;
np=gp-pf;
[Link]("gross pay:"+gp);
[Link]("net pay:"+np);
}
}
class empsal
{
public static void main(String args[])
{
sal s=new sal();
[Link]();
[Link]();
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 [Link].*;
class child1 implements Runnable
{
String msg;
Thread t;
child1(String msg)
{
[Link]=msg;
t=new Thread(this,msg);
[Link]();
}
public void run()
{
try
{
[Link]("Multiplication table 5 starts..");
for(int i=1;i<=10;i++)
{
[Link](i+"*5="+i*5);
[Link](500);
}
[Link]("\n multiplication table 5 ends..");
}
catch(InterruptedException e)
{
[Link]("\nError"+e);
}
}
}
class child2 extends Thread
{
String msg;
child2(String msg)
{
[Link]=msg;
start();
}
public void run()
{
try
{
[Link]("\n Multiplication table 10 Statrs..");
for(int i=1;i<=10;i++)
{
[Link](i+"*10="+i*10);
sleep(1000);
}
[Link]("Multipliaction table 10 ends..");
}
catch(InterruptedException e)
{
[Link]("\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

Multiplication table 10 Statrs..


1*10=10
2*5=10
3*5=15
2*10=20
4*5=20
3*10=30
5*5=25
6*5=30
4*10=40
7*5=35
8*5=40
5*10=50
9*5=45
10*5=50
6*10=60

multiplication table 5 ends..


7*10=70
8*10=80
9*10=90
10*10=100
Multipliaction table 10 ends..

Result:
Thus the program was executed and run successfully.
[Link]
String null pointer exception
Date:27.1.25

Aim:

Coding:
class nullexcep
{
public static void main(String args[])
{
try
{
String str=null;
[Link]([Link]());
}
catch(NullPointerException e)
{
[Link]("there is null value");
}
}
}

Output:
there is null value

Result:
Thus the program was executed and run successfully.
[Link]
string array handling exception
Date:27.1.25

Aim:

Coding:
public static void main(String args[])
{
try
{
String str="java";
[Link]([Link]());
char c=[Link](0);
c=[Link](35);
[Link](c);
}
catch(StringIndexOutOfBoundsException e)
{
[Link]("StringIndexOutOfBoundsException!");
}
}
}
Output:
4
StringIndexOutOfBoundsException!

Result:
Thus the program was executed and run successfully.
[Link]
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;
[Link]("result:"+result);
}
catch(ArithmeticException e)
{
[Link]("number cannot be divided");
}
}
}
Output:
number cannot be divided

Result:
Thus the program was executed and run successfully.
[Link]
File
Date:30.1.25

Aim:
To write a java program for file

Coding:
import [Link].*;
public class streamread
{
public static void main(String args[])throws IOException
{
InputStreamReader cin=null;
try
{
cin=new InputStreamReader([Link]);
[Link]("Enter character'e' to exit");
char c;
do
{
c=(char)[Link]();
[Link](c);
}
while(c!='e');
}
finally
{
if(cin!=null)
{
[Link]();
}
}
}
}

Output:
D:\IIcs>java streamread
Enter character'e' to exit
e
e

Result:
Thus the program was executed and run successfully.
[Link]
file copy using stream
Date:30.1.25

Aim:

Coding:
import [Link].*;
class filecopy
{
public static void main(String args[])throws IOException
{
int i;
String src,des;
DataInputStream ob=new DataInputStream([Link]);
[Link]("enter the source file name:");
src=[Link]();
[Link]("enter the destination file name:");
des=[Link]();
try
{
FileInputStream fin=new FileInputStream(src);
FileOutputStream fos=new FileOutputStream(des);
do
{
i=[Link]();
if(i!=-1)
{
[Link]((char)i);
[Link](i);
}
}
while(i!=-1);
[Link]("\n source file:"+src+"is successfully copied to
destination."+des);
}
catch(IOException e)
{
[Link]("file error"+e);
}
}
}

Output:
enter the source file name:[Link]
enter the destination file name:[Link]
nishandhini
karthigaiselvi
selvalakshmi
jansirani
source file:[Link] successfully copied to [Link]

Result:
Thus the program was executed and run successfully.
[Link]
Reterive host name and address
Date:3.2.25

Aim:

Coding:
import [Link].*;
class host
{
public static void main(String args[])throws UnknownHostException
{
[Link]("retrive host name and address:");
InetAddress addr=[Link]();
String name=[Link]();
[Link]("\n name of the localhost is:"+name);
String ip=[Link]();
[Link]("\n address of the localhost is:"+ip);
}
}

Output:
retrive host name and address:

name of the localhost is:DESKTOP-ESBP9HG

address of the localhost is:[Link]

Result:
Thus the program was executed and run successfully.
[Link]
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()
{
[Link]("point:("+x+","+y+")");
}
}
class pointd extends point
{
pointd(int x,int y)
{
super(x,y);
}
void display()
{
[Link]("pointd:("+x+","+y+")");
}
}
class mainp
{
public static void main(String args[])
{
point point=new point(3,5);
pointd pointd=new pointd(7,9);
[Link]();
[Link]();
}
}

Output:
point:(3,5)
pointd:(7,9)
Result:
Thus the program was executed and run successfully.
[Link]
Interface
Date:13.2.25

Aim:

Coding:
import [Link].*;
import [Link].*;
interface stu
{
void info();
}
interface mark
{
void details();
}
class multi implements stu,mark
{
String stuname="karthi";
int regno=12343;
Scanner s=new Scanner([Link]);
int java=[Link]();
int syssoft=[Link]();
public void info()
{
[Link]("student name:"+stuname);
[Link]("register number:"+regno);
[Link]("java:"+java);
[Link]("system software:"+syssoft);
}
public void details()
{
int total;
double avg;
total=java+syssoft;
[Link]("total mark:"+total);
avg=total/2;
[Link]("average mark is:"+avg);
}
}
class std
{
public static void main(String args[])
{
[Link]("enter two mark");
multi m=new multi();
[Link]();
[Link]();
}
}

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 [Link].*;
import [Link].*;
import [Link].*;
/*<applet code="Digital" width="200" height="200"></applet>*/
public class Digital extends Applet implements Runnable
{
Thread t;
public void start()
{
t=new Thread(this);
[Link]();
}
public void run()
{
try
{
for(;;)
{
repaint();
[Link](1000);
}
}
catch(InterruptedException e)
{
[Link]("Error");
}
}
public void paint(Graphics g)
{
showStatus("WELCOME TO DIGITAL CLOCK");
setBackground([Link]);
setForeground([Link]);
Date d=new Date();
[Link](new Font("Times New Roman",[Link],30));
[Link]([Link]()+":"+[Link]()
+":"+[Link](),600,300);
}
}

Output:
D:\IIcs>appletviewer [Link]

Result:
Thus the program was executed and run successfully.
[Link]
Scrolling text using applet and thread
Date:12.3.25

Aim:
To write a java program for scrolling text using

Coding:
import [Link].*;
import [Link].*;
/*<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);
[Link]();
}
public void run()
{
try
{
for(;;)
{
repaint();
[Link](1000);
c=[Link](0);
s=[Link](1)+c;
}
}
catch(InterruptedException e)
{
[Link]("Error");
}
}
public void paint(Graphics g)
{
showStatus("SCROLLING TEXT USING APPLET AND THREAD");
setBackground([Link]);
setForeground([Link]);
[Link](new Font("Monotype corsiva ",[Link],30));
[Link](s,400,400);
}
}
Output:
D:\IIcs>appletviewer [Link]

Result:
Thus the program was executed and run successfully.
[Link]
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 [Link].*;
import [Link].*;
/*<applet code="Face" width="400" height="400"></applet>*/
public class Face extends Applet
{
public void paint(Graphics g)
{
showStatus("Drawing Human Face");
setBackground([Link]);
setForeground([Link]);
[Link]("Drawing Human Face",120,20);
[Link](40,40,120,150);
[Link](57,75,30,20);
[Link](110,75,30,20);
[Link](68,81,10,10);
[Link](121,81,10,10);
[Link](85,100,30,30);
[Link](60,125,80,40,180,180);
[Link](25,92,15,30);
[Link](160,92,15,30);
}
}

Output:
D:\IIcs>appletviewer [Link]

Result:
Thus the program was executed and run successfully.
[Link]
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 [Link].*;
import [Link].*;
import [Link].*;
/*<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);
[Link](100,120,200,30);
g=new Scrollbar(0,0,1,0,256);
[Link](100,150,200,30);
b=new Scrollbar(0,0,1,0,256);
[Link](100,180,200,30);
ta=new TextField(50);
[Link](100,10,200,100);
add(r);
add(g);
add(b);
add(ta);
[Link](this);
[Link](this);
[Link](this);
}
public void adjustmentValueChanged(AdjustmentEvent ae)
{
int cr,cg,cb;
cr=[Link]();
cg=[Link]();
cb=[Link]();
showStatus("Color is-->r="+cr+",g="+cg+",b="+cb);
[Link](new Color(cr,cg,cb));
}
}
Output:
D:\IIcs>appletviewer [Link]

Result:
Thus the program was executed and run successfully.
[Link]
Simple key Event
Date:21.3.25

Aim:
To write a java program for simple key event.

Coding:
import javax . swing.*;
import [Link].*;
import [Link].*;
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...");
[Link](new Font ("arial",[Link],20));
add(label);
addKeyListener(this);
setFocusable(true);
setVisible(true);
}

public void keyPressed(KeyEvent e)


{
[Link]("key pressed:"+ [Link]());
}

public void keyReleased(KeyEvent e)


{
}

public void keyTyped(KeyEvent e)


{
}
public static void main(String args[])
{
new SimpleKeyEvent();
}
}
Output:
D:\IIcs>java SimpleKeyEvent

Result:
Thus the program was executed and run successfully.

Common questions

Powered by AI

The Java program implements logic to convert Fahrenheit to Celsius and vice versa. The conversion from Fahrenheit to Celsius is calculated using the formula `c = (f-32)*5/9`, where `f` is the temperature in Fahrenheit. The reverse conversion from Celsius back to Fahrenheit is attempted using a slightly incorrect formula `(c+32)*9/5` instead of the correct formula `f = c*9/5+32`. This results in an inaccurate conversion back to Fahrenheit, as indicated by the output where the input 23°F converts to -5°C and then incorrectly to 48°F. The program thereby highlights the importance of formula accuracy in conversions .

The Java program for swapping two numbers uses a temporary variable to exchange the values of two float variables, 'a' and 'b'. The key components of the program include the declaration of float variables 'a' and 'b', the assignment of their initial values, and a third variable 't' which acts as a temporary storage to facilitate their swap. Initially, the value of 'a' is assigned to 't', then 'b' is assigned to 'a', and finally 't' (which holds the initial value of 'a') is assigned to 'b'. This effectively swaps the two values without any data loss or the need for additional operations. The program outputs the values before and after the swap to demonstrate functionality .

The Java program for matrix multiplication involves reading dimensions and elements of two matrices, ensuring the number of columns of the first matrix matches the number of rows of the second matrix to proceed with multiplication. If this condition isn't met, the program exits indicating the impossibility of multiplication. When valid, nested loops multiply corresponding elements and sum the products to form a resulting matrix. This matrix result is accurately displayed by iterating through and printing each element. The program successfully demonstrates matrix multiplication, producing correct results as shown in the example where both input matrices have dimensions that comply with multiplication rules, and the output matrix correctly reflects their product .

The Java applet program achieves a scrolling text effect by repeatedly altering the text string within a thread's run loop. It continuously concatenates the first character to the end of the string and updates the display, creating a visual scrolling effect. Key graphical components include the use of the 'paint' method to redraw the applet's background and display the updated text each cycle. The combination of 'showStatus' for status messages and 'setForeground/setBackground' for color settings enhances the visual presentation. This combination of text manipulation and graphical rendering in the applet illustrates dynamic visual effects within Java applets .

The Java program demonstrates multiple inheritance by using interfaces. It defines interfaces 'stu' and 'mark', implemented by the class 'multi'. Each interface declares a method - 'info' and 'details' respectively - which are implemented to provide student information and calculate total and average marks. This approach allows the separation of concerns and capabilities across interfaces, which 'multi' can implement concurrently. By using interfaces, the program benefits from adhering to Java's single inheritance enforcement while flexibly extending a class's functional range without the complexity of class-based multiple inheritance, thereby enhancing modularity and maintainability .

The Java program's purpose is to check if two strings are anagrams, meaning they contain the same characters in a different order. It works by first comparing the lengths of the two strings; if different, the strings cannot be anagrams, and the program exits. If the lengths are the same, both strings are converted to lower case, converted to character arrays, and sorted. The final step is to compare these sorted arrays; if they are identical, the strings are anagrams, otherwise they are not. Its efficacy relies on accurate sorting and comparison which ensures detection of true anagrammatic relationships. This program highlights algorithmic efficiency and correctness in checking anagram conditions.

The Java program calculates the factorial of a number using a simple iterative method within a constructor of the 'factorial' class. When an object of this class is created, it initializes an integer 'fact' to 1 and uses a for-loop running from 1 to 'n', multiplying 'fact' by each number in this range. The program outputs the final factorial value after completing the loop. When executed with the integer 5, the program correctly outputs the factorial value of 120. This program exemplifies the straightforward implementation of factorial calculations using iteration .

The Java program for handling student details is structured utilizing a class named 'student' that encapsulates properties 'name' and 'rno' (roll number). The 'student' class contains a method 'display' to print the details. In the 'main' method, an object 's' of type 'student' is created, and its properties are initialized with 'name' = "AAA" and 'rno' = 10. The 'display' method is then called on this object to print the student details. This program demonstrates encapsulation by packaging data and related behavior within a class, and instantiates an object which carries specific values for the encapsulated properties, outputting the structured student information .

The program for generating prime numbers correctly identifies numbers that have exactly two distinct positive divisors. It takes user input for the range up to which to find prime numbers, initializes counters, and uses a nested loop to check divisibility by counting factors. The correct number of divisors (exactly 2, which indicates primality) is checked. However, logically, the program should start checking from number 2 since 1 is neither prime nor composite. The program does correct this by acknowledging that "1 is neither prime nor composite" but should structurally alter loops to avoid unnecessary checks on 1. Overall, while it performs correctly, refining loop operations for efficiency and completeness is advisable .

The Java program leverages the 'Runtime' class to execute Windows applications such as Notepad, Paint, Calculator, and Explorer. A 'switch' statement is used to respond to user input for selecting and executing the desired application. For each case, the 'Runtime.exec()' method attempts to launch the specified application, and any exceptions during this execution, such as failure to start an application, are caught by individual 'try-catch' blocks. These blocks print error messages to alert the user. This program demonstrates robust control flow management and exception handling, allowing it to recover gracefully from command execution failures .

You might also like