0% found this document useful (0 votes)
13 views55 pages

Java Lab

The document outlines various Java programming exercises related to Object-Oriented Programming (OOP) concepts. It includes code examples for displaying default values of primitive data types, solving quadratic equations, implementing binary search, bubble sort, string manipulation, class mechanisms, method overloading, constructor usage, and single inheritance. Each exercise is accompanied by problem statements, source code, and expected outputs.

Uploaded by

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

Java Lab

The document outlines various Java programming exercises related to Object-Oriented Programming (OOP) concepts. It includes code examples for displaying default values of primitive data types, solving quadratic equations, implementing binary search, bubble sort, string manipulation, class mechanisms, method overloading, constructor usage, and single inheritance. Each exercise is accompanied by problem statements, source code, and expected outputs.

Uploaded by

medamhemanth227
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

OOPS THROUGH JAVA

KALLAM HARANADHAREDDY
INSTITUTE OF TECHNOLOGY
(APPROVED BY AICTE NEW DELHI, AFFILIATED TO
JNTUK, KAKINADA), CHOWDAVARAM, GUNTUR-19

Roll No:

CERTIFICATE

This is to Certify that Bonafide Record of the Laboratory

Work done by Mr/Ms………………………………………………………………………

of…………[Link]/[Link]/Diploma………...Semester in ………..Branch has

completed…………..experiments in …………………………………………………..

Laboratory during the Academic year 20 -20

Faculty-in-charge Head of the Department

Internal Examiner External Examiner


INDEX
EX. PAGE
NO DATE NAME OF THE EXPERIMENT FROM TO MARKS SIGNATURE

.
EX. PAGE
NO DATE NAME OF THE EXPERIMENT FROM TO MARKS SIGNATURE

.
EX. PAGE
NO DATE NAME OF THE EXPERIMENT FROM TO MARKS SIGNATURE

.
OOPS THROUGH JAVA (R23) Regd No:

EXERCISE-1(A) DATE:

Problem Statement: Write a JAVA program to display default value of all


primitive data type of JAVA

Source Code:

public class DefaultValues


{

static boolean bool;


static byte b;
static char c;
static short s;
static int i;
static long l;
static float f;
static double d;

public static void main(String[] args)


{

[Link]("Default values of primitive data types:");

// Printing default values


[Link]("boolean: " + bool);
[Link]("byte: " + b);
[Link]("char:"+ c + " (unicode: " + (int)c + ")");
[Link]("short: " + s);
[Link]("int: " + i);
[Link]("long: " + l);
[Link]("float: " + f);
[Link]("double: " + d);

}// main()

}// class

II-BTECH I-SEM (CSE)

KHIT AUTONOMOUS 1
OOPS THROUGH JAVA (R23) Regd No:

Output:

Signature of the faculty


II-BTECH I-SEM (CSE)

KHIT AUTONOMOUS 2
OOPS THROUGH JAVA (R23) Regd No:

EXERCISE-1(B) DATE:

Problem Statement: Write a java program that display the roots of a


quadratic equation ax2+bx+c=0. Calculate the discriminate D and basing on
value of D, describe the nature of root.

Source Code:

import [Link];

public class QuadraticEquationRoots


