Java - Practical With Output 1 To 48

Download as pdf or txt
Download as pdf or txt
You are on page 1of 63

HEMCHANDRACHARYA NORTH GUJARAT UNIVERSITY, PATAN

B.C.A. Semester – V
BCA: 505 - JAVA Programming

University Examination Duration: 3 Hours (Per Batch)


(Practical List)

1. Write a Java Program find the area of circle.


2. Write a Java Program that will display factorial of the given number.
3. Write a Java Program that will find the largest no from the given two nos.
4. Write a Java Program that will find the largest no from the given three n os.
5. Write a Java Program that shows the use of switch Statement.
6. Write a Java Program to find the sum of the digits of given number.
7. Write a Java Program that will display the Sum of 1+1/2+1/3…..+1/n.
8. Write a Java Program that check whether the given no is prime or not.
9. Write a Java Program that implements the use of break statement.
10.Write a Java Program that implements the use of continue statement.
11. Write a Java Program that will accept Command -line Arguments and display the same.
12.Write a Java Program to sort the elements of an array in Ascending Order.
13.Write a Java Program to create a Student class and generate result of student (Total, Per, Grade).
14. Write a Java Program to create an Employee class and generate Salary Slip for the employe e.
15.Write a java program which shows the use of Static Members.
16.Write a java program which shows the Nesting of Methods.
17.Write a java program which shows the use of Methods Overloading.
18.Write a java program which implements the Default Constructors.
19.Write a java program which implements the Parameterized Constructors.
20.Write a java program which implements the Overloading of Constructors.
21.Write a java program which explains the concept of Single Inheritance.
22.Write a java program which explains the concept of Multilevel Inheritance.
23.Write a java program which explains the concept of Hierarchical Inheritance.
24.Write a java program which shows the Method Overriding.
25.Write a Java Program to implement final class and final method.
26.Write a Java Program to implement abstract class and abstract method.
27.Write a java program which implements Interface.
28.Write a java program which implements Multiple Interfaces.
29.Write a java program which shows importing of classes from other packages.
30.Write a Java Program to implement the methods of Math Class.
31.Write a Java Program to implement the methods of String Class.
32.Write a Java Program to implement the methods of Vector Class.
33.Write a Java Program to implement the methods of Stack Class.
34. Write a Java Program which will read a text and count all occurrences of a particular word.
35.Write a Java Program which will read a string and rewrite it in the alphabetical Order
eg. The word “STRING” should be written a “GINRST”.
36.Write a java program which creates threads using the Thread Class.
37.Write a java program which shows the use of yield(), stop() and sleep() Methods.
38.Write a java program which shows the Priority in Threads.
39.Write a java program which use of Runnable Interface.
Page 1
40.Write a java program which uses try and catch for Exception Handling.
41.Write a java program which uses Multiple catch Blocks.
42.Write a java program which uses finally Statement.
43.Write a java program which uses Nested try Statements.
44.Write a java program which shows throwing our own Exception.
45.Create an Applet program that print Hello Applet.
46.Create an applet that use init(),start(),stop() and destroy() methods of applet.
47.write an applet program to implement the concept of passing parameter to applet.
48.Write a applet program to implement various methods of Graphics class.

Page 2
JAVA Programming
(Practicals With Output)

 Practical: 1
Write a Java Program find the area of circle.
class P1
{
public static void main (String args[])
{
double PI=3.14;
int R=5;
double area;

area=PI*R*R;
System.out.println ("area of circle is" + area);
}
}
 Output: 1

Page 3
 Practical: 2
Write a Java Program that will display factorial of the given number.
class P2
{
public static void main (String args[])
{
int i=1, sum=1;
for (i=1; i<=5; i++)
{
sum=sum*i;
System.out.println ("factorial number is" + sum);
}
}
}
 Output: 2

Page 4
 Practical: 3
Write a Java Program that will find the largest no from the given two nos.
class P3
{
public static void main (String args[])
{
int a=15, b=10;
if (a>b)
{
System.out.println ("a is max");
}
else
{
System.out.println ("b is max");
}
}
}
 Output: 3

Page 5
 Practical: 4
Write a Java Program that will find the largest no from the given three n os.
class P4
{
public static void main (String args[])
{
int a=10, b=50, c=70;
if (a>b)
{
System.out.println ("a is max");
}
else if (b>c)
{
System.out.println ("b is max");
}
else
{
System.out.println ("c is max");
}
}
}
 Output: 4

