[Go to site: main page, start]

0% found this document useful (0 votes)
7 views32 pages

Practical-JavaProgramming With Date

The document contains a series of Java programming exercises covering various topics such as calculating the area and perimeter of a circle, implementing packages and interfaces, exception handling, multithreading, and creating graphical user interfaces with AWT. Each section includes source code examples and sample outputs demonstrating the functionality of the programs. Additionally, it covers database operations and remote method invocation (RMI) for client-server communication.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views32 pages

Practical-JavaProgramming With Date

The document contains a series of Java programming exercises covering various topics such as calculating the area and perimeter of a circle, implementing packages and interfaces, exception handling, multithreading, and creating graphical user interfaces with AWT. Each section includes source code examples and sample outputs demonstrating the functionality of the programs. Additionally, it covers database operations and remote method invocation (RMI) for client-server communication.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Ex.

No Date Page Sign


TABLE OF CONTENTS No
1. To Find the Area and Perimeter of a Circle Using
BufferedReader Class
2. Implementing and importing packages.

3. Implementing Interfaces-Arithmetic Manipulations

4. Exception Handling

5. Multithreading

6. Loading Image onto Applet

7. Implement an application for Arithmetic operation using


AWT.
8. Create a Database Program for Storing and Manipulating
Student Mark list using AWT
9. Program to Send two values to Server Program and get Back
the Result calculated using RMI

10. Incorporating Circle symbol onto BeanBox


1. To Find the Area and Perimeter of a Circle Using
BufferedReader Class

SOURCE CODE:

import [Link].*;
class AreaPeri
{
public static void main(String args[])
{
String s;
float radi,area,peri;
final double pi=3.147;
try
{
BufferedReader br = new BufferedReader(new InputStreamReader([Link]));
[Link]("Enter the Radius value");
s=[Link]();
radi=[Link](s);
area=(float)(pi*radi*radi);
peri =(float)(2.0*pi*radi);
[Link]("Area="+area);
[Link]("Perimeter="+peri);
}
catch(IOException e)
{
[Link]("Input Error" +e);
}
}
}
SAMPLE OUTPUT:
2. Importing and Implementing Packages

Source code:

Package 1

package tree;
public class sample
{
public void add(int a,int b)
{
[Link]("Addation="+(a+b));
}
}

Package 2:

package orange;
public class exam
{
public void mul(int a,int b)
{
[Link]("Multiply="+(a*b));
}
}
Importing & Implementing Packages:-

import [Link].*;
import [Link];
import [Link];
class pkg
{
public static void main(String args[])
{
sample oa = new sample();
[Link](20,20);
exam ob = new exam();
[Link](12,12);
}
}
Sample Output:-
3. Implementing Interface Using – Arithmetic Manipulations
Source Code :

interface sample
{
public void add(int a,int b);
public void sub(int a,int b);
final int c=10;
}
interface example extends sample
{
public void mul(int a,int b);
public void div(int a,int b);
}
interface module
{
public void mod(int a,int b);
final static int d=5;
}
class arith implements example,module
{
public void add(int a,int b)
{
[Link]("Addition is = "+(a+b));
}
public void sub(int a,int b)
{
[Link]("Subtraction is = "+(a-b));
}
public void mul(int a,int b)
{
[Link]("Multiplication is = "+(a*b));
}
public void div(int a,int b)
{
[Link]("Division = "+(a/b));
}
public void mod(int a,int b)
{
[Link]("Modules division is "+(a%b));
}
}
class Iface
{
public static void main(String[] args)
{
arith ob=new arith();
[Link]("\n The multiplication value of c and d is:"+(ob.c*ob.d));
[Link]("\n The value of d is :"+ob.d);
[Link]("\t Methods implemented in interfaces are:\n");
[Link](25,25);
[Link](10,5);
[Link](5,2);
[Link](10,2);
[Link](4,20);
}
}
SAMPLE OUTPUT :
4. Exception Handling
Source Code :
import [Link].*;
class mcexc
{
public static void main(String args[]) throws IOException
{
int a,c[];
int b,f;
BufferedReader br;
try
{
a=[Link];
[Link](a);
int d = 42/a;
br = new BufferedReader(new InputStreamReader([Link]));
[Link]("Enter the Value");
b=[Link]([Link]());
c= new int[-2];
c[56] = 99;
}
catch(ArithmeticException e)
{
[Link]("Arithmetic Error "+e);
}
catch(NegativeArraySizeException o)
{
[Link]("Array Error" +o);
}
catch(NumberFormatException j)
{
[Link]("Number Format Exception " +j);
}
}
}

