Java Lab Final - Mca
Java Lab Final - Mca
JAVA PROGRAMMING
NAME :
BRANCH : SEMESTER
CERTIFICATE
NAME OF LABORATORY: _________________________________________
BRANCH: SEMESTER: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
1 SINGLE INHERITANCE
2 INTERFACE
4 USER-SPECIFIC EXCEPTION
5 APPLET
6 MULTI-THREADING
8 RMI APPLICATION
10 JDBC Connection
Ex.No:1
Date:
SINGLE INHERITANCE
/*Package-MathMeth.java*/
package Mathematical;
public class MathMeth
{
public int sqr(int a)
{
return(a*a);
}
public int cube(int a)
{
return(a*a*a);
}
public int fact(int a)
{
int i,f=1;
for(i=1;i<=5;i++)
{
f=f*i;
}
return(f);
}
}
/*Main Program-PackageDemo.java*/
import Mathematical.*;
import java.io.*;
class PackageDemo
{
public static void main(String args[])
{
ArithMeth a=new ArithMeth();
MathMeth b=new MathMeth();
System.out.println("Using Arithmetic:");
System.out.println("Addition:"+a.add(10,5));
System.out.println("Subtraction:"+a.sub(10,5));
System.out.println("Multiplication:"+a.multi(10,5));
System.out.println("Division:"+a.div(10,5));
System.out.println("Modulo:"+a.mod(10,5));
System.out.println("\nUsing Mathematical:");
System.out.println("Square:"+b.sqr(5));
System.out.println("Cube:"+b.cube(5));
System.out.println("Factorial:"+b.fact(5));
}
}
OUTPUT:
Ex No:4
Date:
USER SPECIFIC EXCEPTION HANDLING
import java.io.*;
import java.lang.*;
import java.lang.Exception;
class UserExcep extends Exception
{
UserExcep(String n)
{
super(n);
}
public static void main(String args[])throws IOException
{
DataInputStream dis=new DataInputStream(System.in);
int age;
try
{
System.out.println("Enter your age:");
age=Integer.parseInt(dis.readLine());
if(age>=18)
{
System.out.println("Eligible for Vote");
}
else
{
throw new UserExcep("Not Eligible for Vote");
}
}
catch(NumberFormatException e)
{
System.out.println(e);
}
catch(UserExcep e)
{
System.out.println(e);
}
}
}
OUTPUT:
Ex. No:5
Date:
APPLET: SMILEY
Output:
> javac Smiley.java
> appletviewer Smiley.java
Ex. No: 6
Date:
MULTI-THREADING
import java.util.*;
import java.io.*;
import java.lang.*;
class MyThread implements Runnable
{
public void run()
{
for(int i=0;i<10;i++)
{
System.out.println("Child Thread");
}
}
}
class Test
{
public static void main(String args[])
{
MyThread t1=new MyThread();
Thread t2=new Thread(t1);
t2.start();
for(int i=0;i<10;i++)
{
System.out.println("Main Thread");
}
}
}
OUTPUT:
e:\java>javac Test.java
e:\java>java Test
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Main Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
Child Thread
e:\java>
Ex. No:7
Date:
ADDINT.JAVA
Import java.rmi.*;
Public interface addint extends Remote
{
Int myadd(int a, int b) throws RemoteException;
}
ADDIMPL.JAVA
Import java.rmi.*;
Import java.rmi.server.*:
Public class addimpl extends unicast RemoteObject implements addint
{
Public addimpl()throws RemoteException
{}
Public int myad(int a, int b) throws RemoteException
{
Int c;
C=a+b;
Return(c);
}
}
ADDSERVER.JAVA
Import java.rmi.*
Public class addserver
{
Public static void main (string args[])throws Exception
{
Addimpl a=new addimpl();
Naming.rebind(“ad”,a);
System.out.println(“server started”);
}
}
ADDCLIENT.JAVA
Import java.io.*;
Import java.rmi.*;
Public class addclient
{
Int a,b;
Addint m=(addint)Naming.lookup(“ad”);
DataInputStream d=new DataInputStream (System.in);
a=integer.parseInt(d.readline());
b=integer.parseInt(d.readline());
system.out.println(m.myadd(a,b));
}
}
OUTPUT:
Ex.No:9
Date:
/*Number.txt*/
/*Go to File menu of BeanBox, and click load jar menu and open the Number.jar*/
/*After drag and drop the Number from the Toolbox into BeanBox*/
Ex. No:10
Date:
JDBC Connection
import java.sql;
public class dbcon
{
public Static void main(String arg[])throws Exception
{
class forname("oracle.jdbc.oracleDriver");
connection
con=driverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe"system","oracle
");
statement st=con.createstatement();
Resultset rs=con.createStatemen();
while(rs.next())
System.out.println(rs.getstring(1)+"\t"+rs.getstring(2));
con.close();
}
Output: