Java Lab Manual

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

JAVA PROGRAMMING (3350703) ENROLLMENT NO.

Practical -1

Aim: Install JDK, write a simple “Hello World” or similar java program,
compilation, debugging, executing using java compiler and interpreter

public class Example


{
public static void main(String[] args)
{
System.out.println("Hello World");
}
}

Output:
Hello World

Page 1
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :

Practical -2

Aim: Write a program in Java to generate first n prime numbers.

import java.util.Scanner;
public class Example
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Enter the value of n:");
int n = in.nextInt();
int counter = 0;
int flag;
for (int num = 2; ; num++)
{
flag = 0;
for (int div = 2; div < num; div++)
{
if (num % div == 0)
{
flag = 1;
break;
}
}
if (flag == 0)
{
System.out.println(num);
counter++;
if(counter == n)
{
break;
}
}
}
}
}

Page 2
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :

Output:

Enter the value of n: 5


2
3
5
7
11

Page 3
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :

Practical -3

Aim: Write a program in Java to find maximum of three numbers using


conditional operator

public class Example


{
public static void main(String[] args)
{
int a=20,b=30,c=10;
int max = (a> b && a>c) ? a : ((b>a && b>c) ? b : c) ;
System.out.println(max + " is maximum");}
}

Output:

30 is maximum

Page 4
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :

Practical -4

Aim: Write a program in Java to find second maximum of n numbers


without using arrays.

import java.util.Scanner;
public class Example
{
public static void main(String[] args)
{
int high=0,sec_high=0;
int n;
Scanner in = new Scanner(System.in);
System.out.println("Enter the value of n:");
n= in.nextInt();
for(int i=1;i<=n;i++)
{
System.out.print("Enter the value:");
int value = in.nextInt();
if(value>high)
{
sec_high=high;
high=value;
}
else
{
if(value>sec_high)
{
sec_high=value;
}
}
}
System.out.println("Highest value:" + high);
System.out.println("Second Highest value:" + sec_high);
}
}

Page 5
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :

Output:

Enter the value of n: 5


Enter the value: 33
Enter the value: 11
Enter the value: 55
Enter the value: 22
Enter the value: 44
Highest value: 55
Second Highest value: 44

Page 6
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :

Practical -5

Aim: Write a program in Java to reverse the digits of a number using


while loop

import java.util.Scanner;
public class Example
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Enter the Value: ");
int value = in.nextInt();
int reversenum=0;
while( value != 0 )
{
reversenum = (reversenum * 10)+(value%10);
value = value/10;
}
System.out.print("Reverse Number: " + reversenum);
}
}

Output:

Enter the Value: 123


Reverse Number: 321

Page 7
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :

Practical -6

Aim: Write a program in Java to convert number into words & print it