Sample Output :-
5. Multithreading

Source Code:

import [Link].*;
class mythread implements Runnable
{
String name;
Thread t;
mythread(String sname)
{
name = sname;
t = new Thread(this);
[Link]("thread name"+t);
[Link]();
}
public void run()
{
try
{
for(int i=0;i<=5;i++)
[Link](name+":"+i);
}
catch(Exception e)
{
}
}
}
class multithreg
{
public static void main(String args[]) throws IOException
{
mythread ob1 = new mythread("one");
mythread ob2 = new mythread("two");
mythread ob3 = new mythread("Three");
try
{
[Link](500);
}
catch(InterruptedException e)
{
}
}
}

Sample Output:-
6. LOADING IMAGE ONTO APPLET
SOURCE CODE:
import [Link].*;
import [Link].*;
import [Link].*;
/*
<applet code= ImageTest Width=500 height=500>
</applet>
*/
public class ImageTest extends Applet
{
private Image img;
private String imgName;

public void init()


{
img = null;
imgName = getParameter("[Link]");
}
public void loadImage()
{
try
{
img = getImage(getCodeBase(), "[Link]");
}
catch(Exception e) { }
}
public void paint(Graphics g)
{
if (img == null)
loadImage();
[Link](img, 0, 0, this);
}
}

OUTPUT
7. IMPLEMENT AN APPLICATION FOR ARITHMETIC OPERATIONS USING
AWT

SOURCE CODE:
import [Link].*;
import [Link].*;

public class grid extends Frame implements ActionListener


