Monday, 19 January 2015

Implement C++/Java/Python program to create a base class called shape. Use this class to store two double type values that could be used to compute the area of figures. Derive two specific classes called function get_data() to initialize base class data members and another member function display_area() to compute and display the area of figures. Make classes to suit their requirements. Using these three classes, design a program that will accept dimension of a triangle or a rectangle interactively, and display the area. Remember the two values given as input will be treated as lengths of two sides in the case of rectangles, and as base and height in the case of triangles, and used as follows: Area of rectangle= x*y Area of triangle =1/2*x*y

////////Java Program

import java.io.*;
import java.util.*;


interface shape
{
  void getdata();
  void area();
}

class triangle implements shape
{
  public double x,y;
  public void getdata()
   {

     System.out.println("\n Enter Base and Height of Triangle :"+"\n BAse :");
     Scanner sc=new Scanner(System.in);
     x=sc.nextDouble();
     System.out.println("\n Height :");
     y=sc.nextDouble();
    }
 public void area()
 {
  System.out.println("\n Area of Triangle is :"+(0.5*x*y));
  }
}


class rectangle implements shape
{
  public double x,y;
  public void getdata()
   {

     System.out.println("\n Enter Sides of Rectangle  :"+"\n Side 1 :");
     Scanner sc=new Scanner(System.in);
     x=sc.nextDouble();
     System.out.println("\n Side 2 :");
     y=sc.nextDouble();
    }
 public void area()
 {
  System.out.println("\n Area of Rectangle is :"+(x*y));
  }
}


class inherit
{
  public static void main(String []std)
  {
       int ch;
       do
      {
       System.out.println("\n Menu 1.Triangle \n 2.Rectangle \n 3.Exit  \n Enter your choice :");
    Scanner s=new Scanner(System.in);
    ch=s.nextInt();
    switch(ch)
        {
        case 1: triangle t1=new triangle();
        t1.getdata();
        t1.area();
    break;
        case 2: rectangle r=new rectangle();
    r.getdata();
    r.area();
    break;
    case 3: System.exit(1);
         }//switch
     }while(ch!=3);    //do
  } //main
}//class





/////        C++ Program



#include <iostream> 
using namespace std;
 
class shape {
   protected:
      int width, height;
   public:
      shape( int a=0, int b=0)
      {
         width = a;
         height = b;
      }
     virtual int area()
      {
         cout << "Parent class area :" <<endl;
         return 0;
      }
};
class Rectangle: public shape{
   public:
      Rectangle( int a=0, int b=0)
      {
        shape(a, b); 
      }
      int area ()
      { 
         cout << "Rectangle class area :" <<endl;
         return (width * height); 
      }
};
class Triangle: public shape{
   public:
      Triangle( int a=0, int b=0)
      {
        shape(a, b); 
      }
      int area ()
      { 
         cout << "Triangle class area :" <<endl;
         return (width * height / 2); 
      }
};
// Main function for the program
int main( )
{
   shape *shape;
   Rectangle rec(10,7);
   Triangle  tri(10,5);

   // store the address of Rectangle
   shape = &rec;
   // call rectangle area.
   shape->area();

   // store the address of Triangle
   shape = &tri;
   // call triangle area.
   shape->area();
   
   return 0;
}

2 comments:

  1. Thanks of sharing this post…Python is the fastest growing language that helps to get your dream job in a developing area. It says every fundamental in a programming, so if you want to become an expertise in python get some training
    Regards,
    Python Training in Chennai

    ReplyDelete