Java Programming Lab Manual
Java Programming Lab Manual
com
Specworld.in jntuworld.xyz
smartworld.asia smartzworld.com
PROGRAM 1
import java.lang.*;
class student
{
String name;
int regno;
int marks1,marks2,marks3;
// null constructor
student()
{
name="raju";
regno=12345;
marks1=56;
marks2=47;
marks3=78;
}
// parameterized constructor
student(String n,int r,int m1,int m2,int m3)
{
name=n;
regno=r;
marks1=m1;
marks2=m2;
marks3=m3;
}
// copy constructor
student(student s)
{
name=s.name;
regno=s.regno;
marks1=s.marks1;
marks2=s.marks2;
marks3=s.marks3;
}
void display()
{
System.out.println(name + "\t" +regno+ "\t" +marks1+ "\t" +marks2+ "\t" + marks3);
}
}
class studentdemo
{
public static void main(String arg[])
{
student s1=new student();
Specworld.in jntuworld.xyz
smartworld.asia smartzworld.com
****************************OUTPUT*************************************
c:\jdk1.6.0_26\bin>javac studentdemo.java
c:\jdk1.6.0_26\bin>java studentdemo
raju 12345 56 47 78
john 34266 58 96 84
raju 12345 56 47 78
Specworld.in jntuworld.xyz
smartworld.asia smartzworld.com
PROGRAM 2
/* Write a Java Program to define a class, define instance methods for setting and
Retrieving values of instance variables and instantiate its object.*/
import java.lang.*;
class emp
{
String name;
int id;
String address;
void getdata(String name,int id,String address)
{
this.name=name;
this.id=id;
this.address=address;
}
void putdata()
{
System.out.println("Employee details are :");
System.out.println("Name :" +name);
System.out.println("ID :" +id);
System.out.println("Address :" +address);
}
}
class empdemo
{
public static void main(String arg[])
{
emp e=new emp();
e.getdata("smith",76859,"gulbarga");
e.putdata();
}
}
**************************************OUTPUT*****************************
c:\jdk1.6.0_26\bin>javac empdemo.java
c:\jdk1.6.0_26\bin>java empdemo
Specworld.in jntuworld.xyz
smartworld.asia smartzworld.com
PROGRAM 3
/* Write a Java Program to define a class, define instance methods and overload them and use
them for dynamic method invocation.*/
import java.lang.*;
class add
{
void display(int a,int b)
{
int c=a+b;
System.out.println("The sum of " + a + " & " + b + " is " + c);
}
class add_demo
{
public static void main(String arg[])
{
add obj=new add();
obj.display(10,20);
obj.display(10.2,20.2);
}
}
**************************************OUTPUT*****************************
c:\jdk1.6.0_26\bin>javac add_demo.java
c:\jdk1.6.0_26\bin>java add_demo
Specworld.in jntuworld.xyz
smartworld.asia smartzworld.com
PROGRAM 4
import java.lang.*;
class parent
{
int m;
void get_m(int m)
{
this.m=m;
}
void display_m()
{
System.out.println("This is from parent : m = " +m);
}
}
class child extends parent
{
int n;
void get_n(int n)
{
this.n=n;
}
void display_n()
{
System.out.println("This is from child : n = " +n);
}
}
class childdemo
{
public static void main(String arg[])
{
child c=new child();
c.get_m(10);
c.get_n(20);
c.display_m();
c.display_n();
}
}
**************************OUTPUT*******************************
C:\jdk1.6.0_26\bin>javac childdemo.java
C:\jdk1.6.0_26\bin>java childdemo
Specworld.in jntuworld.xyz
smartworld.asia smartzworld.com
PROGRAM 5
import java.lang.*;
class outer
{
int m=10;
class inner
{
int n=20;
void display()
{
System.out.println("m = "+m);
System.out.println("n = "+n);
}
}
}
class nesteddemo
{
public static void main(String arg[])
{
outer outobj=new outer();
outer.inner inobj=outobj.new inner();
inobj.display();
}
}
****************************OUTPUT*************************************
C:\jdk1.6.0_26\bin>javac nesteddemo.java
C:\jdk1.6.0_26\bin>java nesteddemo
m = 10
n = 20
Specworld.in jntuworld.xyz
smartworld.asia smartzworld.com
PROGRAM 6
import java.lang.*;
class Employee
{
private String name;
private double salary;
public Employee(String n, double s)
{
name = n;
salary = s;
}
public void print()
{
System.out.println(name + " " + salary);
}
}
******************OUTPUT****************************
C:\jdk1.6.0_26\bin>javac EmployeeTest.java
C:\jdk1.6.0_26\bin>java EmployeeTest
Specworld.in jntuworld.xyz
smartworld.asia smartzworld.com
PROGRAM 7 (A)
Write a Java program to practice using String class and its methods.
import java.lang.String;
class stringdemo
{
public static void main(String arg[])
{
String s1=new String("gpt gulbarga");
String s2="GPT GULBARGA";
System.out.println(" The string s1 is : " +s1);
System.out.println(" The string s1 is : " +s2);
System.out.println(" Length of the string s1 is : " +s1.length());
System.out.println(" The first accurence of r is at the position : " +s1.indexOf('r'));
System.out.println(" The String in Upper Case : " +s1.toUpperCase());
System.out.println(" The String in Lower Case : " +s1.toLowerCase());
System.out.println(" s1 equals to s2 : " +s1.equals(s2));
System.out.println(" s1 equals ignore case to s2 : " +s1.equalsIgnoreCase(s2));
int result=s1.compareTo(s2);
System.out.println("After compareTo()");
if(result==0)
System.out.println( s1 + " is equal to "+s2);
else if(result>0)
System.out.println( s1 + " is greather than to "+s2);
else
System.out.println( s1 + " is smaller than to "+s2);
System.out.println(" Character at an index of 6 is :" +s1.charAt(6));
String s3=s1.substring(4,12);
System.out.println(" Extracted substring is :"+s3);
System.out.println(" After Replacing g with a in s1 : " + s1.replace('g','a'));
String s4=" This is a book ";
System.out.println(" The string s4 is :"+s4);
System.out.println(" After trim() :"+s4.trim());
}
}
*************************OUTPUT************************************
c:\jdk1.6.0_26\bin>javac stringdemo.java
c:\jdk1.6.0_26\bin>java stringdemo
Specworld.in jntuworld.xyz
smartworld.asia smartzworld.com
10
Specworld.in jntuworld.xyz
smartworld.asia smartzworld.com
PROGRAM 7 (B)
Write a Java program to practice using String Buffer class and its methods.
import java.lang.String;
class stringbufferdemo
{
public static void main(String arg[])
{
StringBuffer sb=new StringBuffer("This is my college");
System.out.println("This string sb is : " +sb);
System.out.println("The length of the string sb is : " +sb.length());
System.out.println("The capacity of the string sb is : " +sb.capacity());
System.out.println("The character at an index of 6 is : " +sb.charAt(6));
sb.setCharAt(3,'x');
System.out.println("After setting char x at position 3 : " +sb);
System.out.println("After appending : " +sb.append(" in gulbarga "));
System.out.println("After inserting : " +sb.insert(19,"gpt "));
System.out.println("After deleting : " +sb.delete(19,22));
}
}
**********************************OUTPUT*********************************
c:\jdk1.6.0_26\bin>javac stringbufferdemo.java
c:\jdk1.6.0_26\bin>java stringbufferdemo
11
Specworld.in jntuworld.xyz
smartworld.asia smartzworld.com
PROGRAM 8
import java.lang.*;
import java.util.Vector;
import java.util.Enumeration;
class vectordemo
{
public static void main(String arg[])
{
Vector v=new Vector();
v.addElement("one");
v.addElement("two");
v.addElement("three");
v.insertElementAt("zero",0);
v.insertElementAt("oops",3);
v.insertElementAt("four",5);
System.out.println("Vector Size :"+v.size());
System.out.println("Vector apacity :"+v.capacity());
System.out.println(" The elements of a vector are :");
Enumeration e=v.elements();
while(e.hasMoreElements())
System.out.println(e.nextElement() +" ");
System.out.println();
System.out.println("The first element is : " +v.firstElement());
System.out.println("The last element is : " +v.lastElement());
System.out.println("The object oops is found at position : "+v.indexOf("oops"));
v.removeElement("oops");
v.removeElementAt(1);
System.out.println("After removing 2 elements ");
System.out.println("Vector Size :"+v.size());
System.out.println("The elements of vector are :");
for(int i=0;i<v.size();i++)
System.out.println(v.elementAt(i)+" ");
}
}
**************************OUTPUT******************************
C:\jdk1.6.0_26\bin>javac vectordemo.java
C:\jdk1.6.0_26\bin>java vectordemo
Vector Size :6
Vector apacity :10
The elements of a vector are :
zero
one
two
oops
12
Specworld.in jntuworld.xyz
smartworld.asia smartzworld.com
three
four
13
Specworld.in jntuworld.xyz
smartworld.asia smartzworld.com
PROGRAM 9
}
static float loan(float P,float I,int y)
{
int year=1;
float sum=P;
while(year<=y)
{
sum=sum+(P*I)/100;
year++;
}
return sum;
}
14
Specworld.in jntuworld.xyz
smartworld.asia smartzworld.com
*******************************OUTPUT****************************
C:\jdk1.6.0_26\bin>javac wrapperdemo.java
Note: wrapperdemo.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
C:\jdk1.6.0_26\bin>java wrapperdemo
ENTER THE PRINCIPAL AMOUNT
1000
ENTER THE INTEREST RATE
2
ENTER THE NUMBER OF YEARS
1
FINAL VALUE IS:1020.0
E:\jdk1.6.0_26\bin>java wrapperdemo
ENTER THE PRINCIPAL AMOUNT
1000
ENTER THE INTEREST RATE
2
ENTER THE NUMBER OF YEARS
2
FINAL VALUE IS:1040.0
15
Specworld.in jntuworld.xyz
smartworld.asia smartzworld.com
PROGRAM 10
Write a Java Program to implement inheritance and demonstrate use of method overriding.
import java.lang.*;
class A
{
void display()
{
System.out.println("This is from class A ");
}
}
class B extends A
{
void display()
{
System.out.println("This is from class B ");
}
}
class AB
{
public static void main(String arg[])
{
B obj=new B();
obj.display();
}
}
**********************************OUTPUT*****************
C:\jdk1.6.0_26\bin>javac AB.java
C:\jdk1.6.0_26\bin>java AB
16
Specworld.in jntuworld.xyz
smartworld.asia smartzworld.com
PROGRAM 11
/* Write a Java Program to implement multilevel inheritance by applying various access controls to its
data members and methods. */
import java.io.DataInputStream;
class Student
{
private int rollno;
private String name;
DataInputStream dis=new DataInputStream(System.in);
public void getrollno()
{
try
{
System.out.println("Enter rollno ");
rollno=Integer.parseInt(dis.readLine());
System.out.println("Enter name ");
name=dis.readLine();
}
catch(Exception e){ }
}
void putrollno()
{
System.out.println("Roll No ="+rollno);
System.out.println("Name ="+name);
}
}
17
Specworld.in jntuworld.xyz
smartworld.asia smartzworld.com
*********************OUTPUT*****************************
C:\jdk1.6.0_26\bin>javac MultilevelDemo.java
Note: MultilevelDemo.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
C:\jdk1.6.0_26\bin>java MultilevelDemo
Enter rollno
12345
Enter name
Avinash
Enter marks :
54
78
46
Roll No =12345
Name =Avinash
m1=54
m2=78
m3=46
Total marks :178.0
18
Specworld.in jntuworld.xyz
smartworld.asia smartzworld.com
PROGRAM 12 (A)
************************OUTPUT*************************************
C:\jdk1.6.0_26\bin>javac interfacedemo.java
C:\jdk1.6.0_26\bin>java interfacedemo
Area of rectangle=628.0
Area of circle=2,827.43
19
Specworld.in jntuworld.xyz
smartworld.asia smartzworld.com
PROGRAM 12 (B)
class InterfaceExtendsDemo
{
public static void main(String a[])
{
rectangle rect=new rectangle();
double result=rect.compute(10.2,12.3);
rect.display_result(result);
}
}
************************************OUTPUT************************
C:\jdk1.6.0_26\bin>javac InterfaceExtendsDemo.java
C:\jdk1.6.0_26\bin>java InterfaceExtendsDemo
20
Specworld.in jntuworld.xyz
smartworld.asia smartzworld.com
PROGRAM 13
Write a Java program to implement the concept of importing classes from user defined
package and creating packages.
package p1;
public class Student
{
int regno;
String name;
import p1.*;
class StudentTest
{
public static void main(String arg[])
{
student s=new student();
s.getdata(123,"xyz");
s.putdata();
}
}
*********************************OUTPUT********************************
C:\jdk1.6.0_26\bin>javac p1\Student.java
C:\jdk1.6.0_26\bin>javac StudentTest.java
C:\jdk1.6.0_26\bin>java StudentTest
regno = 123
name = xyz
21
Specworld.in jntuworld.xyz
smartworld.asia smartzworld.com
PROGRAM 14 (A)
import java.lang.Thread;
22
Specworld.in jntuworld.xyz
smartworld.asia smartzworld.com
new C().start();
}
}
*********************************OUTPUT**********************************
thread A is sterted:
thread B is sterted:
thread C is sterted:
from thread A:i=1
from thread B:j=1
from thread C:k=1
from thread A:i=2
from thread B:j=2
from thread C:k=2
from thread A:i=3
from thread B:j=3
from thread C:k=3
from thread A:i=4
from thread B:j=4
from thread C:k=4
from thread A:i=5
from thread B:j=5
from thread C:k=5
exit from thread A:
exit from thread B:
exit from thread C:
23
Specworld.in jntuworld.xyz
smartworld.asia smartzworld.com
PROGRAM 14 (B)
import java.lang.Runnable;
class Runnabletest
{
public static void main(String arg[])
{
X R=new X();
Thread T=new Thread(R);
T.start();
}
}
*********************************OUTPUT**********************************
Thread X:1
Thread X:2
Thread X:3
Thread X:4
Thread X:5
Thread X:6
Thread X:7
Thread X:8
Thread X:9
End of Thread X
24
Specworld.in jntuworld.xyz
smartworld.asia smartzworld.com
PROGRAM 15 (A)
import java.lang.*;
class Exception_handle
{
public static void main(String argv[])
{
int a=10,b=5,c=5,x,y;
try
{
x=a/(b-c);
}
catch(ArithmeticException e)
{
System.out.println("DIVISION BY ZERO");
}
y=a/(b+c);
System.out.println("y="+y);
}
}
********************************OUTPUT***********************************
DIVISION BY ZERO
y=1
25
Specworld.in jntuworld.xyz
smartworld.asia smartzworld.com
PROGRAM 15 (B)
Write a program to implement the concept of Exception Handling by creating user defined
exceptions.
import java.lang.Exception;
import java.lang.*;
import java.lang.Exception;
import java.io.DataInputStream;
class userdef
{
public static void main(String a[])
{
int age;
DataInputStream ds=new DataInputStream(System.in);
try
{
System.out.println("Enter the age (above 15 abd below 25) :");
age=Integer.parseInt(ds.readLine());
if(age<15 || age> 25)
{
throw new MyException("Number not in range");
}
System.out.println(" the number is :" +age);
}
catch(MyException e)
{
System.out.println("Caught MyException");
System.out.println(e.getMessage());
}
catch(Exception e){ System.out.println(e); }
}
}
26
Specworld.in jntuworld.xyz
smartworld.asia smartzworld.com
*****************************OUTPUT 1********************************
c:\jdk1.6.0_26\bin>java userdef
*****************************OUTPUT 2********************************
c:\jdk1.6.0_26\bin>java userdef
27
Specworld.in jntuworld.xyz
smartworld.asia smartzworld.com
PROGRAM 16 (A)
import java.applet.*;
import java.awt.Graphics;
*******************************OUTPUT************************
C:\jdk1.6.0_26\bin>javac Appletdemo.java
C:\jdk1.6.0_26\bin>appletviewer Appletdemo.java
28
Specworld.in jntuworld.xyz
smartworld.asia smartzworld.com
PROGRAM 16 (B)
********************************OUTPUT*********************************
C:\jdk1.6.0_26\bin>javac AppletParamDemo.java
C:\jdk1.6.0_26\bin>appletviewer AppletParamDemo.java
29
Specworld.in jntuworld.xyz
smartworld.asia smartzworld.com
PROGRAM 17 (A)
30
Specworld.in jntuworld.xyz
smartworld.asia smartzworld.com
*******************************OUTPUT*********************************
C:\jdk1.6.0_26\bin>javac Keyevents.java
C:\jdk1.6.0_26\bin>appletviewer Keyevents.java
31
Specworld.in jntuworld.xyz
smartworld.asia smartzworld.com
PROGRAM 17 (B)
32
Specworld.in jntuworld.xyz
smartworld.asia smartzworld.com
}
public void mouseReleased(MouseEvent m)
{
x=m.getX();
y=m.getY();
msg ="Up";
repaint();
}
33
Specworld.in jntuworld.xyz
smartworld.asia smartzworld.com
OUTPUT
34
Specworld.in jntuworld.xyz
smartworld.asia smartzworld.com
PROGRAM 18
import java.applet.*;
import java.awt.*;
35
Specworld.in jntuworld.xyz
smartworld.asia smartzworld.com
OUTPUT
36
Specworld.in jntuworld.xyz