Monday 23 February 2015

10. 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

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


interface shape
{
  void getdata(double i,double j);
  void area();
}

class triangle implements shape
{
  public double x,y;
  public void getdata(double i,double j)
   {
     x=i;
     y=j;
     //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(double i,double j)
   {
     x=i;
    y=j;
     /* 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;
         double a,b;
    System.out.println("\n Enter two values :"+"\n a :");
     Scanner sc=new Scanner(System.in);
     a=sc.nextDouble();
     System.out.println("\n Height :");
     b=sc.nextDouble();
       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(a,b);
        t1.area();
    break;
        case 2: rectangle r=new rectangle();
    r.getdata(a,b);
    r.area();
    break;
    case 3: System.exit(1);
         }//switch
     }while(ch!=3);    //do
  } //main
}//class

1 comment:

  1. Whatever we gathered information from the blogs, we should implement that in practically then only we can understand that exact thing clearly, but it’s no need to do it, because you have explained the concepts very well. It was crystal clear, keep sharing dude.
    Regards,
    Python Training in Chennai|Python Training

    ReplyDelete