Java Lab Programs
Java Lab Programs
Java Lab Programs
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();
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());
}
}
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();
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.
}
class Areashape extends Shape
{
void findCircle(double r)
{
double a=3.14*r*r;
System.out.println("Area of Circle = "+a);
}
}
}
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();
}
}
.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();
}
}
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"); }
}
}
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());
}
}
{
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");
}
}
10a) Write a Java Program that creates Thread extending Thread Class
}
}
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();
//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();
}
}
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();
}
}
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