Page 6
 Practical: 5
Write a Java Program that shows the use of switch Statement.
class P5
{
public static void main (String args[])
{
int marks=70;
int index=marks/10;

switch (index)
{
case 7:
System.out.println ("Distriction");
break;

case 6:
System.out.println ("First Class");
break;

case 5:
System.out.println ("Second Class");
break;

case 4:
System.out.println ("Third Class");
break;

case 3:
System.out.println ("Pass Class");
break;

default:
System.out.println ("Fail");
}
}
}

Page 7
 Output: 5

 Practical: 6
Write a Java Program to find the sum of the digits of given number.
import java.util.Scanner;
public class P6
{
public static void main (String args[])
{
int m, n, sum=0;
Scanner s=new Scanner (System.in);
System.out.println ("enter the number");
m=s.nextInt();
while (m>0)
{
n=m%10;
sum=sum+n;
m=m/10;
}
System.out.println ("sum of digits" + sum);
}
}

Page 8
 Output: 6

 Practical: 7
Write a Java Program that will display the Sum of 1+1/2+1/3…..+1/n.
class P7
{
public static void main (String args[])
{
double i, j;
double sum=0;
double num=3;
for (i=1; i<=num; i++)
{
j=1/i;
sum=sum+j;
}
System.out.println ("sum of series is" + sum);
}
}

Page 9
 Output: 7

 Practical: 8
Write a Java Program that check whether the given no is prime or not.
import java.util.Scanner;
class P8
{
public static void main (String args[])
{
int num, i, count=0;
Scanner scan=new Scanner(System.in);

System.out.println("enter a number");
num=scan.nextInt();

for (i=2; i<num; i++)


{
if (num%i==0)
{
count++;
break;
}
}
if (count==0)
Page 10
{
System.out.println("this is a prime number");
}
else
{
System.out.println("this is not a prime number");
}
}
}
 Output: 8

 Practical: 9
Write a Java Program that implements the use of break statement.
class P9
{
public static void main (String args[])
{
int i;
for (i=1; i<=10; i++)
{
if (i==5)
break;
System.out.println (" " + i);
}
Page 11
}
}
 Output: 9

 Practical: 10
Write a Java Program that implements the use of continue statement.
class P10
{
public static void main (String args[])
{
int i;
for (i=1; i<=10; i++)
{
if (i==5)
continue;
System.out.println (" " + i);
}
}
}

Page 12
 Output: 10

 Practical: 11
Write a Java Program that will accept Command-line Arguments and display the
same.
class P11
{
public static void main (String args[])
{
int count, i=0;
String string;
count=args.length;

System.out.println ("number of arguments=" + count);


while (i<count)
{
string=args[i];
i=i+1;
System.out.println (i+ " : " + " java is" + string + " ! ");
}
}
}

Page 13
 Output: 11

 Practical: 12
Write a Java Program to sort the elements of an array in Ascending Order.
import java.util.Scanner;
class P12
{
public static void main(String []args)
{
int num, i, j, temp;
Scanner input = new Scanner(System.in);
System.out.println("Enter the number of integers to sort:");
num = input.nextInt();
int array[] = new int[num];
System.out.println("Enter " + num + " integers: ");

for (i = 0; i < num; i++)


array[i] = input.nextInt();

for (i = 0; i < ( num - 1 ); i++)


{
for (j = 0; j < num - i - 1; j++)
{
if (array[j] > array[j+1])
Page 14
{
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}

System.out.println("Sorted list of integers:");

for (i = 0; i < num; i++)


System.out.println(array[i]);
}
}
 Output: 12

Page 15
 Practical: 13
Write a Java Program to create a Student class and generate result of student
(Total, Per, Grade).
import java.util.Scanner;
public class P13
{
public static void main(String args[])
{
int mark[] = new int[5];
int i;
float sum=0, avg;
Scanner scan = new Scanner(System.in);
System.out.print("Enter Marks Obtained in 5 Subjects : ");
for(i=0; i<5; i++)
{
mark[i] = scan.nextInt();
sum = sum + mark[i];
}
avg = sum/5;
System.out.print("Your Grade is ");
if(avg>80)
{
System.out.print("A");
}
else if(avg>60 && avg<=80)
{
System.out.print("B");
}
else if(avg>40 && avg<=60)
{
System.out.print("C");
}
else
{
System.out.print("D");
}

Page 16
}
}
 Output: 13

 Practical: 14
Write a Java Program to create an Employee class and generate Salary Slip for the
employee.
import java.lang.*;
import java.io.*;
class Employee
{
int emp_id;
String emp_name;
float basic_salary;
Employee(int id, String name, float sal)
{
emp_id=id;
emp_name=name;
basic_salary=sal;
}
void display()
{
float da=basic_salary*15/100;
float hra=basic_salary*10/100;
Page 17
float gross_sal=basic_salary+da+hra;
System.out.println ("Employee Id= "+emp_id);
System.out.println ("Emplyee Name= "+emp_name);
System.out.println ("Gross Salary= "+gross_sal);
}
}
class P14
{
public static void main(String args[]) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader
(System.in));
System.out.println ("Enter Employee id");
int id = Integer.parseInt(br.readLine());
System.out.println ("Enter Employee Name");
String name = br.readLine();
System.out.println ("Enter Basic Salary");
Float sal = Float.parseFloat(br.readLine());
Employee e = new Employee(id, name, sal);
e.display();
}
}
 Output: 14

