Java lab

                        
import java.util.Scanner;
class Demo
{
	public static void main(String[] args)
	{
		Scanner sc = new Scanner(System.in);
		System.out.print("Enter a digit : ");
		int a = sc.nextInt();
		int digits = 0;
		while(a>0)
		{
			digits++;
			a/=10;
		}
		System.out.println("Number of digits = " + digits);
	}
}
                        
                
                        
import java.util.Scanner;
class Demo
{
	public static void main(String[] args)
	{
		Scanner sc = new Scanner(System.in);
		System.out.print("Enter a number : ");
		int num = sc.nextInt();
		int sum = 0;
		while(num > 0)
		{
			sum += num % 10;
			num /= 10;
		}
		System.out.println("Sum of digits = " + sum);
	}
}
                        
                
                        
import java.util.Scanner;
class Demo
{
	public static void main(String[] args)
	{
		Scanner sc = new Scanner(System.in);
		int[] arr = new int[5];
		// getting array element from user
		System.out.println("Enter 5 elements : ");
		for(int i = 0; i < arr.length; i++)
		{
			arr[i] = sc.nextInt();
		}  
		System.out.println("Displaying array element : ");
		for(int i = 0; i < arr.length; i++)
		{	
			System.out.print(arr[i] + " ");
		}  
	}	
}   	
                        
                
                        
import java.util.Scanner; 
class Demo
{
	public static void main(String[] args)
	{
		Scanner sc = new Scanner(System.in);
		int[][] arr = new int[3][3];
		System.out.println("Enter the elements of array");
		for(int i = 0; i < 3; i++)
		{
			for(int j = 0; j < 3; j++)
			{
				arr[i][j] = sc.nextInt();
			}
		} 
		System.out.println("Displaying data : ");
		for(int i = 0; i < 3; i++)
		{
			for(int j = 0; j < 3; j++)
			{
				System.out.print(arr[i][j] + " ");
			}
			System.out.println("");
		}   
	}	
} 
                        
                
                        
class Demo
{
	public static void main(String[] args)
	{
		int[][] arr1 = {{1, 1, 1}, {2, 2, 2}};
		int[][] arr2 = {{3, 3, 3}, {4, 4, 4}};
		int[][] sum = new int[2][3];
		for(int i = 0; i < 2; i++)
		{
			for(int j = 0; j < 3; j++)
			{
				sum[i][j] = arr1[i][j] + arr2[i][j];
			}
		}
		System.out.println("Displaying the sum");
		for(int i = 0; i < 2; i++)
		{
			for(int j = 0; j < 3; j++)
			{
				System.out.print(sum[I][j] + " ");
			}
			System.out.println("");
		}
	}
}  
                        
                
                        
import java.util.Scanner;

class Electricity {
    private int unit;
    private float bill;

    public void getUnit() {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter your unit: ");
        unit = sc.nextInt();
        sc.close();
    }

    public void calBill() {
        if (unit < 100) {
            bill = (float) 1.20 * unit;
        } else if (unit < 200) {
            bill = (float) (100 * 1.20) + (unit - 100) * 2;
        } else if (unit < 300) {
            bill = (float) (100 * 1.20) + (100 * 2) + (unit - 200) * 3;
        }
    }

    public float getBill() {
        return bill;
    }
}

public class Test {
    public static void main(String[] args) {
        Electricity bill1 = new Electricity();
        bill1.getUnit();
        bill1.calBill();
        System.out.println("Your total bill is = " + bill1.getBill());
    }
}
                        
                
                        
import java.util.Scanner;

class Account {
    private int accno = 0;
    private String name = ""; // Initialize the name with an empty string
    private float balance = 0;
    private String account_type = ""; // Initialize the account_type with an empty string
    private int temp_amount = 0;
    Scanner sc = new Scanner(System.in);

    public void getData() {
        System.out.println("Enter account number : ");
        accno = sc.nextInt();
        sc.nextLine(); // Consume the newline character after reading an integer
        System.out.println("Enter your name : ");
        name = sc.nextLine(); // Read the name as a whole line
        System.out.println("Enter your balance : ");
        balance = sc.nextFloat();
        sc.nextLine(); // Consume the newline character after reading a float
        System.out.println("Enter your Account type : ");
        account_type = sc.nextLine(); // Read the account_type as a whole line
    }

