(1) package pkg; |
(2) importjava.util.; |
(3) public class TestShape |
(4) public static void main(String args) |
(5) String str; |
(6) int a, b; |
(7) Scanner sin = new Scanner(System.in); |
(8) System.out.println("Enter the Color: "); |
(9) str = sin.next(); |
(10) System.out.println("Enter the length and breadth: "); |
(11) a = sin.nextInt(); |
(12) b = sin.nextInt(); |
(13) Shape s1 = new Rectangle(str, a, b); |
(14) System.out.println(s1); |
(15) System.out.println("Area is " + s1.getArea()); |
(16) System.out.println("Enter the Color: "); |
(17) str = sin.next(); |
(18) System.out.println("Enter the length and breadth: "); |
(19) a = sin.nextInt(); |
(20) b = sin.nextInt(); |
(21) Shape s2 = new Triangle(str, a, b); |
(22) System.out.println(s2); |
(23) System.out.println("Area is " + s2.getArea()); |
package pkg; |
(24) public class Triangle <T> extends Shape |
(25) private T base; |
(26) private T height; |
(27) public Triangle(String color, T base, T height) |
(28) super(color); |
(29) this.base = base; |
(30) this.height = height; |
(31) public String toString() |
(32) return "Triangle of base=" + base + " and height=" + height + ", subclass of " + super.toString(); |
(33) public T getArea() |
(34) return 0.5baseheight; |
package pkg; |
(35) public class Rectangle extends Shape |
(36) private int length; |
(37) private int width; |
(38) public Rectangle(String color, int length, int width) |
(39) super(color); |
(40) this.length = length; |
(41) this.width = width; |
(42) public String toString() |
(43) return "Rectangle of length=" + length + " and width=" + width + ", subclass of " + super.toString(); |
(44) public double getArea() |
(45) return lengthwidth; |
package pkg; |
(46) public class Shape |
(47) private String color; |
(48) public Shape (String color) |
(49) this.color = color; |
(50) public String toString() |
(51) return "Shape of color="" + color + """; |
(52) public double getArea() |
(53) System.err.println("Shape unknown! Cannot compute area!"); |
(54) return 0; |