You are on page 1of 17

1-a) Write a JAVA program to display default value of all primitive data types of JAVA

class Default
{
byte a;
short b;
int c;
long d;
float e;
double f;
char g;
boolean h;
void display()
{
System.out.println("Default value of byte is"+a);
System.out.println("Default value of short is"+b);
System.out.println("Default value of Integer is"+c);
System.out.println("Default value of Long is"+d);
System.out.println("Default value of float is"+e);
System.out.println("Default value of double is"+f);
System.out.println("Default value of char is"+g);
System.out.println("Default value of boolean is"+h);
}
public static void main(String args[])
{
Default de=new Default();
de.display();
}
}

1-b) Write a JAVA program that display the roots of quadratic equation ax2+bx+c=0.

import java.util.Scanner;
public class Quadraticequation
{
public static void main(String[] args)
{
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
int c = Integer.parseInt(args[2]);
double sq = Math.sqrt(b * b - 4 * a * c);
double root1 = (-b+sq)/(2*a);
double root2 = (-b-sq)/(2*a);
System.out.println("First root is:"+root1);
System.out.println("Second root is:"+root2);
}
}

2a). Write a JAVA program to search for an element in a given list of elements using binary search
mechanism.
import java.util.Scanner;
class Binarysearch
{
public static void main(String args[])
{
int c, first, last, middle, n, search, array[];
boolean status=false;
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of elements");
n = sc.nextInt();
array = new int[n];
System.out.print("\nEnter " + n + " integers");
for (c = 0; c < n; c++)
array[c] = sc.nextInt();

System.out.print("\nEnter value to find");


search = sc.nextInt();
first = 0;
last = n - 1;
middle = (first + last)/2;
while( first <= last )
{
if ( array[middle] < search )
first = middle + 1;
else if ( array[middle] == search )
{
status=true;
break;
}
else
last = middle - 1;
middle = (first + last)/2;
}
if (status=true)
System.out.println(search + " found at location " + (middle + 1) + ".");
else
System.out.println(search + " is not present in the list.\n");
}
}

Output:-
Enter number of elements 5
Enter 5 integers 12 14 15 18 20
Enter value to find 15
15 found at location 3

2b)Write a JAVA program to sort for an element in a given list of elements using bubble sort
import java.util.Scanner;
class BubbleSort
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter No. of Elements: ");
int n=sc.nextInt();
int a[]=new int[n];
System.out.print("Enter "+n+" Elements: ");
for(int i=0;i<n;i++)
a[i]=sc.nextInt();
for(int i=0;i<n;i++)
{
for(int j=1;j<n-i;j++)
{
if(a[j]<a[j-1])
{
int t=a[j];
a[j]=a[j-1];
a[j-1]=t;
}
}
}
System.out.print("Array After Bubble Sort is: ");
for(int i=0;i<n;i++)
System.out.print(" "+a[i]);
}
}

Output:-
Enter No. of Elements: 5
Enter 5 Elements: 9 8 7 6 5 4
Array After Bubble Sort is: 5 6 7 8 9

3a). Write a JAVA program to implement class mechanism. – Create a class, methods and invoke them
inside main method.
import java.util.Scanner;
class Box
{
double height,width,depth;
double volume()
{
return(height*width*depth);
}
}
class Boxdemo
{
public static void main(String args[])
{
Box b=new Box();
b.height=10;
b.width=5;
b.depth=2;
System.out.println("The volume of Box is"+b.volume());
}
}

3b). Write a JAVA program to implement constructor.

import java.util.Scanner;
class Box
{
double height,width,depth;
Box()
{
height=10;
width=5;
depth=2;

}
double volume()
{
return(height*width*depth);
}
}
class Boxdemo1
{
public static void main(String args[])
{
Box b=new Box();

System.out.println("The volume of Box is"+b.volume());


}
}

4a). Write a JAVA program to implement constructor overloading.

//Program on Constructor Overloading


import java.util.Scanner;
class Box
{
double height,width,depth;
//Parameterized Constructor
Box(double h,double w,double d)
{
height=h;
width=w;
depth=d;
}
//Default Constructor
Box()
{
height=40;
width=23;
depth=7;
}
//Method
double volume()
{
return(height*width*depth);
}
}
class Boxdemo2
{
public static void main(String args[])
{
Box b1=new Box(20,14,2);
Box b2=new Box();
System.out.println("volume of the box"+b1.volume());
System.out.println("volume of the box"+b2.volume());
}
}

4b). Write a JAVA program implement method overloading.

//Program on Method overloading


class Test
{
void print()
{
System.out.println("No Parameters");
}
void print(int x)
{
System.out.println("With Parameters");
System.out.println("The value of x is"+x);
}
}
class Methodoverload
{
public static void main(String args[])
{
Test t=new Test();
t.print();
t.print(10);
}
}

