diff --git a/dmd.java b/dmd.java new file mode 100644 index 00000000..425e2493 --- /dev/null +++ b/dmd.java @@ -0,0 +1,56 @@ +import java.util.*; + +class area +{ + void display() + { + System.out.println("Area"); + } +} + +class triangle extends area +{ + Scanner sc = new Scanner(System.in); + int b,h; + void display() + { + System.out.println("Enter base & height:"); + b = sc.nextInt(); + h = sc.nextInt(); + System.out.println("Area of triangle:"+(0.5*b*h)); + } +} + +class circle extends area +{ + Scanner sc = new Scanner(System.in); + int r; + void display() + { + System.out.println("Enter radius:"); + r = sc.nextInt(); + System.out.println("Area of triangle:"+(3.14*r*r)); + } +} + +class dmd +{ + public static void main(String[] args) + { + int ch; + area a = null; + Scanner sc = new Scanner(System.in); + System.out.println("1.Triangle\n2.Circle\nEnter Choice:"); + ch = sc.nextInt(); + switch(ch) + { + case 1: + a = new triangle(); + break; + case 2: + a = new circle(); + break; + } + a.display(); + } +} \ No newline at end of file diff --git a/excep.java b/excep.java new file mode 100644 index 00000000..f2e18cbc --- /dev/null +++ b/excep.java @@ -0,0 +1,52 @@ +import java.util.*; + +class InvalidAgeException extends Exception +{ + InvalidAgeException(String s) + { + super(s); + } +} + +/*class Testhrow +{ + public static void validate (int age) throws InvalidAgeException + { + if(age < 18) + { + throw new InvalidAgeException("Not valid"); + } + else + { + System.out.println("Valid"); + } + } +}*/ +class excep +{ + public static void main(String args[]) + { + Scanner sc = new Scanner(System.in); + int age = sc.nextInt(); + try + { + if(age < 18) + { + throw new InvalidAgeException("Not valid"); + } + else + { + System.out.println("Valid"); + } + } + catch (Exception e) + { + System.out.println("Exception Occurs"); + } + finally + { + System.out.println("Rest of the code"); + } + + } +} \ No newline at end of file