Page 18
 Practical: 15
Write a java program which shows the use of Static Members.
class P15
{
static int a=3;
static int b;
static void math(int x)
{
System.out.println("x=" + x);
System.out.println("a=" + a);
System.out.println("b=" + b);
}
static
{
System.out.println("static block initialized");
b=a*b;
}
public static void main(String args[])
{
math(42);
}
}
 Output: 15

Page 19
 Practical: 16
Write a java program which shows the Nesting of Methods.
class nesting
{
int a,b;
nesting(int p,int q)
{
a=p;
b=q;
}
int largest()
{
if(a>=b)
{
return(a);
}
else
{
return(b);
}
}
void display()
{
int large=largest();
System.out.println("largest value=" + large);
}
}
class P16
{
public static void main(String args[])
{
nesting n=new nesting(10,20);
n.display();
}
}

Page 20
 Output: 16

 Practical: 17
Write a java program which shows the use of Methods Overloading.
class OverloadDemo
{
void test()
{
System.out.println("No parameters");
}
void test(int a)
{
System.out.println("a: " + a);
}
void test(int a, int b)
{
System.out.println("a and b: " + a + " " + b);
}
double test(double a)
{
System.out.println("double a: " + a);
return a*a;
}
}
Page 21
class P17
{
public static void main(String args[])
{
OverloadDemo ob = new OverloadDemo();
double result;
ob.test();
ob.test(10);
ob.test(10, 20);
result = ob.test(123.2);
System.out.println("Result of ob.test(123.2): " + result);
}
}
 Output: 17

 Practical: 18
Write a java program which implements the Default Constructors.
class room
{
room()
{
System.out.println("This is default constructors");
}
}
Page 22
class P18
{
public static void main(String args[])
{
room r1=new room();
}
}
 Output: 18

 Practical: 19
Write a java program which implements the Parameterized Constructors.
class rectangle
{
int length;
int width;
rectangle(int x, int y)
{
length=x;
width=y;
}
int area()
{
return(length*width);
}

Page 23
}
class P19
{
public static void main(String args[])
{
rectangle r1=new rectangle(10,15);
int a=r1.area();
System.out.println("Area is:"+a);
}
}
 Output: 19

 Practical: 20
Write a java program which implements the Overloading of Constructors.
class Square
{
int height;
int width;
Square()
{
height = 0;
width = 0;
}
Square(int side)
Page 24
{
height = width = side;
}
Square(int sideh, int sidew)
{
height = sideh;
width = sidew;
}
}
class P20
{
public static void main(String args[])
{
Square sObj1 = new Square();
Square sObj2 = new Square(5);
Square sObj3 = new Square(2,3);

System.out.println("Variable values of object1 : ");


System.out.println("Object1 height = " + sObj1.height);
System.out.println("Object1 width = " + sObj1.width);
System.out.println("");

System.out.println("Variable values of object2 : ");


System.out.println("Object2 height = " + sObj2.height);
System.out.println("Object2 width = " + sObj2.width);
System.out.println("");

System.out.println("Variable values of object3 : ");


System.out.println("Object3 height = " + sObj3.height);
System.out.println("Object3 width = " + sObj3.width);
}
}

Page 25
 Output: 20

 Practical: 21
