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

9. Refer the standard template library to use list container and using C++/Java implement following member functions of list class: empty, insert, merge, reverse, sort



#include<iostream>
#include<list>
#include<stdlib.h>

using namespace std;

void display(list <int> &lst)
{
    list<int> :: iterator p;
    cout<<"\n";   
    for(p=lst.begin();p!=lst.end();++p)
    cout<<"\n"<<*p;
    cout<<"\n \n";
}
int main()
{
    list<int> list1;
    list <int> list2(5);
    for(int i=0;i<3;i++)
    list1.push_back(rand()/100);
    list<int>::iterator p;
    for(p=list2.begin();p!=list2.end();++p)
    *p=rand()/100;
    cout<<"list1\n";
    display(list1);
    cout<<"list2\n";
    display(list2);
    //add two elements at the end of list
    list1.push_front(100);
    list1.push_back(200);
    //remove an element at the front of list
    list2.pop_front();
    cout<<" Modified list1\n";
    display(list1);
    cout<<"Modified list2\n";
    display(list2);
    list<int>listA,listB;
    listA=list1;
    listB=list2;
    //Merging Two lists
    list1.merge(list2);
    cout<<"merged unsorted list";
    display(list1);

    //sorting and merging
    listA.sort();
    listB.sort();
    listA.merge(listB);
    cout<<"Merged sorted list\n";
    display(listA);

    //Reversing a list
    listA.reverse();
    cout<<"Reversed sorted list\n";
    display(listA);
    return 0;
}