0% found this document useful (0 votes)
10 views

Java File

The document contains examples of Java programs that demonstrate various programming concepts: 1) The first program calculates the quotient and remainder of two numbers entered by the user. 2) The second program calculates the average of 5 numbers passed as command line arguments. 3) The third program reads 3 numbers from the user and finds the greatest among them.

Uploaded by

Manjinder Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Java File

The document contains examples of Java programs that demonstrate various programming concepts: 1) The first program calculates the quotient and remainder of two numbers entered by the user. 2) The second program calculates the average of 5 numbers passed as command line arguments. 3) The third program reads 3 numbers from the user and finds the greatest among them.

Uploaded by

Manjinder Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 75

Software Lab-IX(Java)

//WAP to find the Quotient & Remainder of the entered numbers.

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

Enter the dividend : 25

Enter the divisor : 6

Quotient = 4

Remainder = 1
C:\Java\jdk1.5.0\bin>

1
Software Lab-IX(Java)

//WAP to find out the average of 5 integers entered through command


line //arguments

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

Avg of 5 numbers is : 44.6

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

Enter 1st number : 25

Enter 2nd number : 86

Enter 3rd number : 95

3rd number is 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

area from ar1==>600

5
Software Lab-IX(Java)

area from ar2==>1200


C:\Java\jdk1.5.0\bin>
//WAP to declare a class & initialize the values of variables declared in class
//using Constructors.

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

m=0 n=0 Sum : 0

6
Software Lab-IX(Java)

m=20 n=30 Sum : 50

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

m=25 n=35 Sum=60

7
Software Lab-IX(Java)

m=50 n=150 Sum=200

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

m=0 n=0 Sum=0

m=10 n=20 Sum=30

m=40 n=50 Sum=90

Number of objects Created of class 'MyClass' are : 3

C:\Java\jdk1.5.0\bin>

9
Software Lab-IX(Java)

//WAP to Overload Area function using different arguments in a single class

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

Area of the Room is : 0

Area of the Room is : 100

Area of the Room is : 600

C:\Java\jdk1.5.0\bin>

11
Software Lab-IX(Java)

//WAP to inherit a Single class properties to another class using inheritance.

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);
}
}

class Cube extends Rectangle


{
int height;
Cube()
{
super(0,0);
height=0;
}
Cube(int x,int y,int z)
{
super(x,y);
height=z;
}
int volume()
{
return (area()*height);

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

Area of 1st Rectangle is :100

Area of 2nd Rectangle is :600

Volume of 1st Cube is :0

Volume of 2nd Cube is :24000

13
Software Lab-IX(Java)

C:\Java\jdk1.5.0\bin>

//WAP to declare 3 classes where second class inherits the properties of


first //class & third class inherits the properties of second class by
implementing //Multilevel inheritance.

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)

class Student extends Sports


{
int grade;
Student()
{
super(0,0,0);
grade=0;
}
Student(int x,int y,int z,int g)
{
super(x,y,z);
grade=g;
}
int grandtotal()
{
return (total()+grade);
}
}
class Multilevel
{
public static void main(String a[])
{
Student St1=new Student();
System.out.println("Total Marks of 1st Student : "+St1.grandtotal());
Student St2=new Student(10,20,3,25);
System.out.println("Total Marks of 2nd Student: "+St2.grandtotal());
}
}

OUTPUT
============================

C:\Java\jdk1.5.0\bin>javac Multilevel.java

15
Software Lab-IX(Java)

C:\Java\jdk1.5.0\bin>java Multilevel

Total Marks of 1st Student : 0

Total Marks of 2nd Student: 58

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)

class Student extends Sports


{
int grade;
Student()
{
super(0,0,0);
grade=0;
}
Student(int x,int y,int z,int g)
{
super(x,y,z);
grade=g;
}
int total()
{
return (sub1+sub2+rank+grade);
}
}
class Overriding
{
public static void main(String a[])
{
Marks M=new Marks(10,20);
System.out.println("Total Marks of subjects : "+M.total());
Sports S=new Sports(10,20,5);
System.out.println("Total Marks of Subjects & Sports : "+S.total());
Student St1=new Student(10,20,5,30);
System.out.println("Total Marks Obtained by Student : "+St1.total());
}
}

OUTPUT
============================

C:\Java\jdk1.5.0\bin>javac Overriding.java