{
public static void main(String[] args)
{
Scanner scanner = new Scanner([Link]);
[Link]("Enter the coefficients of the quadratic
equation:");
[Link]("Enter a: ");
double a = [Link]();
[Link]("Enter b: ");
double b = [Link]();
[Link]("Enter c: ");
double c = [Link]();

// Calculate discriminant
double discriminant = b * b - 4 * a * c;

// Determine nature of roots


if (discriminant > 0)
{
// Two distinct real roots

double root1 = (-b + [Link](discriminant)) / (2 * a);


double root2 = (-b - [Link](discriminant)) / (2 * a);
[Link]("Roots are real and distinct.");
[Link]("Root 1 = " + root1);
[Link]("Root 2 = " + root2);
}

II-BTECH I-SEM (CSE)

KHIT AUTONOMOUS 3
OOPS THROUGH JAVA (R23) Regd No:

else if (discriminant == 0)
{
//One real root (discriminant is zero)
double root = -b / (2 * a);
[Link]("Roots are real and equal.");
[Link]("Root = " + root);
}

else
{
// No real roots (discriminant is negative)
[Link]("No real roots. Roots are complex.");
}

[Link]();

}//main()

}//class

II-BTECH I-SEM (CSE)

KHIT AUTONOMOUS 4
OOPS THROUGH JAVA (R23) Regd No:

Output:

II-BTECH I-SEM (CSE) Signature of the Faculty

KHIT AUTONOMOUS 5
OOPS THROUGH JAVA (R23) Regd No:

EXERCISE-2(A) DATE:

Problem Statement: Write a JAVA program to search for an element in a

given list of elements using binary search mechanism.

Source Code:

public class BinarySearchExample


{
public static void main(String[] args)
{

Scanner scanner = new Scanner([Link]);


[Link]("Enter the number of elements in the array: ");
int n = [Link]();
int[] arr = new int[n];
[Link]("Enter the sorted elements:");

for (int i = 0; i < n; i++)


{
arr[i] = [Link]();
}

[Link]("Enter the element to search for: ");


int target = [Link]();
int index = binarySearch(arr, target);

if (index != -1)
{
[Link]("Element" + target+"found at index" +index);
}

else
{
[Link]("Element"+target+"not found in the array");
}
[Link]();

}// main method

II-BTECH I-SEM (CSE)

KHIT AUTONOMOUS 6
OOPS THROUGH JAVA (R23) Regd No:

// Binary search method

public static int binarySearch(int[] arr, int target)


{

int left = 0;
int right = [Link] - 1;

while (left <= right)


{

int mid = left + (right - left) / 2;


// Checking if the target is present at mid
if (arr[mid] == target)
{
return mid;
}
if (arr[mid] < target)
{
left = mid + 1;
}
else
{
right = mid - 1;
}

} // While END

// returning -1 if an Element not found in the array


return -1;

} // binarySearch

} // class

II-BTECH I-SEM (CSE)

KHIT AUTONOMOUS 7
OOPS THROUGH JAVA (R23) Regd No:

Output:

Signature of the Faculty


II-BTECH I-SEM (CSE)

KHIT AUTONOMOUS 8
OOPS THROUGH JAVA (R23) Regd No:

EXERCISE-2(B) DATE:

Problem Statement: Write a JAVA program to sort for an element in a given

list of elements using bubble sort

Source Code:

import [Link].*;

public class BubbleSort


{

public static void main(String[] args)


{
Scanner scanner = new Scanner([Link]);
[Link]("Enter the number of elements in the array: ");
int n = [Link]();
int[] arr = new int[n];
[Link]("Enter the elements:");

// reading elements from the console and placing into arr


for (int i = 0; i < n; i++)
{
arr[i] = [Link]();
}
// method call statement
bubbleSort(arr);

[Link]("Sorted array:");
//reading elements from the array and displaying
for (int num : arr)
{
[Link](num + " ");
}
[Link]();
[Link]();

II-BTECH I-SEM (CSE)

KHIT AUTONOMOUS 9
OOPS THROUGH JAVA (R23) Regd No:

// Bubble Sort method

public static void bubbleSort(int[] arr)


{
int n = [Link];
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < n - i - 1; j++)
{
if (arr[j] > arr[j + 1])
{
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}//inner for loop
}//outer for loop

}//bubbleSort

}//class

Output

Signature of the Faculty


II-BTECH I-SEM (CSE)

KHIT AUTONOMOUS 10
OOPS THROUGH JAVA (R23) Regd No:

EXERCISE-2(C) DATE:

Problem Statement: Write a JAVA program using StringBuffer to delete,


remove character.

Source Code:

public class StringBufferExample


{
public static void main(String[] args)
{
StringBuffer str = new StringBuffer("Hello, World!");
[Link]("Original StringBuffer: " + str);

[Link](5, 12); // Delete characters from index 5 to 11


[Link]("After deletion: " + str);

[Link](5, " Java"); // Insert " Java" at index 5


[Link]("After insertion: " + str);

[Link](0); // Remove character at index 0


[Link]("After deleting a character: " + str);

[Link]();
[Link]("After reversing: " + str);

} // main

} // class

Output

Signature of the Faculty


II-BTECH I-SEM (CSE)

KHIT AUTONOMOUS 11
OOPS THROUGH JAVA (R23) Regd No:

EXERCISE-3(A) DATE:

Problem Statement: Write a JAVA program to implement class mechanism.

Create a class, methods and invoke them inside main method.

Source Code:

class Person
{
// Instance variables private int age;
private String name;

//Constructor
public Person(String name, int age)
{
[Link] = name;
[Link] = age;
}

public void setName(String name)


{
[Link] = name;
}

public void setAge(int age)


{
[Link] = age;
}

public String getName()


{
return name;
}

public int getAge()


{
return age;
}

public void displayInfo()


{
[Link]("Name: " + name);
[Link]("Age: " + age);
}

}
II-BTECH I-SEM (CSE)

KHIT AUTONOMOUS 12
OOPS THROUGH JAVA (R23) Regd No:

public class MainClass


{
public static void main(String[] args)
{
Person person1 = new Person("Alice", 30);
[Link]();
[Link]("Bob");
[Link](25);
[Link]();
}
}

Output:

II-BTECH I-SEM (CSE) Signature of the Faculty

KHIT AUTONOMOUS 13
OOPS THROUGH JAVA (R23) Regd No:

EXERCISE-3(B) DATE:

Problem Statement: Write a JAVA program implement method overloading.

Source Code:

public class Shape


{
public double area(double radius)
{
return [Link] * radius * radius;
}

public double area(double length, double width)


{
return length * width;
}

public double area(double base, double height, boolean isTriangle)


{
return 0.5 * base * height;
}

public static void main(String[] args)


{
Shape s = new Shape();
double a = [Link](5);
[Link]("Area of the circle with radius 5 is " + a);
a=[Link](4,6);
[Link]("Area of the rectangle with length 4 and width
6 is " + a);
a=[Link](3,7,true);
[Link]("Area of the triangle with base 3 and height 7
is " + a);

}//main method

}//class

Output:

Signature of the Faculty


II-BTECH I-SEM (CSE)

KHIT AUTONOMOUS 14
OOPS THROUGH JAVA (R23) Regd No:

EXERCISE-3(C) DATE:

Problem Statement: Write a JAVA program to implement constructor.

Source Code:

public class Rectangle


{
private int length;
private int width;
//constructor for initializing the state of the Object
public Rectangle(int length, int width)
{
[Link] = length;
[Link] = width;
}

public int calculateArea()


{
return length * width;
}

public int calculatePerimeter()


{
return 2 * (length + width);
}

public void displayRectangleInfo()


{
[Link]("Length: " + length);
[Link]("Width: " + width);
[Link]("Area: " + calculateArea());
[Link]("Perimeter: " + calculatePerimeter());
}

public static void main(String[] args)


{
Rectangle rectangle1 = new Rectangle(4, 6);
[Link]();
Rectangle rectangle2 = new Rectangle(5, 3);
[Link]();

}//main method

}// class

II-BTECH I-SEM (CSE)

KHIT AUTONOMOUS 15
OOPS THROUGH JAVA (R23) Regd No:

Output:

Signature of the Faculty


II-BTECH I-SEM (CSE)

KHIT AUTONOMOUS 16
OOPS THROUGH JAVA (R23) Regd No:

EXERCISE-3(D) DATE:

Problem Statement: Write a JAVA program to implement constructor


overloading.

Source Code:

public class Book


{
//instance variables
private String title;
private String author;
private double price;

// Constructor with No Arguments


public Book()
{
[Link] = "No Title";
[Link] = "No Author";
[Link] = 0.0;
}

// Constructor with title and author


public Book(String title, String author)
{
[Link] = title;
[Link] = author;
[Link] = 0.0;
}

// Constructor with title, author, and price


public Book(String title, String author, double price)
{
[Link] = title;
[Link] = author;
[Link] = price;
}

public void displayBookInfo()


{
[Link]("Title: " + title);
[Link]("Author: " + author);
[Link]("Price: Rs." + price);
}

II-BTECH I-SEM (CSE)

KHIT AUTONOMOUS 17
OOPS THROUGH JAVA (R23) Regd No:

public static void main(String[] args)


{
Book book1 = new Book();
[Link]();
Book book2 = new Book("1984", "George Orwell");
[Link]();
Book book3 = new Book("To Kill a Mockingbird", "Harper Lee",
149.99);
[Link]();

}//main method

}// class

Output:

Signature of the Faculty

II-BTECH I-SEM (CSE)

KHIT AUTONOMOUS 18
OOPS THROUGH JAVA (R23) Regd No:

EXERCISE-4(A) DATE:

Problem Statement: Write a JAVA program to implement Single

Inheritance

Source Code:

// Parent class
class Vehicle
{
protected String brand;
protected int year;
public Vehicle(String brand, int year)
{
[Link] = brand;
[Link] = year;
}
public void displayVehicleInfo()
{
[Link]("Brand: " + brand);
[Link]("Year: " + year);
}

}//Vehicle

// Child class that extends Vehicle


class Car extends Vehicle
{

private String model;


private double price;

public Car(String brand, int year, String model, double price)


{
// Call the constructor of the parent class
super(brand, year);
[Link] = model;
[Link] = price;
}
public void displayCarInfo()
{
// Call the method from the parent class
[Link]();
[Link]("Model: " + model);
[Link]("Price: $" + price);
}
} //Car

II-BTECH I-SEM (CSE)

KHIT AUTONOMOUS 19
OOPS THROUGH JAVA (R23) Regd No:

// Main class to test the inheritance

public class SingleInheritance


{
public static void main(String[] args)
{
Car car = new Car("Toyota", 2022, "Camry", 24000);
[Link]();
}
}

Output:

Signature of the Faculty

II-BTECH I-SEM (CSE)

KHIT AUTONOMOUS 20
OOPS THROUGH JAVA (R23) Regd No:

EXERCISE-4(B) DATE:

Problem Statement: Write a JAVA program to implement multi level


Inheritance

Source Code:

class Animal
{
protected String name;
public Animal(String name)
{
[Link] = name;
}

public void eat()


{
[Link](name + " the animal is eating.");
}

}// Animal

class Bird extends Animal


{
protected boolean canFly;
public Bird(String name, boolean canFly)
{
super(name); // Call parent class constructor to set name
[Link] = canFly;
}

public void sing()


{
[Link](name + " the bird is singing.");
}

} // Bird
class Parrot extends Bird
{
private String trick;
public Parrot(String name, boolean canFly, String trick)
{
super(name, canFly); // Call to parent class constructor
[Link] = trick;
}

II-BTECH I-SEM (CSE)

KHIT AUTONOMOUS 21
OOPS THROUGH JAVA (R23) Regd No:

void performTrick()
{
[Link]([Link] + " the parrot is performing trick:
" + trick);
}

}// Parrot

public class MultiLevelInheritance


{
public static void main(String[] args)
{
Parrot polly = new Parrot("Polly", true, "Talk like a pirate");
[Link](); // Inherited from Parrot
[Link](); // Inherited from Bird
[Link](); // Inherited from Animal
}
}

Output

Signature of the Faculty


II-BTECH I-SEM (CSE)

KHIT AUTONOMOUS 22
OOPS THROUGH JAVA (R23) Regd No:

EXERCISE-4(C) DATE:

Problem Statement: Write a JAVA program for abstract class to find areas

of different shapes

Source Code:

abstract class Shape


{
public abstract double calculateArea();
}

class Circle extends Shape


{
private double radius;
public Circle(double radius)
{
[Link] = radius;
}

public double calculateArea()


{
return [Link] * radius * radius;
}

} //Circle

class Rectangle extends Shape


{

private double length;


private double width;

public Rectangle(double length, double width)


{
[Link] = length;
[Link] = width;
}

public double calculateArea()


{
return length * width;
}

} // Rectangle

II-BTECH I-SEM (CSE)

KHIT AUTONOMOUS 23
OOPS THROUGH JAVA (R23) Regd No:

class Triangle extends Shape


{

private double base;


private double height;

public Triangle(double base, double height)


{
[Link] = base;
[Link] = height;
}

public double calculateArea()


{
return 0.5 * base * height;
}

} // Triangle

public class AbstractClass


{
public static void main(String[] args)
{
Shape circle = new Circle(3.0);
Shape rectangle = new Rectangle(4.0, 5.0);
Shape triangle = new Triangle(6.0, 4.0);
[Link]("Area of Circle: " + [Link]());
[Link]("Area of Rectangle: " + [Link]());
[Link]("Area of Triangle: " + [Link]());
}
}

Output

Signature of the Faculty


II-BTECH I-SEM (CSE)

KHIT AUTONOMOUS 24
OOPS THROUGH JAVA (R23) Regd No:

EXERCISE-5(A) DATE:
Problem Statement: Write a JAVA program to illustrate the “super” keyword.
Source Code:

class Person
{
String name;

Person(String name)
{
[Link] = name;
}

void message()
{
[Link]("Name of the Person is "+name);
}

}// Person

class Student extends Person


{

String place;

Student(String name,String place)


{
super(name);
[Link]=place;
}

void message()
{
[Link]("Name of the Student is "+[Link]);
[Link]("Place of the Student is "+place);
}

void display()
{
message();
[Link]();
}

}// Student

II-BTECH I-SEM (CSE)

KHIT AUTONOMOUS 25
OOPS THROUGH JAVA (R23) Regd No:

class TestSuper
{
public static void main(String args[])
{
Student s = new Student("Ajay Kumar", "Tenali");
[Link]();
}
}

Output

Signature of the Faculty


II-BTECH I-SEM (CSE)

KHIT AUTONOMOUS 26
OOPS THROUGH JAVA (R23) Regd No:

EXERCISE-5(B) DATE:

Problem Statement: Write a JAVA program to implement Interface. kind of


Inheritance can be achieved?

Source Code:
interface Add_Sub
{
public void add(double x, double y);
public void subtract(double x, double y);
}
interface Mul_Div
{
public void multiply(double x, double y);
public void divide(double x, double y);
}
public class MyCalculator implements Add_Sub, Mul_Div
{
public void add(double x, double y)
{
double result = x + y;
[Link]("Sum is: "+result);
}
public void subtract(double x, double y)
{
double result = x - y;
[Link]("Difference is: "+result);
}
public void multiply(double x, double y)
{
double result = x * y;
[Link]("Product is: "+result);
}
public void divide(double x, double y)
{
double result = x / y;
[Link]("Quotient is: "+result);
}
public static void main(String args[])
{
MyCalculator c = new MyCalculator(); [Link](5, 10);
[Link](35, 15);
[Link](6, 9);
[Link](45, 6);
}//main

}//class

II-BTECH I-SEM (CSE)

KHIT AUTONOMOUS 27
OOPS THROUGH JAVA (R23) Regd No:

Output:

Signature of the Faculty

II-BTECH I-SEM (CSE)

KHIT AUTONOMOUS 28
OOPS THROUGH JAVA (R23) Regd No:

EXERCISE-5(C) DATE:

Problem Statement: Write a JAVA program that implements Run-time


Polymorphism

Source Code:

class A
{
void callme()
{
[Link]("Inside A's callme method");
}
}
class B extends A
{
void callme()
{
[Link]("Inside B's callme method");
}
}
class C extends A
{
void callme()
{
[Link]("Inside C's callme method");
}
}

public class Dispatch


{
public static void main(String args[])
{
A a = new A(); // object of type A
B b = new B(); // object of type B
C c = new C(); // object of type C
A r; // obtain a reference of type A
r = a; // r refers to an A object
[Link](); // calls A's version of callme
r = b; // r refers to a B object
[Link](); // calls B's version of callme
r = c; // r refers to a C object
[Link](); // calls C's version of callme
}
}

II-BTECH I-SEM (CSE)

KHIT AUTONOMOUS 29
OOPS THROUGH JAVA (R23) Regd No:

Output:

Signature of the Faculty


II-BTECH I-SEM (CSE)

KHIT AUTONOMOUS 30
OOPS THROUGH JAVA (R23) Regd No:

EXERCISE-6(A) DATE:

Problem Statement: Write a JAVA program that describes


exception handling mechanism

Source Code:

import [Link].*;
import [Link].*;
public class ExceptionHandling
{
public static void main(String[] args)
{
try
{
Scanner s = new Scanner([Link]);
[Link]("Input the name of the file");
String filename=[Link]();
File newFile = new File(filename);
FileInputStream stream = new FileInputStream(newFile);
[Link]("File Exists");
}
catch (FileNotFoundException e)
{
[Link](e);
}
}//main
}//class

II-BTECH I-SEM (CSE)

KHIT AUTONOMOUS 31
OOPS THROUGH JAVA (R23) Regd No:

Output:

Signature of the Faculty


II-BTECH I-SEM (CSE)

KHIT AUTONOMOUS 32
OOPS THROUGH JAVA (R23) Regd No:

EXERCISE-6(B) DATE:

Problem Statement: Write a JAVA program Illustrating Multiple


catch clauses

Source Code:

public class MultipleCatch


{
public static void main (String args[])
{
int a,b, result=0;
try
{
a = [Link](args[0]);
b = [Link](args[1]);
[Link]("Value of a = "+a+" b= "+b);
result=a/b;
}

catch(ArithmeticException e)
{
[Link] ( "\nDivision by Zero is not Possible");
}

catch(ArrayIndexOutOfBoundsException e)
{
[Link]("Input must include two integers");
}

catch(NumberFormatException e)
{
[Link]("Input Type Mismatch - Input only Integers");
}

[Link]( "\nDivision Result : " + result);


}//main
}//class

Output:

Signature of the Faculty


II-BTECH I-SEM (CSE)

KHIT AUTONOMOUS 33
OOPS THROUGH JAVA (R23) Regd No:

EXERCISE-6(C) DATE:

Problem Statement: Write a JAVA program illustrating the


Java’s Built-in Exceptions

Source Code:
import [Link].*;
public class IllegalArgumentExceptionDemo
{
public static void main(String[] args)
{
int radius;
Scanner S = new Scanner([Link]);
[Link]("Input Radius of the Circle");
radius = [Link]();
try
{
calculateArea(radius);
}

catch (IllegalArgumentException e)
{
[Link]("Illegal Argument Exception
occurred: " + [Link]());
}
}//main

public static void calculateArea(double radius)


{
if (radius < 0)
throw new IllegalArgumentException("Radius cannot be
negative");
double area = [Link] * radius * radius;
[Link]("Area: " + area);

}//calculateArea

}//class

II-BTECH I-SEM (CSE)

KHIT AUTONOMOUS 34
OOPS THROUGH JAVA (R23) Regd No:

Output:

II-BTECH I-SEM (CSE) Signature of the Faculty

KHIT AUTONOMOUS 35
OOPS THROUGH JAVA (R23) Regd No:

EXERCISE-6(D) DATE:

Problem Statement: Write a JAVA program for creation of User


Defined Exception

Source Code:

import [Link].*;
class InvalidAgeException extends Exception
{
public InvalidAgeException(String message)
{
super(message);
}
}

public class UserDefinedException


{
public static void main(String[] args)
{
Scanner s = new Scanner([Link]);
[Link]("Input the age of a Person");
int age=[Link]();
try
{
if (age < 18)
throw new InvalidAgeException("Minimum age is 18 to vote");
else
[Link]("Age is Valid");
}

catch (InvalidAgeException e)
{
[Link]([Link]());
}

}//main

}//class

Output:

II-BTECH I-SEM (CSE)


Signature of the Faculty

KHIT AUTONOMOUS 36
OOPS THROUGH JAVA (R23) Regd No:

EXERCISE-7(A)(i) DATE:

Problem Statement: Write a JAVA program that creates threads by extending


Thread class.

Source Code:

class ThreadA extends Thread


{
public void run()
{
for(int i=1;i<20;i++)
{
try
{
[Link](1000);
}
catch(InterruptedException e)
{
[Link]();
}
[Link]("Thread A Says Good Morning...");
}
}//run()
}//ThreadA

class ThreadB extends Thread


{
public void run()
{
for(int i=1;i<20;i++)
{
try
{
[Link](2000);
}
catch(InterruptedException e)
{
[Link]();
}
[Link]("Thread B Says Hello...");
}
}//run()
}//ThreadB

II-BTECH I-SEM (CSE)

KHIT AUTONOMOUS 37
OOPS THROUGH JAVA (R23) Regd No:

class ThreadC extends Thread


{
public void run()
{
for(int i=1;i<20;i++)
{
try
{
[Link](3000);
}
catch(InterruptedException e)
{
[Link]();
}
[Link]("Thread C Says Welcome...");
}
}//run()

}ThreadC

public class MultiThreading


{
public static void main(String[] args)
{
ThreadA t1 = new ThreadA();
ThreadB t2 = new ThreadB();
ThreadC t3 = new ThreadC();
[Link]();
[Link]();
[Link]();
}//main
}//class

II-BTECH I-SEM (CSE)

KHIT AUTONOMOUS 38
OOPS THROUGH JAVA (R23) Regd No:

Output:

Signature of the Faculty


II-BTECH I-SEM (CSE)

KHIT AUTONOMOUS 39
OOPS THROUGH JAVA (R23) Regd No:

EXERCISE-7(A)(ii) DATE:

Problem Statement: Write a JAVA program that creates threads


by implementing the Runnable Interface

Source Code:

class ThreadA implements Runnable


{
public void run()
{
for(int i=1;i<10;i++)
{
try
{
[Link](1000);
}
catch(InterruptedException e)
{
[Link]();
}
[Link]("Thread A Says Good Morning...");
}
}
}//ThreadA

class ThreadB extends Thread


{
public void run()
{
for(int i=1;i<10;i++)
{
try
{
[Link](2000);
}
catch(InterruptedException e)
{
[Link]();
}
[Link]("Thread B Says Hello...");
}
}
}//ThreadB

II-BTECH I-SEM (CSE)

KHIT AUTONOMOUS 40
OOPS THROUGH JAVA (R23) Regd No:

class ThreadC extends Thread


{
public void run()
{
for(int i=1;i<10;i++)
{
try
{
[Link](3000);
}
catch(InterruptedException e)
{
[Link]();
}
[Link]("Thread C Says Welcome...");
}
}
}//ThreadC

public class MultiThreading


{
public static void main(String [] args)
{
Thread t1 = new Thread(new ThreadA());
Thread t2 = new Thread(new ThreadB());
Thread t3 = new Thread(new ThreadC());
[Link]();
[Link]();
[Link]();
}
}

II-BTECH I-SEM (CSE)

KHIT AUTONOMOUS 41
OOPS THROUGH JAVA (R23) Regd No:

Output:

Signature of the Faculty


II-BTECH I-SEM (CSE)

KHIT AUTONOMOUS 42
OOPS THROUGH JAVA (R23) Regd No:

EXERCISE-7(B) DATE:

Problem Statement: Write a program illustrating isAlive() and


join()

Source Code:

class SampleThread extends Thread


{
public void run()
{
[Link]("Worker thread started...");
for (int i = 0; i < 5; i++)
{
try
{
[Link](1000);
[Link]("Worker: " + i);
}
catch (InterruptedException e)
{
[Link]();
}
}
[Link]("Worker thread finished.");
}

} //SampleThread

public class ThreadJoinExample


{
public static void main(String[] args) throws InterruptedException
{
SampleThread workerThread = new SampleThread();
[Link]("Is worker thread alive before starting? " +
[Link]());
[Link]();
[Link]("Is worker thread alive after starting? " +
[Link]());
[Link]();
[Link]("Is worker thread alive after joining? " +
[Link]());
[Link]("Main thread finished.");

}
}

II-BTECH I-SEM (CSE)

KHIT AUTONOMOUS 43
OOPS THROUGH JAVA (R23) Regd No:

Output:

Signature of the Faculty

II-BTECH I-SEM (CSE)

KHIT AUTONOMOUS 44
OOPS THROUGH JAVA (R23) Regd No:

EXERCISE-7(C) DATE:

Problem Statement: Write a program illustrating Daemon Threads

Source Code:

public class DaemonThread extends Thread


{
public DaemonThread(String name)
{
super(name);
}
public void run()
{
if([Link]().isDaemon())
{
[Link](getName() + " is Daemon thread");
}
else
{
[Link](getName() + " is User thread");
}
} //run

public static void main(String[] args)


{
DaemonThread t1 = new DaemonThread("t1");
DaemonThread t2 = new DaemonThread("t2");
DaemonThread t3 = new DaemonThread("t3");
[Link](true);
[Link]();
[Link]();
[Link](true);
[Link]();
}//main

}//DaemonThread

II-BTECH I-SEM (CSE)

KHIT AUTONOMOUS 45
OOPS THROUGH JAVA (R23) Regd No:

Output:

Signature of the Faculty


II-BTECH I-SEM (CSE)

KHIT AUTONOMOUS 46
OOPS THROUGH JAVA (R23) Regd No:

EXERCISE-7(D) DATE:

Problem Statement: Write a Java Program to implement Producer


Consumer Problem

Source Code:

import [Link];
public class ThreadSynchronization
{
public static void main(String[] args) throws InterruptedException
{
final PC pc = new PC();
Thread t1 = new Thread(new Runnable()
{
public void run()
{
try
{
[Link]();
}
catch (InterruptedException e)
{
[Link]();
}
}
});

Thread t2 = new Thread(new Runnable()


{
public void run()
{
try
{
[Link]();
}
catch (InterruptedException e)
{
[Link]();
}
}
});
[Link]();
[Link]();
[Link]();
[Link]();
}// main

II-BTECH I-SEM (CSE)

KHIT AUTONOMOUS 47
OOPS THROUGH JAVA (R23) Regd No:

public static class ProducerConsumer


{
LinkedList<Integer> list = new LinkedList<>();
int capacity = 2;

public void produce() throws InterruptedException


{
int value = 0;
while (true)
{

synchronized (this)
{
while ([Link]() == capacity)
wait();

[Link]("Producer produced-"+ value);


[Link](value++);
notify();
[Link](1000);
} // synchronized

} //produce ()

public void consume() throws InterruptedException


{
while (true)
{
synchronized (this)
{
while ([Link]() == 0)
wait();

int val = [Link]();


[Link]("Consumer consumed-"+ val);
notify();
[Link](1000);
} // Synchronized

} // While
} //consume()

} // ProducerConsumer

}//ThreadSynchronization

II-BTECH I-SEM (CSE)

KHIT AUTONOMOUS 48
OOPS THROUGH JAVA (R23) Regd No:

Output:

Signature of the Faculty


II-BTECH I-SEM (CSE)

KHIT AUTONOMOUS 49
OOPS THROUGH JAVA (R23) Regd No:

Exercise-8 Date:

Problem Statement: Write a java program that import and use the user defined
packages.

Source code:

//creating the user defined package


package userDefined;

public class Display {

public void show()


{
[Link](" Welcome to Java World!");
}

public static void main(String args[])


{
Display obj = new Display();
[Link]();
}
}

//importing the package "userDefined" in the another class called


//"PackageImportTest"

import [Link];

public class PackageImportTest


{
public static void main(String args[])
{
Display obj = new Display();
[Link]();
}
}

Output:

Signature of the Faculty


II-BTECH I-SEM (CSE)

KHIT AUTONOMOUS 50

You might also like