Monday 19 January 2015

Implement C++/Java/Python program for bubble sort using function template.

 // b_sort.cpp
                                                                                                                                                                                                                                                                                                                                         
#include<iostream> 
#include<iostream>
using namespace std;
template <class T>
void bubble(T a[],int n)
{
    for(int i=0;i<n-1;i++)
    {
    for(int j=0;j<n-i-1;j++)
    {
        if(a[j]>a[j+1])
        {
        T temp=a[j];
        a[j]=a[j+1];
        a[j+1]=temp;
        }
    }
    }
}

        int main()
            {
                int x[5];
                float y[5];
                cout<<"Enter values of array x:\n";
                for(int i=0;i<5;i++)
                 cin>>x[i];
                cout<<"Enter values of array y:\n";
                for(int i=0;i<5;i++)
                cin>>y[i];
                bubble(x,5);
                bubble(y,5);
                cout<<"Sorted array X:";
                for(int i=0;i<5;i++)
                cout<<x[i]<<"\t";
                cout<<endl;
                cout<<"Sorted array Y:";
                for(int i=0;i<5;i++)
                cout<<y[i]<<"\t";
                cout<<endl;
                return 0;
            }


//java code

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

class BubbleSort
{
  public static <T extends Comparable<T>> void bubbleSort (T[] list, int size)
  {
    T temp;
    // swapOccurred helps to stop iterating if the array gets sorted before
    // outCounter reaches to size
    for (int i=0;i<list.length;i++)
    {
      for (int j=0;j<list.length-1;j++)
      {
        if (list[j].compareTo(list[j+1]) > 0)
        {
          temp = list[j];
          list[j] = list[j+1];
          list[j+1] = temp;
        }
      }
    }
  }
}

public class BubbleSortDemo
{
  public static void main (String[] args)
  {
    Integer arr[] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
    BubbleSort.bubbleSort(arr, arr.length);

    System.out.println("Sorted Array: ");
    for(Integer i : arr)
    {
      System.out.println(i);
    }
    Double arr1[] = {10.5, 2.9, 6.8, 1.7, 6.8, 5.5, 4.4, 1.3, 2.91, 1.11};
    BubbleSort.bubbleSort(arr1, arr1.length);

    System.out.println("Sorted Array: ");
    for(Double i : arr1)
    {
      System.out.println(i);
    }


  }
}
       

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;
}