Java Programs for Key Concepts
Java Programs for Key Concepts
OUTPUT:-
OVERLOADING
Program:-
class overloaddemo
{
void test()
{
[Link]("no parameters");
}
void test(int a)
{
[Link]("a: "+a);
}
void test(int a, int b)
{
[Link]("a & b:" +a+" "+b);
}
double test(double a)
{
[Link]("double a:" +a);
return a*a;
}}
class overload
{
public static void main(String[] args)
{
overloaddemo od=new overloaddemo();
[Link]();
[Link](15);
[Link](2,8);
double result=[Link](54.88);
[Link]("result of [Link](54.88):" +result);
}}
OUTPUT:-
OVERRIDDEN
Program:-
class funcover
{
public void calc(int x,int y)
{
int z;
z=x*y;
[Link]("multiplication="+z);
}
}
class override extends funcover
{
public void calc(int x,int y)
{
int z;
z=x/y;
[Link]("division="+z);
}
}
class overridee
{
public static void main(String arg[])
{
funcover f1=new funcover();
[Link](9,8);
override f2=new override();
[Link](6,9);
}
}
OUTPUT:-
3. WAP the use Boolean data type and print the prime number series
upto 50.
Program:-
class primee
{
public static void main(String args[])
{
int num,i;
boolean prime=true;
for(num=2;num<=50;num++)
{
prime=true;
for(i=2;i<=num/2;i++)
{
if(num%i==0)
{
prime=false;
}
}
if(prime)
[Link](""+num);
}
}
}
OUTPUT:-
OUTPUT:-
OUTPUT:-
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=0;
for(k=0;k<3;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}}}
[Link]("Multiplication of matrix is =");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
[Link]("\t"+c[i][j]);
}
[Link]("\n");
}}}
OUTPUT:-
OUTPUT:-
OUTPUT:-
[Link]("Length of String:"+[Link]());
for(int i=0;i<[Link]();i++)
{
int p=i+1;
[Link]("Character at_ position:"+p+"is"+[Link](i));
}
[Link](pos,"Oriented");
[Link]("Modified string:"+str);
//Modifying characters
[Link](6,'-');
[Link]("String now: " +str);
[Link]("improves security");
[Link]("Appended string: " +str);
}
}
OUTPUT:-
OUTPUT:-
Program:-
OUTPUT:-
[Link] where single class implements more than one interfaces and with
help to interface reference variable user call the method.
Program:-
//[Link]
interface Area
{
final static float pi=3.14f;
float compute(float x,float y);
}
class Rectangle implements Area
{
public float compute (float x,float y)
{
return(x*y);
}}
class Circle implements Area
{
public float compute (float x,float y)
{
return (pi*x*x);
}}
class InterfaceTest
{
public static void main(String args[])
{
Rectangle rect=new Rectangle();
Circle cir=new Circle();
Area area;
area=rect;
[Link]("Area of Rectangle="+[Link](17,23));
area=cir;
[Link]("Area of circle="+[Link](11,22));
}}
OUTPUT:-
[Link] that use the multiple catch statements within the try-catch
mechanism.
Program:-
class error4
{
public static void main(String arg[])
{
int a[]={5,10};
int b=5;
try
{
int x=a[2]/b-a[1];
}
catch(ArithmeticException e)
{
[Link]("division by zero");
}
catch(ArrayIndexOutOfBoundsException e)
{
[Link]("Array index error");
}
catch(ArrayStoreException e)
{
[Link]("wrong datatype");
}
int y=a[1]/a[0];
[Link](" y="+y);
}
}
OUTPUT:-
[Link] for multithread using the is Alive (), join () and synchronized ()
method of thread class.
Program:-
//Join and Alive
class abc
{
synchronized void show(String s)
{
try
{
for(int i=1;i<=5;i++)
{
[Link]("in" + s + "i="+i);
}
[Link]("exiting " +s);
}
catch(Exception e)
{
[Link](""+e); }}}
class Mythread implements Runnable
{
String s;
abc a;
public Mythread(String t1)
{
s=t1;
a=new abc();
}
public void run()
{
try
{
[Link](s);
}
catch(Exception e)
{
[Link](""+e);
}}
class joining
{
public static void main(String args[])
{
Thread t=[Link]();
OUTPUT:-
17. WAP to create a package using command and one package will
import another package.
Program:-
package p1;
public class Addition
{
public Addition()
{
a=0;
b=0;
}
public Addition (int first , int second)
{
a= first;
b = second;
}
public int getResult(){
return (a+b);
}
public int add(int first , int second)
{
return (first+second);
}
private int a;
private int b;
}
import p1.*;
public class packagetest2
{
public static void main(String[] args)
{
Addition ad = new Addition();
[Link]("addition of 10 and 15 is :"+[Link](10, 15));
}}
OUTPUT:-
18. WAP for AWT to create menu and Popup menu for Frame.
Program:-
import [Link].*;
import [Link].*;
class menudemo extends Frame
{
MenuBar mbr;
Menu m1,m2,sm;
MenuItem mi1,mi2,mi3,mi4,mi5,mi6,mi7,mi8,mi9,mi10,pmi6,pmi7,pmi8,pmi9,pmi10;
PopupMenu p;
MenuShortcut msc1;
menudemo()
{
setTitle("Menu");
mbr= new MenuBar();
setMenuBar(mbr);
m1=new Menu("File");
msc1=new MenuShortcut('F');
mi1=new MenuItem("New");
mi2=new MenuItem("Open");
mi3=new MenuItem("Save");
mi4=new MenuItem("SaveAs");
mi5=new MenuItem("Exit");
[Link](mi1);
[Link](mi2);
[Link]();
[Link](mi3);
[Link](mi4);
[Link](mi5);
[Link](m1);
m2=new Menu("Edit");
mi6=new MenuItem("Cut");
mi7=new MenuItem("Copy");
mi8=new MenuItem("Paste");
[Link](mi6);
[Link](mi7);
[Link](mi8);
sm=new Menu("Tools");
[Link](sm);
mi9=new MenuItem("Undo");
mi10 = new MenuItem("Redo");
[Link](mi9);
[Link](mi10);
[Link](m2);
p= new PopupMenu("Floating Menu");
pmi6= new MenuItem("Cut");
pmi7= new MenuItem("Copy");
pmi8= new MenuItem("Paste");
OUTPUT:-
OUTPUT:-
20. WAP which support the TCP/IP protocol, where client gives the
message and server will receive the message.
Program:-
import [Link].*;
import [Link].*;
public class Client
{
public static void main(String ar[])
{
int port=9999;
String ins;
BufferedReader br=new BufferedReader(new InputStreamReader([Link]));
try
{
InetAddress add=[Link](null);
Socket st=new Socket(add,port);
OutputStream os=[Link]();
OutputStreamWriter osw=new OutputStreamWriter(os);
PrintWriter pw=new PrintWriter(osw);
while((ins=[Link]())!=null)
{
[Link](ins);
[Link]();
if([Link]().equals("quit"))
[Link](0);
}}
catch(Exception e) { [Link](e); }
}
}
OUTPUT:-
OUTPUT:-
22. WAP for JDBC to insert the values into the existing table by using
prepared statement.
Program:-
import [Link].*;
class jdbc1
{
public static void main(String arg[])
{
int r;
String n;
try
{
[Link]("[Link]");
Connection c=[Link]("jdbc:odbc:stud");
Statement s=[Link]();
ResultSet p=[Link]("select * from stud");
while([Link]())
{
r=[Link](1);
n=[Link](2);
[Link]("roll:-"+r);
[Link]("name:-"+n);
}
[Link]();
[Link]();
}
catch(ClassNotFoundException e)
{
[Link](e);
}
catch(SQLException e)
{
[Link](e);
}}}
OUTPUT:-
23. WAP for JDBC to display the records from the existing table.
Program:-
import [Link].*;
class show
{
public static void main(String[] args) throws SQLException
{
try
{
[Link]("[Link]");
Connection conn=[Link]("jdbc:odbc:stu");
Statement st= [Link]();
String str= "Select * from student";
ResultSet rs=[Link](str);
if([Link]())
{
while([Link]())
{
[Link]([Link]("Rollno")+"\t");
[Link]([Link]("Name")+"\t");
[Link]([Link]("Marks")+"\n");
[Link]("----------------------------------------");
}
}
}
catch(ClassNotFoundException cnfe)
{
}
}}
OUTPUT:-
OUTPUT:-
OUTPUT:-
26. WAP for display the checkboxes, Labels and TextFields on an AWT.
Program:-
import [Link].*;
import [Link].*;
class awtframe
{
public static void main(String[] args)
{
Frame fr=new Frame("Frame");
Button bt=new Button("Button");
Label lb= new Label("Label");
TextArea ta=new TextArea(30,100);
[Link]("This is text area");
TextField txt= new TextField("TextBox",20);
[Link](500,300);
[Link](100,100);
[Link](new FlowLayout());
[Link](true);
[Link](lb);
[Link](txt);
[Link](bt);
[Link](ta);
[Link](true);
[Link](new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
[Link](0);
}});}}
OUTPUT:-
void findSquare(double s) {
class area {
{double l, b, h, r, s;
b = [Link]();
h = [Link]();
[Link](b, h);
l = [Link]();
b = [Link]();
[Link](l, b);
s = [Link]();
[Link](s);
r = [Link]();
[Link](r);
}}
OUTPUT:-
[Link] for creating a file and to store data into that file. (Using the
FileWriterIOStream).
Program:-
import [Link].*;
public class file
{
public static void main(String[] args) throws IOException
{
BufferedReader in= new BufferedReader(new InputStreamReader([Link]));
[Link]("please enter a file name");
String file_name=[Link]();
File file=new File(file_name);
boolean exist=[Link]();
if(!exist)
{
[Link]("file already exists");
[Link](0);
}
else
{
FileWriter fstream= new FileWriter(file_name);
BufferedWriter out =new BufferedWriter(fstream);
[Link]([Link]());
[Link]();
[Link]("File created successfully");
}}}
OUTPUT:-
29. WAP to read file & display its content using FILE INPUT STREAM
RANDOM ACCESS FILE.
Program:-
import [Link].*;
class RandomIO
{
public static void main(String arg[])
{
RandomAccessFile file=null;
try
{
file=new RandomAccessFile("[Link]","rw");
[Link]('K');
[Link](2302);
[Link](0);
[Link]([Link]());
[Link]([Link]());
[Link](2);
[Link]([Link]());
[Link]();
}
catch(IOException e)
{
[Link](e);
}
}
}
OUTPUT:-
30. WAP accepting two input as a source and target file name and write
the content from the source to the target.
Program:-
import [Link].*;
class copy
{
public static void main(String arg[])
{
File infile=new File("[Link]");
File out=new File(“[Link]");
FileReader ins=null;
FileWriter outs=null;
try
{
ins=new FileReader(infile);
outs=new FileWriter(out);
int ch;
while((ch=[Link]())!=-1)
{
[Link](ch);
}}
catch(IOException e)
{
[Link](e);
[Link](-1);
}
finally
{
try
{
[Link]();
[Link]();
}
catch(IOException e)
{
}}}}
OUTPUT:-
31. WAP to display your file in DOS console use the input/output Stream.
Program:-
import [Link].*;
class Byteread
{
public static void main(String arg[])
{
int b;
try
{
FileInputStream s=new FileInputStream("[Link]");
while((b=[Link]())!=-1)
{
[Link]((char)b);
}}
catch(IOException e)
{
[Link](e);
}}}
OUTPUT:-
32. WAP to create an applet using the HTML file, where parameter
pass for font size and font type and message will change to
corresponding parameters.
Program:-
import [Link].*;
import [Link].*;
/* <applet code= [Link] height=100 width=400>
<param name t1 value="Comic Sans MS">
<param name=t2 value=38>
</applet>*/
public class app extends Applet
{
int n;
String style;
public void init()
{
style= getParameter("t1");
String s= getParameter("t2");
n=[Link](s);
Font f= new Font(style,[Link],n);
setFont(f);
setBackground([Link]);
setVisible(true);
}
public void paint (Graphics g)
{
[Link]("WELCOME TO JAVA",80,60);
}
}
OUTPUT:-