Write a java program which explains the concept of Single Inheritance.
class a
{
int i,j;
void showij()
{
System.out.println("i and j :" + i + " " +j);
}
}
class b extends a
{
int k;
void showk()
{
System.out.println("k:" +k);
}
void sum()
{
System.out.println("i+j+k:" + (i+j+k));
}
}
Page 26
class P21
{
public static void main(String args[])
{
a superob=new a();
b subob=new b();

superob.i=10;
superob.j=20;

System.out.println("contents of superob:");

superob.showij();
System.out.println();

subob.i=7;
subob.j=8;
subob.k=9;

System.out.println("contents of subob:");

subob.showij();
subob.showk();
System.out.println();
System.out.println("sum of i,j and k in subob");

subob.sum();
}
}

Page 27
 Output: 21

 Practical: 22
Write a java program which explains the concept of Multilevel Inheritance.
import java.lang.*;
import java.io.*;
class Account
{
String cust_name;
int acc_no;
Account(String a, int b)
{
cust_name=a;
acc_no=b;
}
void display()
{
System.out.println ("Customer Name: "+cust_name);
System.out.println ("Account No: "+acc_no);
}
}
class Saving_Acc extends Account
{
int min_bal,saving_bal;
Page 28
Saving_Acc(String a, int b, int c, int d)
{
super(a,b);
min_bal=c;
saving_bal=d;
}
void display()
{
super.display();
System.out.println ("Minimum Balance: "+min_bal);
System.out.println ("Saving Balance: "+saving_bal);
}
}
class Acct_Details extends Saving_Acc
{
int deposits, withdrawals;
Acct_Details(String a, int b, int c, int d, int e, int f)
{
super(a,b,c,d);
deposits=e;
withdrawals=f;
}
void display()
{
super.display();
System.out.println ("Deposit: "+deposits);
System.out.println ("Withdrawals: "+withdrawals);
}
}
class P22
{
public static void main(String args[])
{
Acct_Details A = new Acct_Details("PuRu",666,1000,5000,500,9000);
A.display();
}

Page 29
}
 Output: 22

 Practical: 23
Write a java program which explains the concept of Hierarchical Inheritance.
class HierarchicalInheritance
{
void DisplayA()
{
System.out.println("This is a content of parent class");
}
}
class A extends HierarchicalInheritance
{
void DisplayB()
{
System.out.println("This is a content of child class 1");
}
}
class B extends HierarchicalInheritance
{
void DisplayC()
{
System.out.println("This is a content of child class 2");
Page 30
}
}
class P23
{
public static void main(String args[])
{
System.out.println("Calling for child class C");
B b = new B();
b.DisplayA();
b.DisplayC();
System.out.println("Calling for child class B");
A a = new A();
a.DisplayA();
a.DisplayB();
}
}
 Output: 23

Page 31
 Practical: 24
Write a java program which shows the Method Overriding.
class a
{
int i,j;
a(int a,int b)
{
i=a;
j=b;
}
void show()
{
System.out.println("i and j:" + i + " " + j);
}
}
class b extends a
{
int k;
b(int a,int b, int c)
{
super(a,b);
k=c;
}
void show()
{
System.out.println("k:"+ k);
}
}
class P24
{
public static void main(String args[])
{
b subob=new b(1,2,3);
subob.show();
}
}

Page 32
 Output: 24

 Practical: 25
Write a Java Program to implement final class and final method.
final class xyz
{
final void display()
{
System.out.println("This is the final class and method");
}
}
class abc
{
void display()
{
System.out.println("This is simple class and method");
}
}
class P25
{
public static void main(String args[])
{
abc fi=new abc();
fi.display();

Page 33
}
}
 Output: 25

 Practical: 26
Write a Java Program to implement abstract class and abstract method.
abstract class Bike
{
abstract void run();
}
class P26 extends Bike
{
void run()
{
System.out.println("Running Safely...");
}
public static void main(String args[])
{
Bike obj = new P26();
obj.run();
}
}

Page 34
 Output: 26

 Practical: 27
Write a java program which implements Interface.
interface callback
{
void callback(int param);
}
class client implements callback
{
public void callback(int p)
{
System.out.println("callback called with:" + p);
}
}
class P27
{
public static void main(String args[])
{
callback c=new client();
c.callback(42);
}
}

Page 35
 Output: 27

 Practical: 28
