Вы находитесь на странице: 1из 41

Name: AKASH SAGAR

Roll No. 84904

Assignment on Java

Q.1 WAP that implements the Concepts of Encapsulation.


Program:class Room
{
float length;
float breadth;
void getdata(float a,float b)
{
length=a;
breadth= b;
}
}
class Roomarea
{
public static void main(String args[])
{
float area;
Room r1=new Room();
r1.getdata(14,10);
area=r1.length*r1.breadth;
System.out.println("Area="+area);
}
}
OutPut:-

Mahant LaxmiNarayan das College

PGDCA 2nd Semester

Page 1

Name: AKASH SAGAR

Roll No. 84904

Assignment on Java

Q.2 WAP to demonstrate concept of Polymorphism . (Overloading &


Overriden)
Program:class Func
{
public void calc(int x,int y)
{
int z;
z=x*y;
System.out.println("Multiplication = "+z);
}
}
class Override extends Func
{
public void calc(int x,int y)
{
int z;
z=x/y;
System.out.print("Division = "+z);
}
}
class PolyOverr
{
public static void main(String arg[])
{
Override f2=new Override();
f2.calc(20,5);
}
}

Mahant LaxmiNarayan das College

PGDCA 2nd Semester

Page 2

Name: AKASH SAGAR

Roll No. 84904

Assignment on Java

OutPut:-

Mahant LaxmiNarayan das College

PGDCA 2nd Semester

Page 3

Name: AKASH SAGAR

Roll No. 84904

Assignment on Java

Q.3 WAP the use Boolean data type and print the Prime number Series up
to 50.
Program:class Primes
{
public static void main(String args[])
{
int n,i;
for(n=1;n<=50;n++)
{
boolean temp=false;
for(i=2;i<=n-1;i++)
{
if(n%i==0)
{
temp=true;
}
}
if(temp==false)
{
System.out.print(n);
System.out.print(" ");
}
}
}
}
OutPut:-

Mahant LaxmiNarayan das College

PGDCA 2nd Semester

Page 4

Name: AKASH SAGAR

Roll No. 84904

Assignment on Java

Q.4 WAP for matrix multiplication using input/output Stream.


Program:import java.io.*;
class Mat
{
public static void main(String arg[]) throws Exception
{
BufferedReader
br=newBufferedReader(newInputStreamReader(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.parse.Int(br.readLine());
}
}
System.out.println("Input matrix A is = ");
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.parse.Int(br.readLine());
}

Mahant LaxmiNarayan das College

PGDCA 2nd Semester

Page 5

Name: AKASH SAGAR

Roll No. 84904

Assignment on Java

}
System.out.println("Input matrix B is = ");
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++)
{
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");
}
}
}

Mahant LaxmiNarayan das College

PGDCA 2nd Semester

Page 6

Name: AKASH SAGAR

Roll No. 84904

Assignment on Java

OutPut:-

Mahant LaxmiNarayan das College

PGDCA 2nd Semester

Page 7

Name: AKASH SAGAR

Roll No. 84904

Assignment on Java

Q.5 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.*;
class languageVector
{
public static void main (String args[])
{
Vector list=new Vector();
int Length=args.length;
for(int i=0;i<Length;i++)
{
list.addElement(args[i]);
}
list.insertElementAt("cobol",3);
int size=list.size();
String listArray[]= new String[size];
list.copyInto(listArray);
System.out.println("list of language");
for(int i=0;i<size;i++)
System.out.println(listArray[i]);
}
}
OutPut:-

Mahant LaxmiNarayan das College

PGDCA 2nd Semester

Page 8

Name: AKASH SAGAR

Roll No. 84904

Assignment on Java

Q.6 WAP to check that the given String is palindrome or not.


