java exp

all files=

Experiment 1

        
package experiment;

public class Exp1 {

    public static void main(String[] args) {
        if (args.length == 0) {
            System.out.println("Please provide some numbers as command-line arguments.");
            return;
        }

        double sum = 0;
        int validCount = 0;

        for (int i = 0; i < args.length; i++) {
            try {
                double num = Double.parseDouble(args[i]);
                sum += num;
                validCount++;
            } catch (NumberFormatException e) {
                System.out.println("Invalid number skipped: " + args[i]);
            }
        }

        if (validCount == 0) {
            System.out.println("No valid numbers were provided.");
            return;
        }

        double average = sum / validCount;

        System.out.println("Sum: " + sum);
        System.out.println("Average: " + average);
    }
}
    

Experiment 2

    
    package experiment;

public class Exp2 {

	public static void main(String[] args) {

        // 1. Widening Type Casting (Implicit Casting)
        int intValue = 100;
        long longValue = intValue; 
        float floatValue = longValue; 

        System.out.println("Widening Casting (Implicit):");
        System.out.println("int to long: " + longValue);
        System.out.println("long to float: " + floatValue);

        // 2. Narrowing Type Casting (Explicit Casting)
        double doubleValue = 99.99;
        float narrowFloatValue = (float) doubleValue; 
        int narrowIntValue = (int) narrowFloatValue; 

        System.out.println("\nNarrowing Casting (Explicit):");
        System.out.println("double to float: " + narrowFloatValue);
        System.out.println("float to int: " + narrowIntValue);

        // 3. Char to int type casting (Implicit Casting)
        char charValue = 'A';
        int charToInt = charValue; 

        System.out.println("\nChar to int (Widening):");
        System.out.println("char to int: " + charToInt); 

        // 4. Narrowing with potential data loss
        int largeIntValue = 130;
        byte narrowByteValue = (byte) largeIntValue; 
        
        System.out.println("\nNarrowing with data loss:");
        System.out.println("int to byte (130 to byte): " + narrowByteValue); 
    }

}

  

Experiment 3

        
        
package experiment;
import java.util.Scanner;

public class Exp3 {
	
	static int fact(int num) {
		if (num < 0 ) {
		 return -1;
		}
		else if (num == 0){
			return 1;
		}
		else {
		  return num * fact(num -1);
	  }
	
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);

		System.out.print("Enter number for factorial: ");

		int num = sc.nextInt();
		int res = fact(num);
		if(res == -1) {
			System.out.println("Negative numbers factorial cannot be calculated");
		}
		else {
		System.out.println("Factorial of " + num + " : " + res);
		}

	}
	
}
    

Experiment Exp 4

package experiment;
import java.util.Scanner;

public class Exp4 {

	 public static void main(String[] args) {
	        Scanner sc = new Scanner(System.in);

	        try {
	            System.out.print("Enter a number to divide 100: ");
	            int num = sc.nextInt();
	            int result = 100 / num;
	            System.out.println("Result: 100 / " + num + " = " + result);

	            int[] arr = {1, 2, 3, 4, 5};
	            System.out.print("Enter an index (0 to 4) to access array: ");
	            int index = sc.nextInt();
	            System.out.println("Array element at index " + index + ": " + arr[index]);

	            System.out.print("Enter a number as a string: ");
	            String str = sc.next(); 
	            int convertedNumber = Integer.parseInt(str); 
	            System.out.println("Converted number: " + convertedNumber);
	            
	        } catch (ArithmeticException e) {
	            System.out.println("ArithmeticException: Cannot divide by zero!");
	        } catch (ArrayIndexOutOfBoundsException e) {
	            System.out.println("ArrayIndexOutOfBoundsException: Invalid array index!");
	        } catch (NumberFormatException e) {
	            System.out.println("NumberFormatException: Invalid input, not a valid integer!");
	        } catch (Exception e) {
	            System.out.println("General Exception: Something went wrong!");
	        } finally {
	            sc.close(); 
	            System.out.println("End");
	        }
	    }
}
    

Experiment 5

        
package experiment;

//Abstract class Shape
abstract class Shape {
// Abstract method (no implementation)
 abstract double calculateArea();
 
// A concrete method with implementation
 public void displayShape() {
     System.out.println("This is a shape.");
 }
}

//Concrete class Circle that extends Shape
class Circle extends Shape {
 double radius;

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

 // Implementing the abstract method
@Override
 double calculateArea() {
     return Math.PI * radius * radius;
 }
}

//Concrete class Rectangle that extends Shape
class Rectangle extends Shape {
 double length;
 double width;

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

 // Implementing the abstract method
 @Override
 double calculateArea() {
     return length * width;
 }
}

public class Exp5 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		// Create Circle object
        Shape circle = new Circle(6);
        System.out.println("Area of Circle: " + circle.calculateArea());

        // Create Rectangle object
        Shape rectangle = new Rectangle(5, 2);
        System.out.println("Area of Rectangle: " + rectangle.calculateArea());
        
        // Demonstrating calling a concrete method from the abstract class
        circle.displayShape();
        rectangle.displayShape();

	}

}
    

Experiment 6

        
        package experiment;

class CustomerOrder implements Runnable {
    private String customerName;
    private String order;

    public CustomerOrder(String customerName, String order) {
        this.customerName = customerName;
        this.order = order;
    }

    @Override
    public void run() {
        System.out.println(customerName + " placed an order for " + order + ".");
        processOrder();
        System.out.println(customerName + "'s order for " + order + " is ready!");
    }
    
    private void processOrder() {
        try {
            // Simulate time taken to process the order (e.g., preparing food)
            Thread.sleep(2000);  // 2 seconds to process each order
        } catch (InterruptedException e) {
            System.out.println("Order processing interrupted for " + customerName);
        }
    }
}


public class Exp9 {

	public static void main(String[] args) {
        // Create and start threads representing multiple customer orders
        Thread order1 = new Thread(new CustomerOrder("Alice", "Pizza"));
        Thread order2 = new Thread(new CustomerOrder("Bob", "Burger"));
        Thread order3 = new Thread(new CustomerOrder("Charlie", "Pasta"));

        order1.start();
        order2.start();
        order3.start();

        try {
            // Wait for all orders to be completed
            order1.join();
            order2.join();
            order3.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println("All orders have been processed!");
    }
}

    

Comments

Popular posts from this blog

Machine Learning

Blockchain Experiments