Write a java program which implements Multiple Interfaces.
interface car
{
int speed=90;
public void distance();
}
interface bus
{
int distance=100;
public void speed();
}
class vehicle implements car,bus
{
public void distance()
{
int distance=speed*100;
System.out.println("Distance Travelled is="+distance);
}
public void speed()
{
int speed=distance/100;

Page 36
}
}
class P28
{
public static void main(String args[])
{
System.out.println("Vehicle");
vehicle v1=new vehicle();
v1.distance();
v1.speed();
}
}
 Output: 28

 Practical: 29
Write a java program which shows importing of classes from other packages.
package packageP29;
public class subtraction
{
public int add(int x,int y)
{
return(x-y);
}
}

Page 37
import packageP29.*;
class P29
{
public static void main(String args[])
{
subtraction a=new subtraction();
System.out.println("The subtraction is : "+a.add(5,4));
}
}
 Output: 29

 Practical: 30
Write a Java Program to implement the methods of Math Class.
import java.lang.Math;
class P30
{
public static void main(String args[])
{
int num = 9;
if (Math.sqrt(num) * Math.sqrt(num) == num)
System.out.println(num + " is a perfect square");
else
System.out.println(num + " is not a perfect square");
num = -2;

Page 38
System.out.println("Value of num : " + Math.abs(num));
System.out.println("value when Math.ceil is used : " +Math.ceil
(Math.PI));
System.out.println("Value when Math.floor is used : " +Math.floor
(Math.PI));
num = (int) (Math.random() * 10);
System.out.println("Random number between 0 and 10 : "+ num);
}
}
 Output: 30

 Practical: 31
Write a Java Program to implement the methods of String Class.
public class P31
{
public static void main(String args[])
{
String str="StudyTonight";
System.out.println(str.indexOf('u'));
System.out.println(str.indexOf('t', 3));

String subString="Ton";
System.out.println(str.indexOf(subString));
System.out.println(str.indexOf(subString,7));

Page 39
}
}
 Output: 31

 Practical: 32
Write a Java Program to implement the methods of Vector Class.
import java.util.*;
public class P32
{
public static void main(String args[])
{
Vector v = new Vector(3, 2);
System.out.println("Initial size: " + v.size());
System.out.println("Initial capacity: " + v.capacity());

v.addElement(new Integer(1));
v.addElement(new Integer(2));
v.addElement(new Integer(3));
v.addElement(new Integer(4));
System.out.println("Capacity after four additions: " + v.capacity());

v.addElement(new Double(5.45));
System.out.println("Current capacity: " + v.capacity());

Page 40
v.addElement(new Double(6.08));
v.addElement(new Integer(7));
System.out.println("Current capacity: " + v.capacity());

v.addElement(new Float(9.4));
v.addElement(new Integer(10));
System.out.println("Current capacity: "+ v.capacity());

v.addElement(new Integer(11));
v.addElement(new Integer(12));
System.out.println("First element: " + (Integer)v.firstElement());
System.out.println("Last element: " + (Integer)v.lastElement());

if(v.contains(new Integer(3)))
System.out.println("Vector contains 3.");

Enumeration vEnum = v.elements();


System.out.println("\nElements in vector:");

while(vEnum.hasMoreElements())
System.out.print(vEnum.nextElement() + " ");
System.out.println();
}
}

Page 41
 Output: 32

 Practical: 33
Write a Java Program to implement the methods of Stack Class.
import java.util.*;
public class P33
{
static void showpush(Stack stack1, int a)
{
stack1.push(new Integer(a));
System.out.println("push(" + a + ")");
System.out.println("stack: " + stack1);
}
static void showpop(Stack stack1)
{
Integer a = (Integer) stack1.pop();
System.out.println(a);
System.out.println("stack: " + stack1);
}
public static void main(String args[])
{
Stack stack1 = new Stack();
System.out.println("stack: " + stack1);

Page 42
showpush(stack1, 40);
showpush(stack1, 50);
showpush(stack1, 60);
showpop(stack1);
showpop(stack1);
showpop(stack1);
try
{
showpop(stack1);
}
catch (EmptyStackException e)
{
System.out.println("it Is Empty Stack");
}
}
}
 Output: 33

Page 43
 Practical: 34
