0% found this document useful (0 votes)
123 views44 pages

Mayur Java Final

The document contains 9 Java programs demonstrating different Java concepts like encapsulation, polymorphism, arrays, sorting, strings, loops, and more. Each program is presented with the code, expected output, and a brief description. The programs cover basic to intermediate Java programming concepts.

Uploaded by

Krishna Kant
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
123 views44 pages

Mayur Java Final

The document contains 9 Java programs demonstrating different Java concepts like encapsulation, polymorphism, arrays, sorting, strings, loops, and more. Each program is presented with the code, expected output, and a brief description. The programs cover basic to intermediate Java programming concepts.

Uploaded by

Krishna Kant
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 44

JAVA 2018-19

1. WAP that implements the concept of encapsulation.


Program:-
class emp
{
private int empid;
private String name;
public emp(int x, String y)
{
empid=x;
name=y;
}
public void show()
{
System.out.println("empid= "+empid);
System.out.println("name= "+name);
}
}
class encaps
{
public static void main(String arg[])
{
int eid= Integer.parseInt(arg[0]);
String ename=arg[1];
emp e1=new emp(eid,ename);
e1.show();
}
}

OUTPUT:-

GAURAV BULCHANDANI Page 1


JAVA 2018-19

2. WAP to demonstrate concept of polymorphism (overloading and


overridden).

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:-

GAURAV BULCHANDANI Page 2


JAVA 2018-19

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:-

GAURAV BULCHANDANI Page 3


JAVA 2018-19

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:-

GAURAV BULCHANDANI Page 4


JAVA 2018-19

4. WAP to print 10 number of the followings Series using Do-While


Loops
0, 1, 1, 2, 3, 5, 8, 11…………..
Program:-
class Demo
{
int a=-1,b=1,c;
void series()
{
int i=1;
do
{
c=a+b;
System.out.println(c);
a=b;
b=c;
i++;
}while(i<=10);
}
}
class F
{
public static void main(String args[])
{
Demo d1=new Demo();
d1.series();
}
}

OUTPUT:-

GAURAV BULCHANDANI Page 5


JAVA 2018-19

5. WAP to sort the element of one dimensional array in ascending order.


Program:-
class sortno
{
public static void main(String ar[])
{
int temp;
int S[]={9,3,8,1,4,7,5,0};
int l=S.length;
for(int i=0;i<l;i++)
{
for(int j=i+1;j<l;j++)
{
if(S[i]>S[j])
{
temp=S[i];
S[i]=S[j];
S[j]=temp;
}
}
}
for(int i=0;i<l;i++)
{
System.out.println(S[i]);
}
}
}

OUTPUT:-

GAURAV BULCHANDANI Page 6


JAVA 2018-19

6. WAP for matrix multiplication using input/output stream.


Program:-
import java.io.*;
class mult
{
public static void main(String arg[])throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int a[][]=new int[3][3];
int b[][]=new int[3][3];
int c[][]=new int[3][3];
int i,j,k;
System.out.println("enter the A matrix=");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
a[i][j]=Integer.parseInt(br.readLine());
}}
System.out.println("input matrix A are=");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
System.out.print("\t"+a[i][j]);
}
System.out.print("\n");
}
System.out.println("enter the B matrix=");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
b[i][j]=Integer.parseInt(br.readLine());
}}
System.out.println("input matrix B are=");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
System.out.print("\t"+b[i][j]);
}
System.out.print(" \n");
}

for(i=0;i<3;i++)
{

GAURAV BULCHANDANI Page 7


JAVA 2018-19

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:-

GAURAV BULCHANDANI Page 8


JAVA 2018-19

7. WAP to add the elements of vector as arguments of main method(run


time) and rearrange them, and copy it into an array.
Program:-
import java.util.*;
import java.io.*;
class vectord
{
public static void main(String[] args) throws Exception
{
int n;
Vector v =new Vector();
n= Integer.parseInt(args[0]);
for(int i=1;i<=n;i++)
v.addElement(args[i]);
v.insertElementAt(v.elementAt(n-1),0);
v.insertElementAt(v.elementAt(n-1),1);
v.insertElementAt(v.elementAt(n-1),2);
v.removeElementAt(v.size()-1);
v.removeElementAt(v.size()-1);
v.removeElementAt(v.size()-1);
System.out.println("After Rearrangement: "+v);
System.out.println("Size of Vector: "+v.size());
Object array[]=new Object[v.size()];
v.copyInto(array);
System.out.println("content of array");
for(int i=0;i<n;i++){
System.out.println(array[i]);
}}}

OUTPUT:-

GAURAV BULCHANDANI Page 9


JAVA 2018-19

8. WAP to check that the given string is palindrome or not.


Program:-
class stringordering
{
static String name[]={"Gaurav","Aayush","Mayur","Siddhu"};
public static void main(String arg[])
{
int size=name.length;
String temp=null;
for(int i=0;i<size;i++)
{
for(int j=i+1;j<size;j++)
{
if(name[i].compareTo(name[j])>0)
{
//swap the string
temp=name[i];
name[i]=name[j];
name[j]=temp;
}}}
for(int i=0;i<size;i++)
{
System.out.println(name[i]);
}}}
OUTPUT:-

GAURAV BULCHANDANI Page 10


JAVA 2018-19

9. WAP to arrange the string in alphabetical order.


Program:-
class stringsort
{
public static void main(String sk[])
{
String temp=null;
String name[]={"Raipur","Bangolore","Calcutta" ,"Bombay","Ahmedabad"};
int i,j;
int size = name.length;
for(i=0;i<size;i++)
{
for(j=i+1;j<size;j++)
{
if(name[i].compareTo(name[j])>0)
{
temp=name[i];
name[i]=name[j];
name[j]=temp;
}
}
}
for(i=0;i<size;i++)
System.out.println(" "+name[i]);
}
}

OUTPUT:-

GAURAV BULCHANDANI Page 11


JAVA 2018-19

10.WAP to StringBuffer class which perform all methods of that class.


Program:-
class StringManu
{
public static void main(String args[])
{
StringBuffer str=new StringBuffer("Object language");
System.out.println("Original String:"+str);

//obtaining string length

System.out.println("Length of String:"+str.length());

//Accessing characters in a string

for(int i=0;i<str.length();i++)
{
int p=i+1;
System.out.println("Character at_ position:"+p+"is"+str.charAt(i));
}

//Inserting a string in the middle


String aString=new String(str.toString());
int pos=aString.indexOf("language ");

str.insert(pos,"Oriented");
System.out.println("Modified string:"+str);

//Modifying characters

str.setCharAt(6,'-');
System.out.println("String now: " +str);

//Appending a string at the end

str.append("improves security");
System.out.println("Appended string: " +str);
}
}

GAURAV BULCHANDANI Page 12


JAVA 2018-19

OUTPUT:-

GAURAV BULCHANDANI Page 13


JAVA 2018-19

11.WAP to calculate simple interest using the Wrapper class.


Program:-
class wrap
{
public static void main(String args[])
{
float p=Float.parseFloat(args[0]);
System.out.println("Principal amount is : " +p);
float r= Float.parseFloat(args[1]);
System.out.println("Rate is : " +r);
float t= Float.parseFloat(args[2]);
System.out.println("Time is : "+t);
float si=(p*r*t)/100;
System.out.println("Simple Interest is:-"+si);
}
}

OUTPUT:-

GAURAV BULCHANDANI Page 14


JAVA 2018-19

12. WAP to calculate area of various geometrical figures using the


abstract class.

Program:-

abstract class shape


{
public abstract void area();
public abstract void perimeter();
}
class circle extends shape
{
private int radius;
public circle(int r)
{
radius=r;
}
public void area()
{
float a;
a=3.14f*radius*radius;
System.out.println("area="+a);
}
public void perimeter()
{
float p;
p=2*3.14f*radius;
System.out.println("perimeter="+p);
}}
class abstractex
{
public static void main(String arg[])
{
circle c1=new circle(5);
c1.area();
c1.perimeter();
}}

GAURAV BULCHANDANI Page 15


JAVA 2018-19

OUTPUT:-

GAURAV BULCHANDANI Page 16


JAVA 2018-19

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:-

GAURAV BULCHANDANI Page 17


JAVA 2018-19

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:-

GAURAV BULCHANDANI Page 18


JAVA 2018-19

15.WAP where user will create a self-Exception using the “throw”


keyword.
Program:-
import java.lang.Exception;
class SelfException extends Exception
{
SelfException(String message)
{
super(message);
}}
class TestSelfException
{
public static void main(String args[])
{
int x=5, y=1000;
try
{
float z=(float)x /(float) y;
if(z<0.01)
{
throw new SelfException("No. is not enlarged");
}}
catch(SelfException e)
{
System.out.println("Caught my exception");
System.out.println(e.getMessage());
}
finally
{
System.out.println("java is a pure Object Oriented Language");
System.out.println("We like java");
}}}
OUTPUT:-

GAURAV BULCHANDANI Page 19


JAVA 2018-19

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();

GAURAV BULCHANDANI Page 20


JAVA 2018-19
Thread t1=new Thread(new Mythread("first"));
Thread t2=new Thread(new Mythread("second"));
t1.start();
t2.start();
System.out.println("State of main" + t.isAlive());
System.out.println("State of First" + t1.isAlive());
System.out.println("State of Second" +t2.isAlive());
try
{
t1.join();
t2.join();
}
catch(Exception e)
{
System.out.println(""+e);
}dd
System.out.println("State of First" + t1.isAlive());
System.out.println("State of main" + t.isAlive());
}}