Program:class Palin
{
public static void main(String ar[])
{
String s1=ar[0];
StringBuffer sb1=new StringBuffer (s1);
StringBuffer sb2=new StringBuffer ();
sb2=sb1.reverse();
String s2;
s2=sb2.toString();
if(s1.equals(s2))
{
System.out.println("A palindrome.");
}
else
{
System.out.println("Not a palindrome.");
}
}
}
OutPut:-

Mahant LaxmiNarayan das College

PGDCA 2nd Semester

Page 9

Name: AKASH SAGAR

Roll No. 84904

Assignment on Java

Q.7 WAP to arrange the String in alphabetical order.


Program:class StringOrdering
{
static String name[]={"Vinay","Omesh","Mohit","Ram","Gyanu"};
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]);
System.out.print("\n");
}
}
}

Mahant LaxmiNarayan das College

PGDCA 2nd Semester

Page 10

Name: AKASH SAGAR

Roll No. 84904

Assignment on Java

OutPut:-

Mahant LaxmiNarayan das College

PGDCA 2nd Semester

Page 11

Name: AKASH SAGAR

Roll No. 84904

Assignment on Java

Q.8 WAP to StringBuffer class which perform the all methods of that
class.
Program:class Stringmanupulation
{
public static void main (String args[])
{
StringBuffer str = new StringBuffer ("object lang");
System.out.println("original string" +str);
System.out.println("String length=" +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.setCharAt (6,'_');
System.out.println("String now" +str);
str.append("Improve Sequrity");
System.out.println("Append string:" +str);
}
}
OutPut:-

Mahant LaxmiNarayan das College

PGDCA 2nd Semester

Page 12

Name: AKASH SAGAR

Roll No. 84904

Assignment on Java

Q.9 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 Abstracts
{
public static void main(String arg[])
{
Circle c1=new Circle(6);
c1.area();
c1.perimeter();
}

Mahant LaxmiNarayan das College

PGDCA 2nd Semester

Page 13

Name: AKASH SAGAR

Roll No. 84904

Assignment on Java

}
OutPut:-

Mahant LaxmiNarayan das College

PGDCA 2nd Semester

Page 14

Name: AKASH SAGAR

Roll No. 84904

Assignment on Java

Q.10 WAP where Single class implements more than one interface and
with help of interface reference variable user call the methods.
Program: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(10, 20));
area = cir;
System.out.println("Area of Circle ="+area.compute(10, 0));
}
}

Mahant LaxmiNarayan das College

PGDCA 2nd Semester

Page 15

Name: AKASH SAGAR

Roll No. 84904

Assignment on Java

OutPut:-

Mahant LaxmiNarayan das College

PGDCA 2nd Semester

Page 16

Name: AKASH SAGAR

Roll No. 84904

Assignment on Java

Q.11 WAP that use the multiple catch statements within the try-catch
mechanism.
Program:class TryCatch
{
public static void main(String arg[])
{
int a[]={5,10};
int b=5;
try
{
int x=a[1]/(b-a[0]);
}
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]/(b+a[0]);
System.out.println("Y = "+y);
}
}

Mahant LaxmiNarayan das College

PGDCA 2nd Semester

Page 17

Name: AKASH SAGAR

Roll No. 84904

Assignment on Java

OutPut:-

Mahant LaxmiNarayan das College

PGDCA 2nd Semester

Page 18

Name: AKASH SAGAR

Roll No. 84904

Assignment on Java

Q.12 WAP where user will create a self-Exception using the throw
keywords.
Program:import java.lang.Exception;
class myException extends Exception
{
myException(String msg)
{
super(msg);
}
}
class testmyException
{
public static void main(String args[])
{
int x=5,y=1000;
try
{
float z=(float)x/(float)y;
if(z<(0.01))
{
throw new myException("number is
small");
}
}
catch(myException e)
{
System.out.println("caugth my exception e");
System.out.println(e.getMessage());
}
finally
{
System.out.println("i am always here");
}
}
}
OutPut:-

Mahant LaxmiNarayan das College

PGDCA 2nd Semester

too

Page 19

Name: AKASH SAGAR

Mahant LaxmiNarayan das College

Roll No. 84904

Assignment on Java

PGDCA 2nd Semester

Page 20

Name: AKASH SAGAR

Roll No. 84904

Assignment on Java

Q.13 WAP for multithread using is Alive(),join() and Synchronized()


methods of Thread class.
Program: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);
}

