You are on page 1of 42

/* Program to convert Fahrenheit temperature into Celsius temperature

using command line. */

import java.io.*;
class convert
{
public static void main (String[] arg)
{
try
{
int i=Integer.parseInt(arg[0]);
int p =(5 *(i-32))/9;
System.out.println("Temperature in Celsius: "+p);
}
catch(Exception e)
{
System.out.println("error");
}
}
}

Output:

1|P ag e Java Practical File


/* Program to find square root of a number.*/

import java.io.*;

class square_root
{
public static void main (String[] arg)
{
try
{
DataInputStream dis1 =new DataInputStream(System.in);
System.out.println("Enter any number: ");
int i=Integer.parseInt(dis1.readLine());
double p =Math.sqrt(i);
System.out.println("Square Root is: "+p);
}

catch(Exception e)
{
System.out.println("error");
}
}

Output:

2|P ag e Java Practical File


/* Program to find the sum of the digits of a number. */
import java.io.*;

class digits_sum
{
public static void main (String[] arg)
{
try
{
DataInputStream dis1 =new DataInputStream(System.in);
System.out.println("Enter any number of 4 digits: ");
int j,k,l,m,g;
String i=dis1.readLine();
j=Integer.parseInt(i.substring(0,1));
k=Integer.parseInt(i.substring(1,2));
l=Integer.parseInt(i.substring(2,3));
m=Integer.parseInt(i.substring(3,4));
g=j+k+l+m;
System.out.println("Sum of digits are: "+g);
}

catch(Exception e)
{
System.out.println("error"+e);
}
}
}

Output:

3|P ag e Java Practical File


/* Program of that implements the basic functionality of a calculator using
switch. */
import java.io.*;
class calculator
{
public static void main(String[] args)
{
int a, b,c;
DataInputStream dis1=new DataInputStream(System.in) ;
System.out.println ("Enter 1st number:");
a = Integer.parseInt(dis1.readLine ());
System.out.println("Enter 2nd number:");
b = Integer.parseInt(dis1.readLine ());
System.out.println ("Press");
System.out.println ("1.Addition");
System.out.println ("2.Subtraction");
System.out.println ("3.Division");
c=Integer.parseInt(dis1.readLine ());

switch (c)
{
case 1:
System.out.println("Sum is:" + (a+b));
break;
case 2:
System.out.println("Minus is:" +(a-b));
break;
case 3:
System.out.println("Division is:" +(a/b));
break;
}
}
}

4|P ag e Java Practical File


Output:

5|P ag e Java Practical File


/* Program to check whether a number is palindrome or not. */
import java.io.*;
class palindrome
{
public static void main(String[] args)
{
try
{
DataInputStream dis = new DataInputStream(System.in);
int num, r, temp, sum=0;
System.out.println("Enter the number:");
num = Integer.parseInt(dis.readLine());

temp = num;

while (num !=0)


{
r = num % 10;
num = num / 10;
sum = sum * 10 + r;
}
if (temp == sum)
System.out.println(temp +" is a palindrome number.");
else
System.out.println(temp +" is not a palindrome number.");
}
catch(Exception e)
{
}
}
Output:

6|P ag e Java Practical File


/* Program to implement overloading of methods. */

class OverloadDemo
{
void test()
{
System.out.println("No parameters");
}

void test(int a)
{
System.out.println("Method with a parameter: " + a+" is called");
}
}

class overload
{
public static void main(String args[])
{
OverloadDemo ob = new OverloadDemo();
ob.test();
ob.test(10);
}
}

Output:

7|P ag e Java Practical File


/* Program to implement overriding of methods. */
class A
{
int i, j;
A(int a, int b)
{
i = a;
j = b;
}
void show()
{
System.out.println("i and j: " + i + " " + j);
}
}

class B extends A
{
int k;
B(int a, int b, int c)
{
super(a, b);
k = c;
}
void show()
{
System.out.println("k: " + k);
}
}
class override
{
public static void main(String args[])
{
B subOb = new B(1, 2, 3);
subOb.show(); // this calls show() in B
}

8|P ag e Java Practical File


}
Output:

9|P ag e Java Practical File


/* Program that implements this and super keyword. */
class A
{
int i, j;
A(int i, int b)
{
this.i = i; // ‘this.i’ points to ‘i’ in class A
j = b;
}
void show()
{
System.out.println("i and j: " + i + " " + j);
}
}

class B extends A
{
int k;
B(int i, int b, int c)
{
super(i, b);
k = c;
}
void show()
{
System.out.println("k: " + k);
}
}
class override
{
public static void main(String args[])
{
B subOb = new B(1, 2, 3);
subOb.show(); // this calls show() in B
}

10 | P a g e Java Practical File


}

Output:

11 | P a g e Java Practical File


/* Program to implement the concept of inheritance. */
class A
{
int x;
int y;
int get(int p, int q)
{
x=p; y=q; return(0);
}
void Show()
{
System.out.println(x+" "+y);
}
}

class inheritance extends A


{
public static void main(String args[])
{
A a = new A();
a.get(5,6);
a.Show();
}

Output:

12 | P a g e Java Practical File


/* Program to search an element in an array */
import java.io.*;
class search
{

public static void main(String arg[])


{
int j=4;
System.out.println("Array contains the command line arguments");
System.out.println("Enter the element to search: ");
DataInputStream dis = new DataInputStream(System.in);
String s=dis.readLine();
for(int i=0;i<arg.length;i++)
{
if (arg[i].contains(s))
{
j=1;
break;
}
else
{
j=0;
}
}
if (j == 1)
{
System.out.println("element found");
}
else
{
System.out.println("element not found");
}
}

13 | P a g e Java Practical File


}

Output:

14 | P a g e Java Practical File


/* Program to illustrate the use of methods of String class. */
class str
{
public static void main(String ar[])
{
String s1=new String("HellO");
String s2=new String("hELLo");
String s3=null;
System.out.println("String 1="+s1+" String 2="+s2);
System.out.println("\n\tConverting String 2 to upper case:
"+s2.toUpperCase());
System.out.println("\n\tConverting String 1 to lower case:
"+s2.toLowerCase());
System.out.println("\n\tChecking for equality: "+s1.equals(s2));
System.out.println("\n\tChecking for equality (by ignoring case):
"+s1.equalsIgnoreCase(s2));
System.out.println("\n\tBefore Trimming: "+s1.length());
System.out.println("\n\tOn Trimming: "+s1.length());
System.out.println("\n\tExtracting Substring: "+s1.substring(1));
System.out.println("\n\tChecking Character at position 3: "+s1.charAt(3));
System.out.println("\n\tOn replacing h with j: "+s1.replace('H','J'));}
}
Output:

15 | P a g e Java Practical File


/* Program to calculate area of rectangle and circumference of circle.*/
class cal
{
float l,b,ar,cir,r;
final float pi=3.147f;
void area(float x, float y)
{
l=x;
b=y;
ar=l*b;
}
void cir(float z)
{
r=z;
cir=2*pi*r;
}
void disp()
{
System.out.println("\nArea of rectangle: "+ar +"\n\nCircumfrance of circle is: "+cir);
}
}
class ract_cir
{
public static void main(String ar[])
{
cal obj=new cal();
obj.area(2,3);
obj.cir(5);
obj.disp();
}
}

16 | P a g e Java Practical File


Output:

17 | P a g e Java Practical File


/* Program that implements the concept of multiple inheritance.*/
interface i
{
int a=10;
void disp();
}
interface i2
{
int x=20;
void disp1();
}
class dc implements i,i2
{
public void disp()
{
System.out.println("\nValue in variable a is: "+a);
}
public void disp1()
{
System.out.println("\nValue in variable x is: "+x);
}
}
class mul_inter
{
public static void main(String ar[])
{
dc d=new dc();
d.disp();
d.disp1();
}
}

18 | P a g e Java Practical File


Output:

19 | P a g e Java Practical File


/* Program to count total no. of objects created for a class.*/

class chck

{
static int count=0;

chck()
{
count=count+1;
}
chck(int n1)
{
count=count+1;
}
void disp()
{
System.out.println("\nNumber of objects created are: "+count);

}
}
class obj_count
{
public static void main(String ar[])
{
chck obj=new chck();
chck obj1=new chck(8);
obj.disp();
}
}

Output:

20 | P a g e Java Practical File


/* Program to convert a decimal no into binary, octal, hexadecimal. */
class conv
{
public static void main(String ar[])
{
int v=Integer.parseInt(ar[0]);
System.out.println("\nOn Binary Conversion: "+Integer.toBinaryString(v));
System.out.println("\nOn Hexadecimal Conversion:
"+Integer.toHexString(v));
System.out.println("\nOn Octal Conversion: "+Integer.toOctalString(v));
}
}

Output:

21 | P a g e Java Practical File


/* Program to implement multithreading. */
class a extends Thread
{
public void run()
{
for (int i=0;i<=5;i++)
{
System.out.println("From Thread A : i = "+i);
}
System.out.println("Exit from A");
}
}

class b extends Thread


{
public void run()
{
for (int j=1;j<=6;j++)
{
System.out.println("From Thread B : j = "+j);
}
System.out.println("Exit from B");
}
}

class mul_th
{
public static void main( String arg[])
{
new a().start();
new b().start();
}
}

22 | P a g e Java Practical File


Output:

23 | P a g e Java Practical File


/* Program to implement thread using runnable interface.*/
class A implements Runnable
{
public void run()
{
for(int i=0;i<=5;i++)
{
System.out.println("i= "+i);
}
}
}
class B implements Runnable
{
public void run()
{
for(int k=0;k<=5;k++)
{
System.out.println("k= "+k);
}
}
}
class mul_rn_th
{
public static void main(String ar[])
{
A o1=new A();
B o2=new B();
Thread t1=new Thread(o1);
Thread t2=new Thread(o2);
t1.start();
t2.start();
}
}

24 | P a g e Java Practical File


Output:

25 | P a g e Java Practical File


/* Program to handle any three in-built exceptions. */
class error_3
{
public static void main(String arg[])
{
int a[]={5,10};
int b=5;
try
{
int x =a[2] / (b-a[1]);
}
catch(ArithmeticException e)
{
System.out.println("ArithmeticException");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("ArrayIndexOutOfBoundsException");
}
catch(ArrayStoreException e)
{
System.out.println("ArrayStoreException");
}

}
}

Output:

26 | P a g e Java Practical File


/* Program to implement throw and throws.*/
import java.lang.Exception;

class paras extends Exception


{
paras(String f)
{
super(f);
}
}

class throw_exe
{
void dis()
{
int x=5, y=1000;
try
{
float z= (float) x/ (float) y;
if(z<0.01)
{
throw new paras("Throw Exception: Number is too small");
}
}
catch(paras e)
{
System.out.println(e.getMessage());
}
}
}

/* PTO */

27 | P a g e Java Practical File


/* continue */

class throws_exe
{
void dis1() throws ArithmeticException
{
int x=2,y=0,z=x/y;
}
}
class test
{
public static void main(String arg[])
{
throw_exe t1=new throw_exe();
t1.dis();
throws_exe t2 = new throws_exe();
try
{
t2.dis1();
}
catch(Exception e)
{
System.out.println("Throws Exception: "+e);
}
}
}

Output:

28 | P a g e Java Practical File


/* Draw smiling face. */
import java.awt.*;
import java.applet.*;

public class face extends Applet


{
public void paint(Graphics g)
{
g.drawOval(40,40,120,150);
g.drawOval(57,75,30,20);
g.drawOval(110,75,30,20);
g.fillOval(121,81,10,10);
g.fillOval(68,81,10,10);
g.drawOval(85,100,30,30);
g.fillArc(60,125,80,40,180,180);
}
}

Output:

29 | P a g e Java Practical File


/* Make an application with 3 checkbox labeled with different color name
and a button. When a color is selected and clicked on the button the color of
polygon will change. */
import java.awt.*;
import java.applet.*;

public class change extends Applet


{
CheckboxGroup radioGroup;
Checkbox radio1;
Checkbox radio2;
Checkbox radio3;
Button btn;
public void init()
{
btn=new Button("Change Color");
add(btn);
radio1 = new Checkbox("Red", radioGroup,false);
radio2 = new Checkbox("Blue", radioGroup,false);
radio3 = new Checkbox("Green", radioGroup,false);
add(radio1);
add(radio2);
add(radio3);
}

public void paint(Graphics g)


{
int x[]= {220,320,420,230};
int y[]={224,420,400,320};
int n=x.length;
g.drawPolygon(x,y,n);
if (radio1.getState()) g.setColor(Color.red);
else if (radio2.getState()) g.setColor(Color.blue);
else g.setColor(Color.green);
g.fillPolygon(x,y,n);

30 | P a g e Java Practical File


}
public boolean action(Event ev, Object ob)
{
if (ev.target instanceof Button)
{

repaint();
}
return true;
}

}
Output:

31 | P a g e Java Practical File


/* Make an application representing a bio-data form.*/

/* First applet to display input fields */


import java.awt.*;
import java.applet.*;

public class inter extends Applet


{

TextField txt1, txt2,txt3,txt4,txt5,txt6,txt7;


Button btn;
CheckboxGroup radioGroup;
// The radio buttons to be selected
Checkbox radio1;
Checkbox radio2;

public void init()


{
txt1=new TextField(20);
txt2=new TextField(20);
txt3=new TextField(20);
txt4=new TextField(20);
txt5=new TextField(20);
String str="Name:";
radioGroup = new CheckboxGroup();
radio1 = new Checkbox("BCA", radioGroup,false);
radio2 = new Checkbox("MCA", radioGroup,true);
add(txt1);
add(txt2);
add(radio1);
add(radio2);
add(txt3);
add(txt4);
add(txt5);
btn = new Button ("Submit");
add(btn);
32 | P a g e Java Practical File
}

public void paint(Graphics g)


{
String first=txt1.getText();
String second=txt2.getText();
String result=first +" "+ second;
g.drawString(result,40,140);
}

public boolean action(Event ev, Object ob)


{
if (ev.target instanceof Button)
{
repaint();
}
return true;
}
}

/* Second applet to display label fields */


import java.awt.*;
import java.applet.*;

public class name extends Applet


{

public void init()


{

public void paint(Graphics g)


{

33 | P a g e Java Practical File


g.drawString("First Name",140,20);
g.drawString("Last Name",140,50);
g.drawString("Course",150,80);
g.drawString("Father's Name",110,110);
g.drawString("E-mail id",140,140);
g.drawString("Contact No",140,165);
}
}

Output:

34 | P a g e Java Practical File


/* Program to print pattern

11

101

1 0 0 1 */
class prnt
{
public static void main(String ar[])
{
int i,j,k=1;
System.out.println("Floyd's triangle");
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
if(j==1 || j==i)
k=1;
else
k=0;
System.out.print(k+" ");
}
System.out.print("\n"); }
}
}
Output:

35 | P a g e Java Practical File


/* Program to print sum of series 1+x+x2+x3+......+xn */

import java.io.*;
import java.lang.Math;
class series1
{
public static void main(String ar[])
{
double i,x=0,n=0,sum=0;
DataInputStream obj= new DataInputStream(System. in);

try
{
System.out.println("Enter the value of x:");
x=Integer.parseInt(obj.readLine());
System.out.println("Enter the value of n:");
n=Integer.parseInt (obj.readLine ());
}

catch (IOException e)
{
}

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


sum=sum+Math.pow(x,i);
System.out.println ("Sum is:"+sum);
}

}
Output:

36 | P a g e Java Practical File


/* Program to adds two matrices. */
import java.io.*;
class add
{
public static void main(String ar[])
{
try
{
int i,j;
int arr1[][]=new int [2][2];
int arr2[][]=new int [2][2];
int arr3[][]=new int [2][2];
DataInputStream d1=new DataInputStream(System.in);
System.out.print("enter elements of first array");
for( i=0;i<2;i++)
{
for( j=0;j<2;j++)
{
arr1[i][j]=Integer.parseInt(d1.readLine());
}
}
System.out.println("first array is");
{
for( i=0;i<2;i++)
{
for( j=0;j<2;j++)
{
System.out.print(" " +arr1[i][j]);
}
System.out.println(" ");
}
}

System.out.println("enter elements of second array");


{

37 | P a g e Java Practical File


for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
arr2[i][j]=Integer.parseInt(d1.readLine());
}
}
}
System.out.println("second array is");
{
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
System.out.print(" " +arr2[i][j]);
}
System.out.println(" ");
}
}

for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
arr3[i][j]=arr1[i][j]+arr2[i][j];
}
}
System.out.println("result of addition of matrix");
{
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
System.out.print(" " +arr3[i][j]);
}

38 | P a g e Java Practical File


System.out.println(" ");
}
}

catch(Exception e)
{
System.out.print("error");
}
}
}

