1.
Write a java program to accept a year number from the key and test if it is
leap or not.
import [Link].*;
class Program{
public static void main(String args[])
{
int n=2015;
year y=[Link](n);
Boolean flag=[Link]();
If(flag)
{
[Link](“%n year %d is Leap.”,n);
}
else
{
[Link](“%n year %d is not Leap.”,n);
}
}
}
Output:
c:\>javac [Link]
c:\>java Program
year 2015 is not leap.
[Link] a java program for calculating and displaying area
of a circle.
import [Link].*;
class Circle
{
public static void main (String args[])
{
final double PI=(double)22/7;
double r=15.5;
double area=PI*r*r;
[Link](“Area=”+Area);
}
}
Output:
C:\>javac [Link]
C:\>java Circle
Area=755.0714285714286
[Link] a java program to create a push button and add it to a frame.
import [Link].*;
import [Link].*;
class But extends Frame
{
But()
{
Button b=new Button(“ok”);
Add(b);
[Link](new Myclass());
}
Public static void main(String args[])
{
But obj=new But();
[Link](400,300);
[Link](true);
}
}
Class Myclass implements ActionListener
{
Public viod actionPerformed(ActionEvent ae)
{
[Link](0);
}
}
Output:
C:\>javac [Link]
C:\>java But
[Link] a java program to find the areas of square and Rectangle by deriving
them from Shape.
import [Link].*;
class Shape
{
Protected double1;
Shape(double 1)
{
this.1=1;
}
}
class Square extends Shape
{
Square(double 1)
{
Super(1);
}
Void area()
{
[Link](“Area of Rectangle=”+(1*b));
}
}
class Inherit
{
Public static void main(String args[])
{
Square s=new Square(5.5);
[Link]();
Rectangle r=new Rectangle(5.5,6);
[Link]();
}
}
Output:
C:\>javac [Link]
C:\>java Inherit
Area of Square=30.25
Area of Rectangle=33.0
[Link] a java program to make cloning Employee class object by writing own
myclone()method,from where object class clone()method is called.
import [Link].*;
class Employee implements Cloneable
{
int id;
String name;
Employee(int id,String name)
{
[Link]=id;
[Link]=name;
}
void getdata()
{
[Link](“id=”+id);
[Link](“name=”+name);
}
Public object myClone() throws CloneNotSupportedException
{
return [Link]();
}
}
class CloneDemo
{
Public static void main(String args[])throws CloneNotSupportedException
{
Employee e1=new Employee(10,”teja”);
[Link](“original object :”);
[Link]();
Employee e2=(Employee)[Link]();
[Link](“Cloned object:”);
[Link]();
}
}
Output:
C:\>javac [Link]
C:\>java CloneDemo
Original object:
Id=9
Name=teja
Cloned object:
Id=9
Name=teja
[Link] a java program for calculating electricity bill for commercial and domestic plans by using
abstract class.
abstract class Plan
{
protected double rate;
public abstract void getRate();
public void calculateBill(int units)
{
[Link](“Bill amount for”+units+”units:”);
[Link](rate*units);
}
}
class commercialPlan extends Plan
{
public void getRate()
{
rate=5.00;
}
}
class Calculate
{
public static void main(String args[])
{
Plan P;
[Link](“Commercial connection:”);
P= new CommercialPlan();
[Link]();
[Link](250);
[Link](“domestic connection:”);
P=new DomesticPlan();
[Link]();
[Link](150);
}
}
Output:
C:\>javac [Link]
C:\>java Calculate
Commercial connection:
Bill amount for 250 units: 1250.0
Domestic connection:
Bill amount for 150 units: 390.0
7. Write a java program to create a package and store Addition class and
subtraction class in it.
import [Link];
import [Link];
class Use
{
public static void main(String args[])
Addition obj=new Addition(10,15.5)
[Link]();
double res=[Link](10,15.5);
[Link](“Result=”+res);
}
}
Output:
C:\>javac [Link]
C:\>java Use
sum=25.5
Result=5.5
[Link] a java program to convert int into binary ,hexadecimal and octal format.
import [Link].*;
class Convert
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader([Link]));
[Link](“Enter an integer:”);
Srting str=[Link]();
int i=[Link](str);
[Link](“In decimal:”+i);
str=[Link](i);
[Link](“In binary:”+str);
str=[Link](i);
[Link](“In hexadecimal:”+str);
str=[Link](i);
[Link](“In octal:”+str);
}
}
Output:
C:\>javac [Link]
C:\>java Convert
Enter an Integer: 456
In decimal: 456
In binary : 111001000
In hexadecimal: 1c8
In Octal:710
9)Write a java program for Multiple Exception Handling.
class Ex1
{
public static void main(String args[])
{
try
{
[Link](“open files”);
int n=[Link];
[Link](“n=”+n);
int a=45/n;
[Link](“a=”+a);
int b[]={10,20,30};
b[50]=100;
}
Catch(ArithematicException|ArrayIndexOutof BoundsException ae)
[Link](ae);
}
finally
{
[Link](“close files”);
}
}
}
Output:
C:\> javvac [Link]
C:\>java Ex1
Open files
N=0
[Link]:/by zero
Close files
C:\>java Ex1 11 22
Open files
N=2
A=22
[Link]
Close files
10 ) Write a java program to throw a User Defined Exception.
class MyException extends Exception
{
private static int accno[]={1001,1002,1003,1004,1005};
private static string name[]={“Mounika”, “Nireekshana”, “Hemapriya”, “Haritha”, “Laxmi Devi”};
private static double bal[]={10000.00, 12000.00, 5600.50, 999.00, 1100.55};
MyException()
{
}
MyException(string str)
{
Super(str);
}
public static void main(String args[])
{
try{
[Link](“ACCNO”+”\t”+”CUSTOMER”+”\t”+ “BALANCE”);
For(int i=0; i<5; i++)
{
[Link](accno[i]+”\t”+name[i]+”\t”+ bal[i]);
If(bal[i]<1000)
{
MyException me=new MyException(“Balance amount is less”);
throw me;
}
}
}
catch(MyException me){
[Link]();
}
}
}
Output:
C:\>javac [Link]
C:\>java MyException
ACCNO CUSTOMER BALANCE
1001 Mounika 10000.00
1002 Nireekshana 12000.0
1003 Hema priya 5600.5
1004 Haritha 999.0
MyException: Balance amount is less
At [Link]([Link])
[Link] a java program for to synchronize the threads acting on the same object. Synchronize block
can be executed only one thread at a time.
class Reserve implements Runnable
{
int available=1;
int wanted;
Reserve(int i)
{
Wanted=I;
}
public void run()
{
Synchronized(this)
{
[Link](“Avaliable Berths=”+available);
if(available>=wanted)
{
String name=[Link]().getName();
[Link](wanted=”Berths reserved for”+name);
try
{
[Link](1500);
available=available-wanted;
}catch(InterruptedException ie){}
}
else [Link](“sorry, no berths”);
}
}
}
class Safe
{
Public static void main(String args[])
Reserve obj=new Reserve(1);
Thread t1=new Thread(obj);
Thread t2=new Thread(obj);
[Link](“first person”);
[Link](“second person”);
[Link]();
[Link]();
}
}
Output:
C:\>javac [Link]
C:\>java Safe
Avaliable Berths=1
1 Berths reserved for first person
Avaliable Berths=0
Sorry, no Berths.
[Link] a java program for Thread Deadlock.
Class BookTicket extends Thread
Object train,comp;
BookTicket(object train, object comp)
{
[Link]=train;
[Link]=comp;
}
Public void run()
{
Synchronized(train)
{
[Link](“BookTicket locked on train”);
try{
[Link](150);
}catech(InterruptedException e){}
[Link](“BookTicket now waiting to lock on compartment…”);
Synchronized(comp)
{
[Link](BookTicket locked on compartment”);
}
}
}
class CancelTicket extends Thread
{
Object train, comp;
cancelTicket(object train, object comp)
{
[Link]=train;
[Link]=comp;
}
public void run()
{
Synchronized(comp)
{
[Link](“CancelTicket locked on compartment”);
try{
[Link](200);
}Catch(InterruptedException e){}
[Link](“CancelTicket now waiting to lock on train…”);
Synchronized(train)
{
[Link](“CancelTicket locked on train”);
}
}
}
}
class DeadLock
{
public static void main(String args[]) throws Exception
{
object train = new object();
object compartment = new object();
BookTicket obj1 = new BookTicket (train, compartment);
CancelTicket obj2 = new CancelTicket (train, compartment);
Thread t1= new Thread(obj1);
Thread t2= new Thread(obj2);
[Link]();
[Link]();
}
}
Output:
C:\> javac [Link]
C:\> java DeadLock
BookTicket locked on train
CancelTicket locked on compartment
BookTicket now waiting to lock on compartment…
CancelTicket now waiting to lock on train…
[Link] a java program for creating Thread Groups.
class TGroups
{
public static void main(String args[]) throws Exception
{
Reservation res = new Reservation();
Cncellation can = new Cancellation();
ThreadGroup tg = new ThreadGroup(“First Group”);
Thread t1 = new Thread(tg, res, “First thread”);
Thread t2 = new Thread(tg, res, “Second thread”);
ThreadGroup tg1 = new ThreadGroup(tg, “Second Group”);
Thread t3 = new Thread(tg1, can, “Third thread”);
Thread t4 = new Thread(tg1, can, “Fourth thread”);
[Link](“parent of tg1= “+[Link]());
[Link](7);
[Link](“Thread group of t1= “+ [Link]());
[Link](“Thread group of t3= “+ [Link]());
[Link]();
[Link]();
[Link]();
[Link]();
[Link](“No of threads active in tg = “+[Link]());
}
}
class Reservation extends Thread
{
public void run()
{ [Link](“ I am reservation thread”); }
}
class Cancellation extends Thread
{
public void run()
{
[Link],println(“ I am cancellation thread”);
}
}
Output:
C:\> javac [Link]
C:\> java TGroups
Parent of tg1= [Link][name=First Group,maxpri= 10]
Thread group of t1= [Link][name=First Group,maxpri=10]
Thread group of t3= [Link][name=Second Group,maxpri=7]
No of threads active in tg= 4
I am reservation thread
I am reservation thread
I am cancellation thread
I am cancellation thread
[Link] a java program for animates the things using threads.
import [Link].*;
class Banner extends Frame implements Runnable
{
String str=”Dream tech publications”;
Banner()
{
setLayout(null);
setBackground([Link]);
setForeground([Link]);
}
Public void paint(Graphics g)
{
Font f=new Font(“courier”,[Link],40);
[Link](f);
[Link](str,10,100):
}
public void run()
{
For(;;){
Repaint();
try{
[Link](400);
}
catch(InterruptedException ie){}
char ch=[Link](0);
str=[Link](1, [Link]());
str=str+ch;
}
}
public static void main(String args[])
{
Banner b=new Banner();
[Link](400,400);
[Link](“My banner”);
[Link](true);
Thread t=new Thread(b);
[Link]();
}
}
Output:
C:\>javac [Link]
C:\>java Banner
[Link] a java program for creating a frame and close it is using Window Adapter
class.
import [Link].*;
import [Link].*;
class MyFrame extends Frame
{
public static void main(String args[])
{
MyFrame f=new MyFrame();
[Link](“My AWT frame”);
[Link](300,250);
[Link](true);
[Link](new Myclass());
}
}
class Myclass extends windowAdapter
{
public void windowClosing(windowEvent e)
{
[Link](0);
}
}
Output:
C:\>javac [Link]
C:\>java MyFrame
CLICK ON CLOSE BUTTON, THE FRAME CLOSES
[Link] a java program for drawing a smiling face in a frame with filled colors.
import [Link].*;
import [Link].*;
class Draw2 extends Frame
{
[Link](new windowAdepter()
{
public void windowClosing(windowEvent e)
{
[Link](0);}
});
}
public void paint(Graphics g)
{
[Link]([Link]);
[Link](40,40,200,200);
[Link]([Link]);
[Link](90,70,80,80);
[Link]([Link]);
[Link](110,95,5,5);
[Link](145,95,5,5);
[Link](130,95,130,115);
[Link]([Link] );
[Link](113,115,35,20,0,-180);
}
public static void main(String args[])
{
Draw2 d=new Draw2();
[Link](400,400);
[Link](“My drawing”);
[Link](true);
}
}
Output:
C:\>javac [Link]
C:\>java Draw2
[Link] a java program to display an image in the frame and also in the title bar of the
frame.
import [Link].*;
import [Link].*;
class Images extends Frame
{
Static Image img;
Images()
{
img=[Link]().getImage(“[Link]”);
MediaTraker Track=new MediaTracker(this);
[Link](img,0);
try{
[Link](0);
}catch(InterruptedException ie){}
addwindowListener(new windowAdpter()
{
public void windowClosing(windowEvent we)
{
[Link](0);
}
});
}
public void paint(Graphics g)
{
[Link](img,50,50,null);
}
public static void main(String args[])
{
Images i=new Images();
[Link](500,400);
[Link](“My images”);
[Link](img);
[Link](true);
}
}
Output:
C:\>javac [Link]
C:\>java Images
18 .Write a java program to compress or uncompress the data.
A) Compress:
import [Link].*;
import [Link].*;
class Zip
{
public static void main(String args[])
{
FileInputStream fis=new FileInputStream(“file1”);
FileOutputSream fos=new FileOutputStream(“file2”);
DeflaterOutputStream dos=new DeflaterOutputStream(fos);
int data;
while((data=[Link]())!=-1)
[Link](data);
[Link]();
[Link]();
}
}
Output:
C:\>javac [Link]
C:\>java Zip
19.)Uncompress:
import [Link].*;
import [Link].*;
class UnZip
{
public static void main(String args[])
FileInputStream fis=new FileInputStream(“file2”);
FileOutputStream fos=new FileOutputStream(“file3”);
InflaterInputStream iis=new InflaterInputStream(fis);
int data;
while((data=[Link]()) !=-1)
[Link](data);
[Link]();
[Link]();
}
}
Output:
C:\>javac [Link]
C:\>java UnZip
[Link] a java program for Serialization and De-Serialization of objects.
A) Serialization:
Import [Link].*;
Import [Link].*;
class StoreObj
{
Public static void main(String args[])
{
BufferedReader br=new BufferedReader(new InputStreamReader([Link]);
FileOuputStream fos=new FileOuputStream(“objfile”);
ObjectOuputStream oos=new ObjectOuputStream(fos);
[Link](“how many employees?”);
Int n=[Link]([Link]());
For(int i=0;i<n;i++)
{
Employee e=new Employee();
[Link]();
[Link](e);
}
[Link]();
}
}
Output:
C:\>javac [Link]
C:\>java StoreObj
How many objects?2
Enter emp id:1001
Enter name:Hari
Enter salary:10250.50
Enter date of joining:
Enter day:10
Enter month:7
Enter year:2017
Enter emp id:1002
Enter name:Teja
Enter salary:15000.00
Enter date of joining:
Enter day:2
Enter month:6
Enter year:2018