Javajam PDF

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

JAVA LAB

1. Write a Java program to read the radius of a circle and to


find the area and circumference.
import java.util.Scanner;
import java.lang.*;
class Area
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
float r;
double area,cir;
System.out.println("Enter the radius of Circle::");
r=sc.nextFloat();
area=(3.142*r*r);
cir=(2*3.142*r);
System.out.println("Area of Circle with radius "+r+"
is "+area);
System.out.println("Circumference of Circle with
radius "+r+" is "+cir);

}
}
Output:

Prepared by: Chandrashekhar K, Asst. Prof, Dept. of BCA Page 1


JAVA LAB

2. Write a program to demonstrate String Operators


import java.lang.*;
class StringOperation
{
public static void main(String[] args)
{
String s1="kle bca";
String s2="college";
System.out.println("Length::"+s1.length());
System.out.println("Uppercase::"+s1.toUpperCase());
System.out.println("Concatination::"+s1.concat(s2));
System.out.println("Char AT::"+s1.charAt(2));
System.out.println("Equals::"+s1.equals(s2));
System.out.println("Substring::"+s1.substring(3));
System.out.println("Substring::"+s1.substring(3,6));
System.out.println("Replace::"+s1.replace('c','b'));
System.out.println("Index Of::"+s2.indexOf('l'));
char a[]=s2.toCharArray();
for (int i=0;i<a.length;i++ )
{
System.out.println(a[i]);

}
}
}
Output:

Prepared by: Chandrashekhar K, Asst. Prof, Dept. of BCA Page 2


JAVA LAB

3. Write a Java program to find N prime numbers reading N as