import java.util.Scanner;
public class Example
{
public static void word(int n,String s)
{
String one[] =
{"","one","two","three","four","five","six","seven","eight","nine","te
n","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seven
teen","eighteen","nineteen"};
String ten[] =
{"","","twenty","thirty","forty","fifty","sixty","seventy","eighty","nine
ty"};
if(n>19)
{
System.out.print(ten[n/10] + " " + one[n%10] + s);
}
else
{
if(n>0)
System.out.print(one[n] + s);
}
}
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Enter the Value: ");
int value = in.nextInt();
word(value/10000000," crore ");
value = value%10000000;
word(value/100000," lack ");
value = value%100000;
word(value/1000," thousand ");
value = value%1000;
word(value/100," hundred ");
value = value%100;
Page 8
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :

word(value,"");
}
}

Output -1:

Enter the Value: 12515456


one crore twenty five lack fifteen thousand four hundred fifty six

Output -2:

Enter the Value: 54321


fifty four thousand three hundred twenty one

Output -3:

Enter the Value: 123


one hundred twenty three

Page 9
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :

Practical -7

Aim: Write programs in Java to use Wrapper class of each primitive data
types

public class Example


{
public static void main(String[] args)
{
int i=5;
float f=2.3f;
char c='A';
boolean b=true;
//Converting primitive data type into object.
Integer i1 = new Integer(i);
Float f1= new Float(f);
Character c1 = new Character(c);
Boolean b1 = new Boolean(b);
//getting primitive data type from object.
System.out.println("i =" + i1.intValue());
System.out.println("f =" + f1.floatValue());
System.out.println("c =" + c1.charValue());
System.out.println("b =" + b1.booleanValue());
}
}

Output:

i =5
f =2.3
c =A
b =true

Page 10
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :

Practical -8

Aim: Write a program in Java to multiply two matrixes

import java.util.Scanner;
public class Example
{
public static void main(String[] args)
{
int a[][]= {{1,2},{3,4}}, b[][] = {{3,4},{1,2}};
int c[][]=new int[2][2];
for(int i=0;i<=1;i++)
{
for(int j=0;j<=1;j++)
{
c[i][j]=0;
for(int k=0;k<=1;k++)
{
c[i][j]=c[i][j] + (a[i][k]*b[k][j]);
}
}
}
System.out.print("Result:\n");
for(int i=0;i<=1;i++)
{
for(int j=0;j<=1;j++)
{
System.out.print(c[i][j] + " ");
}
System.out.print("\n");
}
}
}
Output:

Result:
58
13 20

Page 11
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :

Practical -9
Aim: Write a static block which will be executed before main( ) method in
a class

public class Example


{
//static block will be executed before main method.
static
{
System.out.println("Inside static block.");
}
public static void main(String[] args)
{
System.out.println("Inside main method.");
}
}

Output:

Inside static block.


Inside main method

Page 12
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :

Practical -10

Aim: Write a program in Java to demonstrate use of this keyword.


Check whether this can access the private members of the class or not.

class Test
{
private int n;
void setdata(int n)
{
this.n = n; //accessing private member (n) using this keyword.
}
void getdata()
{
System.out.println("n=" + n);
}
}
public class Example
{
public static void main(String[] args)
{
Test obj1 = new Test();
obj1.setdata(5);
obj1.getdata();
}
}

Output:

n=5

Page 13
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :

Practical -11
Aim :- Write a program in Java to develop overloaded constructor. Also
develop the copy constructor to create a new object with the state of the
existing object.

class Box

int length;

int width;

int height;

Box()

System.out.println("Inside Default Constructor");

length = 10;

width = 10;

height = 10;

Box(int l, int w,int h)

System.out.println("Inside Parameterized Constructor");

length = l;

width = w;

height = h;

Page 14
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :

Box(Box b)

System.out.println("Inside Copy Constructor");

length = b.length;

width = b.width;

height = b.height;

void volume()

int vol = length * width * height;

System.out.println("Volume of the Box: " + vol );

public class Example

public static void main(String[] args)

Box b1= new Box();

b1.volume();

Box b2= new Box(5,5,10);

b2.volume();

Box b3= new Box(b1);

b3.volume();

Page 15
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :

Output:
Inside Default Constructor

Volume of the Box: 1000

Inside Parameterized Constructor

Volume of the Box: 250

Inside Copy Constructor

Volume of the Box: 1000

Page 16
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :

Practical -12

Aim: Write a program in Java to demonstrate the use of private


constructor and also write a method which will count the number of
instances created using default constructor only.

class Box
{
int length;
int width;
int height;
static int count=0;
Box()
{
length = 10;
width = 10;
height = 10;
Box.count_instances();
}
Box(int l, int w,int h)
{
length = l;
width = w;
height = h;
}
//following method counts number of objects created using default constructor.
static void count_instances()
{
count++;
}
}
public class Example
{
public static void main(String[] args)
{
Box b1 = new Box();
Box b2 = new Box();
System.out.println("No of Objects Created: " + Box.count );
}

Page 17
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :

Output:

No of Objects Created: 2

Page 18
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :

Practical -13

Aim: Write a program in Java to demonstrate the use of 'final' keyword


in the field declaration. How it is accessed using the objects.

class FinalDemo
{
final double PI=3.14159;
final void display()
{
System.out.println("PI : " + PI);
}
}
public class Example
{
public static void main(String[] args)
{
FinalDemo obj = new FinalDemo();
obj.display();
}
}

Output:
PI: 3.14159

Page 19
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :

Practical -14

Aim: Develop minimum 4 program based on variation in methods i.e.


passing by value, passing by reference, returning values and returning
objects from methods.

Practical: 14.1 (Pass by Value)

class PBV
{
int i;
void set_data(int a)
{
i=a;
}
void display()
{
System.out.print("i : " + i);
}
}
public class Example
{
public static void main(String[] args)
{
PBV obj = new PBV();
obj.set_data(5); //Pass by Value
obj.display();
}
}

Output:
i:5

Page 20
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :

Practical: 14.2 (Pass by Reference/Object)

class PBR
{
int i;
void set_data(PBR o)
{
i = o.i;
}
void display()
{
System.out.print("i : " + i);
}
}
public class Example
{
public static void main(String[] args)
{
PBR obj1 = new PBR();
PBR obj2 = new PBR();
obj1.i=7;
obj2.set_data(obj1); //Pass by Reference
obj2.display();
}
}

Output:
i:7

Page 21
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :

Practical: 14.3 (Return by Value & Pass by Value)

class RBV
{
int i;
void set_data(int a)
{
i=a;
}
int incrByOne()
{
i++;
return (i);
}
}
public class Example
{
public static void main(String[] args)
{
RBV obj1 = new RBV();
obj1.set_data(5); //Pass by Value
int value = obj1.incrByOne(); // Return by Value
System.out.println("i : " + value);
}
}

Output:
i:6

Page 22
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :

Practical: 14.4 (Return by Reference /Object)

class RBR
{
int i;
RBR Copy()
{
RBR temp = new RBR();
temp.i = i;
return temp;
}
void display()
{
System.out.println("i : "+ i);
}
}
public class Example
{
public static void main(String[] args)
{
RBR obj1 = new RBR();
obj1.i=7;
RBR obj2 = obj1.Copy(); //Return by Reference
obj1.display();
obj2.display();
}
}

Output:

i:7
i:7

Page 23
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :

Practical -15
Aim: - Write a program in Java to demonstrate single inheritance,
multilevel inheritance and hierarchical inheritance.

Practical: 15.1 (Single Inheritance)


class A
{
int i;
}
class B extends A
{
int j;
void sum() }
{
System.out.println("i+j : " + (i+j));
}
public class Example
{
public static void main(String[] args)
{
B b = new B();
b.i=5;
b.j=7;
b.sum();
}
}

Output:

i+j : 12

Page 24
JAVA PROGRAMMING (3350703) ENROLLMENT
NO. :
Practical: 15.2 (Multilevel Inheritance)

class A
{
int i;
}
class B extends A
{
int j;
}
class C extends B
{
int k;
void sum()
{
System.out.println("i+j+k : " + (i+j+k));
}
}
public class Example
{
public static void main(String[] args)
{
C c = new C();
c.i=5;
c.j=7;
c.k=9;
c.sum();
}
}

Output:

i+j+k : 21

Page 25
JAVA PROGRAMMING (3350703) ENROLLMENT
NO. :
Practical: 15.3 (Hierarchical Inheritance)

class A
{
int i;
}
class B extends A
{
int j;
void sum()
{
System.out.println("i+j : " + (i+j));
}
}
class C extends A
{
int k;
void sum()
{
System.out.println("i+k : " + (i+k));
}
}
public class Example
{
public static void main(String[] args)
{
B b = new B();
b.i=2;
b.j=4;
b.sum();
C c = new C();
c.i=3;
c.k=5:
c.sum();
}
}

Output: i+j=6
i+k=8

Page 26
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :

Practical -16

Aim: - Create a class to find out whether the given year is leap year
or not. (Use inheritance for this program).

class LeapYear
{
int year;
void isLeapYear()
{
if(year%4==0)
System.out.println(year + " is leap year.");
else
System.out.println(year + " is not leap year.");
}
}
public class Example
{
public static void main(String[] args)
{
LeapYear l = new LeapYear();
l.year = 2015;
l.isLeapYear();
l.year = 2016;
l.isLeapYear();
}
}

Output:

2015 is not leap year.


2016 is leap year.

Page 27
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :

Practical -17
Aim: - Write an application that illustrates how to access a hidden
variable. Class A declares a static variable x.The class B extends A
and declares an instance variable x. display() method in class B
displays both of these variables.

class A
{
int x;
}
class B extends A
{
int x;
B(int a,int b)
{
super.x = a; //super.x refers variable x of class A
x = b; // x refers to variable x of class B
}
void display()
{
//super.x prints value of variable x of class A
System.out.println("X of A :" + super.x);
//x prints value of variable x of class B
System.out.println("X of B :" + x);
}
}
public class Example
{
public static void main(String[] args)
{
B obj = new B(2,4);
obj.display();
}
}

Output:

X of A :2
X of B :4
Page 28
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :

Practical -18
Aim: - Write a program in Java in which a subclass constructor
invokes the constructor of the super class and instantiate the values.

class A
{
int x;
A(int a)
{
x=a;
}
}
class B extends A
{
int y;
B(int a,int b)
{
super(a); //super() calls the constructor of the class A
y = b;
}
void display()
{
System.out.println("X :" + x);
System.out.println("Y :" + y);
}
}
public class Example
{
public static void main(String[] args)
{
B obj = new B(2,4);
obj.display();
}
}

Output:

X:2
Y:4

Page 29
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :

Practical -19
Aim: - Write a program that illustrates interface inheritance.
Interface P12 inherits from both P1 and P2. Each interface declares
one constant and one method. The class Q implements P12.
Instantiate Q and invoke each of its methods. Each method displays
one of the constants.

interface P1
{
int I = 1;
void showI();
}
interface P2
{
int J = 2;
void showJ();
}
interface P12 extends P1, P2
{
int K = 3;
void showK();
}
class Q implements P12
{
public void showI()
{
System.out.println("I : " + I);
}
public void showJ()
{
System.out.println("J : " + J);
}
public void showK()
{
System.out.println("K : " + K);
}
}

Page 30
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :

public class Example


{
public static void main(String[] args)
{
Q obj = new Q();
obj.showI();
obj.showJ();
obj.showK();
}
}

Output:

I:1
J:2
K:3

Page 31
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :

Practical – 20
Aim: - (1) Write an application that illustrates method overriding.
(2) Also demonstrate accessibility rules in inside and outside
packages.

1. Method Overriding:

class A
{
void show()
{
System.out.println("Inside Class-A");
}
}
class B extends A
{
// Overriding method show of class A
void show()
{
System.out.println("Inside Class-B");
}
}
public class Example
{
public static void main(String[] args)
{
B obj = new B();
obj.show();
//show() method of class-B will be called.

Output:
I:1
J:2
K:3

Page 32
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :

2. Accessibility Rule inside and outside package

2.1 Accessibility Rule Inside package

package p1;
public class Protection
{
int n = 1;
public int n_pub = 2;
}
public class Example
{
public static void main(String[] args)
{
Protection p1 = new Protection();
System.out.println("n = " + p1.n);
System.out.println("n_pub = " +p1.n_pub);
}
}

Output:
n=1
n= 2

Page 33
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :

2.2 Accessibility Rule outside package

Protection.java
package p1;
public class Protection
{
int n = 1;
public int n_pub = 2;
}
Example.java
import p1.*;
public class Example
{
public static void main(String[] args)
{
Protection p1 = new Protection();
System.out.println("n = " + p1.n); //error
System.out.println("n_pub = " +p1.n_pub);
}
}

Page 34
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :

Practical - 21
Aim: - Describe abstract class called Shape which has three
subclasses say Triangle, Rectangle, Circle. Define one method area()
in the abstract class and override this area() in these three
subclasses to calculate for specific object i.e. area() of Triangle
subclass should calculate area of triangle etc. Same for Rectangle
and Circle.

abstract class Shape


{
abstract void area();
}
class Triangle extends Shape
{
int base,height;
Triangle(int b,int h)
{
base=b;
height=h;
}
void area()
{
float ar = (0.5f)* (base*height);
System.out.println("Area of Triangle: "+ar);
}
}
class Rectangle extends Shape
{
int length,width;
Rectangle(int l,int w)
{
length=l;
width=w;
}
void area()
{
float ar = length*width;
System.out.println("Area of Rectangle: "+ar);

Page 35
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :

}
}
class Circle extends Shape
{
int radius;
Circle(int r)
{
radius = r;
}
void area()
{
float ar = 3.14f * radius * radius;
System.out.println("Area of Circle: "+ar);
}
}
public class Example
{
public static void main(String[] args)
{
Triangle t1 = new Triangle(2,4);
t1.area();
Rectangle r1 = new Rectangle(3,5);
r1.area();
Circle c1= new Circle(2);
c1.area();
}
}

Output:

Area of Triangle: 4.0


Area of Rectangle: 15.0
Area of Circle: 12.56

Page 36
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :

Practical - 22
Aim: - Write a program in Java to demonstrate implementation of
multiple inheritance using interfaces.

interface A
{
void method1();
}
interface B
{
void method2();
}
class InterfaceDemo implements A , B
{
public void method1()
{
System.out.println("Inside Method-1");
}
public void method2()
{
System.out.println("Inside Method-2");
}
}
public class Example
{
public static void main(String[] args)
{
InterfaceDemo obj = new InterfaceDemo();
obj.method1();
obj.method2();
}
}

Output:
Inside Method-1
Inside Method-2

Page 37
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :

Practical - 23
Aim: - Write a program in Java to demonstrate use of final class.

final class A
{
int i;
void showI()
{
System.out.println("I : " + i);
}
}
// The following class is illegal.
// ERROR! Can't subclass A
class B extends A
{
int j;
void showJ()
{
System.out.println("J : " + j);
}
}
public class Example
{
public static void main(String[] args)
{
B obj = new B();
obj.i = 2;
obj.j = 3;
obj.showI();
obj.showJ();
}
}

Page 38
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :

Output:

error: cannot inherit from final A


class B extends A
^
1 error

Page 39
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :

Practical - 24
Aim: - Write a program in Java to develop user defined exception
for 'Divide by Zero' error.

class MyException extends Exception


{
public String toString()
{
return "Attempt to divide by zero.";
}
}
public class Example
{
public static void main(String[] args)
{
B obj = new B();
try
{
throw new MyException();
}
catch(MyException e )
{
System.out.println(e);
}
}
}

Output:

Attempt to divide by zero.

Page 40
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :

Practical - 25
Aim: Write a program in Java to demonstrate multiple try block and
multiple catch exception.

public class Example


{
public static void main(String[] args)
{
try
{
int a= 4/0;
try
{
int b[] = {1,2};
System.out.println(b[5]);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Array index is out of bound.");
}
}
catch(ArithmeticException e)
{
System.out.println("Divide by zero.");
}
}
}

Output:

Divide by zero.

Page 41
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :

Practical - 26
Aim: Write a small application in Java to develop Banking Application
in which user deposits the amount Rs 1000.00 and then start
withdrawing of Rs 400.00, Rs 300.00 and it throws exception "Not
Sufficient Fund" when user withdraws Rs. 500 thereafter.

class MyException extends Exception


{
public String toString()
{
return "Not Sufficient Fund.";
}
}
class Account
{
double amount;
Account(double a)
{
amount = a;
}
void deposite(double a)
{
amount = amount + a;
System.out.println(a + " rupees deposited.");
System.out.println("New balance is : " + amount);
}
void withdraw(double a)
{
if((amount - a)<0)
{
System.out.println("Attempt to withdraw " + a + " rupees is
failed.");
try
{
throw new MyException();
}
catch(MyException e)
{
System.out.println(e);
Page 42
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :

}
}
else
{
amount = amount -a;
System.out.println(a + " rupees withdrawn.");
System.out.println("New balance is : " + amount);
}
}
}
public class Example
{
public static void main(String[] args)
{
Account a1 = new Account(1000);
a1.withdraw(400.00);
a1.withdraw(300.00);
a1.withdraw(500.00);
}
}

Output:

400.0 rupees withdrawn.


New balance is : 600.0
300.0 rupees withdrawn.
New balance is : 300.0
Attempt to withdraw 500.0 rupees is failed.
Not Sufficient Fund.

Page 43
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :

Practical - 27
Aim: Write a program that executes two threads. One thread displays
“Thread1” every 2,000 milliseconds, and the other displays “Thread2”
every 4,000 milliseconds. Create the threads by extending the Thread
class.

class MyThread extends Thread


{
MyThread(String s)
{
Super(s);
Start();
}
Public void run()
{
For(int i=0;i<5;i++)
{
System.out.println(Thread.cureentThread().getName());
Try
{
If(Thread.currentThread().getName()==”Thread1”)
Thread.sleep(2000);
else
Thread.sleep(4000);
}
Catch(Exception e){}
}
}
}

Page 44
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :

public class MultiThreadDemo


{
Public static void main(String args[])
{
System.out.println(”Thread
Name:”+Thread.currentThread().getName());
MyThread m1 = new MyThread(“Thread 1”);
MyThread m2 = new MyThread(“Thread 2”);

}
}

Page 45
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :

Practical - 28
Aim: Write a program that executes two threads. One thread will print
the even numbers and the another thread will print odd numbers from 1
to 50.

public class OddEvenThread


{
Public static void main(String[] args)
{
OddEven o1 =new OddEven(“Odd Number Thread”,1);
OddEven o2 =new OddEven(“Odd Number Thread”,2);
}
}
Class OddEven extends Thread
{
private int number;
public OddEven(String name, int p_number)
{
Super(name)
This.number = p_number;
}
Public void run()
{
for(int i=0;i<25;i++)
{
System.out.println(getName()+this.number);
this.number +=2;
}
}
}

Page 46
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :

Practical - 29
Aim: Write a program in Java to demonstrate use of synchronization of
threads when multiple threads are trying to update common variable.

Class Printing
{
Synchronized void printnumber(int n)
{
System.out.println(“start”);
for(int j =n;j>0;j--)
{
try
{
if(j==n/2)
Thread.sleep(100);
}
catch(InterruptedException e)
{
;
}
System.out.println(“”+j);
}
System.out.println(“End”);
}
}
class Threadserve implements Runnable
{
int n;
Printing pt;
Thread th;

Page 47
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :

Threadserve(Printing p, intx)
{
n=x;
pt=p;
th=new Thread(this);
th.start();
}
Public void run()
{
pt.printnuber(n);
}

}
public class SynchronizedThread
{
Public static void main(String args[])
{
Printing p= new Printing();
Threadserve ts1= new Threadserve(p,16);
Threadserve ts2= new Threadserve(p,8);
Threadserve ts3= new Threadserve(p,10);
}
}

Page 48
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :

Practical - 30
Aim: Write a program in Java to create, write, modify, read operation on
a Text file.

import java.io.*;
public class FileDemo
{
public static void main(String args[])
{
try
{
FileOutputStream fout= new FileOutputStream(“f.txt”);
System.out.println(“File Created”);
String s = “India is Great”;
byte b[] = s.getBytes();
fout.write(b);
fout.close();
System.out.println(“Writing Completed!”);
}
catch(Exception e)
{
System.out.println(e);
}

Page 49
JAVA PROGRAMMING (3350703) ENROLLMENT NO. :

try
{
FileInputStream fin = new FileInputStream(“f.txt”);
int i;
while((i=fin.read())!=-1)
System.out.println((char)i);
Fin.close();
System.out.println(“Reading Complete!”);
}
catch(Exception e)
{
System.out.println(e);
}
}
}

Page 50

You might also like