0% found this document useful (0 votes)
124 views24 pages

Java Lab Final - Mca

Uploaded by

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

Java Lab Final - Mca

Uploaded by

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

MADURAI KAMARAJ UNIVERSITY COLLEGE ALAGARKOVIL

ROAD, MADURAI – 624710

JAVA PROGRAMMING

RECORD NOTE BOOK

NAME :

BRANCH : SEMESTER

UNIVERSITY REGISTER NO:


MADURAI KAMARAJ UNIVERSITY COLLEGE ALAGARKOVIL
ROAD, MADURAI – 624710

CERTIFICATE
NAME OF LABORATORY: _________________________________________

UNIVERSITY REGISTER NO: _________________________________________

BRANCH: SEMESTER: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

Certified that this is the bonafide record of work done by


during the semester ( ) in the year
2022-2023.

Staff in Charge Head of Department


Date:

Submitted for the Practical Examination held on at


Madurai

Kamaraj University College, Madurai.

External Examiner Internal Examiner


INDEX
Pag
S. No Date Program e Signature
No

1 SINGLE INHERITANCE

2 INTERFACE

3 CREATION OF USER-SPECIFIC PACKAGES

4 USER-SPECIFIC EXCEPTION

5 APPLET

6 MULTI-THREADING

7 READING AND WRITING TEXT FILE

8 RMI APPLICATION

9 CREATION AND USAGE OF JAVABEANS

10 JDBC Connection
Ex.No:1
Date:
SINGLE INHERITANCE

import java.io.*; class StudentInfo


{
String name; int regno;
void get(String a,int b)
{
name=a; regno=b;
}
void put()
{
System.out.println("Student Detail's:"); System.out.println("Name:"+name);
System.out.println("Regno:"+regno);
}
}
class StudentMark extends StudentInfo
{
int m1,m2,m3,tot; double avg;
void getmark(int c,int d,int e)
{
m1=c; m2=d; m3=e;
tot=m1+m2+m3; avg=tot/3;
}
void putmark()
{
System.out.println("\nStudent Mark's:"); System.out.println("M1:"+m1); System.out.println("M2:"+m2);
System.out.println("M3:"+m3); System.out.println("Total:"+tot); System.out.println("Average:"+avg);
}
}
class SingleInh
{
public static void main(String args[])throws IOException
{
StudentMark s=new StudentMark(); s.get("karthi",306); s.getmark(95,97,99);
s.put();
s.putmark();
}
}
OUTPUT:
Ex No:2
Date:
INTERFACE
import java.io.*;
import java.lang.Math.*;
interface Interest
{
final static double r=2.5;
final static double n=5.0;
double Interests(double p);
}
class SimpInterest implements Interest
{
public double Interests(double p)
{
return((p*n*r)/100);
}
}
class CompInterest implements Interest
{
double a;
public double Interests(double p)
{
a=Math.pow((1+(r/100)),n);
return(p*(a-1));
}
}
class InterfaceDemo
{
public static void main(String args[])
{
Interest i;
SimpInterest si=new SimpInterest();
CompInterest ci=new CompInterest();
i=si;
System.out.println("Simple Interest:"+i.Interests(500));
i=ci;
System.out.println("Compound Interest:"+ci.Interests(1000));
}
}
OUTPUT:
Ex No:3
Date:
CREATION OF USER SPECIFIC PACKAGES
/*Package-ArithMeth.java*/
package Mathematical;
public class ArithMeth
{
public int add(int a,int b)
{
return(a+b);
}
public int sub(int a,int b)
{
return(a-b);
}
public int multi(int a,int b)
{
return(a*b);
}
public int div(int a,int b)
{
return(a/b);
}
public int mod(int a,int b)
{
return(a%b);
}
}

/*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

// Java program to Draw a Smiley using Java Applet


import java.applet.*;
import java.awt.*;
/*<applet code ="Smiley" width=600 height=600>
</applet>*/

public class Smiley extends Applet {


public void paint(Graphics g)
{

// Oval for face outline


g.drawOval(80, 70, 150, 150);

// Ovals for eyes


// with black color filled
g.setColor(Color.BLACK);
g.fillOval(120, 120, 15, 15);
g.fillOval(170, 120, 15, 15);

// Arc for the smile


g.drawArc(130, 180, 50, 20, 180, 180);
}
}

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:

Microsoft Windows [Version 6.1.7601]


Copyright (c) 2009 Microsoft Corporation. All rights reserved.

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:

READING AND WRITING TEXT FILE


import java.io.*;
import java.lang.*;
class FileInput
{
void fread()throws IOException
{
int size;
InputStream f=new FileInputStream("File1.txt");
size=f.available();
System.out.println("Number of bytes available for read:"+size);
for(int i=0;i<10;i++)
System.out.println((char)f.read());
byte[] b=new byte[size];
f.read(b);
int s=b.length;
System.out.println("Reading from the buffer...\n");
System.out.println(new String(b,0,s));
f.close();
}
}
class FileOutput
{
void fwrite()throws FileNotFoundException,IOException,SecurityException
{
String source="this is good morning";
byte buf[]=source.getBytes();
OutputStream fo=new FileOutputStream("File1.txt");
fo.write(buf);
fo.close();
}
}
class FileDemo
{
public static void main(String args[])throws IOException
{
FileOutput fo=new FileOutput();
fo.fwrite();
FileInput fi=new FileInput();
fi.fread();
}
}
OUTPUT:

/*Open the File1.txt*/


Ex No:8
Date:
RMI APPLICATION TO ACCESS A REMOTE METHOD

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:

CREATION AND USAGE OF JAVABEAN


import
java.io.Serializable;
import java.beans.*;
import java.awt.*;
public class Number extends Canvas implements Serializable
{
private int num;
public Number()
{
num=54;
}
public void setNum(int num)
{
this.num=num;
}
public int getNum()
{
return num;
}
public void paint(Graphics g)
{
setBackground(Color.pink);
Font f=new Font("Times New Roman",Font.BOLD,20);
g.setFont(f);
g.setColor(Color.cyan);
g.drawString("Number is:"+num,50,50);
}
}
OUTPUT:

/*Number.txt*/

/*Open the Cmd*/


/*Click the run*/

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

You might also like