Mahant LaxmiNarayan das College

PGDCA 2nd Semester

Page 21

Name: AKASH SAGAR

Roll No. 84904

Assignment on Java

catch(Exception e)
{
System.out.println(""+e);
}
}
}
class Joinn
{
public static void main(String args[])
{
Thread t=Thread.currentThread();
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);
}
System.out.println("State of First" + t1.isAlive());
System.out.println("State of main" + t.isAlive());
}
}

Mahant LaxmiNarayan das College

PGDCA 2nd Semester

Page 22

Name: AKASH SAGAR

Roll No. 84904

Assignment on Java

OutPut:-

Mahant LaxmiNarayan das College

PGDCA 2nd Semester

Page 23

Name: AKASH SAGAR

Roll No. 84904

Assignment on Java

Q.14 WAP to create a package using command and one package will
import another package.
Program:Step1:-package package1;
public class A
{
public void displayA( )
{
System.out.println("class A");
}
}
Step2:-import package1.A;
class packagetest
{
public static void main(String args[] )
{
A ob=new A( );
ob.displayA( );
}
}
Step3:-package package2;
public class B
{
protected int m=10;
public void displayB( )
{
System.out.println("class B");
System.out.println("m="+m);
}
}
Step4:-import package1.A;
import package2.B;
class packagetest2
{
public static void main(String args[] )
{
A Aob=new A( );
B Bob=new B( );
Aob.displayA( );
Bob.displayB( );
}
}

Mahant LaxmiNarayan das College

PGDCA 2nd Semester

Page 24

Name: AKASH SAGAR

Roll No. 84904

Assignment on Java

OutPut:-

Mahant LaxmiNarayan das College

PGDCA 2nd Semester

Page 25

Name: AKASH SAGAR

Roll No. 84904

Assignment on Java

Q.15 WAP for Applet that handle the keyboard Events.


Program:import java.lang.*;
import java.awt.*;
import java.applet.*;
/*<Applet code=UserIn.class
width=200
height=300>
</Applet>*/
public class UserIn extends Applet
{
TextField text1, text2;
public void init( )
{
text1=new TextField(8);
text2=new TextField(8);
add(text1);
add(text2);
text1.setText("0");
text2.setText("0");
}
public void paint (Graphics g)
{
int x=0, y=0, z=0;
String s1, s2, s;
g.drawString("Input a number in each box" ,10,50);
try
{
s1=text1.getText();
x=Integer.parseInt(s1);
s2=text2.getText();
y=Integer.parseInt(s2);
}
catch(Exception ex) { }
z=x+y;
s=String.valueOf(z);
g.drawString("The sum is:",10,75);

Mahant LaxmiNarayan das College

PGDCA 2nd Semester

Page 26

Name: AKASH SAGAR

Roll No. 84904

Assignment on Java

g.drawString(s,100,75);
}
public boolean action (Event event, Object object)
{
repaint();
return true;
}
}
OutPut:-

Mahant LaxmiNarayan das College

PGDCA 2nd Semester

Page 27

Name: AKASH SAGAR

Roll No. 84904

Assignment on Java

