Sunteți pe pagina 1din 19

Program 1(A)

Aim: - Write a program to implement Static Polymorphism (Method Overloading).


Source Code: package staticpolymorphism;
public class StaticPolymorphism {
void sum(int a,int b) {
System.out.println("\n Sum is "+(a+b));
}
void sum(int a,int b,int c) {
System.out.println("\n Sum is "+(a+b+c));
}
public static void main(String[] args) {
StaticPolymorphism obj=new StaticPolymorphism();
obj.sum(10,10,10);
obj.sum(20,20);
}
}

Output: -

Program 1(B)
Aim: - Write a program to implement Dynamic Polymorphism (Method
Overriding).
Source Code: package dynamicpolymorphism;
class Animal {
public void move() {
System.out.println("Animals can move");
}
}
class Dog extends Animal {
public void move() {
System.out.println("Dogs can walk and run");
}
}
public class DynamicPolymorphism {
public static void main(String[] args) {
Animal a = new Animal(); // Animal reference and object
Animal b = new Dog(); // Animal reference but Dog object
a.move();//output: Animals can move
b.move();//output: Dogs can walk and run
}
}

Output: -

Program - 2
Aim: - Write a program to implement Abstract class and Interface.
Source Code: package animal;
interface Legs{
void no_of_legs();
}
abstract class Animal {
public void move(){ }
}
class Dog extends Animal implements Legs {
public void move() {
System.out.println("Dogs can walk and run.");
}
public void no_of_legs(){
System.out.println("Dogs have 4 legs.");
}
public static void main(String[] args) {
Dog d = new Dog(); // Dog object
d.move(); // Output: Dogs can walk and run
d.no_of_legs(); // Output: Dogs have 4 legs
}
}

Output: -

Program - 3
Aim: - Write a program to implement Exception Handling.
Source Code: package example;
import java.io.*;
public class Main {
public static void main(String[] args) {
int a,b;
DataInputStream in=new DataInputStream(System.in);
try {
System.out.println("Enter the values of a : ");
a= Integer.parseInt(in.readLine());
System.out.println("Enter the values of b : ");
b= Integer.parseInt(in.readLine());
float c=a/b;
System.out.println(c);
}
catch(Exception e) {
System.out.println("Divide by zero exception"+e);
}
}
}

Output: -

Program 4(A)
Aim: - Write a program to implement Multithreading by extending Thread class.
Source Code: package example;
class A extends Thread {
public void run() {
int i;
for(i=1;i<=5;i++) {
System.out.println("i = "+i);
}
try {
Thread.sleep(2000);
}
catch (Exception e) {
}
}
}
class B extends Thread {
public void run()
{
int j;
for(j=1;j<=5;j++) {
System.out.println("j = "+j);
}
}
}
public class Main {

public static void main(String[] args) {


A obja=new A();
B objb= new B();
obja.start();
objb.start();
}
}

Output: -

Program 4(B)
Aim: - Write a program to implement Multithreading by implementing Runnable
interface.
Source Code: package example;
class NewThread implements Runnable {
Thread t;
public NewThread() {
t=new Thread(this,"DemoTHREAD");
System.out.println("Child Thread : "+t);
t.start();
}
public void run(){
try {
for(int i=5;i>0;i--) {
System.out.println("Child Thread: "+i);
Thread.sleep(500);
}
}
catch (Exception e) {
System.out.println("Child Interupted");
System.out.println("Exiting Child Thread");
}
}
}
class ThreadDemo{
public static void main(String[] args) {

new NewThread();
try {
for(int i=5;i>0;i--) {
System.out.println("Main Thread: "+i);
Thread.sleep(1000);
}
}
catch(InterruptedException e) {
System.out.println("Main Thread Interrupted");
}
System.out.println("Main Thread Exiting");
}
}

Output: -

