0% found this document useful (0 votes)
10 views6 pages

Set 9

The document contains code examples for creating classes in Java including a Cube class with constructors and methods to calculate volume, a Thread class example to perform addition and multiplication in separate threads, a Circle class with properties and methods for area calculation, an Applet class example to draw a polygon, and an example of reading a file using FileReader.

Uploaded by

html backup
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)
10 views6 pages

Set 9

The document contains code examples for creating classes in Java including a Cube class with constructors and methods to calculate volume, a Thread class example to perform addition and multiplication in separate threads, a Circle class with properties and methods for area calculation, an Applet class example to draw a polygon, and an example of reading a file using FileReader.

Uploaded by

html backup
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
You are on page 1/ 6

1)

public class Cube{


double length,bredth,height;
Cube(){
length = 5;
bredth = 5;
height = 5;

}
Cube(double l,double b, double h){
length = l;
bredth = b;
height = h;

}
Cube(Cube c2){
length = c2.length;
bredth = c2.bredth;
height = c2.height;
}
void volume(){
System.out.println("Volume of Cube : "+ (length * bredth * height));
}
public static void main(String[] args){
Cube c1 = new Cube();
Cube c2 = new Cube(9,9,9);
Cube c3 = new Cube(c2);

c1.volume();
c2.volume();
c3.volume();
}
}

2)

class Addition extends Thread


{
int sum=0;
public void run()
{
try
{
for(int i=1;i<=10;i++)
{
sum=sum+i;
System.out.println("addition : "+sum);
}
Thread.sleep(500);
}
catch(Exception e)
{
System.out.println(e);
}
}
}

class Mul extends Thread


{
int mul=1;
public void run()
{
try
{
for(int i=1;i<=10;i++)
{
mul=mul*i;
System.out.println("multiplication : "+mul);
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
class P9_2
{
public static void main(String args[])
{
Addition a=new Addition();
Mul m=new Mul();

a.start();
m.start();
}
}
3)
class Circle
{
double radius;
double pi=3.14;
Circle(double r)
{
radius=r;
}

void display()
{
System.out.println("Radius : "+radius);
System.out.println("PI : "+pi);
}
void area()
{
System.out.println("Area : "+(pi*radius*radius));
}
}

class P9_3
{
public static void main(String args[])
{
Circle c=new Circle(7);
c.display();
c.area();
}
}
4)
import java.applet.Applet;
import java.awt.*;

public class P9_4 extends Applet


{
public void paint(Graphics g)
{
int xPoints[]={10,170,80,10};
int yPoints[]={20,40,140,20};
int n1=4;

g.drawPolygon(xPoints,yPoints,n1);
}
}

/*
<applet code="P9_4" width=500 height=500>
</applet>
*/
5)
import java.io.FileReader;
public class P9_5
{
public static void main(String args[]) throws IOException
{
FileReader fin=new FileReader(args[0]);
int i;
while((i=fin.read())!= -1)
{
System.out.print((char)i);
}
fin.close();
}
}

You might also like