0% found this document useful (0 votes)
38 views5 pages

Java Programs for Class 12 IT-802

The document contains 15 Java programs for Class 12 IT-802, covering various fundamental concepts such as printing 'Hello World', checking even or odd numbers, finding the largest of three numbers, generating a multiplication table, and implementing exception handling. It also includes examples of classes and objects, string manipulation, and threading. Each program is designed to demonstrate specific programming techniques and problem-solving skills in Java.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views5 pages

Java Programs for Class 12 IT-802

The document contains 15 Java programs for Class 12 IT-802, covering various fundamental concepts such as printing 'Hello World', checking even or odd numbers, finding the largest of three numbers, generating a multiplication table, and implementing exception handling. It also includes examples of classes and objects, string manipulation, and threading. Each program is designed to demonstrate specific programming techniques and problem-solving skills in Java.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Class 12 IT-802 Java Programs

Program 1: Hello World


public class HelloWorld {
public static void main(String[] args) {
[Link]("Hello World");
}
}

Program 2: Display Alphabets A to Z


public class Alphabets {
public static void main(String[] args) {
for (char c = 'A'; c <= 'Z'; c++) {
[Link](c + " ");
}
}
}

Program 3: Even or Odd


import [Link];
public class EvenOdd {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();
if (num % 2 == 0) {
[Link](num + " is even.");
} else {
[Link](num + " is odd.");
}
[Link]();
}
}

Program 4: Positive, Negative or Zero


import [Link];
public class NumberCheck {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();
if (num > 0) [Link]("Positive");
else if (num < 0) [Link]("Negative");
else [Link]("Zero");
[Link]();
}
}

Program 5: Largest of Three Numbers


import [Link];
public class LargestOfThree {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter three numbers: ");
int a = [Link](), b = [Link](), c = [Link]();
int largest = (a > b) ? (a > c ? a : c) : (b > c ? b : c);
[Link]("Largest = " + largest);
[Link]();
}
}

Program 6: Multiplication Table


import [Link];
public class Table {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter number: ");
int n = [Link]();
for (int i = 1; i <= 10; i++) {
[Link](n + " x " + i + " = " + (n * i));
}
[Link]();
}
}

Program 7: Sum of First N Natural Numbers


import [Link];
public class SumNatural {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter N: ");
int n = [Link]();
int sum = 0;
for (int i = 1; i <= n; i++) sum += i;
[Link]("Sum = " + sum);
[Link]();
}
}

Program 8: Factorial of a Number


import [Link];
public class Factorial {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int n = [Link]();
long fact = 1;
for (int i = 1; i <= n; i++) fact *= i;
[Link]("Factorial = " + fact);
[Link]();
}
}
Program 9: Fibonacci Series
import [Link];
public class Fibonacci {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter terms: ");
int n = [Link]();
int a = 0, b = 1;
[Link](a + " " + b);
for (int i = 2; i < n; i++) {
int c = a + b;
[Link](" " + c);
a = b; b = c;
}
[Link]();
}
}

Program 10: Find Max and Min in an Array


import [Link];
public class ArrayMaxMin {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter array size: ");
int n = [Link]();
int[] arr = new int[n];
[Link]("Enter elements:");
for (int i = 0; i < n; i++) arr[i] = [Link]();
int max = arr[0], min = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > max) max = arr[i];
if (arr[i] < min) min = arr[i];
}
[Link]("Max = " + max + ", Min = " + min);
[Link]();
}
}

Program 11: String Reverse


import [Link];
public class StringReverse {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a string: ");
String s = [Link]();
String rev = "";
for (int i = [Link]() - 1; i >= 0; i--) rev += [Link](i);
[Link]("Reversed: " + rev);
[Link]();
}
}

Program 12: Class and Object (Student Example)


class Student {
String name;
int roll;
Student(String n, int r) {
name = n; roll = r;
}
void display() {
[Link]("Name: " + name + ", Roll: " + roll);
}
public static void main(String[] args) {
Student s1 = new Student("Amit", 101);
[Link]();
}
}

Program 13: Exception Handling (Division by Zero)


import [Link];
public class DivByZeroDemo {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
try {
[Link]("Enter dividend: ");
int a = [Link]();
[Link]("Enter divisor: ");
int b = [Link]();
int result = a / b;
[Link]("Result = " + result);
} catch (ArithmeticException e) {
[Link]("Error: Division by zero not allowed.");
} finally {
[Link]();
}
}
}

Program 14: Threads Example


