Program:
import java.io.*;
interface area
{
final static float pi=3.14f;
float compute(float x, float y);
}
class rectangle implements area
{
public float compute(float x,float y)
{
return(x*y);
}
}
class circle implements area
{
public float compute(float x,float y)
{
return(pi*x*x);
}
}
class interfacte
{
public static void main(String args[])throws IOException
{
rectangle rect=new rectangle();
circle cir=new circle();
area ar;
ar=rect;
System.out.println("area of the rectangle" +ar.compute(10,20));
ar=cir;
System.out.println("area of the circle" +ar.compute(10,0));
}
}
Output:
C:\>set path="C:\Program Files\Java\jdk1.6.0\bin"
C:\>k:
K:\>javac interfacte.java
K:\>java interfacte
area of the rectangle200.0
area of the circle314.0
K:\>
Program:
circle.java
package saurav;
import java.io.*;
public class circle
{
public void area(double x)
{
double k=3.14*x*x;
System.out.println("Area of circle:"+k);
}
public void circum(double x)
{
double cir=2.0*3.14*x;
System.out.println("Circumference of circle:"+cir);
}
}
rectangle.java
package saurav;
import java.io.*;
public class rectangle
{
public void area(double x,double y)
{
double z=x*y;
System.out.println("the area:"+z);
}
public void circum(double x,double y)
{
double r=2*(x+y);
System.out.println("the circum.:"+r);
}
}
triangle.java
package saurav;
import java.io.*;
public class triangle
{
public void area(double x,double y)
{
double z=(x*y)/(double)2;
System.out.println("the area is:"+z);
}
public void circum(double x,double y,double k)
{
double v=x+y+k;
System.out.println("the circumference is:"+v);
}
}
shapes.java
import java.io.*;
import saurav.rectangle;
import saurav.triangle;
import saurav.circle;
class shapes
{
public static void main(String args[])throws IOException
{
double x=10,y=10,z=5;
rectangle r=new rectangle();
r.area(x,y);
r.circum(x,y);
triangle t=new triangle();
t.area(x,y);
t.circum(x,y,z);
circle c=new circle();
c.area(x);
c.circum(x);
}
}
Output:
C:\>set path="C:\Program Files\Java\jdk1.6.0\bin"
C:\>k:
K:\>cd saurav
K:\saurav>javac circle.java
K:\saurav>javac rectangle.java
K:\saurav>javac triangle.java
K:\saurav>cd..
K:\>javac shapes.java
K:\>java shapes
the area:100.0
the circumference:40.0
the area:50.0
the circumference:25.0
the area:314.0
the circumference:62.80
K:\>
Program:
import java.io.*;
public class samplethread extends Thread
{
public samplethread(String x)
{
super(x);}
public static void main(String args[])throws IOException
{
Thread t=new Thread();
System.out.println("current thread"+t);
System.out.println("after change"+t);
try
{
for(int n=5;n>0;n--)
{
System.out.println(n);
Thread.sleep(5000);
}
}
catch(InterruptedException e)
{
System.out.println("main thread interrupted");
}
}}
Output:
C:\>set path=”C:\Program Files\Java\jdk1.5.0_06\bin”
C:\>k:
K:\>javac samplethread.java
K:\>java samplethread
current threadThread[Thread-2,5,main]
after changeThread[Thread-2,5,main]
5
4
3
2
1
K:\>
Program:
import java.applet.*;
import java.awt.*;
public class CircleLine extends Applet{
int x=300,y=100,r=50;
public void paint(Graphics g){
g.drawLine(3,300,200,10);
g.drawString("Line",100,100);
g.drawOval(x-r,y-r,100,100);
g.drawString("Circle",275,100);
g.drawRect(400,50,200,100);
g.drawString("Rectangel",450,100);
}
}
Circle.html
<HTML>
<HEAD>
</HEAD>
<BODY>
<div align="center">
<APPLET CODE="CircleLine.class" WIDTH="800" HEIGHT="500"></APPLET>
</div>
</BODY>
</HTML>
Output:
C:\>set path=”C:\Program Files\Java\jdk1.5.0_06\bin”
C:\>k:
K:\>javac CirleLine.java
K:\>appletviewer CircleLine.java
K:\>
Applet Viewer X
Program:
import java.applet.*;
import java.awt.*;
public class pgmLabel extends Applet
{
Label label;
public void init()
{
resize(320, 240);
add(new Label("Example EX05D: Labels and Buttons"));
label = new Label("Social Security Number:", Label.RIGHT);
add(label);
add(new TextField(25));
add(new Button("Change"));
}
public boolean action(Event evt, Object obj)
{
boolean result=false;
if("Change".equals(obj)) {
if(label.getText().equals("Name:"))
label.setText("Social Security Number:");
else
label.setText("Name:");
}
result = true;
return result;
}
}
Output:
C:\>set path=”C:\Program Files\Java\jdk1.5.0_06\bin”
C:\>k:
K:\>javac pgmLabel.java
K:\>appletviewer pgmLabel.java
K:\>
Program:
import java.applet.*;
import java.awt.*;
public class EX05F extends Applet
{
Choice genderChoice = new Choice();
List sportList = new List(5, true);
TextArea results = new TextArea(10, 25);
TextField newSport = new TextField(15);
public void init()
{
add(new Label("Gender: "));
genderChoice.addItem("Male");
genderChoice.addItem("Female");
add(genderChoice);
add(new Label("Sports: "));
sportList.addItem("Swim");
sportList.addItem("Bike");
sportList.addItem("Run");
sportList.addItem("Row");
sportList.addItem("Weight Lift");
add(sportList);
add(new Label("New Sport: "));
add(newSport);
add(new Button("Add"));
add(new Button("Delete"));
add(new Button("Save"));
add(results);
resize(320, 240);
}
public boolean action(Event evt, Object obj)
{
boolean result=false;
int i;
if("Save".equals(obj)) {
// clear the results area
results.setText("");
// display the gender
String gender = genderChoice.getSelectedItem();
results.appendText(gender + "\r\n");
for(i=0;i<sportList.countItems();i++) {
if(sportList.isSelected(i))
results.appendText(sportList.getItem(i) + "\r\n");
}
result = true;
}
else if("Add".equals(obj)) {
String sport = newSport.getText();
if(sport.length() > 0) {
sportList.addItem(sport);
newSport.setText("");
}
}
else if("Delete".equals(obj)) {
for(i=sportList.countItems()-1; i>=0; i-) {
if(sportList.isSelected(i))
sportList.delItem(i);
}
}
return result;
}
}
Output:
C:\>set path=”C:\Program Files\Java\jdk1.5.0_06\bin”
C:\>k:
K:\>javac pgmLabel.java
K:\>appletviewer pgmLabel.java
K:\>
Program:
import java.awt.event.*;
import java.awt.*;
import java.applet.*;
/*<applet code="MDem" width=300 height=300></applet>*/
public class MDem extends Applet implements MouseListener,MouseMotionListener
{
int x,y;
public void init()
{
SetBackground(colour white);
SetForeground(colour blue);
addMouseListener(this);
}
public void mouseClicked(mouseEvent me)
{
x=me.getx();
y=me.gety();
show status("mouseClicked at"+x+" "+y);
repaint();
}
public void mouseEntered(mouseEvent me)
{
x=me.get x();
y=me.get y();
show status("mouseEntered at"+x+" "+y);
}
public void mouseExited(mouseEvent me)
{
x=me.getx();
y=me.gety();
show status("mouseExited at"+x+" "+y);
}
repaint();
}
public void mousePressed(mouseEvent me)
{
x=me.get x();
y=me.get y();
show status("mousePressed at"+x+" "+y);
repaiant();
}
public void mouseReleased(mouseEvent me)
{
x=me.get x();
y=me.get y();
show status("mouseReleased at"+x+" "+y);
repaint();
}
public void mouseMoved(mouseEvent me)
{
x=me.get x();
y=me.get y();
show status("mouseMoved at"+x+" "+y);
repaint();
}
repaint();
public void mouseDragged(mouseEvent me)
{
x=me.get x();
y=me.get y();
show status("mouseDragged at"+x+" "+y);
repaint();
}
public void paint(Graphics G)
{
G.fillOval(x,y,20,20);
G.drawString("circle following mouse",x+20,y+20);
}
Output:
AppletViewer mDem
Applet
Mouse clicked at 304 152
Program:
import java.awt.*;
import java.applet.*;
public class Add extends Applet
{
TextField text1,text2;
Button calculate;
Button clr;
public void init()
{
setLayout(new FlowLayout());
text1=new TextField(8);
text2=new TextField(8);
add(text1);
add(text2);
text1.setText("0");
text2.setText("0");
calculate=new Button("Calculate");
clr=new Button("Clear");
add(calculate);
add(clr);
}
public void paint(Graphics g)
{
int x=0,y=0,z=0;
String s1,s2,s;
g.drawString("input a number in each box",10,50);
try
{
s1=text1.getText();
x=Integer.parseInt(s1);
s2=text2.getText();
y=Integer.parseInt(s2);
}
catch(Exception ex){}
z=x+y;
s=String.valueOf(z);
g.drawString("the sum is ",10,75);
g.drawString(s,100,75);
}
public boolean action(Event event,Object object)
{
if (event.target instanceof Button)
{
if(event.arg.equals("Calculate"))
{
repaint();
}
}
if (event.target instanceof Button)
{
if(event.arg.equals("Clear"))
{
text1.setText("0");
text2.setText("0");
repaint();
}
}
return true;
}
}
Output:
AppletViewer mDem
Applet
Mouse clicked at 304 152