Write a Java Program which will read a text and count all occurrences of a
particular word.
import java.util.Scanner;
public class P34
{
public static int countWords(String str)
{
int count = 1;
for(int i=0; i<=str.length()-1; i++)
{
if(str.charAt(i) == ' ' && str.charAt(i+1)!=' ')
{
count++;
}
}
return count;
}
public static void main(String args[])
{
String sentence;
Scanner scan = new Scanner(System.in);
System.out.print("Enter a Sentence : ");
sentence = scan.nextLine();
System.out.print("Total Number of Words in Entered Sentence is " +
countWords(sentence));
}
}

Page 44
 Output: 34

 Practical: 35
Write a Java Program which will read a string and rewrite it in the alphabetical
order eg. The word “STRING” should be written a “GINRST”.
import java.io.*;
import java.util.*;
class P35
{
String alphaOrder(String str)
{
char[] charArray=str.toCharArray();
Arrays.sort(charArray);
String aString=new String(charArray);
return aString;
}
public static void main(String[] args)throws IOException
{
System.out.println("Enter the String->");
BufferedReader br=new BufferedReader(new InputStreamReader
(System.in));
String inputString=br.readLine();
P35 obj=new P35();
String alphaString=obj.alphaOrder(inputString);
System.out.println("String in the Alphabetic Order :" +alphaString);
Page 45
}
}
 Output: 35

 Practical: 36
Write a java program which creates threads using the Thread Class.
class mythread extends Thread
{
public void run()
{
for(int i=1; i<=10; i++)
{
System.out.println(i);
}
}
}
class P36
{
public static void main(String args[])
{
mythread obj=new mythread();
Thread t=new Thread(obj);
t.start();
}

Page 46
}
 Output: 36

 Practical: 37
Write a java program which shows the use of yield(), stop() and sleep() Methods.
class a extends Thread
{
public void run()
{
for(int i=0;i<5;i++)
{
if(i==0) yield();
System.out.println("From Class a i ="+i);
}
}
}
class b extends Thread
{
public void run()
{
for(int i=0;i<5;i++)
{
if(i==2) stop();
System.out.println("From Class b i ="+i);

Page 47
}
}
}
class c extends Thread
{
public void run()
{
for(int i=0;i<5;i++)
{
if(i==1)
{
try
{
sleep(1000);
}
catch(Exception e)
{

}
}
System.out.println("From Class c i ="+i);
}
}
}
public class P37
{
public static void main(String[] args)
{
new a().start();
new b().start();
new c().start();
}
}

Page 48
 Output: 37

 Practical: 38
Write a java program which shows the Priority in Threads.
import java.lang.*;
class P38 extends Thread
{
public void run()
{
System.out.println("Inside run method");
}
public static void main(String args[])
{
P38 t1 = new P38();
P38 t2 = new P38();
P38 t3 = new P38();

System.out.println("t1 thread priority : " + t1.getPriority());


System.out.println("t2 thread priority : " + t2.getPriority());
System.out.println("t3 thread priority : " + t3.getPriority());

t1.setPriority(2);
t2.setPriority(5);
t3.setPriority(8);
Page 49
System.out.println("t1 thread priority : " + t1.getPriority());
System.out.println("t2 thread priority : " + t2.getPriority());
System.out.println("t3 thread priority : " + t3.getPriority());

System.out.print(Thread.currentThread().getName());
System.out.println("Main thread priority : " + Thread.currentThread().
getPriority());

Thread.currentThread().setPriority(10);
System.out.println("Main thread priority : " + Thread.currentThread().
getPriority());
}
}
 Output: 38

 Practical: 39
Write a java program which use of Runnable Interface.
class P39
{
public static void main(String args[])
{
StringThread t=new StringThread("Java",5);
new Thread(t).start();

Page 50
}
}
class StringThread implements Runnable
{
String str;
int num;
StringThread(String s,int n)
{
str=new String(s);
num=n;
}
public void run()
{
for(int i=1; i<=num; i++)
{
System.out.println(str+" ");
}
}
}
 Output: 39

Page 51
 Practical: 40
Write a java program which uses try and catch for Exception Handling.
class P40
{
public static void main(String args[])
{
int d,a;
try
{
d=0;
a=42/d;
System.out.println("This will not be printed");
}
catch(ArithmeticException e)
{
System.out.println("Division by zero");
}
System.out.println("After catch statement");
}
}

 Output: 40

Page 52
 Practical: 41
