Java 3
Java 3
2. Factorial of a number
import java.util.Scanner;
long factorial = 1;
for(int i = 1; i <= number; i++) {
factorial *= i;
}
scanner.close();
}
}
while(temp > 0) {
binary = (temp % 2) + binary;
temp = temp / 2;
}
if(number <= 1) {
isPrime = false;
} else {
for(int i = 2; i <= Math.sqrt(number); i++) {
if(number % i == 0) {
isPrime = false;
break;
}
}
}
if(isPrime) {
System.out.println(number + " is a prime number.");
} else {
System.out.println(number + " is not a prime number.");
}
scanner.close();
}
}
// StringBuffer methods
StringBuffer buffer = new StringBuffer("Hello");
System.out.println("Original StringBuffer: " + buffer);
// setCharAt()
buffer.setCharAt(1, 'a');
System.out.println("After setCharAt(1, 'a'): " + buffer);
// append()
buffer.append(" World");
System.out.println("After append(' World'): " + buffer);
// insert()
buffer.insert(5, " Java");
System.out.println("After insert(5, ' Java'): " + buffer);
// setLength()
buffer.setLength(10);
System.out.println("After setLength(10): " + buffer);
}
}
char c = 'A';
display(c); // Will call display(int) - automatic conversion
from char to int
}
}
// Public access
System.out.println("publicVar = " + demo.publicVar);
demo.publicMethod();
// Final keyword
final int finalInt = 100;
// finalInt = 200; // Uncomment to see compilation error
// Static method
public static void displayStatic() {
System.out.println("Static variable value: " + staticVar);
}
// Instance method
public void displayInstance() {
System.out.println("Accessing static variable from instance method:
" + staticVar);
}
// Boxing/Unboxing in expressions
Integer num1 = 30;
Integer num2 = 40;
int sum = num1 + num2; // Auto-unboxing occurs before addition
import java.util.Scanner;
File: MessageDisplay.java
File: MultiFileProgram.java
package fibonacci;
if(n > 1) {
fibSeries[1] = 1;
return fibSeries;
}
}
File: fibonacci/util/FibonacciDisplay.java
package fibonacci.util;
File: FibonacciDemo.java
import fibonacci.FibonacciGenerator;
import fibonacci.util.FibonacciDisplay;
import java.util.Scanner;
scanner.close();
}
}
package protection;
public class Base {
private int privateVar = 10;
protected int protectedVar = 20;
public int publicVar = 30;
int defaultVar = 40; // package-private (default)
void defaultMethod() {
System.out.println("Default method in Base");
}
File: protection/SubClass.java
package protection;
File: otherpackage/OtherSubClass.java
package otherpackage;
import protection.Base;
public class OtherSubClass extends Base {
public void accessInheritedMembers() {
System.out.println("Accessing inherited members from
OtherSubClass:");
// System.out.println("privateVar = " + privateVar); // Cannot
access
System.out.println("protectedVar = " + protectedVar); // Can access
protected
System.out.println("publicVar = " + publicVar);
// System.out.println("defaultVar = " + defaultVar); // Cannot
access
// privateMethod(); // Cannot access
protectedMethod(); // Can access protected
publicMethod();
// defaultMethod(); // Cannot access
}
}
File: otherpackage/OtherClass.java
package otherpackage;
import protection.Base;
File: ProtectionDemo.java
import protection.Base;
import protection.SubClass;
import otherpackage.OtherSubClass;
import otherpackage.OtherClass;
base.accessBaseMembers();
System.out.println();
subClass.accessInheritedMembers();
System.out.println();
otherSubClass.accessInheritedMembers();
System.out.println();
otherClass.accessBaseMembers();
}
}
try {
int result = divide(a, b);
System.out.println("Result of " + a + " / " + b + " = " +
result);
} catch(ArithmeticException e) {
System.out.println("Error: " + e.getMessage());
System.out.println("Cannot divide by zero!");
}
scanner.close();
}
try {
System.out.println("Inner try block 1 starts");
int result = arr[5] / 0; // ArrayIndexOutOfBoundsException
& ArithmeticException
System.out.println("Result: " + result);
System.out.println("Inner try block 1 ends");
} catch(ArithmeticException e) {
System.out.println("Inner catch block 1: " +
e.getMessage());
}
try {
System.out.println("Inner try block 2 starts");
String str = null;
System.out.println("String length: " + str.length()); //
NullPointerException
System.out.println("Inner try block 2 ends");
} catch(NullPointerException e) {
System.out.println("Inner catch block 2: " +
e.getMessage());
}
class AgeVerifier {
public static void verifyAge(int age) throws InvalidAgeException {
if(age < 0) {
throw new InvalidAgeException("Age cannot be negative");
} else if(age < 18) {
throw new InvalidAgeException("Age should be at least 18");
} else if(age > 120) {
throw new InvalidAgeException("Age seems too high to be
valid");
}
System.out.println("Age verification successful: " + age);
}
}
public class CustomExceptionDemo {
public static void main(String[] args) {
int[] ages = {-5, 16, 25, 150};