Posts

Showing posts from June, 2023

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); } }

Write a program in Java to convert number into words and print it.

// Write a program in Java to convert number into words and print it import java.util.*; public class Code6 { public void chk(int n, String ch) { String one[] = { "", " one", " two", " three", " four", " five", " six", " seven", " eight", " nine", " ten", " eleven", " twelve", " thirteen", " fourteen", " fifteen", " sixteen", " seventeen", " eighteen", " nineteen" }; String ten[] = { "", "", " twenty", " thirty", " forty", " fifty", " sixty", " seventy", " eighty", " ninety" }; if (n > 19) { System.out.print(ten[n / 10] + " " + one[n % 10]); ...

Write a program in Java to reverse the digits of a number using while loop

// Write a program in Java to reverse the digits of a number using while loop import java.util.*; public class Code5 { public static void main(String arg[]) { int n, rev = 0; Scanner sc = new Scanner(System.in); System.out.print("Enter any number to Reverse: "); n = sc.nextInt(); while (n != 0) { rev = rev * 10; rev = rev + n % 10; n = n / 10; } System.out.print("Reverse: " + rev); } }

Write a program in Java to find second maximum of n numbers without using arrays

// Write a program in Java to find second maximum of n numbers without using arrays import java.util.*; public class Code4 { public static void main(String arg[]) { int max = 0, secondMax = 0, temp, numbers; Scanner sc = new Scanner(System.in); System.out.println("How many numbers do you want to enter?"); numbers = sc.nextInt(); System.out.println("Enter Numbers"); for (int i = 0; i if (i == 0) { max = sc.nextInt(); } else { temp = sc.nextInt(); if (temp > max) { secondMax = max; max = temp; } else if (temp > secondMax) { secondMax = temp; } } } System.out.println("Second Maximum number is " + secondMax); } }

Write a program in Java to find maximum of three numbers using conditional operator.

// Write a program in Java to find maximum of three numbers using conditional operator import java.util.*; public class Code3 { public static void main(String arg[]) { int max, a, b, c; Scanner sc = new Scanner(System.in); System.out.print("Enter 1st Number: "); a = sc.nextInt(); System.out.print("Enter 2nd Number: "); b = sc.nextInt(); System.out.print("Enter 3rd Number: "); c = sc.nextInt(); max = a > b ? (a > c ? a : c) : (b > c ? b : c); System.out.println("\nMaximum number is " + max); } }

Write a program in Java to generate first n prime numbers.

//Write a program in Java to generate first n prime numbers. public class Code2 { static boolean isPrime(int n) { if (n == 1 || n == 0) return false; for (int i = 2; i if (n % i == 0) return false; } return true; } public static void main(String[] args) { int N = 100; for (int i = 1; i if (isPrime(i)) { System.out.print(i + " "); } } } }

Write a Program to Print "Hello World"

// Write a Program to Print Hello World public class Code1 { public static void main(String[] args) { //simply print hello world System.out.println("Hello World"); } }

Write a Java program to add two integers

//Java program that adds two numbers: import java.util.Scanner; public class codingwithsankar { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter your first number: "); //take input from user int first_number = scanner.nextInt(); System.out.print("Enter the your second number: "); //take input from user int second_number = scanner.nextInt(); //addition of two numbers int sum = first_number + second_number; System.out.println("The sum of " + first_number + " and " + second_number + " is: " + sum); scanner.close(); } }