    public void display() {
        System.out.println("Account number : " + accno);
        System.out.println("Name : " + name); // Display the name
        System.out.println("Balance : " + balance);
        System.out.println("Account type : " + account_type);
    }

    public void withdraw() {
        System.out.println("Enter the amount you want to withdraw : ");
        temp_amount = sc.nextInt();
        if (temp_amount > balance) {
            System.out.println("Can't withdraw this amount");
            return;
        }
        balance -= temp_amount;
    }

    public void deposit() {
        System.out.println("Enter the amount you want to deposit : ");
        temp_amount = sc.nextInt();
        balance += temp_amount;
    }
}

public class Test {
    public static void main(String[] args) {
        Account first_account = new Account();
        first_account.getData();
        first_account.deposit();
        first_account.withdraw();
        first_account.display();
    }
}
                        
                
                        
class Student {
    public int roll_no = 0;
    public String name = "";
    public int age = 0;
    public String course = "";

    public Student() {
        roll_no = 11;
        name = "Henry Ford";
        age = 18;
        course = "B.com";
    }
}

class Test {
    public static void main(String[] args) {
        Student first = new Student();
        System.out.println("Default Data \n Name : " + first.name + "\n Roll number : " + first.roll_no + "\n Age : "
                + first.age + "\n Course : " + first.course);
    }
}
                        
                
                        
class Test {
    public static void main(String[] args) {
        if(args.length < 1)
    {
        System.out.println("Provide some arguments");
        return;
    }
        int num = Integer.parseInt(args[0]);
        int a = 0;
        int b = 1;
        System.out.println(a);
        System.out.println(b);
        for(int i = 0; i < num - 2; i++)
        {
            int temp = a + b;
            System.out.println(temp);
            a = b;
            b = temp;
        }
    }
}
                        
                
                        
class Rectangle {
    private int length;
    private int breadth;
    private int area;

    public Rectangle(int l, int b) {
        length = l;
        breadth = b;
    }

    public void calArea() {
        area = length * breadth;
    }

    public void display() {
        System.out.println("The area is = " + area);
    }
}

class Test {
    public static void main(String[] args) {
        Rectangle rect = new Rectangle(10, 20);
        rect.calArea();
        rect.display();
    }
}
                        
                
                        
class InterestCalculator {
    double principal;
    double rate;
    double time;
    double interest;
    double amount;

    // Constructor with three parameters (principal, rate, and time)
    InterestCalculator(double principal, double rate, double time) {
        this.principal = principal;
        this.rate = rate;
        this.time = time;
        calculateInterest();
        calculateAmount();
    }

    // Constructor with two parameters (principal and rate) assuming one year as time
    InterestCalculator(double principal, double rate) {
        this(principal, rate, 1.0); // Call the three-parameter constructor with a default time of 1 year
    }

    // Method to calculate simple interest
    void calculateInterest() {
        interest = (principal * rate * time) / 100.0;
    }

    // Method to calculate the final amount
    void calculateAmount() {
        amount = principal + interest;
    }

    // Display the results
    void display() {
        System.out.println("Principal: " + principal);
        System.out.println("Rate: " + rate);
        System.out.println("Time: " + time);
        System.out.println("Simple Interest: " + interest);
        System.out.println("Final Amount: " + amount);
    }
}

public class Main {
    public static void main(String[] args) {
        // Create an object using the three-parameter constructor
        InterestCalculator calculator1 = new InterestCalculator(1000.0, 5.0, 2.5);
        System.out.println("Details for Calculator 1:");
        calculator1.display();

        // Create an object using the two-parameter constructor
        InterestCalculator calculator2 = new InterestCalculator(1500.0, 3.5);
        System.out.println("\nDetails for Calculator 2:");
        calculator2.display();
    }
}
                        
                
                        
class Student {
    int rollno;
    String name = "";

    // Default Constructor
    public Student() {
    }

    // Copy Constructor
    public Student(Student obj) {
        this.rollno = obj.rollno;
        this.name = obj.name;
    }

    public void setValue(int r, String n) {
        name = n;
        rollno = r;
    }

    public void display() {
        System.out.println("Name = " + name);
        System.out.println("Roll number = " + rollno);
    }
}