Q.16 WAP for JDBC to insert the values into the existing table by using
prepared Statements.
Program:import java.sql.*;
public class Myinsert
{
public static void main(String ar[]) throws Exception
{
Connection con;
Statement stmt;
String query;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:pgdca");
stmt=con.createStatement();
stmt.executeUpdate("insert into table1"+"
values(4,'Monu','bca-3',98261982)");
ResultSet
rs=stmt.executeQuery("select*from
pgdca");
System.out.println("My DATA");
while(rs.next())
{
System.out.print(rs.getInt(1));
System.out.print("\t");
System.out.print(rs.getString(2));
System.out.print("\t");
System.out.print(rs.getString(3));
System.out.print("\t");
System.out.print(rs.getInt(4));
System.out.print("\n");
}
stmt.close();
con.close();
}
catch(SQLException ex)

Mahant LaxmiNarayan das College

PGDCA 2nd Semester

Page 28

Name: AKASH SAGAR

Roll No. 84904

Assignment on Java

{
System.err.println("SQLException:"+ex.getMessage());
}
}
}
OutPut:-

Mahant LaxmiNarayan das College

PGDCA 2nd Semester

Page 29

Name: AKASH SAGAR

Roll No. 84904

Assignment on Java

Q.17 WAP for JDBC to display the record from the existing table.
Program:import java.sql.*;
public class Odbc
{
public static void main(String ar[]) throws Exception
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection
con=DriverManager.getConnection("jdbc:odbc:pgdca");
System.out.println("Connection Ok.");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from
table1");
System.out.println("=========");
while(rs.next())
{
System.out.print(rs.getInt(1));
System.out.print("\t");
System.out.print(rs.getString(2));
System.out.print("\t");
System.out.print(rs.getString(3));
System.out.print("\t");
System.out.print(rs.getInt(4));
System.out.print("\n");
}
con.close();
}
catch(Exception ex)
{
System.err.println("Exception: "+ex.getMessage());
}
}
}

Mahant LaxmiNarayan das College

PGDCA 2nd Semester

Page 30

Name: AKASH SAGAR

Roll No. 84904

Assignment on Java

OutPut:-

Mahant LaxmiNarayan das College

PGDCA 2nd Semester

Page 31

Name: AKASH SAGAR

Roll No. 84904

Assignment on Java

Q.18 WAP to demonstrate the Border Layout using applet.


Program:import java.awt.*;
import java.applet.*;
/*<applet code="Borders.class"
width=400
height=800>
</applet> */

public class Borders extends Applet


{
Button b1,b2,b3,b4,b5;
public void init()
{
setLayout (new BorderLayout());
b1=new Button("East");
add(b1,BorderLayout.EAST);
b2=new Button("West");
add(b2,BorderLayout.WEST);
b3=new Button("North");
add(b3,BorderLayout.NORTH);
b4=new Button("SOUTH");
add(b4,BorderLayout.SOUTH);
}
}
OutPut:-

Mahant LaxmiNarayan das College

PGDCA 2nd Semester

Page 32

Name: AKASH SAGAR

Mahant LaxmiNarayan das College

Roll No. 84904

Assignment on Java

PGDCA 2nd Semester

Page 33

Name: AKASH SAGAR

Roll No. 84904

Assignment on Java

Q.19 WAP for Applet who generates the MouseMotionListener Event.