Write a java program which uses Multiple catch Blocks.
public class P41
{
public static void main(String args[])
{
int array[]={20,10,30};
int num1=15,num2=0;
int res=0;
try
{
res=num1/num2;
System.out.println("The result is" + res);
for(int ct=2; ct>=0; ct--)
{
System.out.println("The value of array are" + array[ct]);
}
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Error... Array is out of Bounds");
}
catch(ArithmeticException e)
{
System.out.println("can't be divided by zero");
}
}
}

Page 53
 Output: 41

 Practical: 42
Write a java program which uses finally Statement.
class P42
{
public static void main(String args[])
{
int a[]=new int[2];
try
{
System.out.println("Access element three :" + a[3]);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Access element three :" + e);
}
finally
{
a[0]=6;
System.out.println("First element value :" + a[0]);
System.out.println("The Finally statement is executing...");
}
}

Page 54
}
 Output: 42

 Practical: 43
Write a java program which uses Nested try Statements.
public class P43
{
public static void main(String args[])
{
int[] num={12,24,36,48,60,72};
int[] den={2,4,0,6};
for(int i=0;i<num.length;i++)
{
try
{
try
{
System.out.println(num[i]+"/"+den[i]+" = "+num[i]/
den[i]);
}
catch(ArithmeticException e)
{
System.out.println("Inner: integer division y 0 not
possible");

Page 55
}
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Outer: Array Index out of
Bounds");
}
}
}
}
 Output: 43

 Practical: 44
Write a java program which shows throwing our own Exception.
import java.lang.Exception;
class MyException extends Exception
{
MyException(String Message)
{
super(Message);
}
}
class P44
{

Page 56
public static void main(String args[])
{
int x=5,y=1000;
try
{
float z=x/y;
if(z<0.01)
{
throw new MyException("Number is two small");
}
}
catch(MyException e)
{
System.out.println("Caught my Exception");
System.out.println(e.getMessage());
}
finally
{
System.out.println("I am always here");
}
}
}
 Output: 44

Page 57
 Practical: 45
Create an Applet program that print Hello Applet.
import java.applet.Applet;
import java.awt.Graphics;
/*
<applet code="P45" width="500" height="250">
</applet>
*/
public class P45 extends Applet
{
public void paint(Graphics g)
{
g.drawString("Hello Applet!", 100, 70);
}
}
 Output: 45

Page 58
 Practical: 46
Create an applet that use init(),start(),stop() and destroy() methods of applet.
import java.applet.Applet;
import java.awt.Graphics;
import javax.swing.JOptionPane;
/*
<applet code="P46" width="500" height="250">
</applet>
*/
public class P46 extends Applet
{
public void init()
{
JOptionPane.showMessageDialog(null,"Applet : Initialized");
}
public void start()
{
JOptionPane.showMessageDialog(null,"Applet : Started");
}
public void stop()
{
JOptionPane.showMessageDialog(null,"Applet : Stopped");
Page 59
}
public void paint(Graphics g)
{
g.drawString("Applet : Running", 100, 100);
}
public void destroy()
{
JOptionPane.showMessageDialog(null,"Applet : Destroyed");
}
}
 Output: 46

Page 60
 Practical: 47
write an applet program to implement the concept of passing parameter to applet.
import java.applet.*;
import java.awt.*;
/*
<applet code="P47" width="500" height="250">
</applet>
*/
public class P47 extends Applet
{
private String strDefault = "Hello! Java Applet.";
public void paint(Graphics g)
{
String strParameter = this.getParameter("Message");
if (strParameter == null)
strParameter = strDefault;
g.drawString(strParameter, 50, 25);
}
}
 Output: 47

Page 61
 Practical: 48
Write a applet program to implement various methods of Graphics class.
import java.applet.Applet;
import java.awt.*;
/*
<applet code="P48" width=300 height=200>
</applet>
*/
public class P48 extends Applet
{
public void paint(Graphics g)
{
g.setColor(Color.red);
g.drawString(" Various Methods Of Graphics:- ",50, 50);
g.drawLine(50,30,20,300);
g.drawRect(70,100,30,30);
g.fillRect(170,100,30,30);
g.drawOval(70,200,30,30);

g.setColor(Color.pink);
g.fillOval(170,200,30,30);

Page 62
g.drawArc(90,150,30,30,30,270);
g.fillArc(270,150,30,30,0,180);

}
}
 Output: 48

Page 63

You might also like