Output:

39 | P a g e Java Practical File


/* Program to multiply two matrices. */
import java.io.*;
class mul
{
public static void main(String ar[])
{
try
{
int i,j,k;
int arr1[][]=new int [2][2];
int arr2[][]=new int [2][2];
int arr3[][]=new int [2][2];
DataInputStream d1=new DataInputStream(System.in);
System.out.print("enter elements of first array");
for( i=0;i<2;i++)
{
for( j=0;j<2;j++)
{
arr1[i][j]=Integer.parseInt(d1.readLine());
}
}
System.out.println("first array is");
{
for( i=0;i<2;i++)
{
for( j=0;j<2;j++)
{
System.out.print(" " +arr1[i][j]);
}
System.out.println(" ");
}
}

System.out.println("enter elements of second array");

40 | P a g e Java Practical File


{
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
arr2[i][j]=Integer.parseInt(d1.readLine());
}
}
}
System.out.println("second array is");
{
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
System.out.print(" " +arr2[i][j]);
}
System.out.println(" ");
}
}

for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
for(k=0;k<2;k++)
{

arr3[i][j]=arr3[i][j]+arr1[i][j]*arr2[j][k];
}
}
}
System.out.println("result of multiplication of matrix");
{
for(i=0;i<2;i++)

41 | P a g e Java Practical File


{
for(j=0;j<2;j++)
{
System.out.print(" " +arr3[i][j]);
}
System.out.println(" ");
}
}

catch(Exception e)
{
System.out.print("error");
}
}
}

Output:

42 | P a g e Java Practical File

You might also like