1. WAP to demonstrate switch case, if, if-else, and for loop.
import java.util.Scanner;
public class Switch {
public static void main(String[] args) {
Scanner Src=new Scanner(System.in);
System.out.println("1.Add/Even");
System.out.println("2.Calculator");
System.out.println("3.Print Table");
System.out.println("4.Exit");
System.out.println("Enter your choice:");
int choice=Src.nextInt();
switch (choice) {
case 1:
System.out.println("Enter Number:");
int n1=Src.nextInt();
if(n1%2==0){
System.out.println(n1+" is even");
else
System.out.println(n1+" is odd");
break;
case 2:
System.out.println("1. Add");
System.out.println("2. Sub");
System.out.println("3. Multiply");
System.out.println("4. Divide");
System.out.println("Enter your choice: ");
int ch=Src.nextInt();
System.out.println("Enter two numbers: ");
int num1=Src.nextInt();
int num2=Src.nextInt();
switch (ch) {
case 1:
System.out.println(num1+num2);
break;
case 2:
System.out.println(num1-num2);
break;
case 3:
System.out.println(num1*num2);
break;
case 4:
System.out.println(num1/num2);
break;
case 3:
System.out.println("Enter a number for multiplication table:");
int n=Src.nextInt();
System.out.println("Multiplication of Table of "+n+":");
for(int i=1;i<=10;i++)
{
System.out.println(n+ " X " +i+ " = "+(n*i));
case 4:
System.out.println("Exit the Program");
default:
System.out.println("Thank you");
break;
2. WAP to demonstrate the working of methods.
public class Method {
public static void greets(String Name){
System.out.println("Hello "+Name);
public static int sum(int a, int b){
System.out.println("Sum is "+(a+b));
return a+b;
public static int cal_fact(int n){
if(n==1||n==0){
return 1;
}
else
return n*cal_fact(n-1);
public static void main(String[] args) {
greets("Manyata");
sum(5, 10);
int fact=cal_fact(5);
System.out.println("Factorial is "+ fact);
3. WAP with has four methods - add(), sub(), multiply(), and divide() and demonstrate a simple
constant calculator.
import java.util.Scanner;
public class Calculator {
public static int add(int a,int b){
return a+b;
public static int sub(int a,int b){
return a-b;
public static int mul(int a,int b){
return a*b;
public static int divide(int a,int b){
if(b!=0){
return a/b;
else{
System.out.println("Error: Division by Zero");
return 0;
public static void main(String[] args) {
Scanner Src=new Scanner(System.in);
System.out.println("Simple Calculator");
System.out.println("1. Add");
System.out.println("2. Sub");
System.out.println("3. Multiply");
System.out.println("4. Divide");
System.out.println("Choose Operation: ");
int op=Src.nextInt();
System.out.println("Enter two numbers: ");
int num1=Src.nextInt();
int num2=Src.nextInt();
switch (op) {
case 1:
System.out.println(num1+num2);
break;
case 2:
System.out.println(num1-num2);
break;
case 3:
System.out.println(num1*num2);
break;
case 4:
System.out.println(num1/num2);
break;
default:
System.out.println("Invalid Choice");
break;
Src.close();
4. WAP to handle different exceptions
import java.util.Scanner;
public class demo2 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
try {
// ArrayIndexOutOfBoundsException Example
int[] numbers = {10, 20, 30, 40, 50};
System.out.print("Enter an index to access in the array (0 to 4): ");
int index = scanner.nextInt();
System.out.println("Value at index " + index + " is: " + numbers[index]);
// ArithmeticException Example
System.out.print("Enter a number to divide 100: ");
int divisor = scanner.nextInt();
int result = 100 / divisor;
System.out.println("Result of division: " + result);
// NumberFormatException Example
System.out.print("Enter a number in string format: ");
String numberString = scanner.next();
int number = Integer.parseInt(numberString);
System.out.println("Parsed integer: " + number);
//NullPointerException
String s=null;
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Exception caught: Invalid array index. Please enter a value between 0 and
4.");
} catch (ArithmeticException e) {
System.out.println("Exception caught: Division by zero is not allowed.");
} catch (NumberFormatException e) {
System.out.println("Exception caught: Invalid format for a number. Please enter a valid
integer.");
catch(NullPointerException n){
System.out.println("Exception caught: Null Value");
finally {
scanner.close();
System.out.println("Program execution completed.");
}
}
5. WAP to create thread and maipulate them.
package Multithreading;
public class Demo extends Thread {
public void run(){
//Get Proiority
System.out.println("Priority of Thread: "+Thread.currentThread().getPriority());
for(int i=1;i<=10;i++){
System.out.println(i);
try{
Thread.sleep(100);
catch(InterruptedException e)
System.out.println("Thread Sleep");
public static void main(String[] args) {
Demo obj1=new Demo();
Demo obj2=new Demo();
Demo obj3=new Demo();
//Set Priority
obj1.setPriority(Thread.MIN_PRIORITY);
obj2.setPriority(Thread.NORM_PRIORITY);
obj3.setPriority(Thread.MAX_PRIORITY);
obj1.start();
System.out.println("Thread 1: "+obj1.isAlive());
try{
obj1.join();
catch(InterruptedException e){
System.out.println("Caught"+e.getMessage());
obj2.start();
try{
obj2.join();
catch(InterruptedException e)
System.out.println("Caught "+ e.getMessage());
System.out.println("Thread 2: "+obj2.isAlive());
obj3.start();
System.out.println("Thread 3: "+obj3.isAlive());
try{
obj3.join();
catch(InterruptedException e)
System.out.println("Caught "+e.getMessage());
System.out.println("All the thread successfully run");
}
6. WAP of Synchronization
public class PrintTable {
public void table(int x){
int i,t;
for(i=1;i<=10;i++){
t=x*i;
System.out.println(t);
class Thread1 extends Thread{
PrintTable obj;
Thread1(PrintTable obj){
this.obj=obj;
public void run(){
obj.table(5);
class Thread2 extends Thread{
PrintTable obj;
Thread2(PrintTable obj){
this.obj=obj;
public void run(){
obj.table(7);
class my{
public static void main(String[] args) {
PrintTable obj=new PrintTable();
Thread1 t1=new Thread1(obj);
Thread2 t2=new Thread2(obj);
t1.start();
t2.start();
7.WAP OF Class object.
import java.util.Scanner;
class Employee {
String name;
int id;
String designation;
float salary, gs, ns;
char gender;
static float hra = 0.5f, da = 0.4f, ta = 0.3f, tax = 0.05f;
public void input() {
Scanner Src = new Scanner(System.in);
System.out.print("Enter name:");
name = Src.nextLine();
System.out.print("Enter Employee id:");
id = Src.nextInt();
Src.nextLine();
System.out.print("Enter designation:");
designation = Src.nextLine();
System.out.println("Enter Gender:");
gender=Src.nextLine().charAt(0);
System.out.print("Enter Salary:");
salary = Src.nextFloat();
public void cal() {
hra = salary * hra;
da = salary * da;
ta = salary * ta;
gs = salary + hra + da + ta;
tax = salary * tax;
ns = gs - tax;
public void show() {
System.out.println("Welcome to " + name + "!");
System.out.println("You are successfully registered.");
System.out.println("You work as a " + designation + ".");
System.out.println("Your salary details mentioned below");
System.out.println("Basic Salary: " + salary);
System.out.println("Tax: " + tax);
System.out.println("Gross Salary: " + gs);
System.out.println("Net Salary: " + ns);
public static void main(String[] args) {
Employee Emp = new Employee();
Emp.input();
Emp.cal();
Emp.show();
}
}