This document contains 10 assignments for Java programs that cover various Java concepts like command line arguments, string operations, constructors, method overloading, static members, inheritance, interfaces, exceptions, and exception handling. Each assignment provides sample code to demonstrate the concept and expected output.
This document contains 10 assignments for Java programs that cover various Java concepts like command line arguments, string operations, constructors, method overloading, static members, inheritance, interfaces, exceptions, and exception handling. Each assignment provides sample code to demonstrate the concept and expected output.
This document contains 10 assignments for Java programs that cover various Java concepts like command line arguments, string operations, constructors, method overloading, static members, inheritance, interfaces, exceptions, and exception handling. Each assignment provides sample code to demonstrate the concept and expected output.
This document contains 10 assignments for Java programs that cover various Java concepts like command line arguments, string operations, constructors, method overloading, static members, inheritance, interfaces, exceptions, and exception handling. Each assignment provides sample code to demonstrate the concept and expected output.
1 Write a java program that will accept command line and display the same. 2 2 Write a java program which will read a text and count all occurrences of a particular word. 3 3 Write a java program which shows the application of constructors. 4 4 Write a java program which shows the use of methods overloading. 6 5 Write a java program which shows the use of static members. 7 6 Write java programs which explain the concept of single inheritance. 9 7 Write a java program which show the method overriding. 10 8 Write a java program which implement interface. 12 9 Write a java program which use try and catch for exception Handling. 14 10 Write a java program which use multiple catch blocks. 15 2 Assignment No: 1 1. Write a java program that will accept command line and display the same. import java.util.*; class cmdline { public static void main( String args[]) { for(int i=0; i<args.length; i++) System.out.println("args " + i + "= " + args[i]); } } Output: 3 Assignment No: 2 2. Write a java programwhich will read a text and count all occurrences of a particular word. import java.util.Scanner; class wordocc { public static void main(String a[]) { Scanner sc = new Scanner(System.in); System.out.print("Enter any line of text = "); String textline = sc.nextLine(); System.out.print("Enter any word to find = "); String findword = sc.nextLine(); int count = 0, i = 0; while(i<=(textline.length()-findword.length())) { String str = textline.substring(i,i+findword.length()); if(str.equalsIgnoreCase(findword)) count++; i++; } System.out.println("--------------------------------------------"); System.out.println("Number of Occurences of given word = "+count); } } Output: 4 Assignment No: 3 3. Write a java program which shows the application of constructors. class Box { double width; double length; double depth; Box() { width=5; length=7; depth=10; } Box(double w,double l, double d) { width=w; length=l; depth=d; } double volume() { return width*length*depth; } Box(Box b) { width=b.width; length=b.length; depth=b.depth; } } class constr { public static void main(String args[]) { Box b = new Box(); Box b1= new Box(6,9,12); double d=b.volume(); System.out.println("Volume of first box is " + d); Box b2 = new Box(b1); 5 d=b1.volume(); System.out.println("Volume of Second box is " + d); d=b2.volume(); System.out.println("Volume of copied box is " + d); } } Output: 6 Assignment No: 4 4. Write a java program which shows the use of methods overloading. class sample { void sum(int x, int y) { int s= x+y; System.out.println("sum of two:" + s); } void sum(int x, int y, int z) { int s= x+y+z; System.out.println("sum of three:" + s); } } class overloading { public static void main(String args[ ]) { sample s=new sample( ); s.sum(10,20); sample s2=new sample( ); s.sum(20,30,50); } } Output: 7 Assignment No: 5 5. Write a java program which shows the use of static members. import java.util.*; class staticclass { Scanner sc=new Scanner(System.in); static int a; static float c; static String name; void Getstat() { int no; float f; String nm; System.out.println("Enter roll no average and name"); no=sc.nextInt(); f=sc.nextFloat(); nm= sc.next(); a=no; c=f; name = nm; } static void Print() { System.out.println(" Rno:= " + a + " Average:= " + c + " name: = " + name); } } class staticmain { public static void main( String args[]) { staticclass s=new staticclass(); s.Getstat(); s.Print(); } } 8 Output: 9 Assignment No: 6 6. Write java programs which explain the concept of single inheritance. class A { int x; } class B extends A { int y; } class p50 { public static void main (String ar[]) { int s; B Obj = new B (); Obj.x = Integer.parseInt (ar[0]); Obj.y = Integer.parseInt (ar[1]); s = Obj.x + Obj.y; System.out.println ("Addition " + s); } } Output: 10 Assignment No: 7 7. Write a java program which show the method overriding. import java.util.*; class A { int j,i; A(int a, int b) { i=a; j=b; } void show() { System.out.println("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 override1 { public static void main(String ar[]) { B subob = new B(1,2,6); subob.show(); } } 11 Output: 12 Assignment No: 8 8. Write a java program which implement interface. interface Daily { double ds=200; void calcdailysal(int h); } interface Monthly { double ms=20000; void calcmonthlysal(int d); } class Employee implements Daily,Monthly { public void calcdailysal(int h) { System.out.println ("Salary " + h * ds); } public void calcmonthlysal(int d) { System.out.println ("Salary " + d * ms / 30); } } class p67 { public static void main ( String ar [] ) { Employee obj = new Employee(); obj.calcdailysal(10); obj.calcmonthlysal(15); } } 13 Output: 14 Assignment No: 9 9. Write a java program which use try and catch for exception Handling. class p2 { public static void main ( String ar[]) { int a,b,c; a = Integer.parseInt (ar[0]); b = Integer.parseInt (ar[1]); c=0; try { c=a/b; } catch (ArithmeticException ex) { System.out.println ("Error Handling Block"); } System.out.println ("Division " + c); } } Output: 15 Assignment No: 10 10.Write a java program which use multiple catch blocks. class p3 { public static void main ( String ar[]) { int a,b,c; c=0; try { a = Integer.parseInt (ar[0]); b = Integer.parseInt (ar[1]); c=a/b; } catch (ArithmeticException e) { System.out.println (e.toString()); } catch (ArrayIndexOutOfBoundsException e) { System.out.println (e.toString()); } System.out.println ("Division " + c); } } Output: