Java Programs PDF
Java Programs PDF
EX.NO : 1
TEMPERATURE CONVERSION
DATE :
AIM:
Write a java program to input Celsius value from user and convert temperature from
user and convert temperature from Celsius to Fahrenheit.
FORMULA:
C =(f-32.0)*5/9.0
F=((9*c)/5.0)+32.0
PROCEDURE:
Create a class with class name prg1 and declare needed variables with their data types.
Calculate Celsius value for given Fahrenheit value using the formula
C=(f-32.0)*5/9.0
Calculate Fahrenheit value for given Celsius value using the formula.
F=((9*c)/5.0)+32.0
Compile and run the program using command prompt,as javac prg1.java and
java prg1.
2
PROGRAM:
//prg1.java
import java.util.Scanner;
class prg1
double f,c;
2.Celsius to Fahrenheit\n");
int ch=sc.nextInt();
switch(ch)
case 1:
f=sc.nextDouble();
c=(f-32.0)*5/9.0;
break;
case 2:
c=sc.nextDouble();
f=((9*c)/5.0)+32.0;
3
System.out.println("Fahrenheit Temperature is="+c);
break;
default:
4
OUTPUT:
5
RESULT:
6
EX.NO : 2
AIM:
To write a java program to perform complex number addition and subtraction using class
and object.
PROCEDURE:
Create a class with class name Complexno and its number function
addComp(),subtractComp(),printComplexNumber().
Define the complex class member functions associated with its task.
Create another class with name complex create fine objects(C1,C2,C3) for class comp.
Compile and run the program using command prompt,as javac Complexno.java and
java Complexno.
7
PROGRAM:
//complexno.java
import java.util.Scanner;
System.out.println("*************************************\n");
System.out.println("*************************************\n");
C1.printComplexNumber();
C2.printComplexNumber();
C3 =C3.addComp(C1,C2);
System.out.println("Sum of");
C3.printComplexNumber();
C3 =C3.subtractComp(C1,C2);
System.out.println("Difference of");
C3.printComplexNumber();
class Complex
8
{
int real,imaginary;
Complex()
real=tempReal;
imaginary=tempImaginary;
temp.real= C1.real+C2.real;
return temp;
temp.real= C1.real-C2.real;
return temp;
void printComplexNumber()
9
{
System.out.println("Complex number:"
+ real+"+"
+imaginary+"i");
10
OUTPUT:
11
RESULT:
12
EX.NO : 3
TO PERFORM VOLUME CALCULATION USING
METHOD OVERLOADING
DATE :
AIM:
FORMULA:
Cube volume=x3(x*x*x)
Cylinder volume=3.14xr*r*h(πr2h)
Rectangle volume=lbh(l*b*h)
PROCEDURE:
13
PROGRAM:
//volume.java
System.out.println("*****************************************************");
System.out.println("****************************************************");
double rectangleBox=overload.area_rectangle(5,8,9);
System.out.println("");
double cube=overload.area_cube(5);
System.out.println("");
double cylinder=overload.area_cylinder(6,12);
class Overload
14
{
return l*w*h;
double area_cube(float l)
return l*l*l;
return 3.1416*r*r*h;
15
OUTPUT:
16
RESULT:
17
EX.NO : 4
USING COMMAND LINE ARGUMENT TEXT IF THE
DATE : GIVEN STRING IS POLYNDROME OR NOT
AIM:
Write a java program using command line argument text if the given string palindrome or not.
PROCEDURE:
Create a class with class name palindrome and declare needed variable, with their data
type.
Calculate the palindrome or not using the for loop and equalsIgnoreCase() string
function.
Compile and run the program using command prompt,as javac palindrome.java and
java palindrome.
18
PROGRAM:
//palindrome.java
import java.util.Scanner;
String a,b="";
System.out.println("************************************");
System.out.println("********************************");
a=s.nextLine();
int n=a.length();
for(int i=n-1;i>=0;i--)
b=b+a.charAt(i);
if(a.equalsIgnoreCase(b)) {
else{
19
OUTPUT:
20
RESULT:
21
EX.NO : 5
MANIPULATING STRING FUNCTIONS
DATE :
AIM:
Write a java program to perform the string functions length, string concatentation, lower case,
PROCEDURE:
Create a class name String and declare needed variables with their data types.
Manipulating the string by using string methods like length(0, concat(), LowerCase(),
UpperCase(), replace().
Compile and run the program using command prompt,as javac string.java and
java string.
22
PROGRAM:
//stringfunctions.java
import java.util.Scanner;
String s1,s2;
System.out.println("***********************************************");
System.out.println("***********************************************");
s1=s.nextLine();
System.out.println("/n");
System.out.println("\n");
System.out.println("\n");
String is:"+s1.toLowerCase());
System.out.println("\n");
String is:"+s1.toUpperCase());
23
System.out.println("/n");
String is:"+s1.replace('h','t'));
System.out.println("\n");
24
OUTPUT:
25
RESULT:
26
EX.NO : 6
NAME SORTING
DATE :
AIM:
Write a java program to perform name sorting the names in reverse order. If the name contains
PROCEDURE:
Declare a class name with class name Sorting_names and declare needed variable with
Sort the given set of array using bubble sort technique and string comparison function
compareTo().
Check the string validation technique using the matches (“[a-zA-Z}*$+”]. If the name
contains any numeric value then display the given name is Invalid Name.
Compile and run the program using command prompt,as javac sorting_names.java and
java sorting_names.
27
PROGRAM:
//sorting.java
import java.util.Scanner;
String temp;
System.out.println("****************************************************\n");
int i,j;
int n=s.nextInt();
s.nextLine();
for(i=0;i<n;i++)
System.out.println("Enter Name["+(i+1)+"]:");
names[i]=s.nextLine();
System.out.println("\n");
28
for(i=0;i<n;i++)
if(names[i].matches("^[a-z A-Z]*$+"))
System.out.println("String["+(i+1)+"]is valid!");
System.out.println("\n");
for(i=0;i<n;i++)
for(j=1;j<n;j++)
if(names[j-1].compareTo(names[j])<0)
temp=names[j-1];
names[j-1]=names[j];
names[j]=temp;
System.out.println("............................................................");
for(i=0;i<n;i++)
System.out.println(names[i]);
}}
29
OUTPUT:
30
RESULT:
31
EX.NO : 7
BUILT-IN EXCEPTION
DATE :
AIM:
Write a java program to demonstrate the use of any two built in exception in java.
PROCEDURE:
1.Arithmetic exception
Based on our exception result any one exception & finally block will be executed.
Compile and run the program using command prompt,as javac built.java and
java built.
32
PROGRAM: //built.java
int a[]={5,10};
int b=5;
System.out.println("*************************************");
System.out.println("BUILT-IN EXCEPTION");
System.out.println("*************************************");
try
int x=a[1]/b-a[1];
int y=a[1]/(5-5);
catch(ArithmeticException e)
System.out.println("Division by Zero");
catch(ArrayIndexOutOfBoundsException e)
33
OUTPUT:
34
RESULT:
35
EX.NO : 8
MATRIX MULTIPLICATION
DATE :
AIM:
Write a java program to perform matrix multiplication using class and object.
PROCEDURE:
Read an inputs from keyboard for 3*3 A matrix and display it.
Read an inputs from keyboard for 3*3 B matrix and display it.
Perform multiplication operation between matrix A and B and Store multiplication result
of matrix C.
Compile and run the program using command prompt,as javac Matrix.java and
java Matrix.
36
PROGRAM:
//matrix.java
import java.util.Scanner;
mat m1,m2,m3;
m1=new mat();
m2=new mat();
m3=new mat();
System.out.println("***********************************");
System.out.println(" **********************************");
m1.get();
m1.print();
m2.get();
m2.print();
m3.mul(m1,m2);
System.out.println("............................................................");
37
m3.print();
class mat
int i,j;
void get()
//i=s.nextInt();
//j=s.nextInt();
for(i=0;i<3;i++)
for(j=0;j<3;j++)
a[i][j]=s.nextInt();
void print()
for(i=0;i<3;i++)
for(j=0;j<3;j++)
38
{
System.out.print(" \t"+a[i][j]);
System.out.println(" \n");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
a[i][j]=a[i][j]+m1.a[i][j]*m2.a[i][j];
39
OUTPUT:
40
RESULT:
41
EX.NO : 9
MULTILEVEL INHERITANCE FOR STUDENT DETAILS
DATE :
AIM:
PROCEDURE:
Calculate two subject marks and store the result inm total variable.
Compile and run the program using command prompt,as javac Multilevel_student.java
42
PROGRAM:
//Multilevel_student.java
import java.util.Scanner;
total=mark1+mark2;
System.out.println("Total:"+total);
System.out.println("*****************************************************");
System.out.println("****************************************************");
f.setstud();
f.setmarks();
f.calc();
f.putstud();
43
f.putmarks();
f.puttotal();
class students
sno=s.nextInt();
s.nextLine();
sname=s.nextLine();
44
{
int mark1,mark2;
mark1= s.nextInt();
mark2=s.nextInt();
System.out.println("Mark1 : "+mark1);
System.out.println("Mark2 : "+mark2);
45
OUTPUT:
46
RESULT:
47
EX.NO : 10
MULTIPLE INHERITANCE (Interface) FOR PAYROLL
DATE : PROCESSING
AIM:
PROCEDURE:
Compile and run the program using command prompt,as javac payroll.java and
Java payroll
48
PROGRAM:
//payroll.java
import java.util.Scanner;
System.out.println("****************************************************");
System.out.println("*****************************************************");
obj.get();
obj.display();
obj.get_hra();
obj.disp_hra();
obj.gross_sal();
double ta=800.0;
49
void gross_sal();
float basic_sal;
void get()
name=s.nextLine();
//s.nextLine();
basic_sal=s.nextFloat();
System.out.println("Name of Employee:"+name);
float hra;
void get_hra()
50
{
hra=s.nextFloat();
System.out.println(".........................................................");
void disp_hra()
display();
double gross_sal=basic_sal+ta+da+hra;
System.out.println("TA of Employee:"+ta);
51
OUTPUT:
52
RESULT:
53
EX.NO : 11
AREA CALCULATION OF SHAPES (Circle, Rectangle)
DATE : USING INTERFACE
AIM:
Write a java program to perform area calculation of different shapes using interface.
PROCEDURE:
Compile and run the program using command prompt,as javac area.java and
java area .
54
PROGRAM:
//area.java
import java.util.Scanner;
import java.lang.*;
System.out.println("***************************************************");
System.out.println("*****************************************************");
obj.input();
obj.area();
interface Shape
void input();
void area();
55
int r=0;
double pi=3.14,ar=0;
r=s.nextInt();
ar=pi*r*r;
System.out.println("Area of Circle:"+ar);
int length=0,breadth=0;
double ar;
super.input();
length=s.nextInt();
56
breadth=s.nextInt();
super.area();
ar=length*breadth;
System.out.println("Area of Rectangle:"+ar);
57
OUTPUT:
58
RESULT:
59
EX.NO : 12
ARITHMETIC OPERATIONS USING PACKAGE
DATE :
AIM:
Write a java program to perform arithmetic operations using package with package name
arithmetic.
PROCEDURE:
Create a package with package name arithmetic and create a class with class name
operation.
Define the operation class for Calculate addition, subtraction, multiplication, division,
Compile and run the program using command prompt,as javac –d . operation.java &
60
PROGRAM:
FILE 1:
//add.java
package mypack;
import java.util.Scanner;
int a,b,c,d,e,f,g,n,q;
a=s.nextInt();
b=s.nextInt();
c=a+b;
System.out.println("ADDITION RESULT:"+c);
d=a-b;
System.out.println("SUBTRACTION RESULT:"+d);
e=a*b;
System.out.println("MULTIPLICATION RESULT:"+e);
f=a/b;
System.out.println("DIVISION RESULT:"+f);
g=a%b;
61
System.out.println("MODULO DIVISION RESULT:"+g);
FILE 2:
//myclass.java
import mypack.add;
System.out.println("******************************");
obj.process();
62
OUTPUT:
63
RESULT:
64
EX.NO : 13
GENERATE FIBONACCI SERIES & PERFECT NUMBERS
DATE : BETWEEN LIMIT USING EXCEPTION
AIM:
Write a java program to create atwo threads such that one of the thread generate Fibonacci
series and another generate perfect numbers between two given limits.
PROCEDURE:
Create a main class with class name fibo_perfect for generate Fibonacci series.
Create another class with class name perfect for generate perfect number.
Create an object t1, t2 for invoke fib, perfect class members with the help of start() and
sleep() Functions.
Compile and run the program using command prompt,as javac fibo_perfect.java and
java fibo_perfect.
65
PROGRAM:
//fibo.java
import java.util.Scanner;
try{
t1.start();
t1.sleep(5000);
t2.start();
catch(Exception e){}
try
int a=0,b=1,c=0;
66
System.out.println("Enter the limit");
int n=s.nextInt();
System.out.println("Fibonacci series:");
while(n>0)
System.out.println(c);
a=b;
b=c;
c=a+b;
n=n-1;
catch(Exception e){}
try
//System.out.println("Perfect no is");
int n=s.nextInt();
67
for(int j=1;j<n;j++)
int counter=0;
for(int i=1;i<j;i++)
if(j%i==0)
counter=counter+i;
if(counter==j)
catch(Exception e){}
68
OUTPUT:
69
RESULT:
70
EX.NO : 14
MARKS OUT OF BOUND EXCEPTION
DATE :
AIM:
Write a java program to define an exception called: Marks out of bound exception that is
thrown if the entered marks are greater than 100.
PROCEDURE:
100 or not.
If the mark is lower than 100 then display ‘Congrats: The Mark you gave Correct’
message.
Compile and run the program using command prompt,as javac Marks.java and
java Marks.
71
PROGRAM:
//Marks.java
import java.util.Scanner;
import java.lang.Exception;
String name;
int mark;
System.out.println("\n");
System.out.println("******************************************************");
System.out.println("******************************************************");
name=s.nextLine();
mark=s.nextInt();
if(mark>100)
try
System.out.println("\n");
72
catch(ArrayIndexOutOfBoundsException e){}
if(mark<=100)
System.out.println("\n");
73
OUTPUT:
74
RESULT:
75
EX.NO : 15
WRAPPER CLASS
DATE :
AIM:
Write a java program to convert one data type value into other type using Wrapper class
PROCEDURE:
Read an integer input fron keyboard for convert into binary value.
Compile and run the program using command prompt,as javac Wrapper.java and
java Wrapper.
76
PROGRAM:
//Wrapper.java
import java.util.Scanner;
System.out.println("********************");
System.out.println("********************");
int a;
a=s.nextInt();
String binary=Integer.toBinaryString(a);
System.out.println("Binary value:"+binary);
77
OUTPUT:
78
RESULT:
79
EX.NO : 16
BYTE STREAM
DATE :
AIM:
PROCEDURE:
Create a class with class name Byte and declare needed variables with their data types.
Initialize the array values as {1,2,3,4} for variable ‘array’ as byte type.
Compile and run the program using command prompt,as javac Byte.java and
java Byte.
80
PROGRAM: //Byte.java
import java.io.ByteArrayInputStream;
byte[]array={1,2,3,4};
System.out.println("************************");
System.out.println("**********************");
try
for(int i=0;i<array.length;i++)
int data=input.read();
System.out.println(data +",");
input.close();
catch(Exception e)
e.getStackTrace();
}}}
81
OUTPUT:
82
RESULT:
83
EX.NO : 17
CHARACTER STTREAM
DATE :
AIM:
PROCEDURE:
Create a class with class name Char and declare needed variables with their data types.
type.
Compile and run the program using command prompt,as javac Char.java and
java Char.
84
PROGRAM:
//Char.java
import java.io.*;
System.out.println("*******************************");
System.out.println("********************************");
try
char[]charArray={'W','E','L','C','O','M','E'};
writer.write(charArray);
writer.flush();
catch(Exception e)
System.out.println(e);
85
OUTPUT:
86
RESULT:
87
EX.NO : 18
DRAW SHAPES USING APPLET
DATE :
AIM:
Write a Java Applet program to draw the following shapes: Cone, Cylinder, Square Inside a
PROCEDURE:
Compile and run the program using command prompt,as javac draw_shapes.java and
appletviewer draw_shapes.java.
88
PROGRAM:
FILE 1:
//draw_shapes.java
import java.applet.Applet;
import java.awt.*;
import java.applet.*;
/*Cylinder*/
g.drawString("(a).Cylinder",10,110);
g.drawOval(10,10,50,10);
g.drawOval(10,80,50,10);
g.drawLine(10,15,10,85);
g.drawLine(60,15,60,85);
/*Cube*/
g.drawString("(b).Cube",95,110);
g.drawRect(80,10,50,50);
g.drawRect(95,25,50,50);
g.drawLine(80,10,95,25);
g.drawLine(130,10,145,25);
g.drawLine(80,60,95,75);
g.drawLine(130,60,145,75);
89
/*Square inside a Circle*/
g.drawOval(180,10,80,80);
g.drawRect(192,22,55,55);
g.drawRect(290,10,80,80);
g.drawOval(290,10,80,80);
/*<html>
</html>*/
FILE 2:
//draw_shapes.html
<html>
</html>
90
OUTPUT:
91
RESULT:
92
EX.NO : 19
DESIGN A SIMPLE CALCULATOR
DATE :
AIM:
PROCEDURE:
Design the calculator by using Button,Textbox, frame properties with the help of init()
method.
Compile and run the program using command prompt,as javac Calculator.java and
appletviewer Calculator.java.
93
PROGRAM:
FILE 1:
//calculator.java
import java.util.Scanner;
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16;
String s1="",s2;
Frame f;
Panel p2;
TextField t;
int n1,n2;
setLayout(new BorderLayout());
t=new TextField();
p2=new Panel();
p2.setLayout(new GridLayout(4,4));
b1 =new Button("1");
b1.addActionListener(this);
94
b2.addActionListener(this);
b3.addActionListener(this);
b4=new Button("+");
b4.addActionListener(this);
b5=new Button("4");
b5.addActionListener(this);
b6=new Button("5");
b6.addActionListener(this);
b7=new Button("6");
b7.addActionListener(this);
b8=new Button("-");
b8.addActionListener(this);
b9=new Button("7");
b9.addActionListener(this);
b10=new Button("8");
b10.addActionListener(this);
b11=new Button("9");
b11.addActionListener(this);
b12=new Button("*");
b12.addActionListener(this);
b13=new Button("c");
b13.addActionListener(this);
b14=new Button("0");
b14.addActionListener(this);
95
b15=new Button("/");
b15.addActionListener(this);
b16=new Button("=");
b16.addActionListener(this);
add(t,"North");
p2.add(b1);
p2.add(b2);
p2.add(b3);
p2.add(b4);
p2.add(b5);
p2.add(b6);
p2.add(b7);
p2.add(b8);
p2.add(b9);
p2.add(b10);
p2.add(b11);
p2.add(b12);
p2.add(b13);
p2.add(b14);
p2.add(b15);
p2.add(b16);
add(p2);
96
String str=e1.getActionCommand();
if(str.equals("+")||str.equals("-")||str.equals("*")||str.equals("/"))
String str1=t.getText();
s2=str;
n1=Integer.parseInt(str1);
s1="";
else if(str.equals("="))
String str2=t.getText();
n2=Integer.parseInt(str2);
int sum=0;
if(s2=="+")
sum=n1+n2;
else if(s2=="-")
sum=n1-n2;
else if(s2=="*")
sum=n1*n2;
else if(s2=="/")
sum=n1/n2;
String str1=String.valueOf(sum);
t.setText(""+str1);
s1="";
97
else if(str.equals("c"))
t.setText("");
else
s1+=str;
t.setText(""+s1);
/*<html>
</html>*/
FILE 2:
//calculator.html
<html>
</html>
98
OUTPUT:
99
RESULT:
100
EX.NO : 20
BALL BOUNCING USING ANIMATION
DATE :
AIM:
PROCEDURE:
Save the program file1 as ball bouncing.java & File2 as ball bouncing.html.
Compile and run the program using command prompt,as javac ball_bouncing.java and
appletviewer ball_bouncing.java.
101
PROGRAM:
FILE 1:
//ball_bouncing.java
import java.applet.Applet;
import java.applet.*;
import java.awt.*;
// create thread.
Thread t;
boolean stopFlag;
t= new Thread(this);
stopFlag=false;
t.start();
g.setColor(Color.green);
102
public void run()
while(true)
if(stopFlag)
break;
x += dx; y += dy;
try
Thread.sleep(100);
catch(Exception e)
System.out.println(e);
repaint();
103
stopFlag=true;
t=null;
/*<html>
</html>*/
FILE 2:
//ball_bouncing.html
<html>
</html>
104
OUTPUT:
105
RESULT:
106
107