{
Button b1,b2,b3,b4;
TextField t1,t2,t3;
public grid()
{
super("Example for Grid Layout");
setLayout(new GridLayout(4,2));
b1=new Button("ADDITION");
b2=new Button("SUBRACTION");
b3=new Button(" MULTIPLICATION");
b4=new Button("DIVISION");
t1=new TextField(10);
t2=new TextField(10);
t3=new TextField(10);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
add(b1);
add(b2);
add(b3);
add(b4);
add(t1);
add(t2);
add(t3);
setSize(300,300);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if([Link]()==b1)
{
int n1=[Link]([Link]());
int n2=[Link]([Link]());
int n3=n1+n2;
[Link]([Link](n3));
}

if([Link]()==b2)
{
int n1=[Link]([Link]());
int n2=[Link]([Link]());
int n3=n1-n2;
[Link]([Link](n3));
}
if([Link]()==b3)
{
int n1=[Link]([Link]());
int n2=[Link]([Link]());
int n3=n1*n2;
[Link]([Link](n3));
}

if([Link]()==b4)
{
int n1=[Link]([Link]());
int n2=[Link]([Link]());
int n3=n1/n2;
[Link]([Link](n3));
}

public static void main(String ar[])


{
new grid();
}
}
OUTPUT:
8. CREATING A DATABASE FOR STORING AND MANIPULATING STUDENT
MARKLIST USING AWT

SOURCE CODE:
import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
public class jdbcframe1 extends Frame implements ActionListener
{
Label l1,l2,l3,l4,l5;
TextField t1,t2,t3,t4,t5;
TextArea ta;
Button b1;
ResultSet rs=null;
public jdbcframe1()
{
super("II BSC COMPUTER SCIENCE");
setLayout(new GridLayout(7,5,6,2));
l1=new Label("name");
add(l1);
t1=new TextField(20);
add(t1);
l2=new Label("age");
add(l2);
t2=new TextField(20);
add(t2);
l3=new Label("m1");
add(l3);
t3=new TextField(20);
add(t3);
l4=new Label("m2");
add(l4);
t4=new TextField(20);
add(t4);
l5=new Label("m3");
add(l5);
t5=new TextField(20);
add(t5);

ta=new TextArea(" ",25,40);


add(ta);
b1=new Button("CLICK");
add(b1);
[Link](this);
}
public void actionPerformed(ActionEvent e)
{
if([Link]()==b1)
{
try
{
[Link]("[Link]");
Connection con=[Link]("jdbc:odbc:orc");
Statement st=[Link]();
String s1=[Link]();
int i=[Link]([Link]());
int i1=[Link]([Link]());
int i2=[Link]([Link]());
int i3=[Link]([Link]());
int tot=i1+i2+i3;
[Link]("insert into csc values('"+s1+"','"+i+"','"+i1+"','"+i2+"','"+i3+"','"+tot+"')");
rs=[Link]("select *from csc");
while([Link]())
{
String data1=[Link](1);
int data2=[Link](2);
int data3=[Link](3);
int data4=[Link](4);
int data5=[Link](5);
int data6=[Link](6);
[Link](data1+" ");
[Link](data2+" ");
[Link](data3+" ");
[Link](data4+" ");
[Link](data5+" ");
[Link](data6+" ");
[Link]("\n");
}
}
catch(Exception ex)
{
[Link]([Link]());
}
}
}
public static void main(String args[])
{
jdbcframe1 f=new jdbcframe1();
[Link](600,400);
[Link]();
}
}
OUTPUT :
DATABASE TABLE
9. PROGRAM TO SEND TWO VALUES TO SERVER PROGRAM AND GET BACK
THE RESULT CALCULATED USING RMI

SOURCE CODE:

/*Client*/
import [Link].*;
public class addc
{
public static void main(String args[])
{
try
{
String addserverURL="rmi://"+args[0]+"/addserver";
addintf ads=(addintf)[Link](addserverURL);
[Link]("the first number is:"+args[1]);
double d1=[Link](args[1]).doubleValue();
[Link]("the second number is:"+args[2]);
double d2=[Link](args[2]).doubleValue();
[Link]("the sum is:"+[Link](d1,d2));
}
catch(Exception e)
{
[Link]("exception"+e);
}
}
}
/*Server*/
import [Link].*;
import [Link].*;
public class adds
{
public static void main(String args[])
{
try
{
addimpl ad=new addimpl();
[Link]("addserver",ad);
}
catch(Exception e)
{
[Link]("exception: "+e);
}
}
}
/*Implementation*/
import [Link].*;
import [Link].*;
public class addimpl extends UnicastRemoteObject implements addintf
{
public addimpl() throws RemoteException
{
}
public double add(double d1,double d2)throws RemoteException
{
return(d1+d2);
}
}
/*Interface*/

import [Link].*;
public interface addintf extends Remote
{
double add(double d1,double d2)throws RemoteException;
}
OUTPUT

/*Server*/

/*Interface*/
/*Implementation*/
/*Client*/
10. INCORPORATING CIRCLE SYMBOL ONTO BEANBOX

SOURCE CODE:
import [Link].*;
import [Link].*;
import [Link].*;

/*<applet code="graph" width=300 height=300>


</applet>
*/

public class graph extends Applet


{
int x[]={330,500,330,500,330};
int y[]={330,330,500,500,330};

public void paint(Graphics g)


{
[Link]("joseph",0,20);
[Link](10,150,200,100);
[Link](50,12,60,80);
[Link](155,12,95,95);
[Link](290,12,60,50,15,15);
[Link](400,12,60,50,15,15);
[Link](100,152,60,80);
[Link](2000,152,60,80 );
[Link](250,112,60,180,0,175);
[Link](350,112,60,180,0,275);
[Link](x,y,5);

}
}
OUTPUT

Creating Jar File

Java Bean using BDK

You might also like