You are on page 1of 10

class Rectangle

{
int length, width;
void getdata(int x, int y)
{
x=length;
y=width;
}
int rectarea()
{
int area=length*width;
return (area);
}}
class Rectarea
{
public static void main(String args[])
{
int area1, area2;
Rectangle rect1=new Rectangle();
Rectangle rect2=new Rectangle();
rect1.length=15;
rect1.width=10;
area1=rect1.length*rect1.width;
rect2.getdata(10,40);
area2=rect2.rectarea();
System.out.println("area1=" +area1);
System.out.println("area2=" +area2);
}}
class Quadrant
{
int a = 10;
int b =5;
int c =2;
double d;
void root()
{
d=(b*b-(4*a*c));
if(d>0)
{
System.out.println("roots are real");
}
else if(d<0)
{
System.out.println("roots are imaginary");
}
else if(d==0)
{
System.out.println("roots are equal");
}}}
class Quadratic
{
public static void main( String args[])
{
Quadrant q= new Quadrant();
q.root();
}}
Code for sample1(pack)
package pack;
public class Sample1
{
public void display()
{
System.out.println("hello");
}}

Code for sample2(subpack)


package pack.subpack;
public class Sample2
{
public void show()
{
System.out.println("welcome");
}}

Code for import


import pack.*;
import pack.subpack.*;
class Imp
{
public static void main(String args[])
{
Sample1 obj1 = new Sample1();
obj1.display();
Sample2 obj2 = new Sample2();
obj2.show();
}}
class Room
{
int length;
int breadth;
Room(int x, int y)
{
length=x;
breadth=y;
}
int area()
{
return(length*breadth);
}}
class Bedroom
{
int height;
Bedroom(int x,int y, int z)
{
super(x,y);
height=z;
}
int volume()
{
return(length*breadth*height);
}}
class Inhertest
{
public static void main(String args[])
{
Bedroom room1=new Bedroom(14,12,20);
int area1=room1.area();
int volume1= room1.volume();
System.out.println("area=" +area1);
System.out.println("volume=" +volume);
}}
Interface Area
{
final static double pi= 3.14;
double compute(double x, double y);
}
class Rectangle implements Area
{
public double compute(double x, double y)
{
return (x*y);
}}
class Circle implements Area
{
public double compute(double x, double y)
{
return(pi*x*x);
}}
class Interfacetest
{
public static void main (String args[])
{
Rectangle rect=new Rectangle();
Circle cir=new Circle();
Area area;
area= rect;
System.out.println("area of rectangle=" +area.compute(10.5,20.5));
area=cir;
System.out.println("area of circle=" +area.compute(10.2,0.0));
}}
Class Erorr
{
public static void main(String args[])
{
int a =10;
int b = 5;
int c=5;
int x,y;
try
{
x = a/(b-c);
}
catch(ArithematicException e)
{
System.out.println(“division by zero”);
}
y =a/(b+c);
System.out.println(“y=”+y);
}
}
Class Error
{
public static void main(String args[])
{
int a[] = {5,10};
int b= 5;
try
{
int x = a[2]/b-a[1];
}
catch(ArtihemeticException e)
{
Sytem.out.println(“division by zero”);
}
catch(ArrayIndexOutOfBoundException e)
{
Sytem.out.println(“array index error”);
}
catch(ArrayStoreException e)
{
Sytem.out.println(“wrong data type”);
}
int y = a[1]/a[0];
System.out.println(“y=”+y);
}
}
import java.io.*;
class CopyBytes
{
public static void main(String args[])
{
FileInputStream infile=null;
FileOutputStream outfile=null;
byte byteRead;
try
{
infile=new FileInputStream ("in.dat");
outfile=new FileOutputStream ("out.dat");
do
{
byteRead =(byte) infile.read();
outfile.write (byteRead);
}
while (byteRead != -1);
}
catch (FileNotFoundException e)
{
System.out.println("File not found");
}
catch (IOException e)
{
System.out.println (e.getMessage () );
}
finally
{
try
{
infile.close();
outfile.close();
}
catch (IOException e) {}
}
}
}

class A extends Thread


{
public void run()
{
for(int i=1;i<=5;i++)
{
System.out.println("\t from thread A: i=" +i);
}
System.out.println("exit from A");
}}
class B extends Thread
{
public void run()
{
for(int j=1;j<=5;j++)
{
System.out.println("\t from thread b: j=" +j);
}
System.out.println("exit from b");
}}
class C extends Thread
{
public void run()
{
for(int k=1;k<=5;k++)
{
System.out.println("\t from thread c: k=" +k);
}
System.out.println("exit from c");
}}
class ThreadTest
{
public static void main (String args[])
{
new A().start();
new B().start();
new C().start();
}}

You might also like