class Test {
    public static void main(String[] args) {
        Student obj1 = new Student();
        obj1.setValue(13, "Henry");
        System.out.println("First obj values:");
        obj1.display();

        System.out.println("Copied object values:");
        Student obj2 = new Student(obj1); // Use the copy constructor
        obj2.display();
    }
}
                        
                
                        
class Laptop
{
    static int no_of_laptop = 0;
    public Laptop()
    {
        increaseCount();
    }
    static void increaseCount()
    {
        no_of_laptop++;
    }
}

class Test
{
    public static void main(String[] args) {
        Laptop one = new Laptop();
        Laptop two = new Laptop();
        Laptop three = new Laptop();
        Laptop four = new Laptop();
        Laptop five = new Laptop();
        System.out.println(Laptop.no_of_laptop);
    }
}
                        
                
                        
class Student {
    int rollno;
    static String college;
    String name = "";

    public void display() {
        System.out.println("College = " + college);
        System.out.println("Name = " + name);
        System.out.println("Roll number = " + rollno);
    }
}

class Test {
    public static void main(String[] args) {
        Student.college = "Graphic Era Hill University";
        Student stu1 = new Student();
        stu1.name = "Henry";
        stu1.rollno = 33;
        stu1.display();
    }
}
                        
                
                        
abstract class DiffArea
{
	abstract int cal_area(int s);
	abstract int cal_area(int l, int b);
	abstract double cal_area(double ba, double h);
} 

class Test extends DiffArea
{
	int cal_area(int s)
	{
		return s * s;
	}
	int cal_area(int l, int b)
	{
		return l * b;
	}
	double cal_area(double ba, double h)
	{
		return 0.5 * ba * h;
	}
} 

class Demo
{
	public static void main(String[] args)
	{
		Test obj = new Test();
		System.out.println("The area of square = " + obj.cal_area(5));
		System.out.println("The area of rectangle = " + obj.cal_area(5, 4));
		System.out.println("The area of triangle = " + obj.cal_area(5.4, 5.9));
	}
} 
                        
                
                        
// Parent class Account
class Account {
    protected int accNo;
    protected String name;
    protected double balance;

    // Constructor
    public Account(int accNo, String name, double balance) {
        this.accNo = accNo;
        this.name = name;
        this.balance = balance;
    }

    // Method to get account data
    public void getData(int accNo, String name, double balance) {
        this.accNo = accNo;
        this.name = name;
        this.balance = balance;
    }

    // Method to display account details
    public void display() {
        System.out.println("Account Number: " + accNo);
        System.out.println("Account Holder Name: " + name);
        System.out.println("Account Balance: $" + balance);
    }
}

// Subclass CurrentAccount
class CurrentAccount extends Account {
    private double overdraftLimit;

    // Constructor
    public CurrentAccount(int accNo, String name, double balance, double overdraftLimit) {
        super(accNo, name, balance);
        this.overdraftLimit = overdraftLimit;
    }

    // Method to display current account details
    public void display() {
        super.display();
        System.out.println("Overdraft Limit: $" + overdraftLimit);
    }
}

// Subclass SavingsAccount
class SavingsAccount extends Account {
    private double interestRate;

    // Constructor
    public SavingsAccount(int accNo, String name, double balance, double interestRate) {
        super(accNo, name, balance);
        this.interestRate = interestRate;
    }

    // Method to display savings account details
    public void display() {
        super.display();
        System.out.println("Interest Rate: " + interestRate + "%");
    }
}

public class Main {
    public static void main(String[] args) {
        // Create a current account
        CurrentAccount currentAccount = new CurrentAccount(1001, "John Doe", 5000.0, 1000.0);
        System.out.println("Current Account Details:");
        currentAccount.display();

        System.out.println();

        // Create a savings account
        SavingsAccount savingsAccount = new SavingsAccount(2001, "Jane Smith", 10000.0, 2.5);
        System.out.println("Savings Account Details:");
        savingsAccount.display();
    }
}
                        
                
                        
abstract class Shape {
    abstract public int area();
}

class Square extends Shape {
    int side;

    Square(int s) {
        side = s;
    }

    public int area() {
        return side * side;
    }
}