OUTPUT:-

GAURAV BULCHANDANI Page 21


JAVA 2018-19

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:-

GAURAV BULCHANDANI Page 22


JAVA 2018-19

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");

GAURAV BULCHANDANI Page 23


JAVA 2018-19

pmi9= new MenuItem("Undo");


pmi10= new MenuItem("Redo");
p.add(pmi6);
p.add(pmi7);
p.add(pmi8);
p.add(pmi9);
p.add(pmi10);
add(p);
addMouseListener(new MouseAdapter()
{ public void mousePressed(MouseEvent me)
{ display(me);
}
public void mouseReleased(MouseEvent me)
{ display(me);
}
public void mouseClicked(MouseEvent me)
{ display(me);
}
}); setVisible(true);
validate();
setLocation(100,10);
setSize(200,200);
}
public void display(MouseEvent me)
{ if(me.isPopupTrigger())
p.show(this,me.getX(),me.getY());
}
public static void main(String []args)
{ menudemo fr= new menudemo();
}}

OUTPUT:-

GAURAV BULCHANDANI Page 24


JAVA 2018-19

19. WAP for applet that handle the keyboard events.


Program:-
import java.awt.Color;
import java.awt.event.KeyAdapter;
import javax.swing.*;
public class KeyBoardEvents extends JApplet{
public KeyBoardEvents()
{
setBackground(Color.red);
JTextArea txt= new JTextArea(20,20);
KeyAdapter event = new java.awt.event.KeyAdapter()
{
public void keyReleased(java.awt.event.KeyEvent evt)
{
JOptionPane.showMessageDialog(rootPane,"Key pressed :
"+evt.getKeyText(evt.getKeyCode()) +"\nKeyCode : "+ evt.getKeyCode());
}
};
txt.addKeyListener(event);
add(txt);
}
public void init() {
setBackground(Color.cyan);
setForeground(Color.red);
}
}

OUTPUT:-

GAURAV BULCHANDANI Page 25


JAVA 2018-19

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:-

GAURAV BULCHANDANI Page 26


JAVA 2018-19

21. WAP to illustrate the use of all methods of URL class.


Program:-
import java.net.*;
public class url
{
public static void main(String[]a)
{
try
{
System.out.println(" ");
URL u=new URL("http","yahoo.com",999,"/report/text/index..htm");
System.out.println(" ");
System.out.println("input was :"+u.toExternalForm());
System.out.println(" ");
System.out.println("Host:"+u.getHost());
System.out.println(" ");
System.out.println("Port:"+u.getPort());
System.out.println(" ");
System.out.println("File:"+u.getFile());
System.out.println(" ");
}
catch(MalformedURLException e)
{
System.out.println("not url");
}
}}

OUTPUT:-

GAURAV BULCHANDANI Page 27


JAVA 2018-19

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:-

GAURAV BULCHANDANI Page 28


JAVA 2018-19

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:-

GAURAV BULCHANDANI Page 29


JAVA 2018-19

24.WAP to demonstrate the border layout using applet.


Program:-
import java.awt.*;
import java.awt.event.*;
class borderdemo extends Frame
{
public borderdemo()
{
Label lbl1=new Label("North");
Label lbl2=new Label("South");
Button btn1= new Button("East");
Button btn2= new Button("West");
TextArea ta=new TextArea(5,5);
ta.append("CENTER");
setLayout(new BorderLayout());
add(lbl1,BorderLayout.NORTH);
add(lbl2,BorderLayout.SOUTH);
add(btn1,BorderLayout.EAST);
add(btn2,BorderLayout.WEST);
add(ta, BorderLayout.CENTER);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}});
}
public static void main(String args[])
{
borderdemo bd= new borderdemo();
bd.setTitle("BorderLayout");
bd.setSize(300,200);
bd.setVisible(true);
}}