C:\Java\jdk1.5.0\bin>java Overriding

Total Marks of subjects : 30

17
Software Lab-IX(Java)

Total Marks of Subjects & Sports : 35

Total Marks Obtained by Student : 65

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);
}
}

class Sphere extends Circle


{
Sphere()
{
super();
}
Sphere(float r)
{
super(r);
}
//void area(){} //Error
void sarea()
{
area=4*PI*r*r;
System.out.println("Area of the Sphere 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

Area of the circle having radius 2.5 is : 19.6375

Area of the circle having radius 0.0 is : 0.0

Area of the Sphere having radius 2.5 is : 78.55

19
Software Lab-IX(Java)

Area of the Sphere having radius 0.0 is : 0.0

C:\Java\jdk1.5.0\bin>

//WAP to define a class with abstract method & class should also be
defined //abstract to show their usage.

abstract class Operation


{
int m,n;
abstract void sum();
void multiply()
{
System.out.println("Multiplication of "+m+" and "+n+" is : "+(m*n));
}
}

class Arithmetic extends Operation


{
void sum()
{
System.out.println("Sum of "+m+" and "+n+" is : "+(m+n));
}
}
class DemoAbstract
{
public static void main(String a[])
{
Arithmetic A=new Arithmetic();
A.m=10;
A.n=20;
A.sum();
A.multiply();
}
}

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

Multiplication of 10 and 20 is : 200

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

Enter Array Limit : 8

Enter Array Elements : 25


35
45
56
67
78
89
91

Enter the element to be searched : 78

Element found at location : 5

C:\Java\jdk1.5.0\bin>java BinarySearch

Enter Array Limit : 8

Enter Array Elements : 25


35
45
56
67
78
89
91

Enter the element to be searched : 50

Element not found

23
Software Lab-IX(Java)

C:\Java\jdk1.5.0\bin>

//WAP to sort an array of Strings

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

Enter total No. of names : 5

Enter Individual Names : Shiv


Arun
gurpash
Modi
Vinod

Names in Sorting order are : Arun


gurpash
Modi
Shiv
Vinod

C:\Java\jdk1.5.0\bin>

25
Software Lab-IX(Java)

//WAP to perform multiplication of two matrices.

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)

InputStreamReader r=new InputStreamReader(System.in);


BufferedReader abc=new BufferedReader(r);
String s[][]=new String[r1][c1];
s[i][j]=abc.readLine();
a[i][j]=Integer.parseInt(s[i][j]);
}
}
System.out.print("Enter the elements of 2nd matrix : ");
for(int i=0;i<r2;i++)
{
for(int j=0;j<c2;j++)
{
InputStreamReader r=new InputStreamReader(System.in);
BufferedReader abc=new BufferedReader(r);
String s[][]=new String[r2][c2];
s[i][j]=abc.readLine();
b[i][j]=Integer.parseInt(s[i][j]);
}
}
System.out.println("Multiplication of the entered matrices is : ");
for(int i=0;i<r1;i++)
{
for(int j=0;j<c2;j++)
{
c[i][j]=0;
for(int k=0;k<r2;k++)
{
c[i][j]+=a[i][k]*b[k][j];
}
System.out.print(c[i][j]+" ");
}
System.out.println();
}
}
else
System.out.println("Multiplication is not possible");
}
}

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

This is display1 function

This is display2 function

This is display3 function

C:\Java\jdk1.5.0\bin>

30
Software Lab-IX(Java)

//WAP to declare multiple Threads by extending Thread class.

class NewThread extends Thread


{
Thread t;
NewThread()
{
t=new Thread(this,"Hello Thead");
System.out.println("Child Thread: "+t);
t.start();
}
public void run()
{
try
{
for(int i=1;i<=10;i++)
{
System.out.println(i);
t.sleep(500); // sleep() method might throw an
IntteruptException that's why we need try\catch block
}
}
catch(InterruptedException e)
{
System.out.println("Child Thread interrupted");
}
System.out.println("Child Thread Exiting");
}
}

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)

System.out.println("Thread name is : "+t.getName());


new NewThread();
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 MyThread1.java

C:\Java\jdk1.5.0\bin>java MyThread1

Thread name is : Thread[main,5,main]

Thread name is : Thread[Shiv,5,main]

Thread name is : Shiv

Child Thread: Thread[Hello Thead,5,main]