class Rectangle extends Shape {
    int length;
    int breadth;

    Rectangle(int l, int b) {
        length = l;
        breadth = b;
    }

    public int area() {
        return length * breadth;
    }
}

class Test {
    public static void main(String[] args) {
        Square s1 = new Square(12);
        System.out.println("The area of square = " + s1.area());
        Rectangle r1 = new Rectangle(12, 14);
        System.out.println("The area of rectangle = " + r1.area());
    }
}
                        
                
                        
class Circle {
    // Final variable for PI
    final double PI = 3.14159265359;

    // Instance variable for radius
    double radius;

    // Constructor to initialize the radius
    public Circle(double radius) {
        this.radius = radius;
    }

    // Method to calculate the circumference of the circle
    public double circumference() {
        return 2 * PI * radius;
    }

    // Method to calculate the area of the circle
    public double area() {
        return PI * radius * radius;
    }
}

public class Main {
    public static void main(String[] args) {
        // Create a circle with a radius of 5.0
        Circle circle = new Circle(5.0);

        // Calculate and print the circumference
        double circumference = circle.circumference();
        System.out.println("Circumference of the circle: " + circumference);

        // Calculate and print the area
        double area = circle.area();
        System.out.println("Area of the circle: " + area);
    }
}
                        
                
                        
class Test {
    public static void main(String[] args) {
        // int[] arr1 = {44,22,66,44,11,55,57}; // this also works
        int[] arr1 = new int[] { 44, 22, 66, 44, 11, 55, 57 };
        int minimum = arr1[0];
        int maximum = arr1[0];
        for (int i = 1; i < arr1.length; i++) {
            if (arr1[i] < minimum) {
                minimum = arr1[i];
            }
            if (arr1[i] > maximum) {
                maximum = arr1[i];
            }
        }
        System.out.println("The minimum number = " + minimum + " and maximum = " + maximum);
    }
}
                        
                    
                        
import java.util.Scanner;

class Test {
    public static void main(String[] args) {
        // int[] arr1 = {44,22,66,44,11,55,57}; // this also works
        int[] arr1 = new int[] { 44, 22, 66, 44, 11, 55, 57 };
        int search = 0;
        int flag = -1;
        int i = 0;
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a number : ");
        search = sc.nextInt();
        for (i = 0; i < arr1.length; i++) {
            if (search == arr1[i]) {
                flag = 1;
            }
        }
        if (flag == 1) {
            System.out.println("Found at index = " + i);
        } else {
            System.out.println("Element not found");
        }
    }
}
                        
                
                        
interface Shape {
    double area();
}

// Triangle class implementing the Shape interface
class Triangle implements Shape {
    private double base;
    private double height;

    public Triangle(double base, double height) {
        this.base = base;
        this.height = height;
    }

    @Override
    public double area() {
        return 0.5 * base * height;
    }
}

// Rectangle class implementing the Shape interface
class Rectangle implements Shape {
    private double length;
    private double width;

    public Rectangle(double length, double width) {
        this.length = length;
        this.width = width;
    }

    @Override
    public double area() {
        return length * width;
    }
}

// Circle class implementing the Shape interface
class Circle implements Shape {
    private double radius;

    public Circle(double radius) {
        this.radius = radius;
    }

    @Override
    public double area() {
        return Math.PI * radius * radius;
    }
}

public class Main {
    public static void main(String[] args) {
        // Create instances of different shapes
        Triangle triangle = new Triangle(6.0, 8.0);
        Rectangle rectangle = new Rectangle(5.0, 10.0);
        Circle circle = new Circle(4.0);

        // Calculate and print the areas
        System.out.println("Area of Triangle: " + triangle.area());
        System.out.println("Area of Rectangle: " + rectangle.area());
        System.out.println("Area of Circle: " + circle.area());
    }
}
                        
                    
                        
class MyThread extends Thread {
    public void run() {
        for (int i = 1; i <= 5; i++) {
            System.out.println("Thread " + Thread.currentThread().getId() + ": Count " + i);
        }
    }
}

public class ThreadExample {
    public static void main(String[] args) {
        MyThread thread1 = new MyThread();
        MyThread thread2 = new MyThread();

        thread1.start(); // Start the first thread
        thread2.start(); // Start the second thread
    }
}