GAURAV BULCHANDANI Page 30


JAVA 2018-19

OUTPUT:-

GAURAV BULCHANDANI Page 31


JAVA 2018-19

25.WAP for applet who generate the MouseMotionListener Event.


Program:-
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*<applet code = encaps.class height = 300 width = 300>
</applet>*/
class emp
{
private int empid;
private String name;
public emp(int x, String y)
{
empid=x;
name=y;
}
public void show()
{
System.out.println("empid= "+empid);
System.out.println("name= "+name);
}
}
class encaps
{
public static void main(String arg[])
{
int eid= Integer.parseInt(arg[0]);
String ename=arg[1];
emp e1=new emp(eid,ename);
e1.show();
}}

OUTPUT:-

GAURAV BULCHANDANI Page 32


JAVA 2018-19

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);
}});}}

GAURAV BULCHANDANI Page 33


JAVA 2018-19

OUTPUT:-

GAURAV BULCHANDANI Page 34


JAVA 2018-19

27.WAP to calculate the area of various geometrical figures using the


abstract class.
Program:-
import java.util.Scanner;

abstract class calcArea {

abstract void findTriangle(double b, double h);

abstract void findRectangle(double l, double b);

abstract void findSquare(double s);

abstract void findCircle(double r);

class findArea extends calcArea {

void findTriangle(double b, double h)

double area = (b*h)/2;

System.out.println("Area of Triangle: "+area);

void findRectangle(double l, double b)

double area = l*b;

System.out.println("Area of Rectangle: "+area);

void findSquare(double s) {

double area = s*s;

System.out.println("Area of Square: "+area);

void findCircle(double r){

double area = 3.14*r*r;

GAURAV BULCHANDANI Page 35


JAVA 2018-19

System.out.println("Area of Circle: "+area); }

class area {

public static void main(String args[])

{double l, b, h, r, s;

findArea area = new findArea();

Scanner get = new Scanner(System.in);

System.out.print("\nEnter Base & Vertical Height of Triangle: ");

b = get.nextDouble();

h = get.nextDouble();

area.findTriangle(b, h);

System.out.print("\nEnter Length & Breadth of Rectangle: ");

l = get.nextDouble();

b = get.nextDouble();

area.findRectangle(l, b);

System.out.print("\nEnter Side of a Square: ");

s = get.nextDouble();

area.findSquare(s);

System.out.print("\nEnter Radius of Circle: ");

r = get.nextDouble();

area.findCircle(r);

}}

GAURAV BULCHANDANI Page 36


JAVA 2018-19

OUTPUT:-

GAURAV BULCHANDANI Page 37


JAVA 2018-19

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:-

GAURAV BULCHANDANI Page 38


JAVA 2018-19

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:-

GAURAV BULCHANDANI Page 39


JAVA 2018-19

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)
{
}}}}

GAURAV BULCHANDANI Page 40


JAVA 2018-19

OUTPUT:-

GAURAV BULCHANDANI Page 41


JAVA 2018-19

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);
}}}

GAURAV BULCHANDANI Page 42


JAVA 2018-19

OUTPUT:-

GAURAV BULCHANDANI Page 43


JAVA 2018-19

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:-

GAURAV BULCHANDANI Page 44

You might also like