5a). Write a JAVA program to implement Single Inheritance


//program on single Inheritance
class A
{
int i;
void showi()
{
System.out.println("The value of i is"+i);
}
}
class B extends A
{
int j;
void showj()
{
System.out.println("The value of j is"+j);
}
void sum()
{
System.out.println("Sum is"+(i+j));
}
}
class Singleinheritance
{
public static void main(String args[])
{
A obj1=new A();
B obj2=new B();
obj1.i=10;
obj2.i=30;
obj2.j=20;
obj1.showi();
obj2.showi();
obj2.showj();
obj2.sum();
}
}

5b). Write a JAVA program to implement multi level Inheritance.

class A
{
int a;
void showa()
{
System.out.println("The value of a is"+a);
}
}
class B extends A
{
int b;
void showb()
{
System.out.println("The value of b is"+b);
}
}
class C extends B
{
int c;
void showc()
{
System.out.println("The value of c is"+c);
System.out.println("Sum is"+(a+b+c));
}
}
class Multilevel
{
public static void main(String[] args)
{
C obj1=new C();
obj1.a=10;
obj1.b=20;
obj1.c=30;
obj1.showa();
obj1.showb();
obj1.showc();
}
}

5c). Write a java program for abstract class to find areas of different shapes.

abstract class Shape


{
abstract void findCircle(double r);
abstract void findTriangle(double b, double h);
abstract void findRectangle(double w, double h);

}
class Areashape extends Shape
{
void findCircle(double r)
{
double a=3.14*r*r;
System.out.println("Area of Circle = "+a);
}

void findTriangle(double b, double h)


{
double a=0.5*b*h;
System.out.println("Area of Triangle = "+a);
}

void findRectangle(double w, double h)


{
double a=w*h;
System.out.println("Area of Rectangle = "+a);
}

public static void main(String[] args)


{
Areashape as=new Areashape();
as.findCircle(4.3);
as.findTriangle(6.1,4.5);
as.findRectangle(5.5,7.2);

}
}

6a)Write a JAVA program to implement Interface

interface Father
{
double HT=6.2;
void height();
}
interface Mother
{
double HT=5.8;
void color();
}
class Child implements Father, Mother
{
public void height()
{
double ht=(Father.HT+Mother.HT)/2;
System.out.println("Child's Height= "+ht);
}
public void color()
{
System.out.println("Child Color= brown");
}
public static void main(String[] args)
{
Child c=new Child();
c.height();
c.color();
}
}

6b) Write a Java Program to implement Super Keyword

.class One

int i=10;
void show()
{
System.out.println("Super Class Method i: "+i);
}
}
class Two extends One
{
int i=20;
void show()
{
System.out.println("Sub Class Method i: "+i);
super.show();
System.out.println("Super Class Variable i: "+super.i);
}
}
class Demo
{
public static void main( String args[] )
{
Two t=new Two();
t.show();
}
}

7a)Write a JAVA program that describes exception handling mechanism

//Program on Exception handling


class Division
{ public static void main(String[] args)
{
try{
System.out.println("WELCOME");
int a=5;
int b=0;
int c=a/b;
System.out.println("The Division is "+c);
}
catch(ArithmeticException ae)
{
System.out.println("Division with zero is not possible");
}
finally{
System.out.println("LOGOUT");
}
}
}

7b).Write a JAVA program Illustrating Multiple catch clauses

//Program on Multiple catches


import java.util.*;
class Division1
{
public static void main(String[] args)
{
try{
System.out.println("WELCOME");
Scanner sc=new Scanner(System.in);
System.out.print("Enter a value: ");
int a=sc.nextInt();
System.out.print("Enter b value: ");
int b=sc.nextInt();
int c=a/b;
System.out.println("The Division is "+c);
}

catch(InputMismatchException ae)
{
System.out.println("Wrong Input");
}
catch(ArithmeticException ae)
{
System.out.println("Division with zero is not possible");

}
finally
{
System.out.println("LOGOUT"); }

}
}

8 a)Write a JAVA program that implements Runtime Polymorphism

class Bank
{
float interest()
{
return 0;
}
}
class SBI extends Bank
{
float interest()
{
return 8.4f;
}
}
class AXIS extends Bank
{
float interest()
{
return 7.3f;
}
}
class Runtimepoly
{
public static void main(String args[])
{
Bank b1=new SBI();
System.out.println("SBI Rate of Interest: "+b1.interest());
Bank b2=new AXIS();
System.out.println("Axis Rate of Interest: "+b2.interest());
}
}

9a) Write a Java Program Illustrating Exception Handling Keywords

//Program on exception handling keywords


class Exception