Program:import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*<applet code="MouseEvents" width=300 height=100>
</applet>*/
public class MouseEvents extends Applet implements MouseListener,
MouseMotionListener
{
String msg=" ";
int mousex=0, mousey=0;
public void init()
{
addMouseListener(this);
addMouseMotionListener(this);
}
//Handle Mouse Clicked
public void mouseClicked(MouseEvent me)
{
mousex=0;
mousey=20;
msg="MouseClicked";
repaint();
}
//Handle Mouse Entered
public void mouseEntered(MouseEvent me)
{
mousex=0;
mousey=20;
msg="MouseEntered";
repaint();
}
//Handle Mouse Exited
public void mouseExited(MouseEvent me)
{
mousex=0;

Mahant LaxmiNarayan das College

PGDCA 2nd Semester

Page 34

Name: AKASH SAGAR

Roll No. 84904

Assignment on Java

mousey=20;
msg="MouseClicked";
repaint();
}
//Handle Button Pressed
public void mousePressed(MouseEvent me)
{
mousex=me.getX();
mousey=me.getY();
msg="Down";
repaint();
}
//Handle Button released
public void mouseReleased(MouseEvent me)
{
mousex=me.getX();
mousey=me.getY();
msg="Up";
repaint();
}
//Handle Button Dragged
public void mouseDragged(MouseEvent me)
{
mousex=me.getX();
mousey=me.getY();
msg="*";
showStatus("Dragging mouse at"+mousex+" , " + mousey);
repaint();
}
//Handle Mouse moved
public void mouseMoved(MouseEvent me)
{
showStatus("Moving mouse
at"+me.getX()+ " , " +
me.getY());
}
//Handle Mouse moved
public void paint(Graphics g)

Mahant LaxmiNarayan das College

PGDCA 2nd Semester

Page 35

Name: AKASH SAGAR

Roll No. 84904

Assignment on Java

{
g.drawString(msg, mousex, mousey);
}
}
OutPut:-

Mahant LaxmiNarayan das College

PGDCA 2nd Semester

Page 36

Name: AKASH SAGAR

Roll No. 84904

Assignment on Java

Q.20 WAP for display the checkboxes, Labels and TextFields on an AWT.
Program:import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
/*<APPLET CODE= Checks.class width=200
height=200>
</APPLET>*/
public class Checks extends Applet implements ItemListener
{
Checkbox ch1,ch2,ch3;
TextField text1;
Label I1;
public void init()
{
I1=new Label("Check Box");
add(I1);
ch1=new Checkbox("1");
add(ch1);
ch1.addItemListener(this);
ch2=new Checkbox("2");
add(ch2);
ch2.addItemListener(this);
ch3=new Checkbox("3");
add(ch3);
ch3.addItemListener(this);
text1=new TextField(20);
add(text1);
}
public void itemStateChanged(ItemEvent e)
{
text1.setText("Check
box"+((Checkbox)e.getItemSelectable()).getLabel()+"clicked");
Mahant LaxmiNarayan das College

PGDCA 2nd Semester

Page 37

Name: AKASH SAGAR

Roll No. 84904

Assignment on Java

}
}
OutPut:-

Mahant LaxmiNarayan das College

PGDCA 2nd Semester

Page 38

Name: AKASH SAGAR

Roll No. 84904

Assignment on Java

Q.21 WAP for creating a file and to store data into that file.(Using the file
WriterIOStream)
Program:-

import java.io.*;
class writebytes
{
public static void main(String args[ ])
{
byte cities[
]={'D','E','L','H','I','\n','M','A','D','R','A','S','\n','L','O','N','D','O','N'
,'\n'};
FileOutputStream outfile=null;
try
{
outfile=new FileOutputStream("city.txt");
outfile.write(cities);
outfile.close( );
}
catch(IOException ioe)
{
System.out.println("ioe" );
System.exit(-1);
}
}
}
OutPut:-

Mahant LaxmiNarayan das College

PGDCA 2nd Semester

Page 39

Name: AKASH SAGAR

Mahant LaxmiNarayan das College

Roll No. 84904

Assignment on Java

PGDCA 2nd Semester

Page 40

Name: AKASH SAGAR

Roll No. 84904

Assignment on Java

Program-22:-WAP to display your file in DOS console use the


Input/Output Stream.
Program:import java.io.*;
class ReadBytes
{
public static void main(String args[ ])
{
// Create an input file Stream
FileInputStream infile=null;
int b;
try
{
// Connect infile Stream to the required file
infile=new FileInputStream(args [0]);
// Read and display data
while( (b=infile.read ( ))!=-1)
{
System.out.print((char)b);
}
infile.close();
}
catch (IOException ioe)
{
System.out.println (ioe);
}
}
}
OUTPUT:

Mahant LaxmiNarayan das College

PGDCA 2nd Semester

Page 41

Оценить