10
1
2
11
3
4
5
12
6
13
7
8
14

32
Software Lab-IX(Java)

9
10
Main Thread Exiting

Child Thread Exiting

C:\Java\jdk1.5.0\bin>

//WAP to create multiple Threads by implementing Runnable Interface.

class NewThread implements Runnable


{
Thread t;
NewThread()
{
t=new Thread(this,"Hello Thead");
System.out.println("Child Thread: "+t);
t.start();
}
public void run()
{
try
{
for(int i=1;i<=10;i++)
{
System.out.println(i);
t.sleep(500);
}
}
catch(InterruptedException e)
{
System.out.println("Child Thread interrupted");
}
System.out.println("Child Thread Exiting");
}
}
class MyThread2
{
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();

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

Thread name is : Thread[main,5,main]

Thread name is : Thread[Shiv,5,main]

Thread name is : Shiv

Child Thread: Thread[Hello Thead,5,main]


10
1
2
11
3
4
12
5
6
13
7
8
14

34
Software Lab-IX(Java)

9
10
Main Thread Exiting

Child Thread Exiting

C:\Java\jdk1.5.0\bin>

//WAP to declare multiple Threads & use thread class methods yield(),
//start(),sleep() etc. in declared thread.

class NewThread extends Thread


{
Thread t;
NewThread()
{
t=new Thread(this,"Hello Thead");
System.out.println("1st Child Thread: "+t);
t.start();
}
public void run()
{
try
{
for(int i=1;i<=10;i++)
{
if(i==5)
yield();
System.out.println(i);
t.sleep(500);
}
}
catch(InterruptedException e)
{
System.out.println("1st Child Thread interrupted");
}
System.out.println("1st Child Thread Exiting");
}
}
class NewThread1 extends Thread
{
Thread t;
NewThread1()
{
t=new Thread(this,"Hello Thead");

35
Software Lab-IX(Java)

System.out.println("2nd Child Thread: "+t);


t.start();
}
public void run()
{
try
{
for(int i=21;i<=30;i++)
{
System.out.println(i);
t.sleep(500);
}
}
catch(InterruptedException e)
{
System.out.println("2nd Child Thread interrupted");
}
System.out.println("2nd Child Thread Exiting");
}
}

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

Thread name is : Thread[main,5,main]

Thread name is : Thread[Shiv,5,main]

Thread name is : Shiv

1st Child Thread: Thread[Hello Thead,5,main]

2nd Child Thread: Thread[Hello Thead,5,main]


10
1
21
2
22
3
23
11
4
24
25
12
5
6
26
13
7
27
8
28
14

37
Software Lab-IX(Java)

9
29
10
30
Main Thread Exiting

2nd Child Thread Exiting

1st Child Thread Exiting


//WAP to define multiple Threads & then define execution sequence
//explicitly by setting the Priorities of these Threads.

class NewThread implements Runnable


{
int n;
Thread t;
NewThread(int n)
{
this.n=n;
t=new Thread(this);
t.setPriority(n);
System.out.println("Child Thread Prioriy: "+t.getPriority());
t.start();
}
public void run()
{
try
{
for(int i=1;i<10;i++)
{
System.out.println(i);
t.sleep(500);
}
}
catch(InterruptedException e)
{
System.out.println("Child Thread interrupted");
}
System.out.println("Child Thread Exiting having priority: "+t.getPriority());
}
}

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

Thread name is : Thread[main,5,main]

Main name is : Shiv

Main Thread Priority is : 10

39
Software Lab-IX(Java)

Child Thread Prioriy: 7

Child Thread Prioriy: 3


10
1
1
2
2
11
3
3
4
4
12
5
5
6
6
13
7
7
8
8
14
9
9
Child Thread Exiting having priority: 7

Child Thread Exiting having priority: 3

Main Thread Exiting having priority:10

C:\Java\jdk1.5.0\bin>

40
Software Lab-IX(Java)

//WAP to import classes from packages.

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

Exception caught : java.lang.ArithmeticException: / by zero

C:\Java\jdk1.5.0\bin>

//WAP to catch multiple runtime exception thrown during the execution


of //program & also show use of finally.

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

Caught OwnException: Number is too small

C:\Java\jdk1.5.0\bin>

//WAP to create a simple Applet

import java.awt.*;
import java.applet.*;

/*
<applet code=MyApplet width=200 height=200>
<param name=message value="Put your hands up up......">
</applet>
*/

public class MyApplet extends Applet


{
String str;
public void init()
{
setBackground(Color.cyan);
setForeground(Color.red);
showStatus("My First Applet");
}
public void start()
{
str=getParameter("message");
if(str==null)
{
str="Not Found";
}
}
public void paint(Graphics g)
{
g.drawString("Students in MCA Class : "+str,10,100);
}
public void stop()
{

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)

//WAP to pass different parameters to an Applet

import java.awt.*;
import java.applet.*;

/*
<applet code=Param.class width=200 height=200>
<param name=MCA value=60>
</applet>
*/

public class Param extends Applet


{
int mca;
String str;
public void start()
{
str=getParameter("MCA");
if(str==null)
{
str="Not Found";
mca=0;
}
else
mca=Integer.parseInt(str);
}
public void paint(Graphics g)
{
g.drawString("Students in MCA Class : "+mca,10,100);
}
}

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.*;

/*<Applet code=SumText width=200 height=200></Applet>*/

public class SumText extends Applet implements ActionListener


{
TextField tf1,tf2,tf3;
Label lb1,lb2,lb3;
Button bt;
String str="0";
int s;
public void init()
{
lb1=new Label("Enter 1st Number : ");
lb2=new Label("Enter 2nd Number : ");
lb3=new Label("Sum is : ");
tf1=new TextField(10);
tf2=new TextField(10);
tf3=new TextField(10);
bt=new Button("Sum");
add(lb1);
add(tf1);
add(lb2);
add(tf2);
add(lb3);
add(tf3);
add(bt);
}
public void start()

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();
}

public void paint(Graphics g)


{
tf3.setText(Integer.toString(s));
showStatus("Calculate Sum of Two Numbers.");
}
}

OUTPUT
============================

C:\Java\jdk1.5.0\bin>javac SumText.java

C:\Java\jdk1.5.0\bin>appletviewer SumText.html

50
Software Lab-IX(Java)

//WAP to draw a Face

import java.awt.*;
import java.applet.*;

/*<applet code=Face width=200 height=200></applet>*/

public class Face extends Applet


{
public void paint(Graphics g)
{
showStatus("Show Face");
g.setColor(Color.green);
g.fillOval(40,40,120,120);
g.setColor(Color.red);
g.fillOval(57,75,20,30);
g.fillOval(123,75,20,30);
g.fillOval(70,110,60,40);
g.setColor(Color.black);
g.fillOval(60,90,15,15);
g.fillOval(126,90,15,15);
g.fillOval(91,115,16,14);
g.drawArc(75,110,50,30,190,160);
g.drawArc(75,111,50,30,190,160);
}
}

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)