{
static void fun() throws ArithmeticException
{
int a=12;
System.out.println(a/0);
}
public static void main(String args[])
{
try
{
fun();
}
catch(ArithmeticException e)
{
System.out.println(e);
}
finally
{
System.out.println("Finally block is executed");
}
}

9b) Write a Java Program on Built-in Exceptions

//Program on Built in Exception


class Numberexception
{
public static void main(String args[])
{
try
{
int num=Integer.parseInt("mic");
System.out.println(num);
}
catch(NumberFormatException e)
{
System.out.println("Number Format Exception");
}
}
}

9c) Write a Java Program on User defined Exceptions

10a) Write a Java Program that creates Thread extending Thread Class

//Program on creating Threads


class Mythread extends Thread
{
String name;
int time;
Mythread(String n,int t)
{
name=n;
time=t;
}
public void run()
{
for(int i=0;i<10;i++)
{
System.out.println(name+"is Executing");
try
{
Thread.sleep(time);
}
catch(InterruptedException ie)
{
System.out.println(ie);
}

}
}
public static void main(String args[])
{
Mythread t1=new Mythread("Thread1",1000);
Mythread t2=new Mythread("Thread2",2000);
Mythread t3=new Mythread("Thread3",3000);
t1.start();
t2.start();
t3.start();

10 b) Write a Java Program Illustrating isAlive() and join()

//Program on isAlive()
public class Mythread1 extends Thread
{
public void run()
{
try
{
Thread.sleep(300);
System.out.println("is run() method is Alive"+Thread.currentThread().isAlive());
}
catch(InterruptedException ie)
{
System.out.println(ie);
}

}
public static void main(String args[])
{
Mythread1 t1=new Mythread1();
System.out.println("Before starting thread is Alive"+t1.isAlive());
t1.start();
System.out.println("After starting Thread is Alive"+t1.isAlive());
}
}

//Program on Join()
public class Mythread2 extends Thread
{
String name;
Mythread2(String s)
{
name=s;
}
public void run()
{
for(int i=0;i<=5;i++)
{
System.out.println(name+" "+i);
}
}
public static void main(String args[])
{
Mythread2 t1=new Mythread2("Thread-1");
Mythread2 t2=new Mythread2("Thread-2");
Mythread2 t3=new Mythread2("Thread-3");
t1.start();
try
{
t1.join();
}
catch(InterruptedException ie)
{
System.out.println(ie);
}
t2.start();
t3.start();
}
}

10 c) Write a Java program to illustrate Daemon Threads

class DaemonThread extends Thread


{
public void run()
{
if(Thread.currentThread().isDaemon())
{
System.out.println("daemon thread work");
}
else
{
System.out.println("user thread work");
}
}
public static void main(String[] args)
{
DaemonThread t1=new DaemonThread();
DaemonThread t2=new DaemonThread();
DaemonThread t3=new DaemonThread();
t1.setDaemon(true);
t1.start();
t2.start();
t3.start();
}
}

11) Write a Java Program on Producer Consumer

class Carrier
{
int plate;//will act as a plate-------------------4
Boolean busy=false;//The plate is empty
synchronized void putvalue(int i){
while(busy==true)
{ try{
wait();
}
catch(Exception e){
System.out.println(e);
}
}

plate=i;
System.out.println("Produced value"+plate);
busy=true;
notify();

}
synchronized void getvalue()
{
while(busy==false)
{
try{
wait();
}
catch(Exception e){
System.out.println(e);
}
}
System.out.println("consumed value"+plate);
busy=false;
notify();
}
}
class Producer extends Thread
{
Carrier cr;
Producer(Carrier cr)
{
this.cr=cr;
}
public void run()
{
for(int i=1;i<=10;i++)
cr.putvalue(i);
}
}
class Consumer extends Thread
{
Carrier cr;
Consumer(Carrier cr)
{
this.cr=cr;
}
public void run()
{
for(int i=1;i<=10;i++)
cr.getvalue();
}
}
public class Communicate
{
public static void main(String args[])
{
Carrier cr=new Carrier();//---------------------------------5
Producer p=new Producer(cr);
Consumer c=new Consumer(cr);
p.start();
c.start();
}
}

12 Write a JAVA program to paint like paint brush in applet

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Painting extends Applet implements MouseMotionListener
{
public void init()
{
addMouseMotionListener(this);
setBackground(Color.white);
}
public void mouseDragged(MouseEvent me)
{
Graphics g=getGraphics();
g.setColor(Color.red);
g.fillOval(me.getX(),me.getY(),5,5);
}
public void mouseMoved(MouseEvent me){}
}
javac Painting.java

<html>
<applet code="Painting.class" width=700 height=500 >
</applet> </html>

Painting.html

You might also like