command line argument
class Prime
{
public static void main(String[] args)
{
int n,i,j,count;
n=Integer.parseInt(args[0]);
System.out.println("Prime Series are::");
for (i=2;i<=n ;i++ )
{
count=0;
for(j=1;j<=i;j++)
{
if(i%j==0)
count++;
}
if(count==2)
{
System.out.println(i);
}

}
}
Output

Prepared by: Chandrashekhar K, Asst. Prof, Dept. of BCA Page 3


JAVA LAB

4. Write a program to find a factorial of number reading input


as command line argument
class Fact
{
public static void main(String[] args)
{
int fact=1,n,i;
n=Integer.parseInt(args[0]);
for (i=1;i<=n ;i++ )
{
fact=fact*i;

}
System.out.println("Factorial of "+n+":"+fact);

}
}
Output

Prepared by: Chandrashekhar K, Asst. Prof, Dept. of BCA Page 4


JAVA LAB

5. Write a program to read N numbers and sort them using one-


dimensional arrays
import java.util.*;
class SortOrder
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter the array size: ");
int size=sc.nextInt();
int arr[]=new int[size];
System.out.println("Enter Array elements: ");
for(int i=0;i<size;i++)
{

arr[i]=sc.nextInt();
}
System.out.print("Before Sorting: ");
for(int i=0;i<size;i++)
{
System.out.print(" "+arr[i]);
}

for(int i=0;i<size;i++)
{
int temp;
for(int j=i+1;j<size;j++)
{
if(arr[i]>arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
System.out.println();
System.out.print("After Sorting in ascending order: ");
for(int i=0;i<size;i++)
{
System.out.print(" "+arr[i]);
}
System.out.println();
System.out.print("After Sorting in descending order: ");
for(int i=size-1;i>=0;i--)
{

Prepared by: Chandrashekhar K, Asst. Prof, Dept. of BCA Page 5


JAVA LAB

System.out.print(" "+arr[i]);
}

}
}
Output

Prepared by: Chandrashekhar K, Asst. Prof, Dept. of BCA Page 6


JAVA LAB

6. Write a Java program to illustrate Method Overloading


import java.util.Scanner;
class MethodOverload
{
public double add(double a, double b)
{
return (a+b);
}
public double add(double a, double b, double c)
{
return (a+b+c);
}
}
class TestOverload
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
MethodOverload m=new MethodOverload();
double p,q,r;
System.out.println("Ethe the values::");
p=sc.nextDouble();
q=sc.nextDouble();
r=sc.nextDouble();
System.out.println("Addition of "+p+" and "+q+" is
"+m.add(p,q));
System.out.println("Addition of "+p+" and "+q+" and
"+r+" is "+m.add(p,q,r));

}
}
Output

Prepared by: Chandrashekhar K, Asst. Prof, Dept. of BCA Page 7


JAVA LAB

7. Write a program to demonstrate Single Inheritance.


import java.lang.*;
class Vehicle
{
String name;
int regNo;
double cost;
public Vehicle(String name, int regNo, double cost)
{
this.name=name;
this.regNo=regNo;
this.cost=cost;
}
public void display()
{
System.out.println("Vehicle Information::");
System.out.println("Name::"+ name +" Registration
Number::"+regNo+" Cost::"+cost);
}

}
class Car extends Vehicle
{
int speed;
public Car(String name, int regNo,double cost,int speed)
{
super(name,regNo,cost);
this.speed=speed;
}
public void show()
{
super.display();
System.out.println("Car speed::"+speed);
}
}
class TestCar
{
public static void main(String[] args)
{
Car c=new Car("Maruthi",2022,100000,100);
c.show();
}
}
Output:

Prepared by: Chandrashekhar K, Asst. Prof, Dept. of BCA Page 8


JAVA LAB

8. Write a program to illustrate Constructor Overloading


class StudDetails
{
String name;
int roll;
double per;
public StudDetails()
{
name="ABC";
roll=20;
per=60.00;
}
public StudDetails(String name,int roll, double per)
{
this.name=name;
this.roll=roll;
this.per=per;
}
public void display()
{
System.out.println("Name::"+name+"\n"+"Roll
No::"+roll+"\n"+"Percentage::"+per);
}

}
class TestStudent
{
public static void main(String[] args)
{
StudDetails s1=new StudDetails();
s1.display();
StudDetails s2=new StudDetails("XYZ",30,70.00);
s2.display();
}
}
Output

Prepared by: Chandrashekhar K, Asst. Prof, Dept. of BCA Page 9


JAVA LAB

9. Write a program to illustrate Method Overriding


class Bike
{
public void run()
{
System.out.println("Bike is running...");
}
}
class Honda extends Bike
{
public void run()
{
System.out.println("Honda bike is running....");
}
}
class TestBike
{
public static void main(String[] args)
{
Honda h=new Honda();
h.run();
}
}

Output

Prepared by: Chandrashekhar K, Asst. Prof, Dept. of BCA Page 10


JAVA LAB

10. Write a Java program demonstrating Multithreading


import java.lang.*;
class MyThread extends Thread
{
public void run()
{
try
{
for (int i=1;i<=5 ;i++ )
{
Thread.sleep(1000);
System.out.println("Current
Thread::"+Thread.currentThread().getId()+" "+ i);

}
}
catch(Exception e)
{

}
}
public static void main(String[] args)
{
MyThread m1=new MyThread();
MyThread m2=new MyThread();
m1.start();
m2.start();

}
}
Output

Prepared by: Chandrashekhar K, Asst. Prof, Dept. of BCA Page 11


JAVA LAB

11. Write a Java program demonstrating Exception Handling.


import java.util.Scanner;
class TestExp
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int a,b,result;
System.out.println("Enter the values for a and
b::");
a=sc.nextInt();
b=sc.nextInt();
try
{
result=(a/b);
System.out.println("Division of "+a+ " and
"+b+" is "+result);
}
catch(ArithmeticException e)
{
System.out.println("Dont input the value b as
Zero::"+e.getMessage());

}
}
Output

Prepared by: Chandrashekhar K, Asst. Prof, Dept. of BCA Page 12


JAVA LAB

12. Write a Java program to demonstrate user defined package


program.
package mypack;
public class Addition
{
public double a,b;
public Addition(double a,double b)
{
this.a=a;
this.b=b;
}
public double sum()
{
return (a+b);
}

}
import mypack.Addition;
public class TestPack
{
public static void main(String[] args)
{
Addition a=new Addition(50.25,20.60);
System.out.println("Addition of two
numbers::"+a.sum());
}

}
Output:

Prepared by: Chandrashekhar K, Asst. Prof, Dept. of BCA Page 13


JAVA LAB

13 Write an Applet program to display Geometrical Figures using


objects
import java.applet.*;
import java.awt.*;
public class Geo extends Applet
{
public void paint(Graphics g)
{
g.setColor(Color.GREEN);
g.drawLine(20,20,100,20);
g.drawRect(20,50,90,90);
g.fillRoundRect(130,50,120,70,15,15);
g.setColor(Color.RED);
g.drawOval(20,160,160,100);
g.fillOval(180,160,160,100);
}
}

HTML FILE
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Geometrical Figures</title>
</head>
<body>
<applet code="Geo.class" width="400" height="400"></applet>
</body>
</html>

Output:
javac Geo.java
appletviewer Geo.html

Prepared by: Chandrashekhar K, Asst. Prof, Dept. of BCA Page 14


JAVA LAB

15 Write an Applet program to change the background color


randomly.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class DemoMouse extends Applet implements MouseListener,
MouseMotionListener
{
String msg=" ";
public void init()
{
addMouseListener(this);
addMouseMotionListener(this);
}
public void paint(Graphics g)
{
g.drawString(msg,20,20);
}
public void mousePressed(MouseEvent me)
{
msg=" mouse pressed";
setBackground(Color.BLUE);
repaint();
}
public void mouseClicked(MouseEvent me)
{
msg=" mouse clicked";
setBackground(Color.RED);
repaint();
}
public void mouseEntered(MouseEvent me)
{
msg=" mouse entered";
setBackground(Color.GRAY);
repaint();
}
public void mouseExited(MouseEvent me)
{
msg=" mouse exited";
setBackground(Color.WHITE);
repaint();
}
public void mouseReleased(MouseEvent me)
{
msg=" mouse released";
setBackground(Color.BLACK);
repaint();

Prepared by: Chandrashekhar K, Asst. Prof, Dept. of BCA Page 15


JAVA LAB

}
public void mouseMoved(MouseEvent me)
{
msg=" mouse moved";
repaint();
}
public void mouseDropped(MouseEvent me)
{
msg=" mouse dropped";
repaint();
}
public void mouseDragged(MouseEvent me)
{
msg=" mouse dragged";
repaint();
}
}

HTML FILE:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Geometrical Figures</title>
</head>
<body>
<applet code="DemoMouse.class" width="400" height="400">
</applet>
</body>
</html>

Output:
javac DemoMouse.java
appletviewer DemoMouse.html

Prepared by: Chandrashekhar K, Asst. Prof, Dept. of BCA Page 16


JAVA LAB

15 Write an Applet program to implement Mouse events.


import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class DemoMouse extends Applet implements MouseListener,
MouseMotionListener
{
String msg=" ";
public void init()
{
addMouseListener(this);
addMouseMotionListener(this);
}
public void paint(Graphics g)
{
g.drawString(msg,20,20);
}
public void mousePressed(MouseEvent me)
{
msg=" mouse pressed";
repaint();
}
public void mouseClicked(MouseEvent me)
{
msg=" mouse clicked";
repaint();
}
public void mouseEntered(MouseEvent me)
{
msg=" mouse entered";
repaint();
}
public void mouseExited(MouseEvent me)
{
msg=" mouse exited";
repaint();
}
public void mouseReleased(MouseEvent me)
{
msg=" mouse released";
repaint();
}
public void mouseMoved(MouseEvent me)
{
msg=" mouse moved";
repaint();
}

Prepared by: Chandrashekhar K, Asst. Prof, Dept. of BCA Page 17


JAVA LAB

public void mouseDropped(MouseEvent me)


{
msg=" mouse dropped";
repaint();
}
public void mouseDragged(MouseEvent me)
{
msg=" mouse dragged";
repaint();
}
}

HTML FILE:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Geometrical Figures</title>
</head>
<body>
<applet code="DemoMouse.class" width="400" height="400">
</applet>
</body>
</html>

Output:
javac DemoMouse.java
appletviewer DemoMouse.html

Prepared by: Chandrashekhar K, Asst. Prof, Dept. of BCA Page 18


JAVA LAB

16 Write an Applet program to implement Keyboard events.


import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class DemoKey extends Applet implements KeyListener
{
String msg=" ";
public void init()
{
addKeyListener(this);
requestFocus();
}
public void paint(Graphics g)
{
g.drawString(msg,100,50);
}
public void keyTyped(KeyEvent ke)
{
msg=String.valueOf((ke.getKeyChar()));
repaint();
}

public void keyPressed(KeyEvent ke)


{
msg="Key Pressed";
repaint();
}
public void keyReleased(KeyEvent ke)
{
msg="keyReleased";
repaint();
}
}

Html file:
<html>
<head>
<title>Keyboard Event</title>
</head>
<body>
<applet code="DemoKey.class" width=400 height=300></applet>
</body>
</html>

javac DemoKey.java
appletviewer Key.html

Prepared by: Chandrashekhar K, Asst. Prof, Dept. of BCA Page 19


JAVA LAB

Output:

Prepared by: Chandrashekhar K, Asst. Prof, Dept. of BCA Page 20

You might also like