www.programsinblog.blogspot.com
www.programsinblog.blogspot.com
1. TITLE OF PROGRAM – Program to convert Fahrenheit temperature into Celsius temperature using command line.
class Temp {
public static void main(String ar[]) { float f; double c; f=Float.valueOf(ar[0]).floatValue(); c=(f-32)/1.8; System.out.println("Temp. in Fahrenheit :"+f) ; System.out.println("Temp. in celsius:"+c) ; }
}
Output:
www.programsinblog.blogspot.com
www.programsinblog.blogspot.com
2.TITLE OF PROGRAM – Program to find square root of a number.
import java.lang.Math; class Sqrt {
public static void main(String ar[]) { int l,x; double sq; x=Integer.parseInt(ar[0]); l=ar.length; sq=Math.sqrt(x); System.out.println("No of command line arguments" + l); System.out.println("Sqrt of " + x +" is " + sq); }
}
Output:
www.programsinblog.blogspot.com
www.programsinblog.blogspot.com
3. TITLE OF PROGRAM - Program to find the sum of the digits of a number
import java.lang.Math; class Sumdig {
public static void main(String s[]) { int n,t,a,b,c,d; n=Integer.parseInt(s[0]); a=n/100; b=n%100; c=b/10; d=b%10; t=a+c+d; System.out.println("Sum of digits is"+t); }
}
Output:
www.programsinblog.blogspot.com
www.programsinblog.blogspot.com
4. TITLE OF PROGRAM - Program of that implements the basic functionality of a calculator using switch . import java.io.*; class Calc { public static void main(String arg[]) { int a=0,b=0,c=0,ans,p=0; DataInputStream ins; ins=new DataInputStream(System.in); System.out.println("1:Addition"); System.out.println("2:Subtraction"); System.out.println("3:Multiplication"); System.out.println("4:Division"); System.out.println("5:Modulo"); do {
try { System.out.print("Enter 1st no. "); a=Integer.parseInt(ins.readLine()); System.out.print("Enter 2nd no. "); b=Integer.parseInt(ins.readLine()); System.out.print("Enter ur choice "); c=Integer.parseInt(ins.readLine()); } catch(IOException e) { }
switch(c) { case 1:ans=a+b;
www.programsinblog.blogspot.com
www.programsinblog.blogspot.com
System.out.println("Ans is "+ans); break; case 2:ans=a-b; System.out.println("Ans is "+ans); break; case 3:ans=a*b; System.out.println("Ans is "+ans); break; case 4:ans=a/b; System.out.println("Ans is "+ans); break; case 5:ans=a%b; System.out.println("Ans is "+ans); break; default: System.out.println("Enter valid choice"); }
try { System.out.println("Enter 1 if u want to continue "); p=Integer.parseInt(ins.readLine()); } catch(IOException e) { } }
while(p==1); } }
www.programsinblog.blogspot.com
www.programsinblog.blogspot.com
Output:
www.programsinblog.blogspot.com
www.programsinblog.blogspot.com
5. TITLE OF PROGRAM- Program to print pattern
1
1 1
1 0 1
1 0 0 1
class Floyd2 { 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"); }
} }
www.programsinblog.blogspot.com
www.programsinblog.blogspot.com
Output:
www.programsinblog.blogspot.com
www.programsinblog.blogspot.com
6.TITLE OF PROGRAM- Program to check whether a number is palindrome or not
import java.io.*; class Palindrome { public static void main(String ar[])
{ int n=0,re=0,q; DataInputStream obj= new DataInputStream(System.in); System.out.println("Enter any integer:");
n=Integer.parseInt(obj.readLine());
q=n;
while(q>0) { re=(re*10)+q%10; q=q/10; }
if(n==re) System.out.println("No.is palindrome"); else System.out.println("No.is not palindrome");
} }
www.programsinblog.blogspot.com
www.programsinblog.blogspot.com
OUTPUT:
www.programsinblog.blogspot.com
www.programsinblog.blogspot.com
7.TITLE OF PROGRAM- 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);
} }
www.programsinblog.blogspot.com
www.programsinblog.blogspot.com
OUTPUT:
www.programsinblog.blogspot.com
www.programsinblog.blogspot.com
8.TITLE OF PROGRAM- Program to convert a decimal no into binary, octal, hexadecimal
import java.io.*; class Conversion { public static void main(String s[])throws IOException { int a; DataInputStream ins=new DataInputStream (System.in); System.out.println("Enter value"); a=Integer.parseInt(ins.readLine()); System.out.println("Binary:"+ Integer.toBinaryString(a)); System.out.println("Hexadecimal:"+ Integer.toHexString(a)); System.out.println("Octal:"+ Integer.toOctalString(a)); } }
OUTPUT:
www.programsinblog.blogspot.com
www.programsinblog.blogspot.com
9.TITLE OF PROGRAM- Program to implement overloading of methods
class Student { int age,marks; float fee; void get() { age=23; fee=23451; marks=79; } void get(int a,int b,float c) { age=a; fee=c; marks=b; } void put() { System.out.println("Age is"+age); System.out.println("Fee is"+fee); System.out.println("Marks is"+marks); } } class Movr { public static void main(String s[]) { Student st=new Student(); st.get(); st.put(); st.get(20,83,12345); st.put(); } }
www.programsinblog.blogspot.com
www.programsinblog.blogspot.com
OUTPUT:
www.programsinblog.blogspot.com
www.programsinblog.blogspot.com
10.TITLE OF PROGRAM- Program to calculate area and circumference of circle
class Circle { final float pi=3.147f; float r=0.0f; float area() { return(pi*r*r); } void circ() { System.out.println((2*pi*r)); } } class Circle_main { public static void main(String arg[]) { Circle c1=new Circle(); c1.r=14.7f; System.out.println(c1.area()); c1.circ(); } }
Output:
www.programsinblog.blogspot.com
www.programsinblog.blogspot.com
11.TITLE OF PROGRAM- Program to search an element in an array
import java.io.*; class Srch_arr { public static void main(String arg[])
{ int a[]=new int[10]; DataInputStream ins=new DataInputStream(System.in); int i,n=0,q=0;
try { System.out.print("Enter no. of elements "); n=Integer.parseInt(ins.readLine()); System.out.println("Enter elements ");
for(i=0;i<n;i++) { a[i]=Integer.parseInt(ins.readLine()); } System.out.println("Enter element to be searched "); q=Integer.parseInt(ins.readLine());
} catch(IOException e) { }
for(i=0;i<n;i++) { if(a[i]==q) System.out.println("Element found at "+(i+1)+" position"); }
} }
www.programsinblog.blogspot.com
www.programsinblog.blogspot.com
Output:
www.programsinblog.blogspot.com
www.programsinblog.blogspot.com
12. TITLE OF PROGRAM -Program to add two matrices.
import java.io.*; class Matrixadd { public static void main(String[] args)throws IOException { int i,j; int a[][]=new int [2][2]; int b[][]=new int [2][2]; int c[][]=new int [2][2]; System.out.println("enter elements for 1st matrix"); DataInputStream in=new DataInputStream(System.in); for(i=0;i<=1;i++) { for(j=0;j<=1;j++) { a[i][j]=Integer.parseInt(in.readLine()); } } for(i=0;i<=1;i++) { for(j=0;j<=1;j++) { System.out.print(a[i][j] + ""); } System.out.println(); } System.out.println("enter elements for 2nd matrix"); DataInputStream ins=new DataInputStream(System.in); for(i=0;i<=1;i++) { for(j=0;j<=1;j++) { b[i][j]=Integer.parseInt(ins.readLine()); }
www.programsinblog.blogspot.com
www.programsinblog.blogspot.com
} for(i=0;i<=1;i++) { for(j=0;j<=1;j++) { System.out.print(b[i][j] + " "); } System.out.println(); } for(i=0;i<=1;i++) { for(j=0;j<=1;j++) { c[i][j]=a[i][j]+b[i][j]; } } System.out.println("Result is:"); for(i=0;i<=1;i++) { for(j=0;j<=1;j++) { System.out.print(c[i][j]+" "); } System.out.println(); } } }
www.programsinblog.blogspot.com
www.programsinblog.blogspot.com
Output:
www.programsinblog.blogspot.com
www.programsinblog.blogspot.com
13. TITLE OF PROGRAM -Program to multiply two matrices.
import java.io.*; class Mul { public static void main(String[] args)throws IOException { int i,j,k; int a[][]=new int [2][2]; int b[][]=new int [2][2]; int c[][]=new int [2][2]; System.out.println("enter elements for 1st matrix"); DataInputStream in=new DataInputStream(System.in); for(i=0;i<=1;i++) { for(j=0;j<=1;j++) { a[i][j]=Integer.parseInt(in.readLine()); } } for(i=0;i<=1;i++) { for(j=0;j<=1;j++) { System.out.print(a[i][j] + ""); } System.out.println(); } System.out.println("enter elements for 2nd matrix"); DataInputStream ins=new DataInputStream(System.in); for(i=0;i<=1;i++) { for(j=0;j<=1;j++) { b[i][j]=Integer.parseInt(ins.readLine()); }
www.programsinblog.blogspot.com
www.programsinblog.blogspot.com
} for(i=0;i<=1;i++) { for(j=0;j<=1;j++) { System.out.print(b[i][j] + " "); } System.out.println(); } for(i=0;i<2;i++) { for(j=0;j<2;j++) { for(k=0;k<2;k++) c[i][j]+=a[i][k]*b[k][j]; System.out.print(c[i][j]); } System.out.println(); } }
Output:
www.programsinblog.blogspot.com
www.programsinblog.blogspot.com
14.TITLE OF PROGRAM- Program to illustrate the use of methods of String class
class strings { public static void main(String s[]) { String s1=new String("java is a simple language"); String s2=new String ("Developed by Sun Microsystem"); String s3; s3=s1.toLowerCase(); //convert to lower case System.out.println(s3); s3=s2.toUpperCase(); //convert to upper case System.out.println(s3); s3=s2+s1; //concatenate strings System.out.println(s3); s3=s1.concat(s2); //concatenate strings System.out.println(s3); int n=s1.indexOf('s'); //find index of first occurance of 's' System.out.println("s is present at "+n); int m=s1.indexOf('s',n+1);//find index of second occurance of 's' System.out.println("second s is present at "+m); n=s1.indexOf('p'); System.out.println("p is present at "+n); s3=s1.substring(m); //generate substring starting from location m System.out.println(s3); s3=s1.substring(m,m+5); //generate substring of 5 characters System.out.println(s3); //starting from location m char ch=s1.charAt(3); //find character at index 3 System.out.println("char at 3="+ch); } }
www.programsinblog.blogspot.com
www.programsinblog.blogspot.com
Output:
www.programsinblog.blogspot.com
www.programsinblog.blogspot.com
15.TITLE OF PROGRAM -Program to illustrate the use of methods of StringBuffer class
class Stringb { public static void main(String s[]) { StringBuffer s1=new StringBuffer("java is platform independent language"); System.out.println("original string:"+s1); int len=s1.length(); System.out.println("length of the above string ="+len); String s2=new String(" hello "); s1.insert(12,s2); System.out.println("modified string:"+s1); s1.setCharAt(5,'-'); System.out.println("modified string:"+s1); s1.append(s2); System.out.println("modified string:"+s1); } }
Output:
www.programsinblog.blogspot.com
www.programsinblog.blogspot.com
16. TITLE OF PROGRAM Program to illustrate the use of methods of vector class
import java.util.*; class vectors { public static void main(String s[]) { Vector v=new Vector(3,2); System.out.println("Initial size:"+v.size()); System.out.println("Initial capacity:"+v.capacity()); v.addElement(new Integer(1)); v.addElement(new Integer(2)); v.addElement(new Integer(3)); v.addElement(new Integer(4)); System.out.println("Capacity after four additions:"+v.capacity()); v.addElement(new Double(5.45)); System.out.println("Current Capacity:"+v.capacity()); v.addElement(new Double(6.08)); v.addElement(new Integer(7)); System.out.println("Current Capacity:"+v.capacity()); v.addElement(new Float(9.4)); v.addElement(new Integer(10)); System.out.println("Current Capacity:"+v.capacity()); } }
Output:
www.programsinblog.blogspot.com
www.programsinblog.blogspot.com
17.TITLE OF PROGRAM Program to count total no. of objects created for a class
import java.io.*; class Abc { static int count; abc() { count++; System.out.println("objects"+count); } } class mn { public static void main(String s[]) { abc a1=new abc(); abc a2=new abc(); } }
Output:
www.programsinblog.blogspot.com
www.programsinblog.blogspot.com
18. TITLE OF PROGRAM Program to implement the concept of inheritance
import java.io.*; class A { int x; int y; void get(int p) { x=p; } void show() { System.out.println("value of x is:"+x); } } class B extends A { int b1; void get1(int q) { b1=q; } void show1() { System.out.println("value of b1 is:"+b1); } } class Result1 { public static void main(String args[]) { B a1=new B(); a1.get(9); a1.show();
www.programsinblog.blogspot.com
www.programsinblog.blogspot.com
a1.get1(45); a1.show1(); } } Output:
www.programsinblog.blogspot.com
www.programsinblog.blogspot.com
19.TITLE OF PROGRAM- Program that implements this and super keyword.
import java.io.*; class Sup { int x; sup(int x) { this .x=x; } void display() { System.out.println("Super x ="+x); } } class Sub extends Sup { int y; Sub(int x, int y) { super(x); this.y=y; } void display() { System.out.println("Super x ="+x); System.out.println("Sub y ="+y); } } class ovride { public static void main(String s[]) { Sub s1=new Sub(100,200); s1.display();
www.programsinblog.blogspot.com
www.programsinblog.blogspot.com
} }
Output:
www.programsinblog.blogspot.com
www.programsinblog.blogspot.com
20.TITLE OF PROGRAM- Program that implements the concept of multiple inheritance
interface Student { int r_no; void getn(int n) { r_no=n; } void putn() { System.out.println("R_no is :"+r_no); } } class Test extends Student { float p1,p2; void getmarks(float m1,float m2) { p1=m1; p2=m2; } void putmarks() { System.out.println("Marks obtained"); System.out.println("p1="+p1); System.out.println("p2="+p2); } } interface Sports { float wt=6.0f; void putwt();
www.programsinblog.blogspot.com
www.programsinblog.blogspot.com
} class Results extends Test implements Sports { float total; public void putwt() { System.out.println("Sports wt="+wt); } void display() { total=p1+p2+wt; putn(); putmarks(); putwt(); System.out.println("Total score:"+total); } } class hybrid { public static void main(String s[]) { results s1=new results(); s1.getn(141); s1.getmarks(27.5f,33.0f); s1.display(); } }
www.programsinblog.blogspot.com
www.programsinblog.blogspot.com
21.TITLE OF PROGRAM Program to implement overriding of methods
import java.io.*; class Sup { int x; sup(int x) { this .x=x; } void display() { System.out.println("Super x ="+x); } } class Sub extends sup { int y; Sub(int x, int y) { super(x); this.y=y; } void display() { System.out.println("Super x ="+x); System.out.println("Sub y ="+y); } } class ovride { public static void main(String s[]) { Sub s1=new Sub(100,200);
www.programsinblog.blogspot.com
www.programsinblog.blogspot.com
s1.display(); } } Output
www.programsinblog.blogspot.com
www.programsinblog.blogspot.com
22.TITLE OF PROGRAM Program to implement multithreading.
import java.lang.Thread; class A extends Thread { public void run() { System.out.println("start A"); for(int i=1;i<=5;i++) System.out.println("Thread A i:" +i); System.out.println("Exit A"); } } class B extends Thread { public void run() { System.out.println("Start B"); for(int i=1;i<=5;i++) System.out.println("Thread B i:" +i); System.out.println("Exit B"); } } class C extends Thread { public void run() { System.out.println("start C"); for(int i=1;i<=5;i++) System.out.println("Thread C i:" +i); System.out.println("Exit C"); } }
www.programsinblog.blogspot.com
www.programsinblog.blogspot.com
class Thread_test { public static void main(String s[]) { new A().start(); new B().start(); new C().start(); } } Output
www.programsinblog.blogspot.com
www.programsinblog.blogspot.com
23.TITLE OF PROGRAM Program to implement thread using runnable interface.
import java.lang.Thread; class X implements Runnable { public void run() { System.out.println("start X"); for(int i=1;i<=10;i++) System.out.println("Thread X i:" +i); System.out.println("Exit X"); } } class run_test { public static void main(String s[]) { X runnable=new X(); Thread threadX= new Thread(runnable); threadX.start(); System.out.println("End of main Thread"); } }
www.programsinblog.blogspot.com
www.programsinblog.blogspot.com
24.TITLE OF PROGRAM Program to handle any three in-built exceptions.
class CheckExcept
{
public void add()
{
try
{
int y=20,z=0;
int x=y/z;
System.out.println(x);
System.out.println("working");
}
catch(ArithmeticException a)
{
System.out.println("error");
}
catch(SecurityException a2)
{
System.out.println("error");
}
www.programsinblog.blogspot.com
www.programsinblog.blogspot.com
catch(ArrayStoreException a1)
{
}
catch(Exception x)
{
System.out.println(x.getMessage());
}
finally
{
System.out.println("finaly");
}
}
public static void main(String a[])
{
checkExcept c=new checkExcept();
c.add();
}
}
www.programsinblog.blogspot.com
www.programsinblog.blogspot.com
Output
www.programsinblog.blogspot.com
www.programsinblog.blogspot.com
25.TITLE OF PROGRAM Draw smiling face. import java.awt.*; import java.applet.*; public class smile1 extends Applet { public void paint(Graphics a) { setBackground(Color.yellow); a.setColor(Color.red); a.drawOval(200,60,200,150); a.setColor(Color.black); a.drawOval(240,85,30,30); a.fillOval(250,100,15,15); a.drawOval(340,85,30,30); a.fillOval(350,100,15,15); a.drawOval(280,105,30,30); a.setColor(Color.blue); a.fillArc(280,135,30,15,180,180); } } Html code <html> <head> <title>Draw Smile face</title> </head> <body> <Applet code="smile1.class" width="500" height="60"> </Applet> </body> </html>
www.programsinblog.blogspot.com
www.programsinblog.blogspot.com
www.programsinblog.blogspot.com
www.programsinblog.blogspot.com
26.TITLE OF PROGRAM Draw concentric circles with random colors.
import java.awt.*; import java.applet.*; import java.util.*; public class Cir_c extends Applet { public void paint(Graphics g) { Random rg=new Random(); for (int i=1;i<=3;i++) { int r =rg.nextInt(255); int gr =rg.nextInt(255); int b =rg.nextInt(255); Color c=new Color(r,gr,b); g.setColor(c); g.drawOval(100+i*5,100+i*5,100-i*10,100-i*10); } } } Html code <html> <head> <title>Draw circles</title> </head> <body> <Applet code="Cir_c.class" width="500" height="60"> </Applet> </body> </html>
www.programsinblog.blogspot.com
www.programsinblog.blogspot.com
Output
www.programsinblog.blogspot.com
www.programsinblog.blogspot.com
27.TITLE OF PROGRAM Create an application that represents biodata form.
import java.lang.*;
import java.io.*;
class EmployeeS
{
int age,num;
String nme,add;
void emp_Info(int x)
{
try
{
num=x;
DataInputStream dts=new DataInputStream(System.in);
System.out.println("Enter Your Name:");
nme=dts.readLine();
System.out.println("\nEnter Your Address:");
add=dts.readLine();
System.out.println("\nEnter Your Age:");
age=Integer.parseInt(dts.readLine());
}
catch(Exception e)
{
www.programsinblog.blogspot.com
www.programsinblog.blogspot.com
System.out.println(e.getMessage());
}
}
}
class EmployeeM extends EmployeeS
{
String dep;
int sal;
void emp_dep(int da)
{
try
{
DataInputStream pts=new DataInputStream(System.in);
System.out.println("\nEnter your Department:");
dep=pts.readLine();
System.out.println("\nEnter your Salary:");
sal=Integer.parseInt(pts.readLine());
sal=sal-((sal*da)/100);
}
catch(Exception e)
{
www.programsinblog.blogspot.com
www.programsinblog.blogspot.com
System.out.println(e.getMessage());
}
}
}
class EmployeeF extends EmployeeM
{
void show_info()
{
System.out.println("\nThe name of Employee is:" + nme);
System.out.println("\nThe number of Employee is:" + num);
System.out.println("\nThe address of Employee is:" + add);
System.out.println("\nThe age of Employee is:" + age);
System.out.println("\nThe department of Employee is:" + dep);
System.out.println("\nThe salary of Employee is:" + sal);
}
}
class Main
www.programsinblog.blogspot.com
www.programsinblog.blogspot.com
{
public static void main(String arg[])
{
EmployeeF ep=new EmployeeF();
ep.emp_Info(30);
ep.emp_dep(5);
ep.show_info();
}
}