You are on page 1of 2

SLIP 11

Q1) Define an interface “Operation” which has method volume( ).Define a constant PI
having a value
3.142 Create a class cylinder which implements this interface (members –
radius,height). Create one object and calculate the volume.
SOLUTION:
import java.util.Scanner;
interface Operation {
double volume();
}

class Cylinder implements Operation {


public static final double PI = 3.142;
private double radius;
private double height;

public Cylinder(double radius, double height) {


this.radius = radius;
this.height = height;
}

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

public class Main {


public static void main(String[] args) {
// Read radius and height from user input
Scanner scanner = new Scanner(System.in);
System.out.print("Enter radius: ");
double radius = scanner.nextDouble();
System.out.print("Enter height: ");
double height = scanner.nextDouble();

// Create a new Cylinder object with user-specified radius and height


Cylinder cylinder2 = new Cylinder(radius, height);

// Calculate and print the volume of the second cylinder


System.out.println("Volume of second cylinder: " + cylinder2.volume());
}
}

Q2) Write a program to accept the username and password from user if username and
password are not same then raise "Invalid Password" with appropriate msg.
SOLUTION:

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.print("Enter username: ");


String username = scanner.nextLine();

System.out.print("Enter password: ");


String password = scanner.nextLine();
if (!username.equals(password)) {
System.out.println("Invalid password");
}
}
}

You might also like