Posts

Write a program in Java to demonstrate the use of 'final' keyword in the field declaration. How it is accessed using the objects.

// Write a program in Java to demonstrate the use of 'final' keyword in the field declaration. How it is accessed using the objects. public class Code13 { final int collageCode = 621; void display() { System.out.println("Collage Code is " + collageCode); } public static void main(String arg[]) { Code13 f = new Code13(); f.display(); } }

Write a program in Java to demonstrate the use of private constructor and also write a method which will count the number of instances created using default constructor only.

// Write a program in Java to demonstrate the use of private constructor and also write a method which will count the number of instances created using default constructor only. public class Code12 { int a, b; static int count; private Code12() { count++; System.out.println("I'm Default Constructor " + count); } private Code12(int a, int b) { this.a = a; this.b = b; } int getNumFirst() { return a; } int getNumSecond() { return b; } public static void main(String arg[]) { Code12 p1 = new Code12(); Code12 p2 = new Code12(); Code12 p3 = new Code12(); Code12 p4 = new Code12(); System.out.println("Instances of Default Constructor : " + count); Code12 p5 = new Code12(count, 5); int x = p5.getNumFirst(); int y = p5.getNumSecond(); int minOutOfTwo = x System.out.println...

Write a program in Java to develop overloaded constructor. Also develop the copy constructor to create a new object with the state of the existing object.

// Write a program in Java to develop overloaded constructor. Also develop the copy constructor to create a new object with the state of the existing object. public class Code11 { int n; Code11() { System.out.println("Default Constructor"); } Code11(int x, int y) { System.out.println("Addition is " + (x + y)); } Code11(int a) { n = a; System.out.println("Number is " + n); int i, j, flag; for (i = 1; i flag = 0; for (j = 2; j if (i % j == 0) { flag++; break; } } if (flag == 0) { System.out.println("Prime is " + i); } } } Code11(Code11 cp, int a) { n = cp.n; int max; max = a > n ? a : n; System.out.println("Maximum number is " + max); ...

Write a program in Java to demonstrate use of this keyword. Check whether this can access the private members of the class or not.

// Write a program in Java to demonstrate use of this keyword. Check whether this can access the private members of the class or not. public class Code10 { private int x, y; void valueGet(int x, int y) { this.x = x; this.y = y; } void display() { int i, j, flag; System.out.println("Prime between " + x + " to " + y); System.out.println("------------------"); for (i = x; i flag = 0; for (j = 2; j if (i % j == 0) { flag = 1; break; } } if (flag == 0) { System.out.println(i); } } } } class ThisDemoClass { public static void main(String a[]) { Code10 T = new Code10(); T.valueGet(5, 20); T.display(); } }

Write a static block which will be executed before main( ) method in a class.

// Write a static block which will be executed before main( ) method in a class. public class Code9 { static { System.out.println("I'm Static Before Main Method"); } public static void main(String arg[]) { System.out.println("I'm Main Method"); } static { System.out.println("I'm Static After Main Method"); } }

Write a program in Java to multiply two matrix.

// Write a program in Java to multiply two matrix import java.util.*; public class Code8 { public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.print("Enter number of rows in A: "); int rowsInA = s.nextInt(); System.out.print("Enter number of columns in A / rows in B: "); int columnsInA = s.nextInt(); System.out.print("Enter number of columns in B: "); int columnsInB = s.nextInt(); int[][] a = new int[rowsInA][columnsInA]; int[][] b = new int[columnsInA][columnsInB]; System.out.println("Enter matrix A"); for (int i = 0; i for (int j = 0; j a[i][j] = s.nextInt(); } } System.out.println("Enter matrix B"); for (int i = 0; i for (int j = 0; j b[i][j] = s.nextInt(); } } int[...

Write programs in Java to use Wrapper class of each primitive data types.

// Write programs in Java to use Wrapper class of each primitive data types public class Code7 { public static void main(String arg[]) { Boolean bool = new Boolean("true"); Byte b = new Byte("1"); Short s = new Short(b); Character c = new Character('c'); Integer i = new Integer(c); Long l = new Long(i); Float f = new Float(l); Double d = new Double(f); System.out.println("" + bool + b + s + c + i + l + f + d); } }