slip No.11

1.
import java.util.Scanner;

interface Operation {
 double PI = 3.142; 
 double volume(); 
}

class Cylinder implements Operation {
 private double radius;
 private double height;
 
 public Cylinder(double radius, double height) {
 this.radius = radius;
 this.height = height;
 }

 public double volume() {
 return PI * radius * radius * height;
 }
}

public class CylinderDemo {
 public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
 System.out.print("Enter radius of cylinder: ");
 double r = sc.nextDouble();
 System.out.print("Enter height of cylinder: ");
 double h = sc.nextDouble();
 Cylinder cylinder = new Cylinder(r, h);
 System.out.println("Volume of the cylinder: " + cylinder.volume());
 sc.close();
 }
}

2.
import java.util.Scanner;

class InvalidPasswordException extends Exception {
 public InvalidPasswordException(String message) {
 super(message);
 }
}
public class LoginDemo {
 public static void main(String[] args) {
 Scanner sc = new Scanner(System.in);
 System.out.print("Enter username: ");
 String username = sc.nextLine();
 System.out.print("Enter password: ");
 String password = sc.nextLine();
 try {
 validateLogin(username, password);
System.out.println("Login successful!");
 } catch (InvalidPasswordException e) {
 System.out.println("Error: " + e.getMessage());
 }
 sc.close();
 }
 
 public static void validateLogin(String username, String password) throws
InvalidPasswordException {
 if (!username.equals(password)) {
 throw new InvalidPasswordException("Invalid Password! Username and
password must be the same.");
 }
 }
}

Comments

Popular posts from this blog

slip 25

slip.1

slip.14