Mayur Java Final
Mayur Java Final
OUTPUT:-
OVERLOADING
Program:-
class overloaddemo
{
void test()
{
System.out.println("no parameters");
}
void test(int a)
{
System.out.println("a: "+a);
}
void test(int a, int b)
{
System.out.println("a & b:" +a+" "+b);
}
double test(double a)
{
System.out.println("double a:" +a);
return a*a;
}}
class overload
{
public static void main(String[] args)
{
overloaddemo od=new overloaddemo();
od.test();
od.test(15);
od.test(2,8);
double result=od.test(54.88);
System.out.println("result of od.test(54.88):" +result);
}}
OUTPUT:-
OVERRIDDEN
Program:-
class funcover
{
public void calc(int x,int y)
{
int z;
z=x*y;
System.out.println("multiplication="+z);
}
}
class override extends funcover
{
public void calc(int x,int y)
{
int z;
z=x/y;
System.out.print("division="+z);
}
}
class overridee
{
public static void main(String arg[])
{
funcover f1=new funcover();
f1.calc(9,8);
override f2=new override();
f2.calc(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)
System.out.println(""+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];
}}}
System.out.println("Multiplication of matrix is =");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
System.out.print("\t"+c[i][j]);
}
System.out.print("\n");
}}}
OUTPUT:-
OUTPUT:-
OUTPUT:-
System.out.println("Length of String:"+str.length());
for(int i=0;i<str.length();i++)
{
int p=i+1;
System.out.println("Character at_ position:"+p+"is"+str.charAt(i));
}
str.insert(pos,"Oriented");
System.out.println("Modified string:"+str);
//Modifying characters
str.setCharAt(6,'-');
System.out.println("String now: " +str);
str.append("improves security");
System.out.println("Appended string: " +str);
}
}
OUTPUT:-
OUTPUT:-
Program:-
OUTPUT:-
13.WAP where single class implements more than one interfaces and with
help to interface reference variable user call the method.
Program:-
//InterfaceTest.java
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;
System.out.println("Area of Rectangle="+area.compute(17,23));
area=cir;
System.out.println("Area of circle="+area.compute(11,22));
}}
OUTPUT:-
14.WAP 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)
{
System.out.println("division by zero");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.print("Array index error");
}
catch(ArrayStoreException e)
{
System.out.println("wrong datatype");
}
int y=a[1]/a[0];
System.out.println(" y="+y);
}
}
OUTPUT:-
16.WAP 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++)
{
System.out.println("in" + s + "i="+i);
}
System.out.println("exiting " +s);
}
catch(Exception e)
{
System.out.println(""+e); }}}
class Mythread implements Runnable
{
String s;
abc a;
public Mythread(String t1)
{
s=t1;
a=new abc();
}
public void run()
{
try
{
a.show(s);
}
catch(Exception e)
{
System.out.println(""+e);
}}
class joining
{
public static void main(String args[])
{
Thread t=Thread.currentThread();
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();
System.out.print("addition of 10 and 15 is :"+ad.add(10, 15));
}}
OUTPUT:-
18. WAP for AWT to create menu and Popup menu for Frame.
Program:-
import java.awt.*;
import java.awt.event.*;
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");
m1.add(mi1);
m1.add(mi2);
m1.addSeparator();
m1.add(mi3);
m1.add(mi4);
m1.add(mi5);
mbr.add(m1);
m2=new Menu("Edit");
mi6=new MenuItem("Cut");
mi7=new MenuItem("Copy");
mi8=new MenuItem("Paste");
m2.add(mi6);
m2.add(mi7);
m2.add(mi8);
sm=new Menu("Tools");
m2.add(sm);
mi9=new MenuItem("Undo");
mi10 = new MenuItem("Redo");
sm.add(mi9);
sm.add(mi10);
mbr.add(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 java.io.*;
import java.net.*;
public class Client
{
public static void main(String ar[])
{
int port=9999;
String ins;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
try
{
InetAddress add=InetAddress.getByName(null);
Socket st=new Socket(add,port);
OutputStream os=st.getOutputStream();
OutputStreamWriter osw=new OutputStreamWriter(os);
PrintWriter pw=new PrintWriter(osw);
while((ins=br.readLine())!=null)
{
pw.println(ins);
pw.flush();
if(ins.trim().equals("quit"))
System.exit(0);
}}
catch(Exception e) { System.err.println(e); }
}
}
OUTPUT:-
OUTPUT:-
22. WAP for JDBC to insert the values into the existing table by using
prepared statement.
Program:-
import java.sql.*;
class jdbc1
{
public static void main(String arg[])
{
int r;
String n;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection c=DriverManager.getConnection("jdbc:odbc:stud");
Statement s=c.createStatement();
ResultSet p=s.executeQuery("select * from stud");
while(p.next())
{
r=p.getInt(1);
n=p.getString(2);
System.out.println("roll:-"+r);
System.out.println("name:-"+n);
}
s.close();
c.close();
}
catch(ClassNotFoundException e)
{
System.out.println(e);
}
catch(SQLException e)
{
System.out.println(e);
}}}
OUTPUT:-
23. WAP for JDBC to display the records from the existing table.
Program:-
import java.sql.*;
class show
{
public static void main(String[] args) throws SQLException
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn=DriverManager.getConnection("jdbc:odbc:stu");
Statement st= conn.createStatement();
String str= "Select * from student";
ResultSet rs=st.executeQuery(str);
if(rs.next())
{
while(rs.next())
{
System.out.print(rs.getInt("Rollno")+"\t");
System.out.print(rs.getString("Name")+"\t");
System.out.print(rs.getString("Marks")+"\n");
System.out.println("----------------------------------------");
}
}
}
catch(ClassNotFoundException cnfe)
{
}
}}
OUTPUT:-
OUTPUT:-
OUTPUT:-
26. WAP for display the checkboxes, Labels and TextFields on an AWT.
Program:-
import java.awt.*;
import java.awt.event.*;
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);
ta.append("This is text area");
TextField txt= new TextField("TextBox",20);
fr.setSize(500,300);
fr.setLocation(100,100);
fr.setLayout(new FlowLayout());
fr.setVisible(true);
fr.add(lb);
fr.add(txt);
fr.add(bt);
fr.add(ta);
fr.setVisible(true);
fr.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}});}}
OUTPUT:-
void findSquare(double s) {
class area {
{double l, b, h, r, s;
b = get.nextDouble();
h = get.nextDouble();
area.findTriangle(b, h);
l = get.nextDouble();
b = get.nextDouble();
area.findRectangle(l, b);
s = get.nextDouble();
area.findSquare(s);
r = get.nextDouble();
area.findCircle(r);
}}
OUTPUT:-
28.WAP for creating a file and to store data into that file. (Using the
FileWriterIOStream).
Program:-
import java.io.*;
public class file
{
public static void main(String[] args) throws IOException
{
BufferedReader in= new BufferedReader(new InputStreamReader(System.in));
System.out.println("please enter a file name");
String file_name=in.readLine();
File file=new File(file_name);
boolean exist=file.createNewFile();
if(!exist)
{
System.out.println("file already exists");
System.exit(0);
}
else
{
FileWriter fstream= new FileWriter(file_name);
BufferedWriter out =new BufferedWriter(fstream);
out.write(in.readLine());
out.close();
System.out.println("File created successfully");
}}}
OUTPUT:-
29. WAP to read file & display its content using FILE INPUT STREAM
RANDOM ACCESS FILE.
Program:-
import java.io.*;
class RandomIO
{
public static void main(String arg[])
{
RandomAccessFile file=null;
try
{
file=new RandomAccessFile("rand.dat","rw");
file.writeChar('K');
file.writeInt(2302);
file.seek(0);
System.out.println(file.readChar());
System.out.println(file.readInt());
file.seek(2);
System.out.println(file.readInt());
file.close();
}
catch(IOException e)
{
System.out.println(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 java.io.*;
class copy
{
public static void main(String arg[])
{
File infile=new File("mayur.txt");
File out=new File(“B.txt");
FileReader ins=null;
FileWriter outs=null;
try
{
ins=new FileReader(infile);
outs=new FileWriter(out);
int ch;
while((ch=ins.read())!=-1)
{
outs.write(ch);
}}
catch(IOException e)
{
System.out.println(e);
System.exit(-1);
}
finally
{
try
{
ins.close();
outs.close();
}
catch(IOException e)
{
}}}}
OUTPUT:-
31. WAP to display your file in DOS console use the input/output Stream.
Program:-
import java.io.*;
class Byteread
{
public static void main(String arg[])
{
int b;
try
{
FileInputStream s=new FileInputStream("Gaurav.txt");
while((b=s.read())!=-1)
{
System.out.println((char)b);
}}
catch(IOException e)
{
System.out.println(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 java.awt.*;
import java.applet.*;
/* <applet code= app.class 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=Integer.parseInt(s);
Font f= new Font(style,Font.BOLD,n);
setFont(f);
setBackground(Color.green);
setVisible(true);
}
public void paint (Graphics g)
{
g.drawString("WELCOME TO JAVA",80,60);
}
}
OUTPUT:-