OBJECT ORIENTED PROGRAMMING THROUGH JAVA LAB
1) Write a JAVA program to display default value of all primitive data type of JAVA.
public class Example {
static boolean a;
static int b;
static long c;
public static void main(String[] args) {
[Link]("Default value of boolean Type = " + a);
[Link]("Default value of int Type = " + b);
[Link]("Default value of long Type = " + c);
Output
Default value of boolean Type = false
Default value of int Type = 0
Default value of long Type = 0
------------------------------------------------------------------------------------------------------------------------------------
2) Write a java program that display the roots of a quadratic equation ax2+bx=0. Calculate
the discriminate D and basing on value of D, describe the nature of root.
public class Main {
public static void main(String[] args) {
// value a, b, and c
double a = 2.3, b = 4, c = 5.6;
double root1, root2;
// calculate the determinant (b2 - 4ac)
double determinant = b * b - 4 * a * c;
// check if determinant is greater than 0
if (determinant > 0) {
// two real and distinct roots
root1 = (-b + [Link](determinant)) / (2 * a);
root2 = (-b - [Link](determinant)) / (2 * a);
[Link]("root1 = %.2f and root2 = %.2f", root1, root2);
}
// check if determinant is equal to 0
else if (determinant == 0) {
// two real and equal roots
// determinant is equal to 0
// so -b + 0 == -b
root1 = root2 = -b / (2 * a);
[Link]("root1 = root2 = %.2f;", root1);
}
// if determinant is less than zero
else {
// roots are complex number and distinct
double real = -b / (2 * a);
double imaginary = [Link](-determinant) / (2 * a);
[Link]("root1 = %.2f+%.2fi", real, imaginary);
[Link]("\nroot2 = %.2f-%.2fi", real, imaginary);
}
}
}
Output
root1 = -0.87+1.30i and root2 = -0.87-1.30i
3) Write a JAVA program to search for an element in a given list of elements using binary
search mechanism.
class BinarySearchExample{
public static void binarySearch(int arr[], int first, int last, int key){
int mid = (first + last)/2;
while( first <= last ){
if ( arr[mid] < key ){
first = mid + 1;
}else if ( arr[mid] == key ){
[Link]("Element is found at index: " + mid);
break;
}else{
last = mid - 1;
mid = (first + last)/2;
if ( first > last ){
[Link]("Element is not found!");
public static void main(String args[]){
int arr[] = {10,20,30,40,50};
int key = 30;
int last=[Link]-1;
binarySearch(arr,0,last,key);
Output:
Element is found at index: 2
4) Write a JAVA program to sort for an element in a given list of elements using bubble
sort.
class BubbleSort {
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]) {
// swap temp and arr[i]
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
// Prints the array
void printArray(int arr[])
{
int n = [Link];
for (int i = 0; i < n; ++i)
[Link](arr[i] + " ");
[Link]();
}
// Driver method to test above
public static void main(String args[])
{
BubbleSort ob = new BubbleSort();
int arr[] = { 64, 34, 25, 12, 22, 11, 90 };
[Link](arr);
[Link]("Sorted array");
[Link](arr);
}
}
Output
Sorted array
11 12 22 25 34 64 90
5) Write a JAVA program using StringBuffer to delete, remove character.
public class StringBufferDeleteExample1 {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("javatpoint");
[Link]("string1: " + sb);
// deleting the substring from index 2 to 6
sb = [Link](2,6);
[Link]("After deleting: " + sb);
sb = new StringBuffer("let us learn java");
[Link]("string2: " + sb);
// deleting the substring from index 0 to 7
sb = [Link](0, 7);
[Link]("After deleting: " + sb);
Output:
string1: javatpoint
After deleting: jaoint
string2: let us learn java
After deleting: learn java
6) Write a JAVA program implement method overloading.
public class Sum {
// Overloaded sum(). This sum takes two int parameters
public int sum(int x, int y) { return (x + y); }
// Overloaded sum(). This sum takes three int parameters
public int sum(int x, int y, int z)
{
return (x + y + z);
}
// Overloaded sum(). This sum takes two double
// parameters
public double sum(double x, double y)
{
return (x + y);
}
// Driver code
public static void main(String args[])
{
Sum s = new Sum();
[Link]([Link](10, 20));
[Link]([Link](10, 20, 30));
[Link]([Link](10.5, 20.5));
}
}
Output
30
60
31.0
7) Write a JAVA program to implement multi level Inheritance
class Shape {
public void display() {
[Link]("Inside display");
}
}
class Rectangle extends Shape {
public void area() {
[Link]("Inside area");
}
}
class Cube extends Rectangle {
public void volume() {
[Link]("Inside volume");
}
}
public class Tester {
public static void main(String[] arguments) {
Cube cube = new Cube();
[Link]();
[Link]();
[Link]();
}
}
Output
Inside display
Inside area
Inside volume
8) Write a JAVA program give example for “super” keyword.
class Animal{
String color="white";
class Dog extends Animal{
String color="black";
void printColor(){
[Link](color);//prints color of Dog class
[Link]([Link]);//prints color of Animal class
class TestSuper1{
public static void main(String args[]){
Dog d=new Dog();
[Link]();
Output:
black
white
9) Write a JAVA program that describes exception handling mechanism
public class JavaExceptionExample{
public static void main(String args[]){
try{
//code that may raise exception
int data=100/0;
}catch(ArithmeticException e){[Link](e);}
//rest code of the program
[Link]("rest of the code...");
Output:
Exception in thread main [Link]:/ by zero
rest of the code...
10) Write a JAVA program Producer Consumer Problem.
import [Link];
public class Threadexample {
public static void main(String[] args)
throws InterruptedException
{
// Object of a class that has both produce()
// and consume() methods
final PC pc = new PC();
// Create producer thread
Thread t1 = new Thread(new Runnable() {
@Override
public void run()
{
try {
[Link]();
}
catch (InterruptedException e) {
[Link]();
}
}
});
// Create consumer thread
Thread t2 = new Thread(new Runnable() {
@Override
public void run()
{
try {
[Link]();
}
catch (InterruptedException e) {
[Link]();
}
}
});
// Start both threads
[Link]();
[Link]();
// t1 finishes before t2
[Link]();
[Link]();
}
// This class has a list, producer (adds items to list
// and consumer (removes items).
public static class PC {
// Create a list shared by producer and consumer
// Size of list is 2.
LinkedList<Integer> list = new LinkedList<>();
int capacity = 2;
// Function called by producer thread
public void produce() throws InterruptedException
{
int value = 0;
while (true) {
synchronized (this)
{
// producer thread waits while list
// is full
while ([Link]() == capacity)
wait();
[Link]("Producer produced-"
+ value);
// to insert the jobs in the list
[Link](value++);
// notifies the consumer thread that
// now it can start consuming
notify();
// makes the working of program easier
// to understand
[Link](1000);
}
}
}
// Function called by consumer thread
public void consume() throws InterruptedException
{
while (true) {
synchronized (this)
{
// consumer thread waits while list
// is empty
while ([Link]() == 0)
wait();
// to retrieve the first job in the list
int val = [Link]();
[Link]("Consumer consumed-"
+ val);
// Wake up producer thread
notify();
// and sleep
[Link](1000);
}
}
}
}
}
Output:
Producer produced-0
Producer produced-1
Consumer consumed-0
Consumer consumed-1
Producer produced-2
11) Write a program illustrating is Alive and join ()
public class MyThread extends Thread
{
public void run()
{
[Link]("r1 ");
try {
[Link](500);
}
catch(InterruptedException ie)
{
// do something
}
[Link]("r2 ");
}
public static void main(String[] args)
{
MyThread t1=new MyThread();
MyThread t2=new MyThread();
[Link]();
[Link]();
[Link]([Link]());
[Link]([Link]());
}
}
Output:
---------------------------------------------------------------------------------------------------
12) Write a JAVA program Illustrating Multiple catch clauses.
public class MultipleCatchBlock1
{
public static void main(String[] args)
{
try{
int a[]=new int[5];
a[5]=30/0;
}
catch(ArithmeticException e)
{
[Link]("Arithmetic Exception occurs");
}
catch(ArrayIndexOutOfBoundsException e)
{
[Link]("ArrayIndexOutOfBounds Exception occurs");
}
catch(Exception e)
{
[Link]("Parent Exception occurs");
}
[Link]("rest of the code");
}
}
Output:
Arithmetic Exception occurs
rest of the code
----------------------------------------------------------------------------------------------------------