1) Write a program to find factorial of list of number reading input as
command line argument. class Factorial { public static void main(String[] arg) { int[] num=new int[10]; if(arg.length==0) { System.out.println("No Command Line argument passed."); return; } for(int i=0;i<arg.length;i++) num[i]=Integer.parseInt(arg[i]); for(int i=0;i<arg.length;i++) { int fact=1; for(int j=1;j<=num[i];j++) fact *=j; System.out.println("The factorial of "+arg[i]+" is : "+fact); } } }
Output
C:\ >javac Factorial.java C:\ >java Factorial No Command Line argument passed.
C:\ >java Factorial 5 6 The factorial of 5 is : 120 The factorial of 6 is : 720
2)Write a program to display all prime numbers between two limits. import java.io.*; class Prime { public static void main(String arg[])throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("enter a positive lower limit"); int l=Integer.parseInt(br.readLine()); System.out.println("enter a positive upper limit"); int u=Integer.parseInt(br.readLine()); for(int n=l;n<=u;n++) { byte flag=1; for(int i=2;i<=(int)n/2;i++) { if(n%i==0) { flag=0; break; } } if(flag==1) System.out.print("\t" +n); } } }
Output enter a positive lower limit 2 enter a positive upper limit 20 2 3 5 7 11 13 17 19
3)Write a program to sort list of elements in ascending and descending order and show the exception handling.
Enter the array size: 5 Enter Array elements: Element No. 1: 5 Element No. 2: 3 Element No. 3: 62 Element No. 4: 1 Element No. 5: 52 Before Sorting: 5 3 62 1 52 After Sorting in ascending order: 1 3 5 52 62 After Sorting in descending order: 62 52 5 3 1
4)Write a program to implement Rhombus pattern reading the limit form user.
import java.util.*; class Rhombus { public static void main(String[] arg) { Scanner sc=new Scanner(System.in); System.out.print("Enter the pattern number: "); int pat=sc.nextInt(); System.out.print("Enter the pattern character symbol: "); String s=sc.next(); char c=s.charAt(0); for(int i=1;i<=pat;i++) { for(int j=1;j<=(pat-i);j++) System.out.print(" "); for(int k=1;k<=i;k++) System.out.print(c+" "); System.out.println(); } for(int i=pat-1;i>=0;i--) { for(int j=1;j<=(pat-i);j++) System.out.print(" "); for(int k=1;k<=i;k++) System.out.print(c+" "); System.out.println(); } } } Output
C:\>javac SortOrder.java C:\>java SortOrder
Enter the array size: 5 Enter Array elements: Element No. 1: 5 Element No. 2: 3 Element No. 3: 62 Element No. 4: 1 Element No. 5: 52 Before Sorting: 5 3 62 1 52 After Sorting in ascending order: 1 3 5 52 62 After Sorting in descending order: 62 52 5 3 1
5)Write a program to implement all string operations.
import java.util.*; class StringOperation { public static void main(String[] args) { String first="",second=""; Scanner sc=new Scanner(System.in); System.out.println("String Operation"); System.out.println(); System.out.print("Enter the first Sting: "); first=sc.nextLine(); System.out.print("Enter the second Sting: "); second=sc.nextLine(); System.out.println("The strings are: "+first+" , "+second); System.out.println("The length of the first string is :"+first.length()); System.out.println("The length of the second string is :"+second.length()); System.out.println("The concatenation of first and second string is :"+ first.concat(" "+second)); System.out.println("The first character of " +first+" is: "+first.charAt(0)); System.out.println("The uppercase of " +first+" is: "+first.toUpperCase()); System.out.println("The lowercase of " +first+" is: "+first.toLowerCase()); System.out.print("Enter the occurrence of a character in "+first+" : "); String str=sc.next(); char c=str.charAt(0); System.out.println("The "+c+" occurs at position " + first.indexOf(c)+ " in " + first); System.out.println("The substring of "+first+" starting from index 3 and ending at 6 is: " + first.substring(3,7)); System.out.println("Replacing 'a' with 'o' in "+first+" is: "+first.replace('a','o'));
boolean check=first.equals(second); if(!check) System.out.println(first + " and " + second + " are not same."); else System.out.println(first + " and " + second + " are same."); } }
Enter the first Sting: The World is Beautiful. Enter the second Sting: Learn to enjoy every moment. The strings are: The World is Beautiful. , Learn to enjoy every moment. The length of the first string is :23 The length of the second string is :28 The concatenation of first and second string is :The World is Beautiful. Learn to enjoy every moment. The first character of The World is Beautiful. is: T The uppercase of The World is Beautiful. is: THE WORLD IS BEAUTIFUL. The lowercase of The World is Beautiful. is: the world is beautiful. Enter the occurrence of a character in The World is Beautiful. : e The e occurs at position 2 in The World is Beautiful. The substring of The World is Beautiful. starting from index 3 and ending at 6 is: Wor Replacing 'a' with 'o' in The World is Beautiful. is: The World is Beoutiful. The World is Beautiful. and Learn to enjoy every moment. are not same.
6)Write a program to find area of geometrical figures using method. import java.util.*; class geofig { double area(double r) { return(3.14*r*r); } float area(float s) { return(s*s); } float area(float l,float b) { return(l*b); } double area(double b,double h) { return(0.5*b*h); } }
class geo { public static void main(String arg[]) { Scanner sc =new Scanner(System.in); geofig g = new geofig(); System.out.println("enter double value for radius of circle"); double r =sc.nextDouble(); System.out.println("area of circle="+g.area(r)); System.out.println("enter float value for side of a square"); float s =sc.nextFloat(); System.out.println("area of square="+g.area(s)); System.out.println("enter float value for length and breadth of rectangle"); float l =sc.nextFloat(); float b =sc.nextFloat(); System.out.println("area of rectangle="+g.area(l,b)); System.out.println("enter double value for base & height of triangle"); double b1 =sc.nextDouble(); double h =sc.nextDouble(); System.out.println("area of triangle="+g.area(b1,h)); } }
Output C:>javac geo.java C:\>java geo enter double value for radius of circle 3.5 area of circle=38.465 enter float value for side of a square 3 area of square=9.0 enter float value for length and breadth of rectangle 3 5 area of rectangle=15.0 enter double value for base & height of triangle 2 4 area of triangle=4.0
7)Write a program to implement constructor overloading by passing different number of parameter of different types. import java.util.*; class box { double length,breadth,height; box() { length=0.0; breadth=0.0; height=0.0; } box(double p) { length=p; breadth=p; height=p; } box(double l,double b,double h) { length = l; breadth = b; height = h; } void disp() { System.out.println("length=" +length+ "\t breadth=" +breadth+ "\t height=" +height); } } class ConstOverload { public static void main(String arg[]) { Scanner sc = new Scanner(System.in); box b1 = new box(); System.out.println("default constructor is invoked "); b1.disp(); System.out.println("enter a double value"); double d = sc.nextDouble(); box b2 = new box(d); System.out.println("one parameter constructor is invoked"); b2.disp(); System.out.println("enter length"); double l = sc.nextDouble(); System.out.println("enter breadth"); double b = sc.nextDouble(); System.out.println("enter height"); double h = sc.nextDouble(); box b3 = new box(l,b,h); System.out.println("three parameter constructor is invoked"); b3.disp(); } }
Output C:\>javac ConstOverload.java C:\>java ConstOverload default constructor is invoked length=0.0 breadth=0.0 height=0.0 enter a double value 23.5 one parameter constructor is invoked length=23.5 breadth=23.5 height=23.5 enter length 6 enter breadth 7 enter height 8 three parameter constructor is invoked length=6.0 breadth=7.0 height=8.0
8)Write a program to create student report using applet, read the input using text boxes and display the o/p using buttons. import java.awt.*; import java.applet.*; import java.awt.event.*; public class StudReport extends Applet implements ActionListener { Label lblTitle,lblRegNo,lblName,lblJava,lblSE,lblCA,lblBI,lblSSPD; TextField txtRegNo,txtName,txtJava,txtSE,txtCA,txtBI,txtSSPD; Button cmdReport; int total; float avg;
9)Write a program to calculate bonus for different departments using method overriding.
import java.util.*; abstract class dept { double bp; dept(double bpay) { bp=bpay; } void disp() { System.out.println("basicpay="+bp); } abstract double bonus(); } class sales extends dept { sales(double bpay) { super(bpay); } public double bonus() { return(0.20*bp); } } class marketing extends dept { marketing(double bpay) { super(bpay); } public double bonus() { return(0.30*bp); } } class hr extends dept { hr(double bpay) { super(bpay); } public double bonus() { return(0.50*bp); } } class MethodOverload { public static void main(String arg[]) { Scanner sc=new Scanner(System.in); System.out.println("enter basic pay"); double bp=sc.nextDouble(); sales s=new sales(bp); s.disp(); System.out.println("bonus for sales dept=" +s.bonus()); marketing m=new marketing(bp); m.disp(); System.out.println("bonus for marketing dept=" +m.bonus()); hr h=new hr(bp); h.disp(); System.out.println("bonus for hr dept=" +h.bonus()); } }
Output C:\>javac MethodOverload.java
C:\>java MethodOverload enter basic pay 5000 basicpay=5000.0 bonus for sales dept=1000.0 basicpay=5000.0 bonus for marketing dept=1500.0 basicpay=5000.0 bonus for hr dept=2500.0
10)Write a program to implement thread priorities. class A extends Thread { public void run() { for(int i=1;i<=10;i++) { System.out.println("i="+i); } } } class B extends Thread { public void run() { for(int j=1;j<=10;j++) { System.out.println("j="+j); } } } class C extends Thread { public void run() { for(int k=1;k<=10;k++) { System.out.println("k="+k); } } } class ThreadPrior { public static void main(String arg[]) { A thA=new A(); B thB=new B(); C thC=new C(); thA.setPriority(1); thB.setPriority(3); thC.setPriority(9); thA.start(); thB.start(); thC.start(); System.out.println("End of main"); } }
13)Write a program to implement keyboard events. import java.awt.*; import java.applet.*; import java.awt.event.*; public class KeyBoard extends Applet implements KeyListener { String msg=" "; public void init() { addKeyListener(this); requestFocus(); } public void keyTyped(KeyEvent ke) { msg=ke.getKeyChar(); repaint(); } public void paint(Graphics g) { g.drawString(msg,100,50); } public void keyReleased(KeyEvent ke) { msg="mouseReleased"; repaint(); } public void keyPressed(KeyEvent ke) { int k=ke.getKeyCode(); switch(k) { case KeyEvent.VK_F1:msg="F1"; break; case KeyEvent.VK_F2:msg="F2"; break; case KeyEvent.VK_F3:msg="F3"; break; case KeyEvent.VK_PAGE_UP:msg="Pageup"; break; case KeyEvent.VK_PAGE_DOWN:msg="Pagedown"; break; case KeyEvent.VK_LEFT:msg="Left arrow"; break; case KeyEvent.VK_RIGHT:msg="Right arrow"; break; } repaint(); } }