********************Program 1*********************
public class program1 {
public static void main(String[] args) {
System.out.println("Hello World");
System.out.println("Size of Primitive Data Types in Bits:");
System.out.println("Byte: "+ Byte.SIZE + " bits");
System.out.println("Short: "+ Short.SIZE + " bits");
System.out.println("Integer: "+ Integer.SIZE + " bits");
System.out.println("Long: "+ Long.SIZE + " bits");
System.out.println("Float: "+ Float.SIZE + " bits");
System.out.println("Double: "+ Double.SIZE + " bits");
System.out.println("Character: "+ Character.SIZE + " bits");
System.out.println("Boolean: Size is JVM dependent (typically 8 bits)");
}
}
Output
Hello World
Size of Primitive Data Types in Bits:
Byte: 8 bits
Short: 16 bits
Integer: 32 bits
Long: 64 bits
Float: 32 bits
Double: 64 bits
Character: 16 bits
Boolean: Size is JVM dependent (typically 8 bits)
**************************************************
********************Program 2*********************
public class program2 {
int a = 10;
static int b = 20;
public void display() {
int c = 30;
System.out.println("Instance Variable: "+ a);
System.out.println("Static Variable: "+ b);
System.out.println("Local Variable: "+ c);
}
public static void main(String[] args) {
program2 obj = new program2();
obj.display();
System.out.println("Accessing Static Variable Without Object:"+ program2.b);
}
}
Output
Instance Variable: 10
Static Variable: 20
Local Variable: 30
Accessing Static Variable Without Object:20
**************************************************
********************Program 3*********************
import java.util.Scanner;
public class program3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the first string: ");
String str1 = sc.nextLine();
System.out.println("Length of the first string: "+ str1.length());
System.out.print("Enter the second string: ");
String str2 = sc.nextLine();
String str3 = str1.concat(str2);
System.out.println("Concatenated String: "+ str3);
System.out.print("Enter the starting index for substring: ");
int startIndex = sc.nextInt();
System.out.print("Enter the ending index for substring: ");
int endIndex = sc.nextInt();
if (startIndex >= 0 && endIndex <= str3.length() && startIndex < endIndex) {
String substr = str3.substring(startIndex, endIndex);
System.out.println("Extracted Substring: "+ substr);
} else {
System.out.println("Invalid index values for substring.");
}
sc.close();
}
}
Output
Enter the first string: Skyward
Length of the first string: 7
Enter the second string: Books
Concatenated String: SkywardBooks
Enter the starting index for substring: 7
Enter the ending index for substring: 11
Extracted Substring: Book
**************************************************
********************Program 4*********************
import java.util.Scanner;
public class program4 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter first number: ");
int a = sc.nextInt();
System.out.print("Enter second number: ");
int b = sc.nextInt();
System.out.print("Enter third number: ");
int c = sc.nextInt();
if ( a >= b && a >= c)
System.out.println(a +" is the largest number.");
else if (b >= c)
System.out.println(b +" is the largest number.");
else
System.out.println(c +" is the largest number.");
sc.close();
}
}
Output
Enter first number: 45
Enter second number: 67
Enter third number: 21
67 is the largest number.
**************************************************
********************Program 5*********************
import java.util.Scanner;
public class program5 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter Number: ");
int num = sc.nextInt();
if (num % 2 == 0)
System.out.println("Number is Even");
else
System.out.println("Number is Odd");
Output
Enter Number:
28
Number is Even
**************************************************
********************Program 6*********************
public class program6 {
int i,j;
program6() {
i=10;
j=20;
}
program6(int a) {
i=a;
j=20;
}
program6(int a,int b) {
i=a;
j=b;
}
void display(){
System.out.println(" i = "+i);
System.out.println(" j = "+j);
}
public static void main(String args[]) {
program6 a1 = new program6();
program6 a2 = new program6(11);
program6 a3 = new program6(111,222);
a1.display();
a2.display();
a3.display();
}
}
Output
i = 10
j = 20
i = 11
j = 20
i = 111
j = 222
**************************************************
********************Program 7*********************
class Student {
String name;
int age;
Student(String name, int age) {
this.name = name;
this.age = age;
}
void display() {
System.out.println("Name: " + name + ", Age: " + age);
}
}
public class program7 {
public static void main(String[] args) {
Student[] students = new Student[3];
students[0] = new Student("Virat Kohli", 20);
students[1] = new Student("Abraham Benjamin de Villiers", 22);
students[2] = new Student("Chris Gayle", 24);
for (int i = 0; i < students.length; i++) {
students[i].display();
}
}
}
Output
Name: Virat Kohli, Age: 20
Name: Abraham Benjamin de Villiers, Age: 22
Name: Chris Gayle, Age: 24
**************************************************
********************Program 8*********************
class X {
int i;
void functionX() {
System.out.println(" In Super Class: i = " + i);
}
}
public class program8 extends X {
int j;
void functionprogram8() {
System.out.println(" In Sub Class: j = " + j);
System.out.println(" In Sub Class: i = " + i);
}
public static void main(String args[]) {
program8 y1 = new program8();
y1.j = 20;
y1.i = 30;
y1.functionprogram8();
y1.functionX();
}
}
Output
In Sub Class: j = 20
In Sub Class: i = 30
In Super Class: i = 30
**************************************************
********************Program 9*********************
interface XYZ {
public void functionX();
}
interface MSD {
public void functionM();
}
interface PQR extends XYZ, MSD {
public void functionP();
}
class ABC implements PQR {
public void functionX() {
System.out.println(" void functionX()");
}
public void functionM() {
System.out.println(" void functionM()");
}
public void functionP() {
System.out.println(" void functionP()");
}
}
class program9 {
public static void main(String args[]) {
ABC a1 = new ABC();
a1.functionX();
a1.functionM();
a1.functionP();
}
Output
void functionX()
void functionM()
void functionP()
**************************************************
********************Program 10********************
import java.applet.Applet;
import java.awt.Graphics;
public class program10 extends Applet {
public void init() {
System.out.println("Applet Initialized");
}
public void start() {
System.out.println("Applet Started");
}
public void stop() {
System.out.println("Applet Stopped");
}
public void destroy() {
System.out.println("Applet Destroyed");
}
public void paint(Graphics g) {
g.drawString("Applet Life Cycle Demo", 20, 20);
}
}
Output
**************************************************
********************Program 11********************
import java.util.Scanner;
public class program11 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the value of a: ");
int a = scanner.nextInt();
System.out.print("Enter the value of b: ");
int b = scanner.nextInt();
try {
int result = a / b;
System.out.println("Result: "+ result);
} catch (ArithmeticException e) {
System.out.println("Error: Division by zero is not allowed.");
}
scanner.close();
}
}
Output
Enter the value of a: 20
Enter the value of b: 10
Result: 2
**************************************************
********************Program 12********************
class program12 {
int add(int a, int b) {
return a + b;
}
float add(float a, float b) {
return a + b;
}
int add() {
int a = 5;
int b = 10;
return a + b;
}
public static void main(String[] args) {
program12 obj = new program12();
System.out.println("Sum of 10 and 20 (integers): "+ obj.add(10, 20));
System.out.println("Sum of 5.5 and 2.3 (floats): "+ obj.add(5.5f, 2.3f));
System.out.println("Sum using default values: "+ obj.add());
}
}
Output
Sum of 10 and 20 (integers): 30
Sum of 5.5 and 2.3 (floats): 7.8
Sum using default values: 15
**************************************************
********************Program 13********************
import java.util.Scanner;
class Animal {
void makeSound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
@Override
void makeSound() {
System.out.println("Dog barks"); Output
}
Choose an animal:
}
class Cat extends Animal { 1. Dog
@Override 2. Cat
void makeSound() { Enter your choice (1 or 2):
System.out.println("Cat meows"); 1
} Dog barks
}
public class program13 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Choose an animal:");
System.out.println("1. Dog");
System.out.println("2. Cat");
System.out.println("Enter your choice (1 or 2): ");
int choice = scanner.nextInt();
Animal myAnimal;
if (choice == 1) {
myAnimal = new Dog();
} else if (choice == 2) {
myAnimal = new Cat();
} else {
System.out.println("Invalid choice! Defaulting to Animal.");
myAnimal = new Animal();
}
myAnimal.makeSound();
scanner.close();
}
}
**************************************************
********************Program 14********************
import java.util.Scanner;
public class program14 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the size of the array: ");
int size = scanner.nextInt();
try {
int[] arr = new int[size];
System.out.println("Array of size "+ size +" created successfully.");
} catch (NegativeArraySizeException e) {
System.out.println("Error: Array size cannot be negative.");
scanner.close();
Output
Enter the size of the array: 10
Array of size 10 created successfully.
**************************************************
********************Program 15********************
import java.util.Scanner;
public class program15 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a string (or press Enter to leave it empty): ");
String str = scanner.nextLine();
if (str.isEmpty()) {
str = null;
}
try {
System.out.println("Length of the string: "+ str.length());
} catch (NullPointerException e) {
System.out.println("Error: Entered string is empty or NULL.");
} finally {
System.out.println("Execution completed. Thank you!");
}
scanner.close();
}
}
Output
Enter a string (or press Enter to leave it empty): java programming
Length of the string: 16
Execution completed. Thank you!
**************************************************
********************Program 16********************
package pack1;
public class program16 {
public void packMethod() {
System.out.println("I'm packMethod() in pack1.program16");
}
}
Importing package pack1 to package pack2
package pack2;
import pack1.*;
public class program161 {
public static void main(String args[]) {
program16 obj = new program16();
obj.packMethod();
}
}
Output
**************************************************
********************Program 17********************
import java.util.Scanner;
public class program17 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
int originalNum = num, reverse = 0, remainder;
while (num > 0) {
remainder = num % 10;
reverse = reverse * 10 + remainder;
num /= 10;
}
if (originalNum == reverse)
System.out.println(originalNum +" is a palindrome.");
else
System.out.println(originalNum +" is not a palindrome.");
sc.close();
}
}
Output
Enter a number: 121
121 is a palindrome.
**************************************************
********************Program 18********************
public class program18 {
public static long factorial(int num) {
if (num < 0) {
throw new IllegalArgumentException("Factorial is not defined for negative numbers.");
}
long result = 1;
for (int i = 1; i <= num; i++) {
result *= i;
}
return result;
}
public static void main(String[] args) {
if (args.length == 0) {
System.out.println("Please provide numbers as command-line arguements.");
return;
}
for (String arg : args) {
try {
int number = Integer.parseInt(arg);
long fact = factorial(number);
System.out.println("Factorial of"+ number +" is: "+ fact);
} catch (NumberFormatException e) {
System.out.println("Invalid input: "+ arg +". Please enter a valid integer.");
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
}
}
}
}
Output
C:\java>javac program18.java
C:\java>java -cp C:\java program18 2 5 10
Factorial of2 is: 2
Factorial of5 is: 120
Factorial of10 is: 3628800
**************************************************
********************Program 19********************
import java.util.Scanner;
public class program19 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the lower limit: ");
int lower = sc.nextInt();
System.out.print("Enter the upper limit: ");
int upper = sc.nextInt();
System.out.println("Prime numbers between "+ lower +" and "+ upper +" are :");
for (int num = lower; num <= upper; num++) {
if (isPrime(num)) {
System.out.print(num +" ");
}
}
sc.close();
}
public static boolean isPrime(int n) {
if (n < 2) return false;
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0)
return false;
}
return true;
}
}
Output
Enter the lower limit: 1
Enter the upper limit: 10
Prime numbers between 1 and 10 are :
2357
**************************************************
********************Program 20********************
class MyThread implements Runnable {
public void run() {
System.out.println(" run() started ");
for (int i = 1; i <= 10; i++) {
System.out.println(" The value is : "+ i);
}
System.out.println(" run() completed ");
}
}
public class ThreadRunnable {
public static void main(String args[]) {
System.out.println("Main Thread started");
MyThread obj = new MyThread();
Thread t1 = new Thread(obj, "Thread One");
t1.start();
System.out.println("Main Thread Completed");
}
}
Output
Main Thread started
Main Thread Completed
run() started
The value is : 1
The value is : 2
The value is : 3
The value is : 4
The value is : 5
The value is : 6
The value is : 7
The value is : 8
The value is : 9
The value is : 10
run() completed
**************************************************