JAVA PRACTICAL LAB RECORD
PROGRAM1:FLOYD
import [Link];
class Floydtriangle
public static void main(String args[])
{
int rows ,number=1,counter,j;
Scanner input=new Scanner([Link]);
[Link]("enter number of row and floyd’s a triangle:");
rows=[Link]();
[Link]("Floyd’s triangle");
[Link]("***************");
for(counter=1;counter<=rows;counter++)
for(j=1;j<=counter;j++)
[Link](number+" ");
[Link]();
}
PROGRAM2:INCREMENT AND DECREMENT OPERATORS
2)Write a Java program to demonstrate increment and decrement operators.
1
import [Link];
public class IncDecDemo {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter an integer value: ");
int num = [Link]();
[Link]("Original value: " + num);
// Pre-increment: increments first, then uses the value
int x = ++num;
[Link]("After pre-increment: " + x );
// Post-increment: uses the value first, then increments
int y= num++;
[Link]("After post-increment : " + y );
// Pre-decrement: decrements first, then uses the value
int w = --num;
[Link]("After pre-decrement : " + w );
// Post-decrement: uses the value first, then decrements
int z = num--;
[Link]("After post-decrement : " + z);
PROGRAM 3 :AREA OF TRIANGLE AND CIRCLE
3) Write a java program to find the area of triangle and circle.
import [Link].*;
import [Link].*;
2
class Area
double Circlerea()
{ return(3.14*r*r);
}
double TraingleArea()
double s;
s=(a+b+c)/2;
return([Link](s*(s-a)*(s-b)*(s-c)));
}
class AreaDemo
public static void main(String args[])
Area myarea1=new Area();
Area myarea2=new Area();
double a;
myarea1.r=10;
a= [Link]();
[Link](“Area of circle is”+a);
myarea2.a=2;
myarea2.b=3;
myarea2.c=4;
a=[Link]();
[Link](“Area of traingle is”+a);
3
}
PROGRAM 4:FIND THE GRADE OF THE STUDENT
4) write a java program to check the grade of student and print the following.
Grade Print
>=90 Outstanding
>=80 Excellent
>=70 Very good
>=60 Good
>=50 Average
<50 Below average
import [Link];
class Grade
public static void main(String args[])
{
int a,b;
double avg;
Scanner obj=new Scanner([Link]);
[Link](“Enter the two marks”);
a=[Link]();
b=[Link]();
avg=(a+b)/2;
if((a>=50)&&(b>=50))
4
{
if(avg>=90)
[Link](“Outstanding”);
else if(avg>=80)
[Link](“Excellent”);
else if(avg>=70)
[Link](“Very Good”);
else if(avg>=60)
[Link](“Good”);
else if(avg>=50)
[Link](“Average”);
else
[Link](“Below Average”);
else
[Link](“Failed”);
PROGRAM 5 :BITWISE OPERATORS
5) write a java program to demonstrate bitwise operators.
class BitwiseDemo {
public static void main(String[] args) {
int a = 30;
int b = 6;
int c=0;
// Bitwise AND
5
[Link]("a & b = " + (a & b));
// Bitwise OR
[Link]("a | b = " + (a | b));
// Bitwise XOR
[Link]("a ^ b = " + (a ^ b));
// Bitwise Complement
[Link]("~a = " + (~a));
// Left shift operator
c=a<<2;
[Link]("a << 1 = " + c);
// Right shift operator
c=a>>2;
[Link]("a >> 1 = " + c);
// Shift right zero fill operator
c=a>>>2;
[Link]("a >>> 1 = " + c);
}
PROGRAM6:PALINDROME
6)Write a Java program to check whether a string is palindrome or not.
import [Link];
class Palindrome
public static boolean isPal (String s)
{
if([Link]()==0 || [Link]()==1)
6
return true;
if([Link](0)==[Link]([Link]()-1))
return isPal([Link](1,[Link]()-1));
return false;
}
public static void main(String[] args)
Scanner scanner= new Scanner([Link]);
[Link]("Enter the string for check:");
String string=scanner .nextLine();
if (isPal(string))
[Link](string+ " is a palindrome");
Else
[Link](string+ " is not a palindrome");
PROGRAM7:REVERSE OF NUMBER
7)Write a Java program to find the reverse of a number.
import [Link];
public class Reverse
public static void main(String[] args)
int num=0;
int reversenum=0;
[Link]("Input number and press enter:");
Scanner in= new Scanner([Link]);
7
num= [Link]();
while(num!=0)
reversenum= reversenum * 10;
reversenum= reversenum + num%10;
num= num/10;
[Link]("Reverse of input number is" +reversenum);
PROGRAM8:MATRIX MULTIPLICATION
8)Write a Java program to show the matrix multiplication.
import [Link];
class MatrixMultiplication
public static void main(String args[])
int m, n, p, q, sum = 0, c, d, k;
Scanner in = new Scanner([Link]);
[Link]("Enter the number of rows and columns of first matrix");
m = [Link]();
n = [Link]();
int first[][] = new int[m][n];
[Link]("Enter elements of first matrix");
for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
8
first[c][d] = [Link]();
[Link]("Enter the number of rows and columns of second matrix");
p = [Link]();
q = [Link]();
if (n != p)
[Link]("The matrices can’t be multiplied with each other.");
else
int second[][] = new int[p][q];
int multiply[][] = new int[m][q];
[Link]("Enter elements of second matrix");
for (c = 0; c < p; c++)
for (d = 0; d < q; d++)
second[c][d] = [Link]();
for (c = 0; c < m; c++)
for (d = 0; d < q; d++)
for (k = 0; k < p; k++)
{
sum = sum + first[c][k]*second[k][d];
multiply[c][d] = sum; sum = 0;
[Link]("Product of the matrices:");
for (c = 0; c < m; c++)
9
{
for (d = 0; d < q; d++)
[Link](multiply[c][d]+"\t");
[Link]("\n");
}
PROGRAM9:METHOD OVERRIDING
9) write a java program to demonstrate method overriding.
class College
{
public void move()
[Link]("college is open");
class Univ extends College
{
public void move()
[Link]("university is open too");
class demo
{
public static void main(String args[])
10
{
College a=new College();
College b=new Univ();
[Link]();
[Link]();
PROGRAM10:METHOD OVERLOADING
10) write a Java program to demonstrate method overloading.
Import [Link];
class RectangleArea
{
int length,breadth;
void getData(int length,int breadth)
[Link]=length;
[Link]=breadth;
Void getData(int length)
{
[Link]=length;
int area(int l){
int ar=l*l;
return(ar);
}
int area(int l,int b)
11
{
int ar=l*b;
return(ar);
}
}
class MethodLoad
public static void main(String args[])
Scanner in=new Scanner([Link]);
RectangleArea ra=new RectangleArea();
int ch=0;
do
[Link](“1 Rectangle”);
[Link](“2 Square”);
[Link](“3 Exit”);
[Link](“Enter your choice”);
ch=[Link]();
switch(ch)
case 1:{ [Link](“Enter length”);
int l=[Link]();
[Link](“Enter breadth”);
int b=[Link]();
[Link](l,b);
int a=[Link](l,b);
12
[Link](“Area of rectangle is “+a);
break;
case 2:{ [Link](“Enter side”);
int l=[Link]();
[Link](l);
int a=[Link](l);
[Link](“Area of square is “+a);
break;
case 3:break;
default:{
[Link](“Invalid choice”);
break;
While(ch!=3);
}
}
PROGRAM11: SORT NAMES IN AN ARRAY
11)Write a Java program to sort names in an array in ascending order.
class SortString
static String a[]={“Athira”,”Subha”,”Namitha”,”Jafna”};
public static void main(String args[])
{
13
for(int i=0;i<[Link];i++)
for(int j=i+1;j<[Link];j++)
{
if(a[i].compareTo a[j]>0)
string t=a[i];
a[i]=a[j];
a[j]=t;
}
[Link](“Names in ascending order”);
}}
PROGRAM12:ABSTRACT CLASS
12) Write a java program to find the area of a circle,rectangle and triangle using the concept of
abstract class.
import [Link].*;
abstract class Shape
{
int length, breadth, radius;
Scanner input = new Scanner([Link]);
abstract void printArea();
}
class Rectangle extends Shape
14
{
void printArea()
[Link]("*** Finding the Area of Rectangle ***");
[Link]("Enter length and breadth: ");
length = [Link]();
breadth = [Link]();
[Link]("The area of Rectangle is: " + length * breadth);
class Triangle extends Shape
{
void printArea()
[Link]("\n*** Finding the Area of Triangle ***");
[Link]("Enter Base And Height: ");
length = [Link]();
breadth = [Link]();
[Link]("The area of Triangle is: " + (length * breadth) / 2);
}
class Circle extends Shape
void printArea()
[Link]("\n*** Finding the Area of Cricle ***");
[Link]("Enter Radius: ");
15
radius = [Link]();
[Link]("The area of Cricle is: " + 3.14f * radius * radius);
}
public class AbstractClassExample
public static void main(String[] args)
Rectangle rec = new Rectangle();
[Link]();
Triangle tri = new Triangle();
[Link]();
Circle cir = new Circle();
[Link]();
PROGRAM13:SINGLE INHERITANCE
13) write a java program to demonstrate single inheritance.
import [Link];
class A
int a,b;
Scanner sc=new Scanner([Link]);
void add()
[Link](" enter two nos to add:");
a=[Link]();
16
b=[Link]();
int c=a+b;
[Link]("c="+c);
}
}
class ScannerExample extends A
int p,q;
Scanner sc=new Scanner([Link]);
void sub()
[Link](" enter two nos to add:");
p=[Link]();
q=[Link]();
int r=p-q;
[Link]("r="+r);
public static void main(String args[])
{
ScannerExample b1=new B();
[Link]();
[Link]();
PROGRAM14: MULTILEVEL INHERITANCE
14) write a java program using multilevel inheritance.
import [Link].*;
17
import [Link].*;
class Employee
int eid;
Scanner sc = new Scanner([Link]);
void display1()
[Link]("Enter the employee id");
eid =[Link]();
class Tax extends Employee
{
double hra,da,bpay,netpay;
Scanner sc = new Scanner([Link]);
void display2()
[Link]("Enter the basic pay");
bpay = [Link]();
hra = bpay*(10.0/100);
da = bpay*(30.0/100);
class Incometax extends Tax
double rate;
void display()
18
{
[Link]("Result");
netpay = (bpay+da+hra);
if((netpay>6000)&&(netpay<3000)
{
rate=(10.0/100)*netpay;
elseif ((netpay>8000)&&(netpay<10000))
rate = (20.0/100)*netpay;
else
{
rate = (300/100)*netpay;
[Link]("eid="+eid);
[Link](" Basic pay*=" +bpay);
[Link]("da=" +da);
[Link]("hra =" +hra);
[Link]("Netpay=" +netpay);
[Link](" In rate=" +rate);
public static void main(String args[])
Incometax income = new Incometax();
income.display1();
income.display2();
19
[Link]();
PROGRAM15: HYBRID INHERITANCE
15) Write a java program using hybrid inheritance.
public class Main
public static void main(String[] args)
D obj = new D();
[Link]();
}
}
// super class
class A
int a=1;
// sub class
class B extends A
int b=2;
// interface
interface C
{
int c=3;
20
}
// extends and implements togther
class D extends B implements C
{
int d = 4;
int sum = a + b + c + d;
public void display()
[Link]("The value of a is " + a);
[Link]("The value of b is " + b);
[Link]("The value of c is " + C.c);
[Link]("The value of d is " + d);
[Link]("The sum is " + sum);
PROGRAM16: USING INTERFACE
21
16) Write a java program to create an interface Myinter that connects to and disconnect from
databases Sybase and OracleDB.
22
PROGRAM17: PRINTER INTERFACE
17) write a java program which contains a Printer interface and its implementation classes to
send text to any printer.
23
24
25
PROGRAM18: MULTIPLE INHERITANCE USING MULTIPLE INTERFACE
18) write a java program to illustrate how to achieve multiple inheritance using multiple
interfaces.
26
PROGRAM19: EXCEPTION HANDLING
19)Write a program to implement exception handling using multiple catch block.
27
PROGRAM20:USER DEFINED EXECPTION
20)Write a Java program to implement a user defined Exception.
class UserException
{
28
public static void main(String args[])
try
{
throw new MyException(2);
// throw is used to create a new exception and throw it.
catch(MyException e)
[Link](e) ;
}
}
class MyException extends Exception
int a;
MyException(int b)
a=b;
}
public String toString()
return ("Exception Number = "+a) ; } }
PROGRAM 21: USE OF FINALLY
21) write a java program to show the use of finally clause in try catch statement.
29
30
PROGRAM 22: PACKAGES
22) write a java program to create a package with the name pack and store addition and
subtraction classes to it.
31
32
PROGRAM 23: STRING OPERATIONS
23)Create a string having an intial value “COCHIN” and do the following.
33
a)Display the character at the 4th position.
b)Compare it with the another string “KOTTAYAM”.
c)Concatenate
d)Find the position of ‘T” in the concatenated string.
e)Replace ‘A’ with ‘B’ in the concatenated string.
34
PROGRAM 24: STRINGBUFFER
24) Write a java program to demonstrate the length() and capacity() methods in StringBuffer.
35
PROGRAM 25: COUNT THE NO. OF WORDS,VOWELS,DIGITSAND SPACES
25) Write a java program to count the
a)Number of words.
b)Number of Vowels.
c)Number of digits.
d)Number of consonants.
36
e)Number of spaces.
37
38
PROGRAM 26: DEMONSTRATE append() METHOD
26) Write a java program to demonstrate the append() method.
39
PROGRAM 27:MULTIPLE THREADS
27) Write a program to implement multiple threads.
40
41
PROGRAM 28:NAME AND PASSWORD
28) Write a program to create two labels and two textfields for entering name and [Link]
password typed by the user in the textfield must be hidden.
42
43
PROGRAM 29:PUSH BUTTON:BACKGROUND COLOR CHANGING
29) Write a program to create push button .Arrange them using FlowLayout manager and setting
background colour for the frame depending on the button clicked by the user.
44
45
PROGRAM 30:MOVING PICTURE USING APPLET
30) Write a program to display moving picture in the applet.
46
47
PROGRAM 31:PASSING PARAMETERS TO AN APPLET
31) Write a program to pass name and salary to an applet and get the tax calculated.
48
49
PROGRAM 32:TRAFFIC LIGHT
32) Write a program to display traffic light.
/*<applet code=”[Link]” WIDTH=”250” HEIGHT=”200”></applet>*/
import [Link].*;
import [Link].*;
public class Traffic extends Applet
public void paint (Graphics g)
[Link]("Traffic signal", 20, 20);
[Link]([Link]);
[Link](100, 100, 150, 450);
[Link](100, 100, 150, 450);
[Link]([Link]);
[Link](125, 150, 100, 100);
[Link](125, 150, 100, 100);
[Link]([Link]);
[Link](125, 275, 100, 100);
[Link](125, 275, 100, 100);
[Link]([Link]);
[Link](125, 410, 100, 100);
[Link](125, 410, 100, 100);
[Link]([Link]);
[Link](165, 550, 30, 200);
[Link](165, 550, 30, 200);
}
}
50
PROGRAM 33:MOVE A STRING USING APPLET
33) Write a program to move a string in an applet.
51
PROGRAM 34:CALCULATOR
34) Write a program to prepare a basic calculator in applet.
import [Link].*;
import [Link].*;
/*java header files required to run this program*/
52
import [Link].*;
/*<applet code=”[Link]” WIDTH=”260” HEIGHT=”310”></applet>*/
public class calculator extends Applet implements ActionListener
{
TextField t1;
Button button1, button2, button3, button4, button5, button6, button7, button8, button9, button0;
/*All the buttons required in a Calculator Program in Java Using AWT and Applet*/
Button add, sub, mul, div, eql, dot; /*Operations we are going to perform*/
String msg = "", tmp;
int a, b;
public void init()
{ setLayout(null);
t1 = new TextField(20);
button1 = new Button("1");
button2 = new Button("2");
button3 = new Button("3");
button4 = new Button("4");
button5 = new Button("5");
button6 = new Button("6");
button7 = new Button("7");
button8 = new Button("8");
button9 = new Button("9");
button0 = new Button("0");
/*Initilizing a 0 to 9 number in buttons */
add = new Button("+");
sub = new Button("-");
div = new Button("/");
53
mul = new Button("*");
dot = new Button(".");
eql = new Button("=");
/*Initilizing all the operator to perform all the calculations in java AWT calculator*/
add(t1); /*result view window */
add(button7);
add(button8);
add(button9);
add(div);
/*First row in a calculator*/
add(button4);
add(button5);
add(button6);
add(mul);
/*Second row in a calculator*/
add(button1);
add(button2);
add(button3);
add(sub);
/*Third row in a calculator*/
add(dot);
add(button0);
add(eql);
add(add);
/*Forth row in a calculator*/
[Link](30, 30, 200, 40);
[Link](30, 80, 44, 44);
54
[Link](82, 80, 44, 44);
[Link](134, 80, 44, 44);
[Link](30, 132, 44, 44);
[Link](82, 132, 44, 44);
[Link](134, 132, 44, 44);
[Link](30, 184, 44, 44);
[Link](82, 184, 44, 44);
[Link](134, 184, 44, 44);
[Link](30, 236, 44, 44);
[Link](82, 236, 44, 44);
[Link](134, 236, 44, 44);
[Link](186, 236, 44, 44);
[Link](186, 184, 44, 44);
[Link](186, 132, 44, 44);
[Link](186, 80, 44, 44);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
//[Link](this);
//[Link](this);
55
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
public void actionPerformed(ActionEvent ae)
String str = [Link]();
if ([Link]("+") || [Link]("-") || [Link]("*") || [Link]("/"))
String str1 = [Link]();
tmp = str;
a = [Link](str1);
msg = "";
else if ([Link]("="))
String str2 = [Link]();
b = [Link](str2);
int sum = 0;
if (tmp == "+")
sum = a + b;
else if (tmp == "-")
sum = a - b;
else if (tmp == "*")
sum = a * b;
56
else if (tmp == "/")
sum = a / b;
String str1 = [Link](sum);
[Link]("" + str1);
msg = "";
Else
//String [Link]();
//str += [Link]();
msg += str; [Link]("" + msg);
}
}
public void paint(Graphics g)
[Link]([Link]);
[Link](20, 20, 220, 270);
}
PROGRAM 35:SMILEY FACE
35) Write a program to draw a smiley face in a frame using the methods of Graphics.
57
58
PROGRAM36:GRID LAYOUT
import [Link].*;
import [Link].*;
public class MyGridLayout{
JFrame f;
MyGridLayout(){ f=new JFrame();
59
JButton b1=new JButton("1");
JButton b2=new JButton("2");
JButton b3=new JButton("3");
JButton b4=new JButton("4");
JButton b5=new JButton("5");
JButton b6=new JButton("6");
JButton b7=new JButton("7");
JButton b8=new JButton("8");
JButton b9=new JButton("9");
// adding buttons to the frame
[Link](b1);
[Link](b2);
[Link](b3);
[Link](b4);
[Link](b5);
[Link](b6);
[Link](b7);
[Link](b8);
[Link](b9);
// setting grid layout of 3 rows and 3 columns
[Link](new GridLayout(3,3));
[Link](300,300);
[Link](true);
public static void main(String[] args)
{
new MyGridLayout();
60
}
PROGRAM37:APPLICATION FORM
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
public class Registration extends JFrame implements ActionListener
JLabel l1, l2, l3, l4, l5, l6, l7, l8;
JTextField tf1, tf2, tf5, tf6, tf7;
JButton btn1, btn2;
JPasswordField p1, p2;
Registration()
setVisible(true);
setSize(700, 700);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Registration Form in Java");
l1 = new JLabel("Registration Form in Windows Form:");
[Link]([Link]);
[Link](new Font("Serif", [Link], 20));
l2 = new JLabel("Name:");
l3 = new JLabel("Email-ID:");
l4 = new JLabel("Create Passowrd:");
61
l5 = new JLabel("Confirm Password:”);
l6 = new JLabel("Country:");
l7 = new JLabel("State:");
l8 = new JLabel("Phone No:");
tf1 = new JTextField();
tf2 = new JTextField();
p1 = new JPasswordField();
p2 = new JPasswordField();
tf5 = new JTextField();
tf6 = new JTextField(); tf7 = new JTextField();
btn1 = new JButton("Submit");
btn2 = new JButton("Clear");
[Link](this);
[Link](this);
[Link](100, 30, 400, 30);
[Link](80, 70, 200, 30);
[Link](80, 110, 200, 30);
[Link](80, 150, 200, 30);
[Link](80, 190, 200, 30);
[Link](80, 230, 200, 30);
[Link](80, 270, 200, 30);
[Link](80, 310, 200, 30);
[Link](300, 70, 200, 30);
[Link](300, 110, 200, 30);
[Link](300, 150, 200, 30);
[Link](300, 190, 200, 30);
62
[Link](300, 230, 200, 30);
[Link](300, 270, 200, 30);
[Link](300, 310, 200, 30);
[Link](50, 350, 100, 30);
[Link](170, 350, 100, 30);
add(l1);
add(l2);
add(tf1);
add(l3);
add(tf2);
add(l4);
add(p1);
add(l5);
add(p2);
add(l6);
add(tf5);
add(l7);
add(tf6);
add(l8);
add(tf7);
add(btn1);
add(btn2);
public void actionPerformed(ActionEvent e)
{
63
if ([Link]() == btn1)
int x = 0;
String s1 = [Link]();
String s2 = [Link]();
char[] s3 = [Link]();
char[] s4 = [Link]();
String s8 = new String(s3);
String s9 = new String(s4);
String s5 = [Link]();
String s6 = [Link]();
String s7 = [Link]();
if ([Link](s9))
try
[Link]("[Link]");
Connection con =[Link]("jdbc:oracle:thin:@mcndesktop07:1521:xe",
"sandeep", "welcome");
PreparedStatement ps = [Link]("insert into reg values(?,?,?,?,?,?)");
[Link](1, s1);
[Link](2, s2);
[Link](3, s8);
[Link](4, s5);
[Link](5, s6);
64
[Link](6, s7);
ResultSet rs = [Link]();
x++;
if (x > 0)
{
[Link](btn1, "Data Saved Successfully");
catch (Exception ex)
[Link](ex);
}
}
else
[Link](btn1, "Password Does Not Match");
else
{
[Link]("");
[Link]("");
[Link]("");
[Link]("");
[Link]("");
[Link]("");
[Link]("");
}
}
65
public static void main(String args[])
new Registration();
}
}
66