public class MyThread extends Thread {
String name;
MyThread(String n) { name = n; }
public void run() {
for (int i = 1; i <= 5; i++) {
[Link](name + " count: " + i);
try { [Link](500); }
catch (InterruptedException e) { }
}
}
public static void main(String[] args) {
MyThread t1 = new MyThread("Thread-1");
MyThread t2 = new MyThread("Thread-2");
[Link](); [Link]();
}
}

Program 15: Wrapper Class Example


public class WrapperDemo {
public static void main(String[] args) {
String s = "123";
int num = [Link](s);
[Link]("String to int: " + num);
Integer obj = num; // Autoboxing
int val = obj; // Unboxing
[Link]("Autoboxed: " + obj + ", Unboxed: " + val);
}
}

Common questions

Powered by AI

The Fibonacci sequence program uses a 'for' loop initialized to iterate from 2 to 'n' (user-defined term number). It starts with variables 'a' and 'b' initialized to 0 and 1. In each iteration, the next term 'c' is calculated as 'a + b', then 'a' is updated to 'b' and 'b' is updated to 'c'. This loop structure and variable reassignment allow the program to sequentially construct the Fibonacci sequence up to 'n' numbers .

The 'Scanner' class is utilized in the Java programs to facilitate user interaction by providing a simple way to read inputs from the console. It allows the programs to accept user input for integers, such as numbers for calculations or strings for processing. By creating a 'Scanner' object and using its methods like 'nextInt()' or 'nextLine()', the programs can become dynamic and interactive, adapting to real-time user input .

The string reversal program uses a 'for' loop to iterate the string from its last character towards the first ('i = s.length() - 1; i >= 0; i--'). With each iteration, characters are appended in reverse order to 'rev', resulting in a new string 'rev' that is the reversed version of the input string. This method efficiently constructs the reversed string without additional data structures .

The 'Hello World' program demonstrates basic Java syntax by introducing essential components of a Java class such as 'public class', 'main' method, and 'System.out.println'. 'public class HelloWorld' defines a class named HelloWorld which is public. The 'public static void main(String[] args)' method is the entry point of any Java application, and 'System.out.println("Hello World")' outputs the string 'Hello World' to the console .

The 'for' loop in computing the sum of natural numbers allows systematic accumulation through sequential iteration. For 'int i = 1; i <= n; i++', each 'i' is added to 'sum', efficiently leveraging iteration to compute the total. This looping construct is crucial for operations involving repeated addition over a defined range. Its computational efficiency lies in the linear traversal from 1 to 'n', making it optimal for summing sequences with minimal overhead .

Exception handling in the division program is demonstrated through the use of 'try' and 'catch' blocks. The 'try' block surrounds code that may throw an exception—in this case, division of two integers. If an 'ArithmeticException' occurs due to division by zero, the 'catch' block outputs a user-friendly error message. This prevents the program from crashing and informs the user of the error, showcasing the importance of handling runtime exceptions gracefully .

Threads enhance Java program functionality by enabling parallel execution of tasks, effectively utilizing CPU resources and improving application performance. The example shows two threads executing concurrently, each looping and pausing using 'Thread.sleep(500)', allowing simultaneous operations. However, challenges such as race conditions, deadlocks, and unpredictable thread scheduling can arise, requiring careful synchronization and resource management to maintain stability and correctness .

The largest of three numbers is determined using nested ternary conditional expressions. The expression '(a > b) ? (a > c ? a : c) : (b > c ? b : c)' first checks if 'a' is greater than 'b'; if true, it then checks if 'a' is greater than 'c', returning 'a' if true, else 'c'. If 'a' is not greater than 'b', it checks if 'b' is greater than 'c', returning 'b' if true, else 'c'. This chain of logic efficiently determines the largest number .

In the number-checking Java program, the conditions used are: if 'num > 0', then it prints 'Positive'; else if 'num < 0', then it prints 'Negative'; otherwise, it prints 'Zero'. These conditions use if-else statements to evaluate the number's value and print the corresponding result .

Autoboxing and unboxing in Java allow automatic conversion between primitive types and their corresponding wrapper class objects, which is crucial for using primitives in Java Collections or APIs that require objects. In the wrapper class example, 'Integer obj = num' demonstrates autoboxing by converting 'int num' to an 'Integer' object. Conversely, 'int val = obj' shows unboxing, converting the 'Integer' back to a primitive 'int'. This feature simplifies coding by reducing manual conversion requirements .

You might also like