Monday, 13 July 2015

Java Programs[Important]

  1.  
  2.   WRITE A JAVA PROGRAM to create an abstract class named Shape, that contains an empty method named numberOfSides(). Provide three classes named Trapezoid, Triangle and Hexagon, such that each one of the classes contains only the method numberOfSides(), that contains the number of sides in the given geometrical figure.
  3.  
  4. Program :
  5.  
  6. abstract class Shape
  7.  { 
  8. abstract void numberOfSides();
  9.  } 
  10. class Trapezoid extends Shape
  11. {

  12. void numberOfSides()
  13.  {
  14.  System.out.println(" Trapezoidal has four sides");
  15.  } 
  16. class Triangle extends Shape
  17.  { 
  18. void numberOfSides()
  19.  { 
  20. System.out.println("Triangle has three sides"); 
  21. }
  22.  }
  23.  class Hexagon extends Shape 
  24. void numberOfSides()
  25.  { 
  26. System.out.println("Hexagon has six sides");
  27.  }
  28.  }
  29.  class ShapeDemo 
  30. public static void main(String args[ ])
  31.  { 
  32. Trapezoid t=new Trapezoid(); 
  33. Triangle r=new Triangle();
  34.  Hexagon h=new Hexagon(); 
  35. Shape s;

  36. s=t; 
  37. s.numberOfSides();
  38.  s=r;
  39.  s.numberOfSides();
  40.  s=h; 
  41. s.numberOfSides();
  42.  }
  43.  }