Program 5(A)
Aim: - Write a program to implement TCP MyServer.
Source Code: import java.io.*;
import java.net.*;
public class MyServer {
DataInputStream dis;
public MyServer() {
try {
System.out.println("!!! Server started !!!");
System.out.println("Waiting for client connection ...");
ServerSocket ss=new ServerSocket(1000);
Socket s=ss.accept();
System.out.println("!!! Client connected !!!\n\t");
dis=new DataInputStream(s.getInputStream());
ServerChat();
}
catch(IOException e) {
System.out.println(e);
}
catch(Exception e) {
System.out.println(e);
}
}
public void ServerChat() throws IOException {
String str;
do {
str=dis.readUTF();
System.out.println("Client Message: "+str);
}while(!str.equals("stop"));
}

public static void main(String args[]) {


new MyServer();
}
}

Output: -

Program 5(B)
Aim: - Write a program to implement TCP MyClient.
Source Code: import java .io.*;
import java .net.*;
public class MyClient {
DataOutputStream dout;
DataInputStream din;
public MyClient() {
try {
Socket s= new Socket("192.168.1.6",1000);
dout =new DataOutputStream(s.getOutputStream());
System.out.println("Enter the message to the server... ");
Clientchat();
}
catch(IOException e) {
System.out.println(e);
}
catch(Exception e) {
System.out.println(e);
}
}
public void Clientchat() throws IOException {
String str;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
do {
str=br.readLine();
dout.writeUTF(str);
}while(!str.equals("stop"));
}

public static void main(String agrs[]) {


new MyClient();
}
}

Output: -

Program 6(A)
Aim: - Write a Program to implement JDBC connectivity.
Source Code: import java.sql.*;
public class Connectivity {
public static void main(String[] args) throws Exception {
int marks;
ResultSet rs=null;
String rollno;
String str_name;
Connection conn;
String db="jdbc:odbc:DB2";
conn = DriverManager.getConnection(db,"","");
System.out.println("** Connected to Database **");
Statement stmt = conn.createStatement();
rs= stmt.executeQuery("Select S.rollno,S.stu_Name,S.marks " + " " + " from
Table1 s" + " " + "where s.rollno='11cse01'");
System.out.println("Rollno Name Marks");
System.out.println(" _ _ _ _ _ _");
while(rs.next()) {
str_name= rs.getString(2);
marks= rs.getInt(3);
rollno=rs.getString(1);
System.out.println(rollno+" "+ str_name+" "+ marks);
}
stmt.close();
conn.commit();
conn.close();
}
}

Output: -

Program 6(B)
Aim: - Write a program to insert the Employee Details in database.
Source Code: package javaapplication12;
import java.sql.*;
public class Main {
public static void main(String[] args)throws Exception {
String EMP_ID ;
String EMP_NAME;
String EMP_ADDRESS;
int EMP_SALARY;
int EMP_PHONENUMBER;
ResultSet rs=null;
Connection conn;
String db="jdbc:odbc:DB4";
try {
conn=DriverManager.getConnection(db,"","");
System.out.println("**Connected to database**");
String sql="insert into employee values('111a','Ritik',25000,'E24
Fridabad',9900)";
PreparedStatement stmt = conn.prepareStatement(sql);
int abc=stmt.executeUpdate();
System.out.println("values inserted");
stmt.close();
conn.commit();
conn.close();
}
catch(Exception e){
}
}

Output: -

Program 7
Aim: - Write a program to implement Remote Method Invocation.
Source Code: MyRemote.java
import java.rmi.*;
public interface MyRemote extends Remote {
public String SayHello() throws RemoteException;
}

MyRemoteImpl.java
import java.rmi.*;
import java.rmi.server.*;
public class MyRemoteImpl extends UnicastRemoteObject implements MyRemote {
public String SayHello() throws RemoteException {
return "Server say, Hey !!!";
}
public MyRemoteImpl() throws RemoteException {}
public static void main(String []args) {
try {
MyRemoteImpl service = new MyRemoteImpl();
Naming.rebind("RemoteHello", service);
}
catch(Exception e)
{
e.printStackTrace();
}
}

MyRemoteClient.java
import java.rmi.*;
public class MyRemoteClient {
public static void main(String []args) {
try {
String url= "rmi://localhost/RemoteHello";
MyRemote service1= (MyRemote)Naming.lookup(url);
String s=service1.SayHello();
System.out.println(s);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}

Output: -

S-ar putea să vă placă și