Java File
Java File
import java.io.*;
class Division
{
public static void main(String args[])
{
int n,d;
try
{
DataInputStream in=new DataInputStream(System.in);
System.out.print("\nEnter the dividend : ");
System.out.flush();
String s=in.readLine();
n=Integer.parseInt(s);
System.out.print("\nEnter the divisor : ");
System.out.flush();
s=in.readLine();
d=Integer.parseInt(s);
System.out.println("\nQuotient = "+(n/d));
System.out.println("\nRemainder = "+(n-(n/d)*d));
}
catch(IOException e)
{
System.out.println("Exception Ocurred :"+e);
}
}
}
OUTPUT
============================
C:\Java\jdk1.5.0\bin>javac division.java
C:\Java\jdk1.5.0\bin>java Division
Quotient = 4
Remainder = 1
C:\Java\jdk1.5.0\bin>
1
Software Lab-IX(Java)
class Average
{
public static void main(String a[])
{
float sum=0;
for(int i=0;i<a.length;i++)
sum=sum+Integer.parseInt(a[i]);
try
{
float avg;
avg=sum/(a.length);
System.out.print("\nAvg of "+a.length + " numbers is : "+avg);
}
catch(Exception e)
{
System.out.println("Exception Ocurred : "+e);
}
}
}
OUTPUT
============================
C:\Java\jdk1.5.0\bin>javac average.java
C:\Java\jdk1.5.0\bin>java Average 10 23 67 89 34
C:\Java\jdk1.5.0\bin>
2
Software Lab-IX(Java)
//WAP to read 3 numbers from the keyboard & find greatest among them
import java.io.*;
class Greatest
{
public static void main(String args[])
{
int a=0,b=0,c=0;
try
{
DataInputStream in=new DataInputStream(System.in);
System.out.print("\nEnter 1st number : ");
System.out.flush();
String s=in.readLine();
a=Integer.parseInt(s);
System.out.print("\nEnter 2nd number : ");
System.out.flush();
s=in.readLine();
b=Integer.parseInt(s);
System.out.print("\nEnter 3rd number : ");
System.out.flush();
s=in.readLine();
c=Integer.parseInt(s);
}
catch(IOException e)
{
System.out.println("Exception Ocurred :"+e);
}
if(a>b)
{
if(a>c)
System.out.println("\n1st number is greatest");
else
System.out.println("\n3rd number is greatest");
}
else
{
if(b>c)
System.out.println("\n2nd number is greatest");
else
System.out.println("\n3rd number is greatest");
}
}
}
3
Software Lab-IX(Java)
OUTPUT
============================
C:\Java\jdk1.5.0\bin>javac greatest.java
C:\Java\jdk1.5.0\bin>java Greatest
C:\Java\jdk1.5.0\bin>
4
Software Lab-IX(Java)
//WAP to declare more than one class and access the variables & methods of
//these classes using objects.
class Rectangle
{
int length,width;
void getdata(int x,int y)
{
length=x;
width=y;
}
int rect()
{
int area=length*width;
return(area);
}
}
class ClassObject
{
public static void main(String arg[])
{
int ar1,ar2;
Rectangle obj1=new Rectangle();
Rectangle obj2=new Rectangle();
obj1.length=20;
obj1.width=30;
ar1=obj1.rect();
obj2.getdata(30,40);
ar2=obj2.rect();
System.out.println("area from ar1==>"+ar1);
System.out.println("area from ar2==>"+ar2);
}
}
OUTPUT
============================
C:\Java\jdk1.5.0\bin>javac ClassObject.java
C:\Java\jdk1.5.0\bin>java ClassObject
5
Software Lab-IX(Java)
class Hello
{
int m;
int n;
Hello()
{
m=0;
n=0;
}
Hello(int x,int y)
{
m=x;
n=y;
}
void sum()
{
System.out.print("m="+m+"\tn="+n);
System.out.println(" Sum : "+(m+n));
}
}
class MyConstructor
{
public static void main(String a[])
{
Hello H1=new Hello();
Hello H2=new Hello(20,30);
H1.sum();
H2.sum();
}
}
OUTPUT
============================
C:\Java\jdk1.5.0\bin>javac MyConstructor.java
C:\Java\jdk1.5.0\bin>java MyConstructor
6
Software Lab-IX(Java)
C:\Java\jdk1.5.0\bin>
//WAP to define a function & call it into the another function of same
class //instead of calling through object in another class.
class Hello
{
int m;
int n;
void getdata(int x,int y)
{
m=x;
n=y;
}
int sum()
{
return (m+n);
}
void display()
{
System.out.println("m="+m+" n="+n+" Sum="+sum());
}
}
class Nested
{
public static void main(String a[])
{
Hello H1=new Hello();
H1.getdata(25,35);
H1.display();
Hello H2=new Hello();
H2.getdata(50,150);
H2.display();
}
}
OUTPUT
============================
C:\Java\jdk1.5.0\bin>javac Nested.java
C:\Java\jdk1.5.0\bin>java Nested
7
Software Lab-IX(Java)
C:\Java\jdk1.5.0\bin>
//WAP to calculate no. of objects created of a class
class MyClass
{
int m;
int n;
static int c;
MyClass()
{
c++;
m=0;
n=0;
}
MyClass(int x,int y)
{
c++;
m=x;
n=y;
}
void sum()
{
System.out.println("\nm="+m+"\tn="+n+"\tSum="+(m+n));
}
static void count()
{
System.out.println("\nNumber of objects Created of class 'MyClass' are : "+c);
}
}
class Count
{
public static void main(String a[])
{
MyClass C1=new MyClass();
MyClass C2=new MyClass(10,20);
MyClass C3=new MyClass(40,50);
C1.sum();
C2.sum();
C3.sum();
MyClass.count();
}
}
8
Software Lab-IX(Java)
OUTPUT
============================
C:\Java\jdk1.5.0\bin>javac Count.java
C:\Java\jdk1.5.0\bin>java Count
C:\Java\jdk1.5.0\bin>
9
Software Lab-IX(Java)
import java.io.*;
class Room
{
int length;
int breadth;
Room()
{
length=breadth=0;
}
Room(int x,int y)
{
length=x;
breadth=y;
}
Room(int z)
{
length=breadth=z;
}
void area()
{
System.out.println("Area of the Room is : "+(length*breadth));
}
void area(int l)
{
Length=breadth=l;
System.out.println("Area of the Room is : "+(length*breadth));
}
}
class RoomArea
{
public static void main(String a[])
{
Room R1=new Room();
R1.area();
Room R2=new Room(10);
R2.area(10);
Room R3=new Room(20,30);
R3.area();
10
Software Lab-IX(Java)
}
}
OUTPUT
============================
C:\Java\jdk1.5.0\bin>javac RoomArea.java
C:\Java\jdk1.5.0\bin>java RoomArea
C:\Java\jdk1.5.0\bin>
11
Software Lab-IX(Java)
class Rectangle
{
int length;
int breadth;
Rectangle()
{
length=breadth=0;
}
Rectangle(int x,int y)
{
length=x;
breadth=y;
}
Rectangle(int z)
{
length=breadth=z;
}
int area()
{
return (length*breadth);
}
}
12
Software Lab-IX(Java)
}
}
class SingleInherit
{
public static void main(String a[])
{
Rectangle R1=new Rectangle(10);
System.out.println("\nArea of 1st Rectangle is :"+R1.area());
Rectangle R2=new Rectangle(20,30);
System.out.println("\nArea of 2nd Rectangle is :"+R2.area());
Cube C1=new Cube();
System.out.println("\nVolume of 1st Cube is :"+C1.volume());
Cube C2=new Cube(20,30,40);
System.out.println("\nVolume of 2nd Cube is :"+C2.volume());
}
}
OUTPUT
============================
C:\Java\jdk1.5.0\bin>javac SingleInherit.java
C:\Java\jdk1.5.0\bin>java SingleInherit
13
Software Lab-IX(Java)
C:\Java\jdk1.5.0\bin>
class Marks
{
int sub1;
int sub2;
Marks()
{
sub1=0;
sub2=0;
}
Marks(int x,int y)
{
sub1=x;
sub2=y;
}
int sum()
{
return (sub1+sub2);
}
}
class Sports extends Marks
{
int rank;
Sports()
{
super(0,0);
rank=0;
}
Sports(int x,int y,int z)
{
super(x,y);
rank=z;
}
int total()
{
return (sum()+rank);
}
14
Software Lab-IX(Java)
OUTPUT
============================
C:\Java\jdk1.5.0\bin>javac Multilevel.java
15
Software Lab-IX(Java)
C:\Java\jdk1.5.0\bin>java Multilevel
C:\Java\jdk1.5.0\bin>
//WAP to override the methods of a super class in its subclass.
class Marks
{
int sub1;
int sub2;
Marks()
{
sub1=0;
sub2=0;
}
Marks(int x,int y)
{
sub1=x;
sub2=y;
}
int total()
{
return (sub1+sub2);
}
}
class Sports extends Marks
{
int rank;
Sports()
{
super(0,0);
rank=0;
}
Sports(int x,int y,int z)
{
super(x,y);
rank=z;
}
int total()
{
return (sub1+sub2+rank);
}
}
16
Software Lab-IX(Java)
OUTPUT
============================
C:\Java\jdk1.5.0\bin>javac Overriding.java
C:\Java\jdk1.5.0\bin>java Overriding
17
Software Lab-IX(Java)
C:\Java\jdk1.5.0\bin>
//WAP to define a class with final method & variables to show how to use
//them.
class Circle
{
final float PI=3.142f;
float r,area;
Circle()
{
r=0;
}
Circle(float r)
{
this.r=r;
}
final void area()
{
area=PI*r*r;
System.out.println("Area of the circle having radius "+r+" is : " +area);
}
}
18
Software Lab-IX(Java)
class DemoFinal
{
public static void main(String a[])
{
Circle C=new Circle(2.5f);
C.area();
Circle C1=new Circle();
C1.area();
Sphere S=new Sphere(2.5f);
S.sarea();
Sphere S1=new Sphere();
S1.sarea();
}
}
OUTPUT
============================
C:\Java\jdk1.5.0\bin>appletviewer Param.html
C:\Java\jdk1.5.0\bin>javac DemoFinal.java
C:\Java\jdk1.5.0\bin>java DemoFinal
19
Software Lab-IX(Java)
C:\Java\jdk1.5.0\bin>
//WAP to define a class with abstract method & class should also be
defined //abstract to show their usage.
OUTPUT
============================
C:\Java\jdk1.5.0\bin>javac DemoAbstract.java
20
Software Lab-IX(Java)
C:\Java\jdk1.5.0\bin>java DemoAbstract
Sum of 10 and 20 is : 30
C:\Java\jdk1.5.0\bin>
//WAP to search an element within an array using Binary Search.
import java.io.*;
class BinarySearch
{
public static void main(String args[])
{
int n,m,i,j;
int a[];
try
{
a=new int[10];
DataInputStream in=new DataInputStream(System.in);
System.out.print("\nEnter Array Limit : ");
System.out.flush();
String s=in.readLine();
n=Integer.parseInt(s);
System.out.print("\nEnter Array Elements : ");
for(i=0;i<n;i++)
{
System.out.flush();
try
{
s=in.readLine();
a[i]=Integer.parseInt(s);
}
catch(IOException e)
{
System.out.println("Exception Ocurred :"+e);
}
}
System.out.print("\nEnter the element to be searched : ");
System.out.flush();
s=in.readLine();
m=Integer.parseInt(s);
int start=0;
int end=n;
int mid=(start+end)/2;
21
Software Lab-IX(Java)
while(start<=end)
{
if(a[mid]==m)
{
System.out.println("Element found at location : "+mid);
System.exit(0);
}
else if(a[mid]>m)
end=mid-1;
else
start=mid+1;
mid=(start+end)/2;
}
System.out.println("Element not found");
}
catch(IOException e)
{
System.out.println("Exception Ocurred :"+e);
}
}
}
22
Software Lab-IX(Java)
OUTPUT
============================
C:\Java\jdk1.5.0\bin>javac BinarySearch.java
C:\Java\jdk1.5.0\bin>java BinarySearch
C:\Java\jdk1.5.0\bin>java BinarySearch
23
Software Lab-IX(Java)
C:\Java\jdk1.5.0\bin>
import java.io.*;
class StringSort
{
public static void main(String args[])
{
int n=0,m,i,j;
String temp="hello";
String name[]={"shiv"};
try
{
name=new String[10];
DataInputStream in=new DataInputStream(System.in);
System.out.print("\nEnter total No. of names : ");
System.out.flush();
String s=in.readLine();
n=Integer.parseInt(s);
System.out.print("\nEnter Individual Names : ");
for(i=0;i<n;i++)
{
System.out.flush();
try
{
name[i]=in.readLine();
}
catch(IOException e)
{
System.out.println("Exception Ocurred :"+e);
}
}
}
catch(IOException e)
{
System.out.println("Exception Ocurred :"+e);
}
System.out.print("\nNames in Sorting order are : ");
for(i=0;i<n;i++)
24
Software Lab-IX(Java)
{
for(j=0;j<n-1-i;j++)
{
if(name[j].compareToIgnoreCase(name[j+1])>0)
{
temp=name[j];
name[j]=name[j+1];
name[j+1]=temp;
}
}
}
for(i=0;i<n;i++)
System.out.println(name[i]);
}
}
OUTPUT
============================
C:\Java\jdk1.5.0\bin>javac StringSort.java
C:\Java\jdk1.5.0\bin>java StringSort
C:\Java\jdk1.5.0\bin>
25
Software Lab-IX(Java)
import java.io.*;
class Mulmatrix
{
public static void main(String args[]) throws IOException
{
int a[][]=new int[5][5];
int b[][]=new int[5][5];
int c[][]=new int[5][5];
int r1,c1,r2,c2;
System.out.print("Entere no. of Rows of the 1st matrix : ");
InputStreamReader rr1=new InputStreamReader(System.in);
BufferedReader abc1=new BufferedReader(rr1);
String row1=abc1.readLine();
r1=Integer.parseInt(row1);
System.out.print("Entere no. of columns of the 1st matrix : ");
InputStreamReader cc1=new InputStreamReader(System.in);
BufferedReader abc2=new BufferedReader(cc1);
String col1=abc1.readLine();
c1=Integer.parseInt(col1);
System.out.print("Entere no. of Rows of the 2nd matrix : ");
InputStreamReader rr2=new InputStreamReader(System.in);
BufferedReader abc3=new BufferedReader(rr2);
String row2=abc3.readLine();
r2=Integer.parseInt(row2);
System.out.print("Entere no. of columns of the 2nd matrix : ");
InputStreamReader cc2=new InputStreamReader(System.in);
BufferedReader abc4=new BufferedReader(cc2);
String col2=abc4.readLine();
c2=Integer.parseInt(col2);
if(c1==r2)
{
System.out.print("Enter the elements of 1st matrix : ");
for(int i=0;i<r1;i++)
{
for(int j=0;j<c1;j++)
{
26
Software Lab-IX(Java)
27
Software Lab-IX(Java)
OUTPUT
============================
C:\Java\jdk1.5.0\bin>javac Mulmatrix.java
C:\Java\jdk1.5.0\bin>java Mulmatrix
Entere no. of Rows of the 1st matrix : 2
Entere no. of columns of the 1st matrix : 3
Entere no. of Rows of the 2nd matrix : 3
Entere no. of columns of the 2nd matrix : 2
Enter the elements of 1st matrix : 1
2
3
4
5
6
Enter the elements of 2nd matrix : 2
4
6
1
2
3
Multiplication of the entered matrices is :
20 15
50 39
C:\Java\jdk1.5.0\bin>
28
Software Lab-IX(Java)
//WAP to use properties of one class by another class but the first class
//should be declared as an Interface.
interface Hello1
{
void display1();
void display2();
}
interface Hello2 extends Hello1
{
void display3();
}
class Hai
{
public void display1()
{
System.out.println("This is display1 function");
}
public void display2()
{
System.out.println("This is display2 function");
}
public void display3()
{
System.out.println("This is display3 function");
}
}
class Exinterface
{
public static void main(String args[])
{
Hai H=new Hai();
H.display1();
H.display2();
H.display3();
}
}
29
Software Lab-IX(Java)
OUTPUT
============================
C:\Java\jdk1.5.0\bin>javac Exinterface.java
C:\Java\jdk1.5.0\bin>java Exinterface
C:\Java\jdk1.5.0\bin>
30
Software Lab-IX(Java)
class MyThread1
{
public static void main(String a[])
{
Thread t=Thread.currentThread();
System.out.println("Thread name is : "+t);
t.setName("Shiv");
System.out.println("Thread name is : "+t);
31
Software Lab-IX(Java)
OUTPUT
============================
C:\Java\jdk1.5.0\bin>javac MyThread1.java
C:\Java\jdk1.5.0\bin>java MyThread1
32
Software Lab-IX(Java)
9
10
Main Thread Exiting
C:\Java\jdk1.5.0\bin>
33
Software Lab-IX(Java)
try
{
for(int i=10;i<15;i++)
{
System.out.println(i);
t.sleep(1000);
}
}
catch(InterruptedException e)
{
System.out.println("Main Thread interrupted");
}
System.out.println("Main Thread Exiting");
}
}
OUTPUT
============================
C:\Java\jdk1.5.0\bin>javac MyThread2.java
C:\Java\jdk1.5.0\bin>java MyThread2
34
Software Lab-IX(Java)
9
10
Main Thread Exiting
C:\Java\jdk1.5.0\bin>
//WAP to declare multiple Threads & use thread class methods yield(),
//start(),sleep() etc. in declared thread.
35
Software Lab-IX(Java)
class MyThread3
{
public static void main(String a[])
{
Thread t=Thread.currentThread();
System.out.println("Thread name is : "+t);
t.setName("Shiv");
System.out.println("Thread name is : "+t);
System.out.println("Thread name is : "+t.getName());
new NewThread();
new NewThread1();
try
{
for(int i=10;i<15;i++)
{
System.out.println(i);
t.sleep(1000);
}
}
catch(InterruptedException e)
{
System.out.println("Main Thread interrupted");
}
System.out.println("Main Thread Exiting");
}
36
Software Lab-IX(Java)
OUTPUT
============================
C:\Java\jdk1.5.0\bin>javac MyThread3.java
C:\Java\jdk1.5.0\bin>java MyThread3
37
Software Lab-IX(Java)
9
29
10
30
Main Thread Exiting
class ThreadPriority
{
public static void main(String a[])
{
38
Software Lab-IX(Java)
Thread t=Thread.currentThread();
System.out.println("Thread name is : "+t);
t.setName("Shiv");
t.setPriority(t.MAX_PRIORITY);
System.out.println("Main name is : "+t.getName());
System.out.println("Main Thread Priority is : "+t.getPriority());
NewThread hi=new NewThread(Thread.NORM_PRIORITY+2);
NewThread low=new NewThread(Thread.NORM_PRIORITY-2);
try
{
for(int i=10;i<15;i++)
{
System.out.println(i);
t.sleep(1000);
}
}
catch(InterruptedException e)
{
System.out.println("Main Thread interrupted");
}
System.out.println("Main Thread Exiting having priority:"+t.getPriority());
}
}
OUTPUT
============================
C:\Java\jdk1.5.0\bin>javac ThreadPriority.java
C:\Java\jdk1.5.0\bin>java ThreadPriority
39
Software Lab-IX(Java)
C:\Java\jdk1.5.0\bin>
40
Software Lab-IX(Java)
import java.io.DataInputStream;
import java.lang.System;
class Star
{
public static void main(String args[])
{
DataInputStream in=new DataInputStream(System.in);
int n=0;
try
{
System.out.print("Enter the value of n=");
System.out.flush();
String s=in.readLine();
n=Integer.parseInt(s);
}
catch(Exception e)
{
System.out.println("I/O Error");
System.exit(1);
}
for(int i=1;i<=+n;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print(j);
System.out.print(" ");
}
System.out.println();
}
}
}
OUTPUT
============================
41
Software Lab-IX(Java)
C:\Java\jdk1.5.0\bin>javac Star.java
C:\Java\jdk1.5.0\bin>java Star
Enter the value of n=5
1
12
123
1234
12345
//WAP to catch runtime exceptions thrown during the execution of pre-
declared class & methods.
class MyException
{
public static void main(String arg[])
{
int a=10,c=10;
try
{
c=a/(a-c);
}
catch(ArithmeticException e)
{
System.out.println("Exception caught : "+e);
}
}
}
OUTPUT
============================
C:\Java\jdk1.5.0\bin>javac MyException.java
42
Software Lab-IX(Java)
C:\Java\jdk1.5.0\bin>java MyException
C:\Java\jdk1.5.0\bin>
class MyException1
{
public static void main(String args[])
{
int a=0,b[]={2,3},c;
int l=args.length;
try
{
c=a/l;
c=a/b[3];
}
catch(ArithmeticException e)
{
System.out.println("Division by zero");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Array out of bound");
}
catch(ArrayStoreException e)
{
System.out.println("wrong data type");
}
finally
{
System.out.println("Hello");
}
}
}
OUTPUT
============================
43
Software Lab-IX(Java)
C:\Java\jdk1.5.0\bin>javac MyException1.java
C:\Java\jdk1.5.0\bin>java MyException1
Division by zero
Hello
C:\Java\jdk1.5.0\bin>java MyException1 6 8
Array out of bound
Hello
C:\Java\jdk1.5.0\bin>
//WAP to throw an user-defined Exception
import java.lang.Exception;
class OwnException extends Exception
{
OwnException(String message)
{
super(message);
}
}
class MyOwnException
{
public static void main(String arg[])
{
int x=5,y=1000;
try
{
float z=(float) x/(float)y;
if(z<0.01)
{
throw new OwnException("Number is too small");
}
}
catch(OwnException e)
{
System.out.println("Caught "+e);
}
}
}
44
Software Lab-IX(Java)
OUTPUT
============================
C:\Java\jdk1.5.0\bin>javac MyOwnException.java
C:\Java\jdk1.5.0\bin>java MyOwnException
C:\Java\jdk1.5.0\bin>
import java.awt.*;
import java.applet.*;
/*
<applet code=MyApplet width=200 height=200>
<param name=message value="Put your hands up up......">
</applet>
*/
45
Software Lab-IX(Java)
showStatus("Applet Stopped");
}
}
OUTPUT
============================
C:\Java\jdk1.5.0\bin>javac MyApplet.java
C:\Java\jdk1.5.0\bin>appletviewer MyApplet.html
46
Software Lab-IX(Java)
import java.awt.*;
import java.applet.*;
/*
<applet code=Param.class width=200 height=200>
<param name=MCA value=60>
</applet>
*/
47
Software Lab-IX(Java)
OUTPUT
============================
C:\Java\jdk1.5.0\bin>javac Param.java
C:\Java\jdk1.5.0\bin>appletviewer Param.html
48
Software Lab-IX(Java)
//WAP to take 2 no’s. in text boxes & calculate their sum in third text box by
//clicking on a button
mport java.awt.*;
import java.awt.event.*;
import java.applet.*;
49
Software Lab-IX(Java)
{
tf1.setText("0");
tf2.setText("0");
tf3.setText("0");
}
public void actionPerformed(ActionEvent ae)
{
s=(Integer.parseInt(tf1.getText()))+(Integer.parseInt(tf2.getText()));
str=Integer.toString(s);
repaint();
}
OUTPUT
============================
C:\Java\jdk1.5.0\bin>javac SumText.java
C:\Java\jdk1.5.0\bin>appletviewer SumText.html
50
Software Lab-IX(Java)
import java.awt.*;
import java.applet.*;
51
Software Lab-IX(Java)
OUTPUT
============================
C:\Java\jdk1.5.0\bin>javac Face.java
C:\Java\jdk1.5.0\bin>appletviewer Face.html
52
Software Lab-IX(Java)
import java.awt.*;
import java.applet.*;
53
Software Lab-IX(Java)
{
public void paint(Graphics g)
{
showStatus("Show me the Boat");
g.drawLine(100,180,400,180);
g.drawLine(150,280,350,280);
g.drawLine(100,180,150,280);
g.drawLine(350,280,400,180);
g.drawLine(220,180,220,20);
g.fillOval(215,15,10,10);
g.drawLine(220,50,170,150);
g.drawLine(170,150,220,130);
g.drawArc(200,40,40,120,0,90);
g.drawArc(200,40,40,120,0,-90);
g.drawArc(100,40,240,120,0,90);
g.drawArc(100,40,240,120,0,-90);
}
}
OUTPUT
============================
C:\Java\jdk1.5.0\bin>javac Boat.java
C:\Java\jdk1.5.0\bin>appletviewer Boat.html
54
Software Lab-IX(Java)
//WAP to handle keyboard events & display the characters being typed
in //Applet.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
55
Software Lab-IX(Java)
OUTPUT
============================
C:\Java\jdk1.5.0\bin>javac SimpleKey.java
C:\Java\jdk1.5.0\bin>appletviewer SimpleKey.html
56
Software Lab-IX(Java)
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
57
Software Lab-IX(Java)
58
Software Lab-IX(Java)
repaint();
}
public void mouseDragged(MouseEvent me)
{
mouseX=me.getX();
mouseY=me.getY();
msg="*";
showStatus("Dragging mouse at " +mouseX+","+mouseY);
repaint();
}
public void mouseMoved(MouseEvent me)
{
showStatus("Moving Mouse at "+me.getX()+","+me.getY());
}
public void paint(Graphics g)
{
g.drawString(msg,mouseX,mouseY);
}
}
OUTPUT
============================
C:\Java\jdk1.5.0\bin>javac MouseEvents.java
C:\Java\jdk1.5.0\bin>appletviewer MouseEvents.html
59
Software Lab-IX(Java)
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
60
Software Lab-IX(Java)
</applet>*/
61
Software Lab-IX(Java)
b12.setBounds(60,170,50,25);
b13.setBounds(120,170,50,25);
b14.setBounds(180,170,50,25);
b15.setBounds(60,200,50,25);
b16.setBounds(120,200,50,25);
b17.setBounds(180,200,50,25);
b18.setBounds(120,230,50,25);
add(t1);
add(b);
add(b1);
add(b2);
add(b3);
add(b4);
add(b5);
add(b6);
add(b7);
add(b8);
add(b9);
add(b10);
add(b11);
add(b12);
add(b13);
add(b14);
add(b15);
add(b16);
add(b17);
add(b18);
b.addActionListener(this);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b10.addActionListener(this);
b11.addActionListener(this);
b12.addActionListener(this);
b13.addActionListener(this);
b14.addActionListener(this);
b15.addActionListener(this);
b16.addActionListener(this);
62
Software Lab-IX(Java)
b17.addActionListener(this);
b18.addActionListener(this);
setBackground(Color.gray);
b11.setForeground(Color.red);
b12.setForeground(Color.red);
b13.setForeground(Color.red);
b14.setForeground(Color.red);
b15.setForeground(Color.red);
b16.setForeground(Color.red);
b17.setForeground(Color.red);
b18.setForeground(Color.red);
}
public void actionPerformed(ActionEvent e)
{
str=(String)e.getActionCommand();
if(str.equals("0")||str.equals("1")||str.equals("2")||str.equals("3")||str.equals("4")||
str.equals("5")||str.equals("6")||str.equals("7")||str.equals("8")||str.equals("9")||
str.equals("."))
{
t1.setText("");
str1=str1+str;
t1.setText(str1);
}
else if(str.equals("+"))
{
strCheck="+";
total=Double.parseDouble(str1);
newTotal=newTotal+total;
str2=(String)Double.toString(newTotal);
t1.setText(str2);
str1="";
count=count+1;
}
else if(str.equals("-"))
{
strCheck="-";
total=Double.parseDouble(str1);
if(count==1)
{
newTotal=total-newTotal;
}
else
{
63
Software Lab-IX(Java)
newTotal=newTotal-total;
}
str2=(String)Double.toString(newTotal);
t1.setText(str2);
str1="";
count=count+1;
}
else if(str.equals("x"))
{
strCheck="x";
total=Double.parseDouble(str1);
if(newTotal==0)
{
newTotal=1;
}
newTotal=newTotal*total;
str2=(String)Double.toString(newTotal);
t1.setText(str2);
str1="";
count=count+1;
}
else if(str.equals("/"))
{
strCheck="/";
total=Double.parseDouble(str1);
if(newTotal==0)
{
newTotal=1;
}
if(count==1)
{
newTotal=total/newTotal;
}
else
{
newTotal=newTotal/total;
}
str2=(String)Double.toString(newTotal);
t1.setText(str2);
str1="";
count=count+1;
}
else if(str.equals("sqrt"))
{
strCheck="sqrt";
total=Double.parseDouble(str1);
64
Software Lab-IX(Java)
newTotal=Math.sqrt(total);
str2=(String)Double.toString(newTotal);
t1.setText(str2);
str1=str2;
}
else if(str.equals("="))
{
if(strCheck.equals("+"))
{
total=Double.parseDouble(str1);
newTotal=newTotal+total;
str2=(String)Double.toString(newTotal);
t1.setText(str2);
str1="0";
}
else if(strCheck.equals("-"))
{
total=Double.parseDouble(str1);
newTotal=newTotal-total;
str2=(String)Double.toString(newTotal);
t1.setText(str2);
str1="0";
}
else if(strCheck.equals("x"))
{
total=Double.parseDouble(str1);
newTotal=newTotal*total;
str2=(String)Double.toString(newTotal);
t1.setText(str2);
str1="0";
}
else if(strCheck.equals("/"))
{
total=Double.parseDouble(str1);
newTotal=newTotal/total;
str2=(String)Double.toString(newTotal);
t1.setText(str2);
str1="0";
}
else if(strCheck.equals("sqrt"))
{
total=Double.parseDouble(str1);
newTotal=Math.sqrt(total);
str2=(String)Double.toString(newTotal);
t1.setText(str2);
65
Software Lab-IX(Java)
str1="0";
}
else
{
t1.setText("0");
}
}//end of if =
else if(str.equals("CE"))
{
t1.setText("0");
str="";
str1="";
str2="";
}
else if(str.equals("C"))
{
t1.setText("0");
str="";
str1="";
str2="";
total=0;
newTotal=0;
count=1;
}
else
{
t1.setText("0");
}
}
}
OUTPUT
============================
C:\Java\jdk1.5.0\bin>javac Calculator.java
66
Software Lab-IX(Java)
C:\Java\jdk1.5.0\bin>appletviewer Calculator.html
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
67
Software Lab-IX(Java)
68
Software Lab-IX(Java)
t1.sleep(5);
}
catch(InterruptedException e)
{
System.out.println(“Exception Ocurred: “+e);
}
y=y-1;
x=x+1;
repaint();
}
69
Software Lab-IX(Java)
catch(InterruptedException e)
{
System.out.println(“Exception Ocurred: “+e);
}
x=x+1;
y=y+1;
repaint();
}
while(x!=(xmax-r1) && y!=ymin)
{
try
{
t1.sleep(5);
}
catch(InterruptedException e)
{
System.out.println(“Exception Ocurred: “+e);
}
y=y-1;
x=x+1;
repaint();
}
} //end of if
else
{
while(x!=(xmax-r1) && y!=(ymax-r1))
{
try
{
t1.sleep(5);
}
catch(InterruptedException e)
{
System.out.println(“Exception Ocurred: “+e);
}
x=x+1;
y=y+1;
repaint();
}
70
Software Lab-IX(Java)
catch(InterruptedException e)
{
System.out.println(“Exception Ocurred: “+e);
}
y=y+1;
x=x-1;
repaint();
}
while(y!=ymin && x!=xmin)
{
try
{
t1.sleep(5);
}
catch(InterruptedException e)
{
System.out.println(“Exception Ocurred: “+e);
}
y=y-1;
x=x-1;
repaint();
}
while(x!=xmax && y!=ymin)
{
try
{
t1.sleep(5);
}
catch(InterruptedException e)
{
System.out.println(“Exception Ocurred: “+e);
}
x=x+1;
y=y-1;
repaint();
}
71
Software Lab-IX(Java)
catch(InterruptedException e)
{
System.out.println(“Exception Ocurred: “+e);
}
x=x+1;
y=y+1;
repaint();
}
} //end of else
} //end of while(true)
}
public void paint(Graphics g)
{
g.setColor(Color.red);
g.fillOval(x,y,r1,r1);
}
}
OUTPUT
============================
C:\Java\jdk1.5.0\bin>javac Bounce.java
C:\Java\jdk1.5.0\bin>appletviewer Bounce.html
72
Software Lab-IX(Java)
import java.awt.*;
import java.applet.*;
73
Software Lab-IX(Java)
catch(InterruptedException e)
{
System.out.println("Interrupted Exception occurred");
}
}
74
Software Lab-IX(Java)
}
public void paint(Graphics g)
{
g.drawString(msg,10,100);
}
public void stop()
{
key=false;
t=null;
}
}
OUTPUT
============================
C:\Java\jdk1.5.0\bin>javac Marquee.java
C:\Java\jdk1.5.0\bin>appletviewer Marquee.html
75