Jp Lab Manual
Jp Lab Manual
“OBJECT ORIENTED
PROGRAMMING USING
JAVA / JAVA
PROGRAMMING”
LABORATORY MANUAL
RD TH
(3 /5 SEMESTER CSE)
ASSIGNMENT NO. 2:
Write a program for the swapping of two numbers.
ASSIGNMENT NO. 3:
Write a program to print all the prime numbers between 100 to 200.
ASSIGNMENT NO. 4:
Write a program to check whether a number is palindrome or not.
ASSIGNMENT NO. 5:
Write a program to display the student name, roll no and age of a student using class
and object concept.
ASSIGNMENT NO. 6:
Write separate programs that shows the implementation of
ASSIGNMENT NO. 7:
Write a program that shows the use of default, parameterized and copy constructor.
ASSIGNMENT NO. 8:
Write separate programs for method overloading and method overriding mechanism.
ASSIGNMENT NO. 9:
Write a program for single inheritance mechanism.
PROGRAM:
class Add
{
public static void main(String args[ ])
{
int x = Integer.parseInt(args[0]);
int y = Integer.parseInt(args[1]);
int z=x+y;
System.out.println(“The Sum=”+z);
}
}
ASSIGNMENT NO. 2:
Write a program for the swapping of two numbers.
PROGRAM:
class Swap
{
public static void main(String args[ ])
{
int x = Integer.parseInt(args[0]);
int y = Integer.parseInt(args[1]);
x=x+y;
y=x-y;
x=x-y;
System.out.println(“After swapping the two numbers are”+x+ “ ”+y );
}
}
ASSIGNMENT NO. 3:
Write a program to print all the prime numbers between 100 to 200.
PROGRAM:
class Prime
{
public static void main(String args[ ])
{
for(int i=100;i<=200;i++)
{
int c=0;
for(int j=1;j<=i;j++)
{
if(i%j==0)
{
c++;
}
}
if(c==2)
{
System.out.println(i);
}
}
}
}
ASSIGNMENT NO. 4:
Write a program to check whether a number is palindrome or not.
PROGRAM:
class Pali
{
public static void main(String args[ ])
{
int x = Integer.parseInt(args[0]);
int n,b,r=0;
n=x;
while(x>0)
{
b=x%10;
r=r*10+b;
x=x/10;
}
if(n==r)
{
System.out.println(“The number is palindrome”);
}
else
{
System.out.println(“The number is not palindrome”);
}
}
}
ASSIGNMENT NO. 5:
Write a program to display the student name, roll no and age of a student using class
and object concept.
PROGRAM:
class Student
String n;
int r,a;
n=i;
r=j;
a=k;
void display( )
System.out.println(“The Name=”+n);
System.out.println(“The Age=”+a);
class Student1
String z=args[0];
int m=Integer.parseInt(args[1]);
int n=Integer.parseInt(args[2]);
Student k = new Student( );
k.accept(z,m,n);
k.display( );
}
ASSIGNMENT NO. 6:
Write separate programs that shows the implementation of
PROGRAM:
(i)
class Use
{
int a;
static int b;
Use(int i,int j)
{
a=i;
b=j;
}
void show()
{
System.out.println(a);
}
static void display()
{
System.out.println(b);
}
}
class Use1
{
public static void main(String args[])
{
Use k=new Use(5,7);
k.show();
k.display();
}
}
(ii)
class Use
{
static int x;
static
{
x=10;
System.out.println(x);
}
public static void main(String args[])
{
System.out.println(“hello”);
}
}
ASSIGNMENT NO. 7:
Write a program that shows the use of default, parameterized and copy constructor.
PROGRAM:
class Use
int a,b;
a=4;
b=9;
System.out.println(a+ “ “ +b);
a=i;
b=j;
System.out.println(a+ “ “+ b);
a=k.a;
b=k.b;
System.out.println(a+ “ “+ b);
class Use1
}
ASSIGNMENT NO. 8:
Write separate programs for method overloading and method overriding mechanism.
PROGRAM:
Method Overloading:
class Use
int a,b;
a=i;
System.out.println(a);
a=i;
b=j;
System.out.println(a+ “ “+ b);
class Use1
k.display(6);
k.display(5,9);
}
Method Overriding:
class A
{
void show( )
{
System.out.println(“hello”);
}
}
class B extends A
{
void show( )
{
System.out.println(“world”);
}
}
class C
{
public static void main(String args[ ])
{
B k=new B( );
A t=new A( );
k.show( );
t.show( );
}
}
ASSIGNMENT NO. 9:
Write a program for single inheritance mechanism.
PROGRAM:
class A
{
void show( )
{
System.out.println(“hello”);
}
}
class B extends A
{
void disp( )
{
System.out.println(“world”);
}
}
class C
{
public static void main(String args[ ])
{
B k=new B( );
k.show( );
k.disp( );
}
}
ASSIGNMENT NO. 10:
Write a program where abstract class concept is used.
PROGRAM:
abstract class A
{
void show( );
}
class B extends A
{
void show( )
{
System.out.println(“world”);
}
}
class C
{
public static void main(String args[ ])
{
B k=new B( );
k.show( );
}
}
ASSIGNMENT NO. 11:
Write a program to pass the arguments from child class constructor to parent class
constructor using super ( ).
PROGRAM:
class A
{
int x,y;
A( int i , int j)
{
x=i;
y=j;
}
}
class B extends A
{
int z;
{
super(i,j);
z=k;
}
void disp( )
{
System.out.println(x+y+z);
}
}
class C
{
public static void main(String args[ ])
{
B k=new B(10,20,30 );
k.disp( );
}
}
ASSIGNMENT NO. 12:
Write a program to display name, department and age of employee using interface
mechanism.
PROGRAM:
public interface A
void name(String n)
System.out.println(n);
void dept(String d)
System.out.println(d);
void age(int a)
System.out.println(a);
B k=new B( );
k.name(“Ram”);
k.dept(“finance”);
k.age(30);
}
ASSIGNMENT NO. 13:
Write a program for the implementation of user defined package.
PROGRAM:
File-1
package Color;
System.out.println(“Red”);
File-2
package Color;
System.out.println(“Green”);
File-3
package Color;
System.out.println(“Blue”);
}
}
Implementation File
import Color.*;
class Pack
r.disp1( );
g.disp2( );
b.disp3( );
}
ASSIGNMENT NO. 14:
Write a program for the manipulation of string using different functions of String class.
PROGRAM:
class Str
System.out.println(s1.indexOf(„l‟));
System.out.println(s1.lastIndexOf(„l‟));
System.out.println(s1.subString(4));
String s2=s1.toUpperCase( );
String s3=s1.toLowerCase( );
}
ASSIGNMENT NO. 15:
Write a program for the execution of multiple threads with their priority.
PROGRAM:
class Use1 extends Thread
System.out.println(“Thread1”);
System.out.println(“Thread2”);
System.out.println(“Thread3”);
class Use
{
Use1 t1=new Use1( );
t1.setPriority(Thread.MAX_PRIORITY);
t2.setPriority(6);
t3.setPriority(t2.getPriority( )+1);
t1.start( );
t2.start( );
t3.start( );
}
ASSIGNMENT NO. 16:
Write a complete program for the execution of applet mechanism.
PROGRAM:
import java.awt.*;
import java.applet.*;
System.out.println(“initialized”);
System.out.println(“started”);
System.out.println(“painting”);
g.drawString(“My Applet”,10,100);
System.out.println(“stopping”);
System.out.println(“destroying”);
}
HTML File:
<html>
<body>
</applet>
</body>
</html>
ASSIGNMENT NO. 17:
Write a program for drawing several GUI components on applet.
PROGRAM:
import java.awt.*;
import java.applet.*;
setFont(new Font(“SansSerif”,Font.BOLD,32));
add(“East”,b1);
add(“West”,b2);
add(“North”,b3);
add(“South”,b4);
add(“Center”,t1);
}
ASSIGNMENT NO. 18:
Write a program for the implementation of several methods of Graphics class.
PROGRAM:
import java.awt.*;
import java.applet.*;
g.drawLine(20,20,90,90);
g.drawRect(20,70,90,90);
g.drawOval(20,20,200,120);
g.setColor(Color.Green);
g.fillOval(70,30,100,100);
g.drawRoundRect(10,100,80,50,10,10);
g.fillRoundRect(20,110,60,30,5,5);
}
ASSIGNMENT NO. 19:
Write a program for handling multiple exceptions.
PROGRAM:
class Exc
try
int x=Integer.parseInt(args[0]);
int y=Integer.parseInt(args[1]);
int z=x/y;
System.out.println(“result=”+z);
catch (NumberFormatException n)
catch (ArrayIndexOutOfBound a)
System.out.println(“give arguments”);
}}}
ASSIGNMENT NO. 20:
Write a complete program for Java Database Connectivity.
PROGRAM:
import java.sql.*;
import java.io.*;
class Conn
Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
Connection con=DriverManager.getConnection(“jdbc:odbc:DSN”,”scott”,”tiger”);
Statement st=con.createStatement( );
While(rs.next( ))
System.out.println(rs.getInt(“empno”)+”:”+rs.getString(“ename”));
Con.close();
}
ASSIGNMENT NO. 21:
Write a program for the use of swing components.
PROGRAM:
import javax.swing.*;
public class FirstSwingExample {
public static void main(String[] args) {
JFrame f=new JFrame();//creating instance of JFrame
PROGRAM:
File: MyServer.java
import java.io.*;
import java.net.*;
public class MyServer {
public static void main(String[] args){
try{
ServerSocket ss=new ServerSocket(6666);
Socket s=ss.accept();//establishes connection
DataInputStream dis=new DataInputStream(s.getInputStream());
String str=(String)dis.readUTF();
System.out.println("message= "+str);
ss.close();
}catch(Exception e){System.out.println(e);}
}
}
File: MyClient.java
import java.io.*;
import java.net.*;
public class MyClient {
public static void main(String[] args) {
try{
Socket s=new Socket("localhost",6666);
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
dout.writeUTF("Hello Server");
dout.flush();
dout.close();
s.close();
}catch(Exception e){System.out.println(e);}
}
}
ASSIGNMENT NO. 23:
Write a program for the multiplication of two matrix.
PROGRAM:
import java.util.Scanner;
class MatrixMultiplication
{
public static void main(String args[])
{
int m, n, p, q, sum = 0, c, d, k;
if ( n != p )
System.out.println("Matrices with entered orders can't be multiplied with each other.");
else
{
int second[][] = new int[p][q];
int multiply[][] = new int[m][q];
multiply[c][d] = sum;
sum = 0;
}
}
System.out.print("\n");
}
}
}
}
ASSIGNMENT NO. 24:
Write a program for the addition of two matrix.
PROGRAM:
import java.util.Scanner;
class AddTwoMatrix
{
public static void main(String args[])
{
int m, n, c, d;
Scanner in = new Scanner(System.in);
System.out.println();
}
}
}
ASSIGNMENT NO. 25:
Write a program to display the transpose of a matrix.
PROGRAM:
import java.util.Scanner;
class TransposeAMatrix
{
public static void main(String args[])
{
int m, n, c, d;
System.out.print("\n");
}
}