//WAP to draw a Boat

import java.awt.*;
import java.applet.*;

/*<applet code=Boat width=200 height=200></applet>*/

public class Boat extends 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.*;

/*<applet code="SimpleKey" width=200 height=200></applet> */

55
Software Lab-IX(Java)

public class SimpleKey extends Applet implements KeyListener


{
String msg="";
int x=10,y=20;
public void init()
{
addKeyListener(this);
requestFocus();
}
public void keyPressed(KeyEvent ke)
{
showStatus("key down");
}
public void keyReleased(KeyEvent ke)
{
showStatus("key up");
}
public void keyTyped(KeyEvent ke)
{
msg+=ke.getKeyChar();
repaint();
}
public void paint(Graphics g)
{
g.drawString(msg,x,y);
}
}

OUTPUT
============================

C:\Java\jdk1.5.0\bin>javac SimpleKey.java

C:\Java\jdk1.5.0\bin>appletviewer SimpleKey.html

56
Software Lab-IX(Java)

//WAP to implement Mouse Events & display the cursor position on


//StatusBar & also show a ‘*’ on Applet window on mouse drag.

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

57
Software Lab-IX(Java)

/*<Applet code="MouseEvents" width=200 height=200></Applet>*/

public class MouseEvents extends Applet implements


MouseListener,MouseMotionListener
{
String msg="";
int mouseX=0,mouseY=0;
public void init()
{
addMouseListener(this);
addMouseMotionListener(this);
}
public void mouseClicked(MouseEvent me)
{
mouseX=0;
mouseY=10;
msg="Mouse Clicked.";
repaint();
}
public void mouseEntered(MouseEvent me)
{
mouseX=0;
mouseY=10;
msg="Mouse Entered.";
repaint();
}
public void mouseExited(MouseEvent me)
{
mouseX=0;
mouseY=10;
msg="Mouse Exited.";
repaint();
}
public void mousePressed(MouseEvent me)
{
mouseX=me.getX();
mouseY=me.getY();
msg="Down";
repaint();
}
public void mouseReleased(MouseEvent me)
{
mouseX=me.getX();
mouseY=me.getY();
msg="Up";

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)

// WAP to Generate Calculator

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

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

60
Software Lab-IX(Java)

</applet>*/

public class Calculator extends Applet implements ActionListener


{
TextField t1;
Button b,b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17,b18;
String str="",str1="",str2="",strCheck="";
double total=0,newTotal=0;
int count=1;
public void init()
{
setLayout(null);
t1=new TextField(10);
t1.setText("0");
b=new Button("0");
b1=new Button("1");
b2=new Button("2");
b3=new Button("3");
b4=new Button("4");
b5=new Button("5");
b6=new Button("6");
b7=new Button("7");
b8=new Button("8");
b9=new Button("9");
b10=new Button(".");
b11=new Button("+");
b12=new Button("-");
b13=new Button("x");
b14=new Button("/");
b15=new Button("=");
b16=new Button("C");
b17=new Button("CE");
b18=new Button("sqrt");
t1.setBounds(60,20,170,25);
b1.setBounds(60,50,50,25);
b2.setBounds(120,50,50,25);
b3.setBounds(180,50,50,25);
b4.setBounds(60,80,50,25);
b5.setBounds(120,80,50,25);
b6.setBounds(180,80,50,25);
b7.setBounds(60,110,50,25);
b8.setBounds(120,110,50,25);
b9.setBounds(180,110,50,25);
b10.setBounds(60,140,50,25);
b.setBounds(120,140,50,25);
b11.setBounds(180,140,50,25);

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

// WAP to Generate Bouncing ball

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

67
Software Lab-IX(Java)

/*<applet code="Bounce" width=700 height=500>


</applet>*/

public class Bounce extends Applet implements Runnable


{
int r1=50,x,y,xmin,ymin,xmax,ymax;
Thread t1;
public void init()
{
xmin=getX();
ymin=getY();
xmax=getWidth();
ymax=getHeight();
x=xmin;
y=ymin;
Thread t1=new Thread(this);
t1.start();
setBackground(Color.green);
}
public void run()
{
while(true)
{
if(x==xmin && y==ymin)
{
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();
}
}
if(y==ymax-r1 || x==xmax-r1)
{
while(x!=(xmax-r1) && y!=(ymin-r1))
{
try
{

68
Software Lab-IX(Java)

t1.sleep(5);
}
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!=xmin && y!=ymax-r1)
{
try
{
t1.sleep(5);
}
catch(InterruptedException e)
{
System.out.println(“Exception Ocurred: “+e);
}
y=y+1;
x=x-1;
repaint();
}

while(x!=xmax && y!=(ymax-r1))


{
try
{
t1.sleep(5);
}

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();
}

while(x!=xmin && y!=(ymax-r1))


{
try
{
t1.sleep(5);
}

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();
}

while(x!=(xmax-1) && y!=(ymax-r1))


{
try
{
t1.sleep(5);
}

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)

//WAP to generate Marquee

import java.awt.*;
import java.applet.*;

73
Software Lab-IX(Java)

//<Applet code="Marquee.class" height="200" width="200"></Applet>

public class Marquee extends Applet implements Runnable


{
Thread t;
Font f;
String msg=".........................Hello, Welcome in this world of moving web using
JAVA.........................Hello, Welcome in this world of moving web using JAVA";
boolean key;
public void init()
{
f=new Font("Arial",Font.BOLD,15);
msg=msg+".";
setFont(f);
}
public void start()
{
setForeground(Color.red);
setBackground(Color.cyan);
t=new Thread(this);
t.start();
key=false;
}
public void run()
{
char ch;
showStatus("Welcome in this new world of Applet");
for(;;)
{
try
{
repaint();
Thread.sleep(250);
ch=msg.charAt(0);
msg=msg.substring(1,msg.length());
msg=msg+ch;
if(key)
break;
}

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

You might also like